Skip to content

Instantly share code, notes, and snippets.

@saxenanickk
Created September 24, 2022 05:55
Show Gist options
  • Save saxenanickk/74469bf745f7a91f9fc275a2220c7cdc to your computer and use it in GitHub Desktop.
Save saxenanickk/74469bf745f7a91f9fc275a2220c7cdc to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.7+commit.e28d00a7.js&optimize=false&runs=200&gist=
// SPDX-License-Identifier: GPL-3.0
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 kid's account
function deposit(address walletAddress) payable public {
addToKidsBalance(walletAddress);
}
function addToKidsBalance(address walletAddress) private onlyOwner {
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 should be able to check 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;
}
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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment