Created
April 7, 2022 12:31
-
-
Save rodgtr1/427a6e0cea78281fb9ad8ea9980bb5a2 to your computer and use it in GitHub Desktop.
CryptoKids Solidity Smart Contract - Tutorial at https://youtu.be/s9MVkHKV2Vw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: Unlicensed | |
pragma solidity ^0.8.7; | |
contract CryptoKids { | |
// owner DAD | |
address owner; | |
event LogKidFundingReceived(address addr, uint amount, uint contractBalance); | |
constructor() { | |
owner = msg.sender; | |
} | |
// define Kid | |
struct Kid { | |
address payable walletAddress; | |
string firstName; | |
string lastName; | |
uint releaseTime; | |
uint amount; | |
bool canWithdraw; | |
} | |
Kid[] public kids; | |
modifier onlyOwner() { | |
require(msg.sender == owner, "Only the owner can add kids"); | |
_; | |
} | |
// add kid to contract | |
function addKid(address payable walletAddress, string memory firstName, string memory lastName, uint releaseTime, uint amount, bool canWithdraw) public onlyOwner { | |
kids.push(Kid( | |
walletAddress, | |
firstName, | |
lastName, | |
releaseTime, | |
amount, | |
canWithdraw | |
)); | |
} | |
function balanceOf() public view returns(uint) { | |
return address(this).balance; | |
} | |
//deposit funds to contract, specifically to a kid's account | |
function deposit(address walletAddress) payable public { | |
addToKidsBalance(walletAddress); | |
} | |
function addToKidsBalance(address walletAddress) private { | |
for(uint i = 0; i < kids.length; i++) { | |
if(kids[i].walletAddress == walletAddress) { | |
kids[i].amount += msg.value; | |
emit LogKidFundingReceived(walletAddress, msg.value, balanceOf()); | |
} | |
} | |
} | |
function getIndex(address walletAddress) view private returns(uint) { | |
for(uint i = 0; i < kids.length; i++) { | |
if (kids[i].walletAddress == walletAddress) { | |
return i; | |
} | |
} | |
return 999; | |
} | |
// kid checks if able to withdraw | |
function availableToWithdraw(address walletAddress) public returns(bool) { | |
uint i = getIndex(walletAddress); | |
require(block.timestamp > kids[i].releaseTime, "You cannot withdraw yet"); | |
if (block.timestamp > kids[i].releaseTime) { | |
kids[i].canWithdraw = true; | |
return true; | |
} else { | |
return false; | |
} | |
} | |
// withdraw money | |
function withdraw(address payable walletAddress) payable public { | |
uint i = getIndex(walletAddress); | |
require(msg.sender == kids[i].walletAddress, "You must be the kid to withdraw"); | |
require(kids[i].canWithdraw == true, "You are not able to withdraw at this time"); | |
kids[i].walletAddress.transfer(kids[i].amount); | |
} | |
} |
thanks homie, 10/10
Thanks a lot for nice tutorial
Very cool demo and tutorial - thanks.
Thankyou for the video
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I really like it.