-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAncestorContract.sol
More file actions
102 lines (91 loc) · 2.95 KB
/
Copy pathAncestorContract.sol
File metadata and controls
102 lines (91 loc) · 2.95 KB
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
92
93
94
95
96
97
98
99
100
101
102
//solidity
pragma solidity ^0.8.17;
contract myContract{
//owner dad
//persists data will stay on contract
address owner;
event LogKidsFunding(address addr,uint amount,uint balance);
constructor(){
owner=msg.sender;
}
//define kid
//memory
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 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
));
}
//functions
//view balance
//pure restricts you to access contract members
function balanceOf() public view returns(uint){
return address(this).balance;
}
//deposit funds to contract specific to 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 LogKidsFunding(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 99;
}
//kids checks if able to withdraw
function avaliableToWithdraw(address walletAddress) public returns(bool){
uint index=getIndex(walletAddress);
require(block.timestamp>kids[index].releaseTime,"you are not ready to withdraw");
if(block.timestamp>kids[index].releaseTime)
{
kids[index].canwithdraw=true;
return true;
}
return false;
}
//withdraw money
function withdraw(address payable walletAddress) payable public{
uint index=getIndex(walletAddress);
require(msg.sender==kids[index].walletAddress,"you are not the rightful kid to withdraw");
require(kids[index].canwithdraw==true,"you are not able to withdraw at this time");
kids[index].walletAddress.transfer(kids[index].amount);
}
}
// uint amount;//non negative integer
// bool isTotal;//booolean
// string 'hello'//string
// address walletAddress;//address