-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSimplePyramid.sol
52 lines (44 loc) · 1.74 KB
/
SimplePyramid.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
//////////////////////////////////////////////////
// see https://etherscan.io/address/0x9b0033bccf2d913dd17c08a5844c9dd31dd34833#code
contract SimplePyramid {
uint public constant MINIMUM_INVESTMENT = 1e15; // 0.001 ether
uint public numInvestors = 0;
uint public depth = 0;
address[] public investors;
mapping(address => uint) public balances;
function SimplePyramid () public payable {
require(msg.value >= MINIMUM_INVESTMENT);
investors.length = 3;
investors[0] = msg.sender;
numInvestors = 1;
depth = 1;
balances[address(this)] = msg.value;
}
function () payable public {
require(msg.value >= MINIMUM_INVESTMENT);
balances[address(this)] += msg.value;
numInvestors += 1;
investors[numInvestors - 1] = msg.sender;
if (numInvestors == investors.length) {
// pay out previous layer
uint endIndex = numInvestors - 2**depth;
uint startIndex = endIndex - 2**(depth-1);
for (uint i = startIndex; i < endIndex; i++)
balances[investors[i]] += MINIMUM_INVESTMENT;
// spread remaining ether among all participants
uint paid = MINIMUM_INVESTMENT * 2**(depth-1);
uint eachInvestorGets = (balances[address(this)] - paid) / numInvestors;
for(i = 0; i < numInvestors; i++)
balances[investors[i]] += eachInvestorGets;
// update state variables
balances[address(this)] = 0;
depth += 1;
investors.length += 2**depth;
}
}
function withdraw () public {
uint payout = balances[msg.sender];
balances[msg.sender] = 0;
msg.sender.transfer(payout);
}
}