-
Notifications
You must be signed in to change notification settings - Fork 0
/
Inheritance.sol
84 lines (68 loc) · 1.83 KB
/
Inheritance.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
// SPDX-License-Identifier: None
pragma solidity ^0.8.7;
contract CrpyptoKids {
address owner;
constructor() {
owner == msg.sender;
}
struct Kid {
address payable wallet;
string name;
string lastName;
uint releaseTime;
uint amount;
bool canWithdraw;
}
Kid[] public kids;
//add kids to contract
function addKid(address payable wallet, string memory name, string memory lastName, uint releaseTime, uint amount, bool canWithdraw) public {
kids.push(Kid(
wallet,
name,
lastName,
releaseTime,
amount,
canWithdraw
));
}
//wallet balance public view
function walletBalance() public view returns(uint) {
return address(this).balance;
}
//add funds to contract, and speccifically to kid(s)
function deposit(address wallet) payable public {
addToBalance(wallet) ;
}
function addToBalance(address wallet) private{
for(uint i = 0; i < kids.length; i++) {
if(kids[i].wallet == wallet) {
kids[i].amount += msg.value;
}
}
}
/// do not like this 999 thing
function getIndex(address wallet) public view returns(uint) {
for(uint i = 0; i < kids.length; i++) {
if (kids[i].wallet == wallet) {
return i;
}
}
return 999;
}
//kid checks if can withdraw (hate all these loops, not gas efficiet i will learn better methods)
//not my contract would not use this time stamp method, vulnerable to hack from miners
function available(address wallet) public returns(bool) {
uint i = getIndex(wallet);
if (block.timestamp > kids[i].releaseTime) {
kids[i].canWithdraw = true;
return true;
} else {
return false;
}
}
//withdraw funds
function withdraw(address payable wallet) payable public {
uint i = getIndex(wallet);
kids[i].wallet.transfer(kids[i].amount);
}
}