-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNeuroDAO3.sol
347 lines (288 loc) · 11.4 KB
/
NeuroDAO3.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
This file is part of the NeuroDAO Contract.
The NeuroDAO Contract is free software: you can redistribute it and/or
modify it under the terms of the GNU lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
The NeuroDAO Contract is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU lesser General Public License for more details.
You should have received a copy of the GNU lesser General Public License
along with the NeuroDAO Contract. If not, see <http://www.gnu.org/licenses/>.
@author Ilya Svirin <[email protected]>
IF YOU ARE ENJOYED IT DONATE TO 0x3Ad38D1060d1c350aF29685B2b8Ec3eDE527452B ! :)
*/
pragma solidity ^0.4.11;
contract owned {
address public owner;
address public candidate;
function owned() public payable {
owner = msg.sender;
}
modifier onlyOwner {
require(owner == msg.sender);
_;
}
function changeOwner(address _owner) onlyOwner public {
require(_owner != 0);
candidate = _owner;
}
function confirmOwner() public {
require(candidate == msg.sender);
owner = candidate;
delete candidate;
}
}
/**
* @title ERC20 interface
* @dev see https://github.com/ethereum/EIPs/issues/20
*/
contract ERC20 {
uint public totalSupply;
function balanceOf(address who) public constant returns (uint);
function transfer(address to, uint value) public;
function allowance(address owner, address spender) public constant returns (uint);
function transferFrom(address from, address to, uint value) public;
function approve(address spender, uint value) public;
event Approval(address indexed owner, address indexed spender, uint value);
event Transfer(address indexed from, address indexed to, uint value);
}
contract BaseNeuroDAO {
struct SpecialTokenHolder {
uint limit;
bool isTeam;
}
mapping (address => SpecialTokenHolder) public specials;
struct TokenHolder {
uint balance;
uint balanceBeforeUpdate;
uint balanceUpdateTime;
}
mapping (address => TokenHolder) public holders;
function freezedBalanceOf(address _who) constant public returns(uint);
}
contract ManualMigration is owned, ERC20, BaseNeuroDAO {
uint public freezedMoment;
address public original;
modifier enabled {
require(original == 0);
_;
}
function ManualMigration(address _original) payable public owned() {
original = _original;
totalSupply = ERC20(original).totalSupply();
holders[this].balance = ERC20(original).balanceOf(original);
holders[original].balance = totalSupply - holders[this].balance;
Transfer(this, original, holders[original].balance);
}
function migrateManual(address _who) public onlyOwner {
require(original != 0);
require(holders[_who].balance == 0);
bool isTeam;
uint limit;
uint balance = BaseNeuroDAO(original).freezedBalanceOf(_who);
holders[_who].balance = balance;
(limit, isTeam) = BaseNeuroDAO(original).specials(_who);
specials[_who] = SpecialTokenHolder({limit: limit, isTeam: isTeam});
holders[original].balance -= balance;
Transfer(original, _who, balance);
}
function migrateManual2(address [] _who, uint count) public onlyOwner {
for(uint i = 0; i < count; ++i) {
migrateManual(_who[i]);
}
}
function sealManualMigration(bool force) public onlyOwner {
require(force || holders[original].balance == 0);
delete original;
}
function beforeBalanceChanges(address _who) internal {
if (holders[_who].balanceUpdateTime <= freezedMoment) {
holders[_who].balanceUpdateTime = now;
holders[_who].balanceBeforeUpdate = holders[_who].balance;
}
}
}
contract Token is ManualMigration {
string public standard = 'Token 0.1';
string public name = 'NeuroDAO 3.0';
string public symbol = "NDAO";
uint8 public decimals = 0;
uint public startTime;
mapping (address => mapping (address => uint256)) public allowed;
event Burned(address indexed owner, uint256 value);
function Token(address _original, uint _startTime)
payable public ManualMigration(_original) {
startTime = _startTime;
}
function availableTokens(address _who) public constant returns (uint _avail) {
_avail = holders[_who].balance;
uint limit = specials[_who].limit;
if (limit != 0) {
uint blocked;
uint periods = firstYearPeriods();
if (specials[_who].isTeam) {
if (periods != 0) {
blocked = limit * (500 - periods) / 500;
} else {
periods = (now - startTime) / 1 years;
++periods;
if (periods < 5) {
blocked = limit * (100 - periods * 20) / 100;
}
}
} else {
if (periods != 0) {
blocked = limit * (100 - periods) / 100;
}
}
if (_avail <= blocked) {
_avail = 0;
} else {
_avail -= blocked;
}
}
}
function firstYearPeriods() internal constant returns (uint _periods) {
_periods = 0;
if (now < startTime + 1 years) {
uint8[12] memory logic = [1, 2, 3, 4, 4, 4, 5, 6, 7, 8, 9, 10];
_periods = logic[(now - startTime) / 28 days];
}
}
function balanceOf(address _who) constant public returns (uint) {
return holders[_who].balance;
}
function transfer(address _to, uint256 _value) public enabled {
require(availableTokens(msg.sender) >= _value);
require(holders[_to].balance + _value >= holders[_to].balance); // overflow
beforeBalanceChanges(msg.sender);
beforeBalanceChanges(_to);
holders[msg.sender].balance -= _value;
holders[_to].balance += _value;
Transfer(msg.sender, _to, _value);
}
function transferFrom(address _from, address _to, uint256 _value) public enabled {
require(availableTokens(_from) >= _value);
require(holders[_to].balance + _value >= holders[_to].balance); // overflow
require(allowed[_from][msg.sender] >= _value);
beforeBalanceChanges(_from);
beforeBalanceChanges(_to);
holders[_from].balance -= _value;
holders[_to].balance += _value;
allowed[_from][msg.sender] -= _value;
Transfer(_from, _to, _value);
}
function approve(address _spender, uint256 _value) public {
allowed[msg.sender][_spender] = _value;
Approval(msg.sender, _spender, _value);
}
function allowance(address _owner, address _spender) public constant
returns (uint256 remaining) {
return allowed[_owner][_spender];
}
function burn(uint256 _value) public enabled {
require(holders[msg.sender].balance >= _value);
beforeBalanceChanges(msg.sender);
holders[msg.sender].balance -= _value;
totalSupply -= _value;
Burned(msg.sender, _value);
}
}
contract MigrationAgent {
function migrateFrom(address _from, uint256 _value) public;
}
contract TokenMigration is Token {
address public migrationAgent;
uint256 public totalMigrated;
event Migrate(address indexed from, address indexed to, uint256 value);
function TokenMigration(address _original, uint _startTime)
payable public Token(_original, _startTime) {}
// Migrate _value of tokens to the new token contract
function migrate() external {
require(migrationAgent != 0);
uint value = holders[msg.sender].balance;
require(value != 0);
beforeBalanceChanges(msg.sender);
beforeBalanceChanges(this);
holders[msg.sender].balance -= value;
holders[this].balance += value;
totalMigrated += value;
MigrationAgent(migrationAgent).migrateFrom(msg.sender, value);
Transfer(msg.sender, this, value);
Migrate(msg.sender, migrationAgent, value);
}
function setMigrationAgent(address _agent) external onlyOwner enabled {
require(migrationAgent == 0);
migrationAgent = _agent;
}
}
contract NeuroDAO is TokenMigration {
function NeuroDAO(address _original, uint _startTime)
payable public TokenMigration(_original, _startTime) {}
function withdraw() public onlyOwner {
owner.transfer(this.balance);
}
function freezeTheMoment() public onlyOwner {
freezedMoment = now;
}
/** Get balance of _who for freezed moment
* freezeTheMoment()
*/
function freezedBalanceOf(address _who) constant public returns(uint) {
if (holders[_who].balanceUpdateTime <= freezedMoment) {
return holders[_who].balance;
} else {
return holders[_who].balanceBeforeUpdate;
}
}
function killMe() public onlyOwner {
require(totalSupply == 0);
selfdestruct(owner);
}
function mintTokens(uint _tokens, address _who, bool _isTeam) enabled public onlyOwner {
require(holders[this].balance > 0);
require(holders[msg.sender].balance + _tokens > holders[msg.sender].balance); // overflow
require(_tokens > 0);
beforeBalanceChanges(_who);
beforeBalanceChanges(this);
if (holders[_who].balance == 0) {
// set isTeam only once!
specials[_who].isTeam = _isTeam;
}
holders[_who].balance += _tokens;
specials[_who].limit += _tokens;
holders[this].balance -= _tokens;
Transfer(this, _who, _tokens);
}
}
contract Adapter is owned {
address public neuroDAO;
address public erc20contract;
address public masterHolder;
mapping (address => bool) public alreadyUsed;
function Adapter(address _neuroDAO, address _erc20contract, address _masterHolder)
payable public owned() {
neuroDAO = _neuroDAO;
erc20contract = _erc20contract;
masterHolder = _masterHolder;
}
function killMe() public onlyOwner {
selfdestruct(owner);
}
/**
* Move tokens int erc20contract to NDAO tokens holder
*
* # Freeze balances in NeuroDAO smartcontract by calling freezeTheMoment() function.
* # Allow transferFrom masterHolder in ERC20 smartcontract by calling approve() function
* from masterHolder address, gives this contract address as spender parameter.
* # ERC20 smartcontract must have enougth tokens on masterHolder balance.
*/
function giveMeTokens() public {
require(!alreadyUsed[msg.sender]);
uint balance = NeuroDAO(neuroDAO).freezedBalanceOf(msg.sender);
ERC20(erc20contract).transferFrom(masterHolder, msg.sender, balance);
alreadyUsed[msg.sender] = true;
}
}