Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // SPDX-License-Identifier: Unlicensed
- pragma solidity ^0.8.7;
- // Data Types
- // uint amountOfMoney (int)
- // bool isTotal
- // string 'Hello'
- // address walletAddress
- // uint[5] names; // declaring array of five
- // uint[] names;
- // names.push('Travis')
- // names[0] = 'Phil'
- // delete names
- // Dictionaries/ Maps
- // mapping (string => uint) public kids
- // Functions
- // function increment(uint x) returns(uint) {
- // x ++;
- // return x
- // }
- // increment(10)
- 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) { // !!! returns !!! / !!! View !!! (instead of view you can use pure, but pure is more strict than view)
- return address(this).balance;
- } // BalanceOf appears on the Depliyed Contract
- // deposit funds to contract, specifical to a kid's account // deposit button in the contract appears
- 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);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement