-
Notifications
You must be signed in to change notification settings - Fork 0
/
Will.sol
71 lines (60 loc) · 2.19 KB
/
Will.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
pragma solidity >=0.4.22 <0.6.0;
contract EthWill {
enum State {INITIATED, COMPLETE}
State currentState;
address benefactor;
address representative;
address payable[] beneficiaries;
mapping(address => uint) public assigned_eth;
constructor(address executer) public {
require(executer != msg.sender);
benefactor = msg.sender;
representative = executer;
}
/*-----------------<Modifiers>--------------------*/
modifier onlyBenefactor() {
require(msg.sender == benefactor);
_;
}
modifier restricted() {
require(msg.sender == representative || msg.sender == benefactor);
_;
}
modifier inState(State expectedState) {
require(currentState == expectedState);
_;
}
/*-----------------<Will Functions>--------------------*/
function assign_eth(address payable[] memory receivers, uint256[] memory eth_finney_amnts) onlyBenefactor payable public {
require(msg.value >= totalArray(eth_finney_amnts) * 1 finney);
for(uint i = 0; i < receivers.length; i ++) {
beneficiaries.push(receivers[i]);
assigned_eth[receivers[i]] = eth_finney_amnts[i];
}
currentState = State.INITIATED;
}
function execute_will() public inState(State.INITIATED) restricted {
for(uint i = 0; i < beneficiaries.length; i ++) {
beneficiaries[i].transfer(assigned_eth[beneficiaries[i]] * 1 finney);
}
currentState = State.COMPLETE;
}
function resetWill(uint wei_amount) public inState(State.INITIATED) onlyBenefactor {
for(uint i = 0; i < beneficiaries.length; i++) {
assigned_eth[beneficiaries[i]] = 0;
}
delete beneficiaries;
msg.sender.transfer(wei_amount * 1 wei);
}
function changeRepresentative(address rep) public onlyBenefactor {
representative = rep;
}
/*-----------------<Helper>--------------------*/
function totalArray(uint256[] memory amounts) private pure returns (uint) {
uint total;
for(uint i = 0; i < amounts.length; i++) {
total += amounts[i];
}
return total;
}
}