-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnippet.sol
91 lines (77 loc) · 2.61 KB
/
snippet.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
79
80
81
82
83
84
85
86
87
88
89
90
91
// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;
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);
}
}
// Tutorial Link: https://youtu.be/s9MVkHKV2Vw