forked from tonyin/the-daio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
daio.sol
224 lines (204 loc) · 7.07 KB
/
daio.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
pragma solidity ^0.4.15;
contract SafeMath {
function safeAdd(uint a, uint b) internal returns(uint) {
uint c = a + b;
assert(c >= a && c >= b);
return c;
}
function safeSub(uint a, uint b) internal returns (uint) {
assert (b <= a);
return a - b;
}
function safeMul(uint a, uint b) internal returns (uint) {
uint c = a * b;
assert(a == 0 || c / a == b);
return c;
}
}
contract Daio is SafeMath {
uint public membersMinimum = 2;
uint public membersTotal;
Member[] public members;
mapping(address => uint256) public memberId;
event MembershipChanged(address member, bool isMember);
struct Member {
address member;
string name;
uint share;
}
uint public fundTotal;
uint public fundShare;
uint public contributionMinimumTime;
uint public fundMinimumTime;
bool public fundActive = false;
event FundingChanged(uint total);
event SurplusReturned(uint share, uint shareMax, uint surplus);
event FundLaunched(uint funds, uint mems, uint share, uint minTime);
event ShareDistributed(address member, string name, uint share);
event FundLiquidated(uint funds, uint mems, uint share);
uint public proposalsTotal;
Proposal[] public proposals;
event ProposalAdded(uint proposalId, address recipient, uint volume, uint price);
event ProposalPassed(uint proposalId, bool passed, uint votesFor);
event ProposalExecuted(uint proposalId, address recipient, uint volume, uint price);
struct Proposal {
address recipient;
uint volume;
uint price;
string description;
uint deadline;
bool passed;
bool executed;
bytes32 proposalHash;
uint votesFor;
uint votesTotal;
Vote[] votes;
mapping(address => bool) voted;
}
event Voted(uint proposalId, bool support, address member);
struct Vote {
address member;
bool support;
}
modifier onlyMembers {
require(memberId[msg.sender] != 0);
_;
}
modifier onlyNew {
require(memberId[msg.sender] == 0);
_;
}
function Daio(uint contributionMinimumMinutes) payable public {
require(msg.value > 0);
contributionMinimumTime = safeAdd(now, contributionMinimumMinutes * 1 minutes);
addMember(0, "daio", 0); // dummy daio for member 0
addMember(msg.sender, "founder", msg.value);
fundShare = msg.value;
fundTotal = safeAdd(fundTotal, msg.value);
FundingChanged(fundTotal);
}
function addMember (address member, string name, uint share) private {
uint id = memberId[member];
if (id == 0) {
memberId[member] = members.length;
id = members.length++;
}
members[id] = Member({
member: member,
name: name,
share: share
});
membersTotal = id;
MembershipChanged(member, true);
}
function removeMember (uint id) private {
address member = members[id].member;
memberId[member] = 0;
delete members[id];
members.length--;
}
function contributeFund(string memberName) payable onlyNew public {
require(!fundActive);
require(msg.value >= fundShare);
uint share = msg.value;
if (fundShare == 0) { // first contribution
fundShare = share;
}
if (share > fundShare) {
uint surplus = safeSub(share, fundShare);
share = safeSub(share, surplus);
msg.sender.transfer(surplus);
SurplusReturned(msg.value, fundShare, surplus);
}
addMember(msg.sender, memberName, share);
fundTotal = safeAdd(fundTotal, share);
FundingChanged(fundTotal);
}
function launchFund(uint fundMinimumMinutes) onlyMembers public {
require(now >= contributionMinimumTime);
require(!fundActive);
require(members.length-1 >= membersMinimum);
fundActive = true;
fundMinimumTime = safeAdd(now, fundMinimumMinutes * 1 minutes);
FundLaunched(fundTotal, membersTotal, fundShare, fundMinimumTime);
}
function addProposal(
address recipient,
uint volume,
uint price,
string description,
uint votingMinutes,
bytes transactionBytecode
) onlyMembers public
{
require(fundActive == true);
uint proposalId = proposals.length++;
Proposal storage p = proposals[proposalId]; // construct explicitly
p.recipient = recipient;
p.volume = volume;
p.price = price;
p.description = description;
p.deadline = safeAdd(now, votingMinutes * 1 minutes);
p.passed = false;
p.executed = false;
p.votesFor = 0;
p.votesTotal = 0;
p.proposalHash = keccak256(recipient, volume, price, transactionBytecode);
proposalsTotal = proposals.length;
ProposalAdded(proposalId, recipient, volume, price);
}
function voteProposal(uint proposalId, bool support) onlyMembers public {
Proposal storage p = proposals[proposalId];
require(!p.voted[msg.sender]);
require(now <= p.deadline);
p.voted[msg.sender] = true;
uint voteId = p.votes.length++;
p.votesTotal = p.votes.length;
if (support) {
p.votesFor += 1;
}
p.votes[voteId] = Vote({
member: msg.sender,
support: support
});
Voted(proposalId, support, msg.sender);
if (p.votesFor > membersTotal / 2) { // simple majority (>50%). Note: standard integer division returns floor
p.passed = true;
ProposalPassed(proposalId, true, p.votesFor);
}
}
function checkProposalCode(
uint proposalId,
address recipient,
uint volume,
uint price,
bytes transactionBytecode
) constant public returns(bool codeCheckCorrect) {
Proposal storage p = proposals[proposalId];
return p.proposalHash == keccak256(recipient, volume, price, transactionBytecode);
}
function executeProposal(uint proposalId, bytes transactionBytecode) onlyMembers public {
Proposal storage p = proposals[proposalId];
require(p.passed);
require(!p.executed);
require(checkProposalCode(proposalId, p.recipient, p.volume, p.price, transactionBytecode));
ProposalExecuted(proposalId, p.recipient, p.volume, p.price);
}
function liquidateFund() onlyMembers payable public {
require(now >= fundMinimumTime);
require(fundActive);
fundActive = false;
uint share = fundTotal / membersTotal;
for (uint i = 1; i < members.length; i++) {
members[i].member.transfer(share);
ShareDistributed(members[i].member, members[i].name, share);
}
FundLiquidated(fundTotal, membersTotal, share);
for (i = 1; i <= members.length; i++) {
removeMember(i);
}
membersTotal = 0;
fundTotal = 0;
fundShare = 0;
}
}