-
Notifications
You must be signed in to change notification settings - Fork 1
/
Shareholders-Association.sol
171 lines (140 loc) · 4.33 KB
/
Shareholders-Association.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
pragma solidity ^0.4.2;
contract token { mapping(address=>uint256) public balanceOf; }
contract admined {
address public admin;
function admined(){
admin = msg.sender;
}
modifier onlyAdmin(){
if(msg.sender != admin) throw;
_;
}
function transferAdminship(address newAdmin) onlyAdmin{
admin = newAdmin;
}
}
contract Association is admined {
uint public minimumQuorum;
uint public debatingPeriodInMinutes;
Proposal[] public proposals;
uint public numProposals;
token public sharesTokenAddress;
modifier onlyShareholders {
if(sharesTokenAddress.balanceOf(msg.sender) == 0) throw;
_;
}
struct Proposal {
address recipient;
uint amount;
string description;
uint votingDeadline;
bool executed;
bool proposalPassed;
uint numberOfVotes;
int currentResult;
bytes32 proposalHash;
Vote[] votes;
mapping (address => bool) voted;
}
struct Vote {
bool inSupport;
address voter;
}
/* First time setup */
function Association(
address sharesAddress,
uint minimumSharesToPassAVote,
uint minutesForDebate,
address leader) payable {
changeVotingRules(sharesAddress, minimumSharesToPassAVote, minutesForDebate);
if(leader == 0) admin = msg.sender;
else admin = leader;
}
/*change rules*/
function changeVotingRules(
address sharesAddress,
uint minimumSharesToPassAVote,
uint minutesForDebate) onlyAdmin {
sharesTokenAddress = token(sharesAddress);
if(minimumSharesToPassAVote == 0) minimumSharesToPassAVote = 1;
minimumQuorum = minimumSharesToPassAVote;
debatingPeriodInMinutes = minutesForDebate;
}
/* Function to create a new proposal */
function newProposal(
address beneficiary,
uint etherAmount,
string jobDescription,
bytes transactionBytecode) onlyShareholders returns (uint proposalID){
proposalID = proposals.length;
proposals.length = proposals.length + 1;
Proposal p = proposals[proposalID];
p.recipient = beneficiary;
p.amount = etherAmount;
p.description = jobDescription;
p.proposalHash = sha3(beneficiary, etherAmount, transactionBytecode);
p.votingDeadline = now + debatingPeriodInMinutes * 1 minutes;
p.executed = false;
p.proposalPassed = false;
p.numberOfVotes = 0;
numProposals = proposalID+1;
return proposalID;
}
/* function to check if a proposal code matches */
function checkProposalCode(
uint proposalNumber,
address beneficiary,
uint etherAmount,
bytes transactionBytecode) constant returns (bool codeChecksOut){
Proposal p = proposals[proposalNumber];
return p.proposalHash == sha3(beneficiary, etherAmount, transactionBytecode);
}
function vote(
uint proposalNumber,
bool supportsProposal) onlyShareholders returns (uint voteID){
Proposal p = proposals[proposalNumber];
if(p.voted[msg.sender]) throw;
p.voted[msg.sender] = true;
voteID = p.votes.length++;
p.votes[voteID] = Vote({inSupport: supportsProposal, voter: msg.sender});
p.numberOfVotes++;
return voteID;
}
function executeProposal(uint proposalNumber, bytes transactionBytecode) {
Proposal p = proposals[proposalNumber];
if(now < p.votingDeadline ||
p.executed ||
p.proposalHash != sha3(p.recipient, p.amount, transactionBytecode)
)
throw;
uint quorum = 0;
uint yea = 0;
uint nay = 0;
for(uint i=0; i< p.votes.length; i++){
Vote v = p.votes[i];
uint voteWeight = sharesTokenAddress.balanceOf(v.voter);
quorum +=voteWeight;
if(v.inSupport){
yea += voteWeight;
}
else{
nay += voteWeight;
}
}
if(quorum <= minimumQuorum){
throw;
}
else if(yea > nay){
p.executed = true;
if(!p.recipient.call.value(p.amount * 1 ether)(transactionBytecode)){
throw;
}
p.proposalPassed = true;
}
else{
p.proposalPassed = false;
}
}
function () payable {
}
}