-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy path0x1776e1f26f98b1a5df9cd347953a26dd3cb46671-NMR-Numeraire.sol
474 lines (382 loc) · 17.8 KB
/
0x1776e1f26f98b1a5df9cd347953a26dd3cb46671-NMR-Numeraire.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
pragma solidity ^0.4.11;
contract Safe {
// Check if it is safe to add two numbers
function safeAdd(uint a, uint b) internal returns (uint) {
uint c = a + b;
assert(c >= a && c >= b);
return c;
}
// Check if it is safe to subtract two numbers
function safeSubtract(uint a, uint b) internal returns (uint) {
uint c = a - b;
assert(b <= a && c <= a);
return c;
}
function safeMultiply(uint a, uint b) internal returns (uint) {
uint c = a * b;
assert(a == 0 || (c / a) == b);
return c;
}
function shrink128(uint a) internal returns (uint128) {
assert(a < 0x100000000000000000000000000000000);
return uint128(a);
}
// mitigate short address attack
modifier onlyPayloadSize(uint numWords) {
assert(msg.data.length == numWords * 32 + 4);
_;
}
// allow ether to be received
function () payable { }
}
// Class variables used both in NumeraireBackend and NumeraireDelegate
contract NumeraireShared is Safe {
address public numerai = this;
// Cap the total supply and the weekly supply
uint256 public supply_cap = 21000000e18; // 21 million
uint256 public weekly_disbursement = 96153846153846153846153;
uint256 public initial_disbursement;
uint256 public deploy_time;
uint256 public total_minted;
// ERC20 requires totalSupply, balanceOf, and allowance
uint256 public totalSupply;
mapping (address => uint256) public balanceOf;
mapping (address => mapping (address => uint256)) public allowance;
mapping (uint => Tournament) public tournaments; // tournamentID
struct Tournament {
uint256 creationTime;
uint256[] roundIDs;
mapping (uint256 => Round) rounds; // roundID
}
struct Round {
uint256 creationTime;
uint256 endTime;
uint256 resolutionTime;
mapping (address => mapping (bytes32 => Stake)) stakes; // address of staker
}
// The order is important here because of its packing characteristics.
// Particularly, `amount` and `confidence` are in the *same* word, so
// Solidity can update both at the same time (if the optimizer can figure
// out that you're updating both). This makes `stake()` cheap.
struct Stake {
uint128 amount; // Once the stake is resolved, this becomes 0
uint128 confidence;
bool successful;
bool resolved;
}
// Generates a public event on the blockchain to notify clients
event Mint(uint256 value);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
event Staked(address indexed staker, bytes32 tag, uint256 totalAmountStaked, uint256 confidence, uint256 indexed tournamentID, uint256 indexed roundID);
event RoundCreated(uint256 indexed tournamentID, uint256 indexed roundID, uint256 endTime, uint256 resolutionTime);
event TournamentCreated(uint256 indexed tournamentID);
event StakeDestroyed(uint256 indexed tournamentID, uint256 indexed roundID, address indexed stakerAddress, bytes32 tag);
event StakeReleased(uint256 indexed tournamentID, uint256 indexed roundID, address indexed stakerAddress, bytes32 tag, uint256 etherReward);
// Calculate allowable disbursement
function getMintable() constant returns (uint256) {
return
safeSubtract(
safeAdd(initial_disbursement,
safeMultiply(weekly_disbursement,
safeSubtract(block.timestamp, deploy_time))
/ 1 weeks),
total_minted);
}
}
// From OpenZepplin: https://github.com/OpenZeppelin/zeppelin-solidity/blob/master/contracts/ownership/Shareable.sol
/*
* Shareable
*
* Effectively our multisig contract
*
* Based on https://github.com/ethereum/dapp-bin/blob/master/wallet/wallet.sol
*
* inheritable "property" contract that enables methods to be protected by requiring the acquiescence of either a single, or, crucially, each of a number of, designated owners.
*
* usage:
* use modifiers onlyowner (just own owned) or onlymanyowners(hash), whereby the same hash must be provided by some number (specified in constructor) of the set of owners (specified in the constructor) before the interior is executed.
*/
contract Shareable {
// TYPES
// struct for the status of a pending operation.
struct PendingState {
uint yetNeeded;
uint ownersDone;
uint index;
}
// FIELDS
// the number of owners that must confirm the same operation before it is run.
uint public required;
// list of owners
address[256] owners;
uint constant c_maxOwners = 250;
// index on the list of owners to allow reverse lookup
mapping(address => uint) ownerIndex;
// the ongoing operations.
mapping(bytes32 => PendingState) pendings;
bytes32[] pendingsIndex;
// EVENTS
// this contract only has six types of events: it can accept a confirmation, in which case
// we record owner and operation (hash) alongside it.
event Confirmation(address owner, bytes32 operation);
event Revoke(address owner, bytes32 operation);
// MODIFIERS
address thisContract = this;
// simple single-sig function modifier.
modifier onlyOwner {
if (isOwner(msg.sender))
_;
}
// multi-sig function modifier: the operation must have an intrinsic hash in order
// that later attempts can be realised as the same underlying operation and
// thus count as confirmations.
modifier onlyManyOwners(bytes32 _operation) {
if (confirmAndCheck(_operation))
_;
}
// CONSTRUCTOR
// constructor is given number of sigs required to do protected "onlymanyowners" transactions
// as well as the selection of addresses capable of confirming them.
function Shareable(address[] _owners, uint _required) {
owners[1] = msg.sender;
ownerIndex[msg.sender] = 1;
for (uint i = 0; i < _owners.length; ++i) {
owners[2 + i] = _owners[i];
ownerIndex[_owners[i]] = 2 + i;
}
if (required > owners.length) throw;
required = _required;
}
// new multisig is given number of sigs required to do protected "onlymanyowners" transactions
// as well as the selection of addresses capable of confirming them.
// take all new owners as an array
function changeShareable(address[] _owners, uint _required) onlyManyOwners(sha3(msg.data)) {
for (uint i = 0; i < _owners.length; ++i) {
owners[1 + i] = _owners[i];
ownerIndex[_owners[i]] = 1 + i;
}
if (required > owners.length) throw;
required = _required;
}
// METHODS
// Revokes a prior confirmation of the given operation
function revoke(bytes32 _operation) external {
uint index = ownerIndex[msg.sender];
// make sure they're an owner
if (index == 0) return;
uint ownerIndexBit = 2**index;
var pending = pendings[_operation];
if (pending.ownersDone & ownerIndexBit > 0) {
pending.yetNeeded++;
pending.ownersDone -= ownerIndexBit;
Revoke(msg.sender, _operation);
}
}
// Gets an owner by 0-indexed position (using numOwners as the count)
function getOwner(uint ownerIndex) external constant returns (address) {
return address(owners[ownerIndex + 1]);
}
function isOwner(address _addr) constant returns (bool) {
return ownerIndex[_addr] > 0;
}
function hasConfirmed(bytes32 _operation, address _owner) constant returns (bool) {
var pending = pendings[_operation];
uint index = ownerIndex[_owner];
// make sure they're an owner
if (index == 0) return false;
// determine the bit to set for this owner.
uint ownerIndexBit = 2**index;
return !(pending.ownersDone & ownerIndexBit == 0);
}
// INTERNAL METHODS
function confirmAndCheck(bytes32 _operation) internal returns (bool) {
// determine what index the present sender is:
uint index = ownerIndex[msg.sender];
// make sure they're an owner
if (index == 0) return;
var pending = pendings[_operation];
// if we're not yet working on this operation, switch over and reset the confirmation status.
if (pending.yetNeeded == 0) {
// reset count of confirmations needed.
pending.yetNeeded = required;
// reset which owners have confirmed (none) - set our bitmap to 0.
pending.ownersDone = 0;
pending.index = pendingsIndex.length++;
pendingsIndex[pending.index] = _operation;
}
// determine the bit to set for this owner.
uint ownerIndexBit = 2**index;
// make sure we (the message sender) haven't confirmed this operation previously.
if (pending.ownersDone & ownerIndexBit == 0) {
Confirmation(msg.sender, _operation);
// ok - check if count is enough to go ahead.
if (pending.yetNeeded <= 1) {
// enough confirmations: reset and run interior.
delete pendingsIndex[pendings[_operation].index];
delete pendings[_operation];
return true;
}
else
{
// not enough: record that this owner in particular confirmed.
pending.yetNeeded--;
pending.ownersDone |= ownerIndexBit;
}
}
}
function clearPending() internal {
uint length = pendingsIndex.length;
for (uint i = 0; i < length; ++i)
if (pendingsIndex[i] != 0)
delete pendings[pendingsIndex[i]];
delete pendingsIndex;
}
}
// From OpenZepplin: https://github.com/OpenZeppelin/zeppelin-solidity/blob/master/contracts/lifecycle/Pausable.sol
/*
* Stoppable
* Abstract contract that allows children to implement an
* emergency stop mechanism.
*/
contract StoppableShareable is Shareable {
bool public stopped;
bool public stoppable = true;
modifier stopInEmergency { if (!stopped) _; }
modifier onlyInEmergency { if (stopped) _; }
function StoppableShareable(address[] _owners, uint _required) Shareable(_owners, _required) {
}
// called by the owner on emergency, triggers stopped state
function emergencyStop() external onlyOwner {
assert(stoppable);
stopped = true;
}
// called by the owners on end of emergency, returns to normal state
function release() external onlyManyOwners(sha3(msg.data)) {
assert(stoppable);
stopped = false;
}
// called by the owners to disable ability to begin or end an emergency stop
function disableStopping() external onlyManyOwners(sha3(msg.data)) {
stoppable = false;
}
}
// This is the contract that will be unchangeable once deployed. It will call delegate functions in another contract to change state. The delegate contract is upgradable.
contract NumeraireBackend is StoppableShareable, NumeraireShared {
address public delegateContract;
bool public contractUpgradable = true;
address[] public previousDelegates;
string public standard = "ERC20";
// ERC20 requires name, symbol, and decimals
string public name = "Numeraire";
string public symbol = "NMR";
uint256 public decimals = 18;
event DelegateChanged(address oldAddress, address newAddress);
function NumeraireBackend(address[] _owners, uint256 _num_required, uint256 _initial_disbursement) StoppableShareable(_owners, _num_required) {
totalSupply = 0;
total_minted = 0;
initial_disbursement = _initial_disbursement;
deploy_time = block.timestamp;
}
function disableContractUpgradability() onlyManyOwners(sha3(msg.data)) returns (bool) {
assert(contractUpgradable);
contractUpgradable = false;
}
function changeDelegate(address _newDelegate) onlyManyOwners(sha3(msg.data)) returns (bool) {
assert(contractUpgradable);
if (_newDelegate != delegateContract) {
previousDelegates.push(delegateContract);
var oldDelegate = delegateContract;
delegateContract = _newDelegate;
DelegateChanged(oldDelegate, _newDelegate);
return true;
}
return false;
}
function claimTokens(address _token) onlyOwner {
assert(_token != numerai);
if (_token == 0x0) {
msg.sender.transfer(this.balance);
return;
}
NumeraireBackend token = NumeraireBackend(_token);
uint256 balance = token.balanceOf(this);
token.transfer(msg.sender, balance);
}
function mint(uint256 _value) stopInEmergency returns (bool ok) {
return delegateContract.delegatecall(bytes4(sha3("mint(uint256)")), _value);
}
function stake(uint256 _value, bytes32 _tag, uint256 _tournamentID, uint256 _roundID, uint256 _confidence) stopInEmergency returns (bool ok) {
return delegateContract.delegatecall(bytes4(sha3("stake(uint256,bytes32,uint256,uint256,uint256)")), _value, _tag, _tournamentID, _roundID, _confidence);
}
function stakeOnBehalf(address _staker, uint256 _value, bytes32 _tag, uint256 _tournamentID, uint256 _roundID, uint256 _confidence) stopInEmergency onlyPayloadSize(6) returns (bool ok) {
return delegateContract.delegatecall(bytes4(sha3("stakeOnBehalf(address,uint256,bytes32,uint256,uint256,uint256)")), _staker, _value, _tag, _tournamentID, _roundID, _confidence);
}
function releaseStake(address _staker, bytes32 _tag, uint256 _etherValue, uint256 _tournamentID, uint256 _roundID, bool _successful) stopInEmergency onlyPayloadSize(6) returns (bool ok) {
return delegateContract.delegatecall(bytes4(sha3("releaseStake(address,bytes32,uint256,uint256,uint256,bool)")), _staker, _tag, _etherValue, _tournamentID, _roundID, _successful);
}
function destroyStake(address _staker, bytes32 _tag, uint256 _tournamentID, uint256 _roundID) stopInEmergency onlyPayloadSize(4) returns (bool ok) {
return delegateContract.delegatecall(bytes4(sha3("destroyStake(address,bytes32,uint256,uint256)")), _staker, _tag, _tournamentID, _roundID);
}
function numeraiTransfer(address _to, uint256 _value) onlyPayloadSize(2) returns(bool ok) {
return delegateContract.delegatecall(bytes4(sha3("numeraiTransfer(address,uint256)")), _to, _value);
}
function withdraw(address _from, address _to, uint256 _value) onlyPayloadSize(3) returns(bool ok) {
return delegateContract.delegatecall(bytes4(sha3("withdraw(address,address,uint256)")), _from, _to, _value);
}
function createTournament(uint256 _tournamentID) returns (bool ok) {
return delegateContract.delegatecall(bytes4(sha3("createTournament(uint256)")), _tournamentID);
}
function createRound(uint256 _tournamentID, uint256 _roundID, uint256 _endTime, uint256 _resolutionTime) returns (bool ok) {
return delegateContract.delegatecall(bytes4(sha3("createRound(uint256,uint256,uint256,uint256)")), _tournamentID, _roundID, _endTime, _resolutionTime);
}
function getTournament(uint256 _tournamentID) constant returns (uint256, uint256[]) {
var tournament = tournaments[_tournamentID];
return (tournament.creationTime, tournament.roundIDs);
}
function getRound(uint256 _tournamentID, uint256 _roundID) constant returns (uint256, uint256, uint256) {
var round = tournaments[_tournamentID].rounds[_roundID];
return (round.creationTime, round.endTime, round.resolutionTime);
}
function getStake(uint256 _tournamentID, uint256 _roundID, address _staker, bytes32 _tag) constant returns (uint256, uint256, bool, bool) {
var stake = tournaments[_tournamentID].rounds[_roundID].stakes[_staker][_tag];
return (stake.confidence, stake.amount, stake.successful, stake.resolved);
}
// ERC20: Send from a contract
function transferFrom(address _from, address _to, uint256 _value) stopInEmergency onlyPayloadSize(3) returns (bool ok) {
require(!isOwner(_from) && _from != numerai); // Transfering from Numerai can only be done with the numeraiTransfer function
// Check for sufficient funds.
require(balanceOf[_from] >= _value);
// Check for authorization to spend.
require(allowance[_from][msg.sender] >= _value);
balanceOf[_from] = safeSubtract(balanceOf[_from], _value);
allowance[_from][msg.sender] = safeSubtract(allowance[_from][msg.sender], _value);
balanceOf[_to] = safeAdd(balanceOf[_to], _value);
// Notify anyone listening.
Transfer(_from, _to, _value);
return true;
}
// ERC20: Anyone with NMR can transfer NMR
function transfer(address _to, uint256 _value) stopInEmergency onlyPayloadSize(2) returns (bool ok) {
// Check for sufficient funds.
require(balanceOf[msg.sender] >= _value);
balanceOf[msg.sender] = safeSubtract(balanceOf[msg.sender], _value);
balanceOf[_to] = safeAdd(balanceOf[_to], _value);
// Notify anyone listening.
Transfer(msg.sender, _to, _value);
return true;
}
// ERC20: Allow other contracts to spend on sender's behalf
function approve(address _spender, uint256 _value) stopInEmergency onlyPayloadSize(2) returns (bool ok) {
require((_value == 0) || (allowance[msg.sender][_spender] == 0));
allowance[msg.sender][_spender] = _value;
Approval(msg.sender, _spender, _value);
return true;
}
function changeApproval(address _spender, uint256 _oldValue, uint256 _newValue) stopInEmergency onlyPayloadSize(3) returns (bool ok) {
require(allowance[msg.sender][_spender] == _oldValue);
allowance[msg.sender][_spender] = _newValue;
Approval(msg.sender, _spender, _newValue);
return true;
}
}