-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCryptoKids.sol
78 lines (77 loc) · 2.58 KB
/
CryptoKids.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// 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 walletAdress;
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 walletAdress, string memory firstname, string memory lastname, uint releaseTime, uint amount, bool canWithdraw) public onlyOwner {
kids.push(Kid(
walletAdress,
firstname,
lastname,
releaseTime,
amount,
canWithdraw
));
}
// view aand pure
function balancedOf() public view returns(uint){
return address(this).balance;
}
// deposit funds to contract, specifically to a kid's account
function deposit(address walletAdress) payable public {
addToKidsBalance(walletAdress);
}
function addToKidsBalance(address walletAdress) private {
for(uint i = 0; i < kids.length; i++) {
if(kids[i].walletAdress == walletAdress) {
kids[i].amount += msg.value;
emit LogKidFundingReceived(walletAdress, msg.value, balancedOf());
}
}
}
function getIndex(address walletAdress) view private returns(uint) {
for(uint i = 0; i < kids.length ; i++) {
if (kids[i].walletAdress == walletAdress) {
return i;
}
}
return 999;
}
// kid checks if able to withdraw
function availableToWithDraw(address walletAdress) public returns(bool) {
uint i = getIndex(walletAdress);
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 walletAdress) payable public {
uint i = getIndex(walletAdress);
require(msg.sender == kids[i].walletAdress, "You must be the kid to withdraw");
require(kids[i].canWithdraw == true, "You are not able to withdraw at this time");
kids[i].walletAdress.transfer(kids[i].amount);
}
}