├── README.md ├── DelegateCalls.sol ├── Lottery.sol └── CryptoKids.sol /README.md: -------------------------------------------------------------------------------- 1 | Smart Contracts Repository 🚀 2 | Welcome to the SmartContracts repository! This repository contains Solidity smart contracts ranging from basic to intermediate to advanced levels. Whether you're a beginner looking to understand the fundamentals or an experienced developer exploring complex contracts, you'll find valuable code here. 3 | 4 | 📌 Contents 5 | 🔹 Basic Contracts – Beginner-friendly contracts covering fundamental Solidity concepts. 6 | 🔹 Intermediate Contracts – Contracts with more complex logic, including function modifiers, events, and external integrations. 7 | 🔹 Advanced Contracts – High-level contracts utilizing DeFi, NFTs, DAOs, and security best practices. 8 | 9 | 🤝 Contributing 10 | Contributions are welcome! If you have improvements or new smart contracts, feel free to open a pull request. 11 | 12 | 📜 License 13 | © 2025 Utkarsh Srivastava. All Rights Reserved. 14 | 15 | -------------------------------------------------------------------------------- /DelegateCalls.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT 2 | pragma solidity ^0.8.26; 3 | 4 | // NOTE: Deploy this contract first 5 | contract B { 6 | // NOTE: storage layout must be the same as contract A 7 | uint256 public num; 8 | address public sender; 9 | uint256 public value; 10 | 11 | function setVars(uint256 _num) public payable { 12 | num = _num; 13 | sender = msg.sender; 14 | value = msg.value; 15 | } 16 | } 17 | 18 | contract A { 19 | uint256 public num; 20 | address public sender; 21 | uint256 public value; 22 | 23 | function setVars(address _contract, uint256 _num) public payable { 24 | // A's storage is set, B is not modified. 25 | (bool success, bytes memory data) = _contract.delegatecall( 26 | abi.encodeWithSignature("setVars(uint256)", _num) 27 | ); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Lottery.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT 2 | pragma solidity ^0.8.19; 3 | 4 | contract Lottery { 5 | address public owner; 6 | address[] public players; 7 | 8 | constructor() { 9 | owner = msg.sender; 10 | } 11 | 12 | function enter() public payable { 13 | require(msg.value == 0.1 ether, "Must send exactly 0.1 ETH"); 14 | players.push(msg.sender); 15 | } 16 | 17 | function getPlayers() public view returns (address[] memory) { 18 | return players; 19 | } 20 | 21 | function pickWinner() public onlyOwner { 22 | require(players.length > 0, "No players joined yet"); 23 | 24 | uint index = random() % players.length; 25 | address winner = players[index]; 26 | 27 | payable(winner).transfer(address(this).balance); 28 | 29 | // Reset the lottery 30 | delete players; 31 | } 32 | 33 | function random() private view returns (uint) { 34 | return uint(keccak256(abi.encodePacked(block.timestamp, block.difficulty, players.length))); 35 | } 36 | 37 | modifier onlyOwner() { 38 | require(msg.sender == owner, "Only owner can call this"); 39 | _; 40 | } 41 | } 42 | 43 | 44 | -------------------------------------------------------------------------------- /CryptoKids.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: Unlicensed 2 | 3 | pragma solidity ^0.8.26; 4 | 5 | contract CryptoKids { 6 | // owner DAD 7 | address owner; 8 | 9 | event LogKidFundingReceived(address addr, uint amount, uint contractBalance); 10 | 11 | constructor() { 12 | owner = msg.sender; 13 | } 14 | 15 | // define Kid 16 | struct Kid { 17 | address payable walletAddress; 18 | string firstName; 19 | string lastName; 20 | uint releaseTime; 21 | uint amount; 22 | bool canWithdraw; 23 | } 24 | 25 | Kid[] public kids; 26 | 27 | modifier onlyOwner() { 28 | require(msg.sender == owner, "Only the owner can add kids"); 29 | _; 30 | } 31 | 32 | // add kid to contract 33 | function addKid(address payable walletAddress, string memory firstName, string memory lastName, uint releaseTime, uint amount, bool canWithdraw) public onlyOwner { 34 | kids.push(Kid( 35 | walletAddress, 36 | firstName, 37 | lastName, 38 | releaseTime, 39 | amount, 40 | canWithdraw 41 | )); 42 | } 43 | 44 | function balanceOf() public view returns(uint) { 45 | return address(this).balance; 46 | } 47 | 48 | //deposit funds to contract, specifically to a kid's account 49 | function deposit(address walletAddress) payable public { 50 | addToKidsBalance(walletAddress); 51 | } 52 | 53 | function addToKidsBalance(address walletAddress) private { 54 | for(uint i = 0; i < kids.length; i++) { 55 | if(kids[i].walletAddress == walletAddress) { 56 | kids[i].amount += msg.value; 57 | emit LogKidFundingReceived(walletAddress, msg.value, balanceOf()); 58 | } 59 | } 60 | } 61 | 62 | function getIndex(address walletAddress) view private returns(uint) { 63 | for(uint i = 0; i < kids.length; i++) { 64 | if (kids[i].walletAddress == walletAddress) { 65 | return i; 66 | } 67 | } 68 | return 999; 69 | } 70 | 71 | // kid checks if able to withdraw 72 | function availableToWithdraw(address walletAddress) public returns(bool) { 73 | uint i = getIndex(walletAddress); 74 | require(block.timestamp > kids[i].releaseTime, "You cannot withdraw yet"); 75 | if (block.timestamp > kids[i].releaseTime) { 76 | kids[i].canWithdraw = true; 77 | return true; 78 | } else { 79 | return false; 80 | } 81 | } 82 | 83 | // withdraw money 84 | function withdraw(address payable walletAddress) payable public { 85 | uint i = getIndex(walletAddress); 86 | require(msg.sender == kids[i].walletAddress, "You must be the kid to withdraw"); 87 | require(kids[i].canWithdraw == true, "You are not able to withdraw at this time"); 88 | kids[i].walletAddress.transfer(kids[i].amount); 89 | } 90 | 91 | } 92 | 93 | 94 | 95 | 96 | --------------------------------------------------------------------------------