-
Notifications
You must be signed in to change notification settings - Fork 2
/
LazyLooseMoney.sol
266 lines (236 loc) · 7.81 KB
/
LazyLooseMoney.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
pragma solidity ^ 0.4.21;
contract LlmFactory {
// contract address storge
address[] public commitments; // store all commitment contract addresses
mapping(address => address) public currentCommitment; // current commitment contract address of msg.sender (the committer)
/* events to log */
event CommitmentCreated(address indexed owner, address commitment, string title, uint daysCount, uint createdAt);
function createCommitment(string _title, uint _days)
public
payable
{
bool noActiveCommitment = false;
if (currentCommitment[msg.sender] == 0x0) {
noActiveCommitment = true;
} else {
if (Commitment(currentCommitment[msg.sender]).getState() == Commitment.State.Closed) {
noActiveCommitment = true;
}
}
/* require(noActiveCommitment); */
address newCommitment;
newCommitment = (new Commitment).value(msg.value)(msg.sender, _title, _days);
commitments.push(newCommitment);
currentCommitment[msg.sender] = newCommitment;
emit CommitmentCreated(msg.sender, newCommitment, _title, _days, now);
}
function getCurrentCommitmentAddress()
public
view
returns (address)
{
return currentCommitment[msg.sender];
}
function getLastCommitmentAddress()
public
view
returns (address)
{
return commitments[commitments.length - 1];
}
}
contract Commitment {
address BURN_ADDRESS = 0x0;
/* data structure */
string public title;
address public owner;
address public guardian;
mapping(address => uint) public supportersFunded;
uint deposit;
uint guardianDeposit;
uint burned;
uint daysCount;
uint reportedDays;
uint completedDays;
uint pendingDays;
uint createdAt;
uint startedAt;
uint guardedAt;
uint finishedAt;
enum State {
Opened,
Guarded,
Started,
Pending,
Closed
}
State state;
struct DailyReport {
bool completed; // guardian report if the commitment for today is completed or not
bool complained; // owner can complain about daily_report correctness
bool pending; // guardian can report a pending day if committer got sick ...
// https://ethereum.stackexchange.com/questions/13021/how-can-you-figure-out-if-a-certain-key-exists-in-a-mapping-struct-defined-insi
bool reported;
}
mapping(uint => DailyReport) dailyReports;
/* modifiers */
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
modifier onlyGuardian() {
require(msg.sender == guardian);
_;
}
/* constructor */
function Commitment(address _creator, string _title, uint _days)
public
payable
{
require(_days > 0);
/* require(msg.value >= _days * 1 finney); */
owner = _creator;
deposit = msg.value;
burned = 0;
title = _title;
daysCount = _days;
reportedDays = 0;
completedDays = 0;
pendingDays = 0;
state = State.Opened;
createdAt = now;
if (daysCount > 10) {
guardianDeposit = 2*(deposit / daysCount);
} else {
guardianDeposit = deposit / 5;
}
emit Created(this, owner, deposit, title, daysCount, createdAt);
}
/* events to log */
event Created(address indexed commitment, address indexed owner, uint deposit, string title, uint daysCount, uint createdAt);
event fundAdded(address indexed commitment, address indexed supporter, uint value, string encouragement, uint fundedAt);
event Started(address indexed commitment, address indexed owner, uint startedAt, uint finishedAt);
event Guarded(address indexed commitment, address indexed owner, address indexed guardian, uint guardianDeposit, uint guardedAt);
event Reported(address indexed commitment, address indexed owner, address indexed guardian, bool completed, uint reportedAt);
event Closed(address indexed commitment, address indexed owner, uint dayscount, uint reportedDays, uint completedDays, uint ownerReward, uint guardianReward, uint closedAt);
/* public functions */
function getState()
public
view
returns (State)
{
return state;
}
function getInfo()
public
view
returns (address, string, uint, uint, uint, State, uint, uint, address)
{
return (owner, title, deposit, guardianDeposit, daysCount, state, startedAt, finishedAt, guardian);
}
function supportFund(string encouragement)
public
payable
{
supportersFunded[msg.sender] += msg.value;
emit fundAdded(this, msg.sender, msg.value, encouragement, now);
}
/* owner functions */
function commit()
public
onlyOwner()
{
require(state == State.Guarded);
startedAt = now;
finishedAt = startedAt + daysCount * 1 days;
state = State.Started;
emit Started(this, owner, startedAt, finishedAt);
}
function cancel()
public
onlyOwner()
{
require(state != State.Closed);
uint x;
uint y;
if (guardian == 0x0) {
/* You loose half money */
x = this.balance / 2;
y = this.balance - x;
BURN_ADDRESS.transfer(y);
owner.transfer(x);
burned = burned + y;
} else {
/* You loose 2/3 money */
x = this.balance / 3;
y = this.balance - 2*x;
BURN_ADDRESS.transfer(y);
guardian.transfer(x);
owner.transfer(x);
burned = burned + y;
}
state = State.Closed;
emit Closed(this, owner, daysCount, reportedDays, completedDays, x, y, now);
}
/* guardian functions */
function release()
public
{
require(msg.sender == guardian || msg.sender == owner);
if (state < State.Started) {
/* If you too lazy to start, guardian get all money after 7 days */
require(now > guardedAt + 7 days);
reportedDays = 1;
} else {
require(state != State.Closed);
require(now > finishedAt);
if (reportedDays == 0) {
/* If guardian did not report at all, yoy get back your money */
completedDays = 1;
}
}
uint x = this.balance / (completedDays + reportedDays);
uint y = x * reportedDays;
x = this.balance - y;
guardian.transfer(y);
owner.transfer(x);
state = State.Closed;
emit Closed(this, owner, daysCount, reportedDays, completedDays, x, y, now);
}
function becomeGuardian()
public
payable
{
require(guardian == 0x0);
require(msg.sender != owner);
require(msg.value >= guardianDeposit);
guardian = msg.sender;
guardianDeposit = msg.value;
state = State.Guarded;
guardedAt = now;
emit Guarded(this, owner, guardian, guardianDeposit, guardedAt);
}
function report(bool completed)
public
onlyGuardian()
{
require(state == State.Started);
require(now < finishedAt + 23 hours);
uint _days = (now - startedAt) / 1 days;
require(_days < daysCount);
require(!dailyReports[_days].reported);
reportedDays = reportedDays + 1;
if (completed) {
completedDays = completedDays + 1;
}
dailyReports[_days].reported = true;
dailyReports[_days].completed = completed;
emit Reported(this, owner, guardian, completed, now);
}
/* helpers */
} // end Commitment construct
/*
// https://blog.aragon.one/advanced-solidity-code-deployment-techniques-dc032665f434
Can use web3 to get abi and bytecode
https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethgetcode
*/