Skip to content

Instantly share code, notes, and snippets.

@rodgtr1
Created April 7, 2022 12:31
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save rodgtr1/427a6e0cea78281fb9ad8ea9980bb5a2 to your computer and use it in GitHub Desktop.
Save rodgtr1/427a6e0cea78281fb9ad8ea9980bb5a2 to your computer and use it in GitHub Desktop.
CryptoKids Solidity Smart Contract - Tutorial at https://youtu.be/s9MVkHKV2Vw
// 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);
}
}
@DeveloperAnas17
Copy link

I really like it.

@EDBCREPO
Copy link

thanks homie, 10/10

@SoulTech2022
Copy link

Thanks a lot for nice tutorial

@YourDailyBlockchain
Copy link

Very cool demo and tutorial - thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment