/
DAOFund.sol
387 lines (305 loc) · 12 KB
/
DAOFund.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
pragma solidity ^0.4.15;
import './IDAOToken.sol';
import './IDAOVault.sol';
import 'zeppelin-solidity/contracts/math/SafeMath.sol';
/// @title ICO fund controlled by the investors
contract DAOFund {
using SafeMath for uint256;
// Vote state
enum ApprovalState {
// default
NotVoted,
Approval,
Disapproval
}
// Decision point description for the DAO
struct KeyPoint {
// duration of the period which is evaluated at this point
uint duration;
// funds share percent to be transfered to the project at this keypoint
uint fundsShare;
}
// Dynamic state of a KeyPoint
struct KeyPointState {
// true iff appropriate actions were taken for the keypoint
bool processed;
// true iff decision was made to further finance the project
bool success;
// votes are no longer accepted at this time
uint votingEndTime;
// current amount of tokens supporting the next tranche to the project
uint approvalVotes;
// current amount of tokens supporting cancelation of the project
uint disapprovalVotes;
// votes of each investor
mapping(address => ApprovalState) approvalState;
// status of an investor as a delegate
mapping(address => DelegateState) delegateState;
// status of an investor as a delegator
mapping(address => DelegatorState) delegatorState;
}
// State of investor to which voting was delegated
struct DelegateState {
// note: votesAccumulated is always up-to-date, even after voting
uint votesAccumulated;
}
// State of investor which delegates voting to other
struct DelegatorState {
address delegate;
}
// event fired when the DAO comes to conclusion about a KeyPoint
event KeyPointResolved(uint keyPointIndex, bool success);
// lazy initialization
modifier initialized {
if (0 == m_keyPointState.length) {
// first tranche after the ICO
m_keyPointState.push(createKeyPointState(getTime()));
m_keyPointState[0].processed = true;
m_keyPointState[0].success = true;
KeyPointResolved(0, true);
m_vault.transferToTeam(getCurrentKeyPoint().fundsShare);
initNextKeyPoint();
assert(isActive());
}
_;
}
modifier onlyActive {
require(isActive());
_;
}
modifier onlyRefunding {
require(isRefunding());
_;
}
modifier onlyFinished {
require(isFinished());
_;
}
modifier onlyTokenHolder {
require(m_token.balanceOf(msg.sender) > 0);
_;
}
modifier onlyToken {
require(msg.sender == address(m_token));
_;
}
modifier notVoted {
require(getCurrentKeyPointState().approvalState[msg.sender] == ApprovalState.NotVoted);
_;
}
modifier notDelegator {
require(address(0) == getDelegatorState(msg.sender).delegate);
_;
}
// PUBLIC interface
function DAOFund(IDAOVault vault, IDAOToken token, uint approveMarginPercent){
m_vault = vault;
m_token = token;
require(approveMarginPercent <= 100);
// fund configuration
// this configuration could be set in an inheriting contract
m_approveMarginPercent = approveMarginPercent;
m_keyPoints.push(KeyPoint({duration: 0 weeks, fundsShare: 25}));
m_keyPoints.push(KeyPoint({duration: 40 weeks, fundsShare: 45}));
m_keyPoints.push(KeyPoint({duration: 20 weeks, fundsShare: 30}));
// end of fund configuration
validateKeyPoints();
}
/// @notice approves of disapproves current keypoint
function approveKeyPoint(bool approval)
external
initialized
onlyActive
onlyTokenHolder
notVoted
notDelegator
{
KeyPointState storage state = getCurrentKeyPointState();
require(getTime() < state.votingEndTime);
state.approvalState[msg.sender] = approval ? ApprovalState.Approval : ApprovalState.Disapproval;
addVotingTokens(msg.sender, m_token.balanceOf(msg.sender).add(getDelegateState(msg.sender).votesAccumulated));
}
/// @notice delegates your vote to other investor
function delegate(address to)
external
initialized
onlyActive
onlyTokenHolder
notVoted
notDelegator
{
require(m_token.balanceOf(to) > 0);
// finding final delegate in the chain
address delegate = getDelegate(to);
// breaking loops
require(delegate != msg.sender);
// transfer accumulated
DelegateState storage senderDelegateState = getDelegateState(msg.sender);
uint senderTotalVotes = senderDelegateState.votesAccumulated.add(m_token.balanceOf(msg.sender));
senderDelegateState.votesAccumulated = 0;
getDelegateState(delegate).votesAccumulated = getDelegateState(delegate).votesAccumulated.add(senderTotalVotes);
addVotingTokens(delegate, senderTotalVotes);
// mark sender as a delegator
getDelegatorState(msg.sender).delegate = delegate;
}
/// @notice takes appropriate action after voting is finished
function executeKeyPoint()
external
initialized
onlyActive
{
KeyPointState storage state = getCurrentKeyPointState();
assert(!state.processed);
require(getTime() >= state.votingEndTime);
state.processed = true;
state.success = isKeyPointApproved();
KeyPointResolved(m_keyPointState.length - 1, state.success);
if (state.success) {
m_vault.transferToTeam(getCurrentKeyPoint().fundsShare);
if (m_keyPointState.length < m_keyPoints.length)
initNextKeyPoint();
}
}
/// @notice requests refund in case the project is failed
function refund()
external
initialized
onlyRefunding
onlyTokenHolder
{
uint numerator = m_token.balanceOf(msg.sender);
uint denominator = m_token.totalSupply();
assert(numerator <= denominator);
m_token.burnFrom(msg.sender, m_token.balanceOf(msg.sender));
m_vault.refund(msg.sender, numerator, denominator);
}
/// @dev callback for the token
function onTokenTransfer(address from, address to, uint amount)
external
initialized
onlyToken
{
if (!isActive())
return;
address delegate = getDelegate(from);
if (delegate == from)
// not a delegator
subVotingTokens(from, amount);
else {
// delegator - updates delegate state
getDelegateState(delegate).votesAccumulated = getDelegateState(delegate).votesAccumulated.sub(amount);
subVotingTokens(delegate, amount);
}
delegate = getDelegate(to);
if (delegate == to)
// not a delegator
addVotingTokens(to, amount);
else {
// delegator - updates delegate state
getDelegateState(delegate).votesAccumulated = getDelegateState(delegate).votesAccumulated.add(amount);
addVotingTokens(delegate, amount);
}
}
/// @notice explicit init function
function init() external initialized {}
// PUBLIC: fund state
function isActive() public constant returns (bool) {
return !isFinished() && !isRefunding();
}
function isRefunding() public constant returns (bool) {
assert(m_keyPoints.length >= m_keyPointState.length);
return getCurrentKeyPointState().processed && !getCurrentKeyPointState().success;
}
function isFinished() public constant returns (bool) {
assert(m_keyPoints.length >= m_keyPointState.length);
return m_keyPoints.length == m_keyPointState.length
&& getCurrentKeyPointState().processed && getCurrentKeyPointState().success;
}
// INTERNALS: keypoints
function validateKeyPoints() private constant {
assert(m_keyPoints.length > 1);
uint fundsTotal;
for (uint i = 0; i < m_keyPoints.length; i++) {
KeyPoint storage keyPoint = m_keyPoints[i];
if (0 == i)
assert(0 == keyPoint.duration); // initial tranche happens immediately
else
assert(keyPoint.duration >= 1 weeks);
fundsTotal = fundsTotal.add(keyPoint.fundsShare);
}
assert(100 == fundsTotal);
}
function initNextKeyPoint() private {
assert(m_keyPoints.length > m_keyPointState.length);
KeyPoint storage keyPoint = m_keyPoints[m_keyPointState.length];
m_keyPointState.push(createKeyPointState(getTime() + keyPoint.duration));
}
function getCurrentKeyPoint() internal constant returns (KeyPoint storage) {
return m_keyPoints[m_keyPointState.length - 1];
}
function getCurrentKeyPointState() internal constant returns (KeyPointState storage) {
return m_keyPointState[m_keyPointState.length - 1];
}
function createKeyPointState(uint votingEndTime) private constant returns (KeyPointState memory) {
return KeyPointState({processed: false, success: false,
votingEndTime: votingEndTime, approvalVotes: 0, disapprovalVotes: 0});
}
// INTERNALS
function addVotingTokens(address tokenOwner, uint amount) private {
KeyPointState storage state = getCurrentKeyPointState();
ApprovalState vote = state.approvalState[tokenOwner];
if (vote == ApprovalState.Approval) {
state.approvalVotes = state.approvalVotes.add(amount);
} else if (vote == ApprovalState.Disapproval) {
state.disapprovalVotes = state.disapprovalVotes.add(amount);
}
}
function subVotingTokens(address tokenOwner, uint amount) private {
KeyPointState storage state = getCurrentKeyPointState();
ApprovalState vote = state.approvalState[tokenOwner];
if (vote == ApprovalState.Approval) {
state.approvalVotes = state.approvalVotes.sub(amount);
} else if (vote == ApprovalState.Disapproval) {
state.disapprovalVotes = state.disapprovalVotes.sub(amount);
}
}
function getDelegateState(address investor) private constant returns (DelegateState storage result) {
result = getCurrentKeyPointState().delegateState[investor];
// mutually exclusive state
assert(0 == result.votesAccumulated || address(0) == getCurrentKeyPointState().delegatorState[investor].delegate);
}
function getDelegatorState(address investor) private constant returns (DelegatorState storage result) {
result = getCurrentKeyPointState().delegatorState[investor];
// mutually exclusive state
assert(address(0) == result.delegate || 0 == getCurrentKeyPointState().delegateState[investor].votesAccumulated);
}
/// @dev finds final delegate in the delegation chain of the investor
function getDelegate(address investor) private constant returns (address result) {
result = investor;
while (true) {
address next = getDelegatorState(result).delegate;
if (address(0) == next)
break;
else
result = next;
}
}
/// @dev main decision maker
function isKeyPointApproved() private constant returns (bool) {
KeyPointState storage state = getCurrentKeyPointState();
uint totalVotes = state.approvalVotes.add(state.disapprovalVotes);
if (0 == totalVotes)
return true;
return state.approvalVotes > state.disapprovalVotes.add(totalVotes.mul(m_approveMarginPercent).div(100));
}
/// @dev to be overridden in tests
function getTime() internal constant returns (uint) {
return now;
}
// FIELDS
IDAOVault public m_vault;
IDAOToken public m_token;
uint public m_approveMarginPercent;
KeyPoint[] public m_keyPoints;
KeyPointState[] public m_keyPointState;
}