forked from Bit-Nation/BITNATION-Constitution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDBVN.sol
200 lines (166 loc) · 7.5 KB
/
DBVN.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
/*
This is a first proposal contract code for the seed of the Bitnation DAO
File lincensed under WTFPL: http://www.wtfpl.net
*/
contract owned {
address public owner;
function owned() {
owner = msg.sender;
}
modifier onlyOwner {
if (msg.sender != owner) throw;
_
}
function transferOwnership(address newOwner) onlyOwner {
owner = newOwner;
}
}
/* The token is used as a voting shares */
contract token { function mintToken(address target, uint256 mintedAmount); }
contract DBVN is owned {
/* Contract Variables and events */
uint public rankThreshold;
uint public debatingPeriodInMinutes;
int public majorityMargin;
Proposal[] public proposals;
uint public numProposals;
mapping (address => uint) public memberId;
Member[] public members;
string public constitutionURL;
event ProposalAdded(uint proposalID, address recipient, uint amount, string description);
event Voted(uint proposalID, bool position, address voter);
event ProposalTallied(uint proposalID, int result, uint quorum, bool active);
event MembershipChanged(address member);
event ChangeOfRules(uint minimumQuorum, uint debatingPeriodInMinutes, int majorityMargin);
struct Proposal {
address recipient;
uint amount;
string description;
uint waitingWindow;
bool executed;
bool proposalPassed;
uint rankSum;
int currentResult;
bytes32 proposalHash;
Vote[] votes;
mapping (address => bool) voted;
}
struct Member {
address member;
uint rank;
bool canAddProposals;
string name;
uint memberSince;
}
struct Vote {
bool inSupport;
address voter;
string justification;
}
/* First time setup */
function DBVN(string constitutionURL, uint totalRankNeededForDecisions, uint minutesForDebate, int marginOfVotesForMajority, address congressLeader) {
rankThreshold = totalRankNeededForDecisions;
debatingPeriodInMinutes = minutesForDebate;
majorityMargin = marginOfVotesForMajority;
members.length++;
members[0] = Member({member: 0, rank: 0, canAddProposals: false, memberSince: now, name: ''});
if (congressLeader != 0) owner = congressLeader;
constitutionURL = constitutionURL;
}
/*make member*/
function changeMembership(address targetMember, uint rank, bool canAddProposals, string memberName) onlyOwner {
uint id;
if (memberId[targetMember] == 0) {
memberId[targetMember] = members.length;
id = members.length++;
members[id] = Member({member: targetMember, rank: rank, canAddProposals: canAddProposals, memberSince: now, name: memberName});
} else {
id = memberId[targetMember];
Member m = members[id];
m.rank = rank;
m.canAddProposals = canAddProposals;
m.name = memberName;
}
MembershipChanged(targetMember);
}
/*change rules*/
function changeVotingRules(uint minimumQuorumForProposals, uint minutesForDebate, int marginOfVotesForMajority) onlyOwner {
rankThreshold = minimumQuorumForProposals;
debatingPeriodInMinutes = minutesForDebate;
majorityMargin = marginOfVotesForMajority;
ChangeOfRules(rankThreshold, debatingPeriodInMinutes, majorityMargin);
}
/* Function to create a new proposal */
function newProposalInWei(address beneficiary, uint weiAmount, string JobDescription, bytes transactionBytecode) returns (uint proposalID) {
if (memberId[msg.sender] == 0 || !members[memberId[msg.sender]].canAddProposals) throw;
proposalID = proposals.length++;
Proposal p = proposals[proposalID];
p.recipient = beneficiary;
p.amount = weiAmount;
p.description = JobDescription;
p.proposalHash = sha3(beneficiary, weiAmount, transactionBytecode);
p.waitingWindow = now + debatingPeriodInMinutes * 1 minutes;
p.executed = false;
p.proposalPassed = false;
p.rankSum = 0;
ProposalAdded(proposalID, beneficiary, weiAmount, JobDescription);
numProposals = proposalID+1;
}
/* Function to create a new proposal */
function newProposalInEther(address beneficiary, uint etherAmount, string JobDescription, bytes transactionBytecode) returns (uint proposalID) {
if (memberId[msg.sender] == 0 || !members[memberId[msg.sender]].canAddProposals) throw;
proposalID = proposals.length++;
Proposal p = proposals[proposalID];
p.recipient = beneficiary;
p.amount = etherAmount * 1 ether;
p.description = JobDescription;
p.proposalHash = sha3(beneficiary, etherAmount * 1 ether, transactionBytecode);
p.waitingWindow = now + debatingPeriodInMinutes * 1 minutes;
p.executed = false;
p.proposalPassed = false;
p.rankSum = 0;
ProposalAdded(proposalID, beneficiary, etherAmount, JobDescription);
numProposals = proposalID+1;
}
/* function to check if a proposal code matches */
function checkProposalCode(uint proposalNumber, address beneficiary, uint amount, bytes transactionBytecode) constant returns (bool codeChecksOut) {
Proposal p = proposals[proposalNumber];
return p.proposalHash == sha3(beneficiary, amount, transactionBytecode);
}
function makeDecision(uint proposalNumber, bool agree) returns (uint voteID){
if (memberId[msg.sender] == 0) throw;
uint rank = members[memberId[msg.sender]].rank;
Proposal p = proposals[proposalNumber]; // Get the proposal
if (p.voted[msg.sender] == true) throw; // If has already voted, cancel
p.voted[msg.sender] = true; // Set this voter as having voted
p.rankSum+= rank; // Increase the number of votes
if (agree) { // If they support the proposal
p.currentResult += int(rank); // Increase score
} else { // If they don't
p.currentResult -= int(rank); // Decrease the score
}
// Create a log of this event
Voted(proposalNumber, agree, msg.sender);
}
function executeProposal(uint proposalNumber, bytes transactionBytecode) returns (int result) {
Proposal p = proposals[proposalNumber];
/* Check if the proposal can be executed */
if (now < p.waitingWindow // has the voting deadline arrived?
|| p.executed // has it been already executed?
|| p.proposalHash != sha3(p.recipient, p.amount, transactionBytecode) // Does the transaction code match the proposal?
|| p.rankSum < rankThreshold) // has minimum quorum?
throw;
/* execute result */
if (p.currentResult > majorityMargin) {
/* If difference between support and opposition is larger than margin */
p.recipient.call.value(p.amount)(transactionBytecode);
p.executed = true;
p.proposalPassed = true;
} else {
p.executed = true;
p.proposalPassed = false;
}
// Fire Events
ProposalTallied(proposalNumber, p.currentResult, p.rankSum, p.proposalPassed);
}
}