-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWill.sol
99 lines (73 loc) · 2.57 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
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
pragma solidity ^0.4.18;
import './Destructible.sol';
contract Will is Destructible {
uint8 public minValidators;
struct Recipient {
address recipient;
uint8 percent;
}
struct Validator {
address validator;
bool hasValidated;
}
Recipient[] public recipients;
Validator[] public validators;
function Will(address[] _recipients, uint8[] _percentages, address[] _validators, uint8 _minValidators) payable public {
uint8 totalPercentage;
assert(_recipients.length == _percentages.length);
recipients.length = _recipients.length;
for (uint i = 0; i < _recipients.length; i++) {
address recipientAddress = _recipients[i];
uint8 percent = _percentages[i];
assert(recipientAddress != address(0));
totalPercentage += percent;
recipients[i] = Recipient(recipientAddress, percent);
}
assert(totalPercentage == 100);
minValidators = _minValidators;
assert(minValidators <= _validators.length);
validators.length = _validators.length;
for (uint j = 0; j < _validators.length; j++) {
address validatorAddress = _validators[j];
bool hasValidated = false;
validators[j] = Validator(validatorAddress, hasValidated);
}
}
function validate() public {
updateValidationState();
if (countValidated() >= minValidators) {
distributeFunds();
}
}
function updateValidationState() private {
for (uint i = 0; i < validators.length; i++) {
if (msg.sender == validators[i].validator) {
validators[i].hasValidated = true;
break;
}
}
}
function countValidated() private view returns (uint) {
uint validated;
for (uint i = 0; i < validators.length; i++) {
if (validators[i].hasValidated == true) {
validated++;
}
}
return validated;
}
function distributeFunds() private {
uint[] memory amounts = calculateAmounts();
for (uint i = 0; i < amounts.length; i++) {
recipients[i].recipient.transfer(amounts[i]);
}
}
function calculateAmounts() private view returns (uint[]) {
uint[] memory amounts = new uint[](recipients.length);
for (uint i = 0; i < recipients.length; i++) {
amounts[i] = (this.balance * recipients[i].percent) / 100;
}
return amounts;
}
function() payable public {}
}