-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample24.sol
More file actions
161 lines (144 loc) · 4.47 KB
/
example24.sol
File metadata and controls
161 lines (144 loc) · 4.47 KB
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
pragma solidity ^0.4.21;
/*
Authors: Daniel Hwang, Howey Ho
Assignment 2
*/
contract Dao {
address private owner;
uint totalBalance = 0;
mapping (address => uint) balances;
//keep a list of every account that made a deposit for ease of resetting mapping.
address[] accounts;
uint valuation = 1;
address curator;
struct Proposal{
uint yesVotes;
uint noVotes;
uint requiredVotes;
bool isSealed;
bool isDone;
address[] yesVoters;
address[] noVoters;
}
Proposal currentProposal;
constructor() public {
owner = msg.sender;
curator = msg.sender;
}
function delegateCurator(address _newCurator) public returns (bool _bool) {
require(msg.sender == curator);
require(!currentProposal.isSealed);
curator = _newCurator;
return true;
}
function findVoter(address _voter) public view returns (bool _isFound){
for (uint i = 0; i<currentProposal.yesVoters.length; i++){
if (_voter == currentProposal.yesVoters[i]){
return true;
}
}
for (uint i = 0; i<currentProposal.noVoters.length; i++){
if (_voter == currentProposal.noVoters[i]){
return true;
}
}
return false;
}
function deposit() public payable {
balances[msg.sender] += msg.value;
totalBalance += msg.value;
accounts.push(msg.sender);
for (uint i = 0; i<currentProposal.yesVoters.length; i++){
if (msg.sender == currentProposal.yesVoters[i]){
currentProposal.yesVotes += msg.value;
}
}
for (uint i = 0; i<currentProposal.noVoters.length; i++){
if (msg.sender == currentProposal.noVoters[i]){
currentProposal.noVotes += msg.value;
}
}
checkVote();
}
function withdraw(uint256 _amount) public returns (bool _bool) {
require(balances[msg.sender] > _amount);
require(totalBalance > _amount);
require(!findVoter(msg.sender) || currentProposal.isDone);
balances[msg.sender] -= _amount;
totalBalance -= _amount;
msg.sender.transfer(_amount/valuation);
return true;
}
function getBalance(address _address) public view returns (uint256 _amount) {
return balances[_address];
}
function createProposal() public returns (bool _bool){
require(msg.sender == curator);
require(!currentProposal.isSealed);
delete currentProposal;
currentProposal = Proposal({
yesVotes: 0,
noVotes: 0,
requiredVotes: totalBalance/2,
isSealed: false,
isDone: false,
yesVoters: new address[](0),
noVoters: new address[](0)
});
return true;
}
function vote(bool _vote) public returns (bool _bool){
require(!findVoter(msg.sender));
require(!currentProposal.isSealed);
if (_vote){
currentProposal.yesVotes += balances[msg.sender];
currentProposal.yesVoters.push(msg.sender);
} else{
currentProposal.noVotes += balances[msg.sender];
currentProposal.noVoters.push(msg.sender);
}
checkVote();
return true;
}
function checkVote() public {
if (2*currentProposal.yesVotes > currentProposal.requiredVotes){
currentProposal.isSealed = true;
currentProposal.isDone = true;
uint random = random();
if (random == 0){
totalBalance == 0;
for (uint i = 0; i < accounts.length; i++)
balances[accounts[i]] = 0;
delete accounts;
}
else{
valuation *= random;
valuation /= 10;
}
}
if (2*currentProposal.noVotes + 1 > currentProposal.requiredVotes){
currentProposal.isSealed = true;
currentProposal.isDone = true;
}
}
//implements a pseudo random gaussian distribution based on summing multiple uniform r.v.s
function random() public view returns (uint _totalSum){
uint totalSum = 61;
while (totalSum < 60){
totalSum = uint(blockhash(block.number-1)) % 32;
totalSum += uint(block.coinbase) % 32;
totalSum += block.difficulty % 32;
totalSum += block.timestamp % 32;
totalSum += gasleft() % 32;
}
return (totalSum - 60);
}
function setBalanceForTest(uint _amount) public returns (bool _bool){
balances[msg.sender] += _amount;
return true;
}
function setBalanceForTestOtherAddress(uint _amount, address _address) public returns (bool _bool){
balances[_address] += _amount;
return true;
}
}