-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnkCoin.sol
294 lines (247 loc) · 8.86 KB
/
snkCoin.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
pragma solidity ^0.4.10;
contract ERC20 {
function totalSupply() constant returns (uint supply);
function balanceOf(address who) constant returns (uint value);
function allowance(address owner, address spender) constant returns (uint _allowance);
function transfer(address to, uint value) returns (bool ok);
function transferFrom(address from, address to, uint value) returns (bool ok);
function approve(address spender, uint value) returns (bool ok);
event Transfer(address indexed from, address indexed to, uint value);
event Approval(address indexed owner, address indexed spender, uint value);
}
contract snkCoin is ERC20{
uint initialSupply;
string name;
string symbol;
uint currentPrice;
address snkFoundation;
string[] questions;
uint voteStartDate;
uint daysAfter;
IterableBalances.itmap balances;
mapping (address => mapping (address => uint256)) allowed;
mapping (address => uint256) approvedDividends;
voteUtils.itmap answers;
mapping (address => uint) donators;
modifier onlyOwner{
if (msg.sender == snkFoundation) {
_;
}
}
modifier tokenHolder{
if (IterableBalances.containsKey(balances, msg.sender)) {
_;
}
}
modifier costs(uint _amount) {
if (msg.value >= _amount)
{
_;
}
}
function totalSupply() constant returns (uint256) {
return initialSupply;
}
function balanceOf(address src) constant returns (uint256) {
return IterableBalances.getValue(balances,src);
}
function allowance(address owner, address spender) constant returns (uint256) {
return allowed[owner][spender];
}
function transfer(address to, uint value) returns (bool) {
if (IterableBalances.getValue(balances,msg.sender) >= value && value > 0) {
IterableBalances.setValue(balances,msg.sender, IterableBalances.getValue(balances,msg.sender)- value);
IterableBalances.setValue(balances,to, IterableBalances.getValue(balances,to) + value);
Transfer(msg.sender, to, value);
return true;
} else
return false;
}
function transferFrom(address from, address to, uint256 value) returns (bool success) {
if (IterableBalances.getValue(balances,from) >= value && allowed[from][msg.sender] >= value && value > 0) {
IterableBalances.setValue(balances,to, IterableBalances.getValue(balances,to) + value);
IterableBalances.setValue(balances,from, IterableBalances.getValue(balances,from) - value);
allowed[from][msg.sender] -= value;
Transfer(from, to, value);
return true;
} else
return false;
}
function approve(address spender, uint value) returns (bool ok) {
if ((value != 0) && (allowed[msg.sender][spender] != 0)) throw;
allowed[msg.sender][spender] = value;
Approval(msg.sender, spender, value);
return true;
}
function snkCoin(uint supply, string _name, string _symbol, uint _initialPrice) {
snkFoundation = msg.sender;
initialSupply = supply;
currentPrice = _initialPrice;
name = _name;
symbol = _symbol;
}
function setCurrentPrice (uint _price) onlyOwner{
currentPrice = _price;
}
function donate() payable{
donators[msg.sender] = msg.value;
//thank you!!
}
function buySnkCoins() payable costs(currentPrice) {
if (msg.value<currentPrice) throw;
uint numberOfSnkCoinsToSend = msg.value/currentPrice;
if (IterableBalances.getValue(balances,this)<numberOfSnkCoinsToSend) throw;
transfer(msg.sender, numberOfSnkCoinsToSend);
}
function approveDividends (uint totalDividendsAmount) onlyOwner{
uint256 dividendsPerToken = totalDividendsAmount / initialSupply;
for (var i = IterableBalances.iterate_start(balances); IterableBalances.iterate_valid(balances, i); i = IterableBalances.iterate_next(balances, i))
{
var (tokenHolder, value) = IterableBalances.iterate_get(balances, i);
approvedDividends[tokenHolder] = value * dividendsPerToken;
}
}
function transferAllDividends() onlyOwner{
for (var i = IterableBalances.iterate_start(balances); IterableBalances.iterate_valid(balances, i); i = IterableBalances.iterate_next(balances, i))
{
var (tokenHolder, value) = IterableBalances.iterate_get(balances, i);
var _value = value;
_value++;
if (approvedDividends[tokenHolder] != 0)
{
tokenHolder.transfer(approvedDividends[tokenHolder]);
approvedDividends[tokenHolder] = 0;
}
}
}
function setVoteStartDate(uint _daysAfter) onlyOwner{
daysAfter = _daysAfter;
voteStartDate = now;
}
function vote(bool[] _answers) tokenHolder{
if (now <= voteStartDate + daysAfter * 1 days) {
voteUtils.setValue(answers,msg.sender,_answers);
}
}
}
/// Slightly modified library, taken from https://github.com/ethereum/dapp-bin/blob/master/library/iterable_mapping.sol
/// nagor[at]academ.org
/// @dev Models a uint -> uint mapping where it is possible to iterate over all keys.
/// http://solidity.readthedocs.io/en/latest/types.html#mappings
library voteUtils
{
struct itmap {
mapping(address => IndexValue) votes;
KeyFlag[] keys;
uint size;
}
struct IndexValue { uint keyIndex; bool[] value; }
struct KeyFlag { address key; bool deleted; }
function insert(itmap storage self, address key, bool[] value) returns (bool replaced) {
uint keyIndex = self.votes[key].keyIndex;
self.votes[key].value = value;
if (keyIndex > 0)
return true;
else {
keyIndex = self.keys.length++;
self.votes[key].keyIndex = keyIndex + 1;
self.keys[keyIndex].key = key;
self.size++;
return false;
}
}
function remove(itmap storage self, address key) returns (bool success) {
uint keyIndex = self.votes[key].keyIndex;
if (keyIndex == 0)
return false;
delete self.votes[key];
self.keys[keyIndex - 1].deleted = true;
self.size --;
}
function getValue(itmap storage self, address key) returns (bool[]) {
return self.votes[key].value;
}
function setValue(itmap storage self, address key, bool[] _value) returns (bool) {
if (self.votes[key].keyIndex < 0) return false;
self.votes[key].value = _value;
return true;
}
function iterate_start(itmap storage self) returns (uint keyIndex) {
return iterate_next(self, uint(-1));
}
function iterate_valid(itmap storage self, uint keyIndex) returns (bool) {
return keyIndex < self.keys.length;
}
function iterate_next(itmap storage self, uint keyIndex) returns (uint r_keyIndex) {
keyIndex++;
while (keyIndex < self.keys.length && self.keys[keyIndex].deleted)
keyIndex++;
return keyIndex;
}
function iterate_get(itmap storage self, uint keyIndex) returns (address key, bool[] value) {
key = self.keys[keyIndex].key;
value = self.votes[key].value;
}
}
/// Slightly modified library, taken from https://github.com/ethereum/dapp-bin/blob/master/library/iterable_mapping.sol
/// nagor[at]academ.org
/// @dev Models a uint -> uint mapping where it is possible to iterate over all keys.
/// http://solidity.readthedocs.io/en/latest/types.html#mappings
library IterableBalances
{
struct itmap {
mapping(address => IndexValue) balances;
KeyFlag[] keys;
uint size;
}
struct IndexValue { uint keyIndex; uint value; }
struct KeyFlag { address key; bool deleted; }
function insert(itmap storage self, address key, uint value) returns (bool replaced) {
uint keyIndex = self.balances[key].keyIndex;
self.balances[key].value = value;
if (keyIndex > 0)
return true;
else {
keyIndex = self.keys.length++;
self.balances[key].keyIndex = keyIndex + 1;
self.keys[keyIndex].key = key;
self.size++;
return false;
}
}
function remove(itmap storage self, address key) returns (bool success) {
uint keyIndex = self.balances[key].keyIndex;
if (keyIndex == 0)
return false;
delete self.balances[key];
self.keys[keyIndex - 1].deleted = true;
self.size --;
}
function getValue(itmap storage self, address key) returns (uint256) {
return self.balances[key].value;
}
function setValue(itmap storage self, address key, uint256 _value) returns (bool) {
if (self.balances[key].keyIndex < 0) return false;
self.balances[key].value = _value;
return true;
}
function containsKey(itmap storage self, address key) returns (bool) {
return self.balances[key].value > 0;
}
function iterate_start(itmap storage self) returns (uint keyIndex) {
return iterate_next(self, uint(-1));
}
function iterate_valid(itmap storage self, uint keyIndex) returns (bool) {
return keyIndex < self.keys.length;
}
function iterate_next(itmap storage self, uint keyIndex) returns (uint r_keyIndex) {
keyIndex++;
while (keyIndex < self.keys.length && self.keys[keyIndex].deleted)
keyIndex++;
return keyIndex;
}
function iterate_get(itmap storage self, uint keyIndex) returns (address key, uint value) {
key = self.keys[keyIndex].key;
value = self.balances[key].value;
}
}