-
Notifications
You must be signed in to change notification settings - Fork 0
/
DolphinPresaleToken.sol
426 lines (338 loc) · 11.9 KB
/
DolphinPresaleToken.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
pragma solidity ^0.4.13;
import "github.com/oraclize/ethereum-api/oraclizeAPI.sol";
library SafeMath {
function mul(uint256 a, uint256 b) internal returns (uint256) {
uint256 c = a * b;
assert(a == 0 || c / a == b);
return c;
}
function div(uint256 a, uint256 b) internal returns (uint256) {
uint256 c = a / b;
return c;
}
function sub(uint256 a, uint256 b) internal returns (uint256) {
assert(b <= a);
return a - b;
}
function add(uint256 a, uint256 b) internal returns (uint256) {
uint256 c = a + b;
assert(c >= a);
return c;
}
}
contract CMCEthereumTicker is usingOraclize {
using SafeMath for uint;
uint centsPerETH;
uint delay;
bool enabled;
address parent;
address manager;
modifier onlyParentOrManager() { require(msg.sender == parent || msg.sender == manager); _; }
event newOraclizeQuery(string description);
event newPriceTicker(string price);
function CMCEthereumTicker(address _manager, uint256 _delay) {
oraclize_setProof(proofType_NONE);
enabled = false;
parent = msg.sender;
manager = _manager;
delay = _delay;
}
function getCentsPerETH() constant returns(uint) {
return centsPerETH;
}
function getEnabled() constant returns(bool) {
return enabled;
}
function enable()
onlyParentOrManager
{
require(enabled == false);
enabled = true;
update_instant();
}
function disable()
onlyParentOrManager
{
require(enabled == true);
enabled = false;
}
function __callback(bytes32 myid, string result) {
if (msg.sender != oraclize_cbAddress()) revert();
centsPerETH = parseInt(result, 2);
newPriceTicker(result);
if (enabled) {
update();
}
}
function update_instant() payable {
if (oraclize.getPrice("URL") > this.balance) {
newOraclizeQuery("Oraclize query was NOT sent, please add some ETH to cover for the query fee");
} else {
newOraclizeQuery("Oraclize query was sent, standing by for the answer..");
oraclize_query("URL", "json(https://api.coinmarketcap.com/v1/ticker/ethereum/?convert=USD).0.price_usd");
}
}
function update() payable {
if (oraclize.getPrice("URL") > this.balance) {
newOraclizeQuery("Oraclize query was NOT sent, please add some ETH to cover for the query fee");
} else {
newOraclizeQuery("Oraclize query was sent, standing by for the answer..");
oraclize_query(delay, "URL", "json(https://api.coinmarketcap.com/v1/ticker/ethereum/?convert=USD).0.price_usd");
}
}
function payToManager(uint _amount)
onlyParentOrManager
{
manager.transfer(_amount);
}
function () payable {}
}
contract PresaleToken {
using SafeMath for uint256;
function PresaleToken(uint256 _limitUSD, uint256 _priceCents) {
tokenManager = msg.sender;
priceCents = _priceCents;
maxSupply = (uint(10)**decimals).mul(100).mul(_limitUSD).div(_priceCents);
}
enum Phase {
Created,
Running,
Finished,
Finalized,
Migrating,
Migrated
}
// maximum token supply
uint public maxSupply;
// price of 1 token in USD cents
uint public priceCents;
// Ticker contract
CMCEthereumTicker priceTicker;
//Phase on contract creation
Phase public currentPhase = Phase.Created;
// amount of tokens already sold
uint supply = 0;
// amount of tokens given via giveTokens
uint public givenSupply = 0;
// Token manager has exclusive priveleges to call administrative
// functions on this contract.
address public tokenManager;
// Migration manager has privileges to burn tokens during migration.
address public migrationManager;
// The last buyer is the buyer that purchased
// tokens that add up to the maxSupply or more.
// During the presale finalization they are refunded
// the excess USD according to lastCentsPerETH.
address lastBuyer;
uint refundValue = 0;
uint lastCentsPerETH = 0;
//ERC 20 Containers
mapping (address => uint256) private balance;
mapping (address => mapping(address => uint256)) private allowed;
//ERC 20 Additional info
string public constant name = "Dolphin Presale Token";
string public constant symbol = "DBIP";
uint8 public constant decimals = 18;
//External access modifiers
modifier onlyTokenManager() { require(msg.sender == tokenManager); _; }
modifier onlyMigrationManager() { require(msg.sender == migrationManager); _; }
//Presale phase modifiers
modifier onlyWhileCreated() {assert(currentPhase == Phase.Created); _;}
modifier onlyWhileRunning() {assert(currentPhase == Phase.Running); _;}
modifier onlyWhileFinished() {assert(currentPhase == Phase.Finished); _;}
modifier onlyWhileFinalized() {assert(currentPhase == Phase.Finalized); _;}
modifier onlyBeforeMigration() {assert(currentPhase != Phase.Migrating && currentPhase != Phase.Migrated); _;}
modifier onlyWhileMigrating() {assert(currentPhase == Phase.Migrating); _;}
//Modifier to defend against shortened address attack
modifier minimalPayloadSize(uint size) {
assert(msg.data.length >= size + 4);
_;
}
//Presale events
event LogBuy(address indexed owner, uint value, uint centsPerETH);
event LogGive(address indexed owner, uint value, string reason);
event LogMigrate(address indexed owner, uint value);
event LogPhaseSwitch(Phase newPhase);
//ERC20 events
event Transfer(address indexed _from, address indexed _to, uint256 _value);
event Approval(address indexed _owner, address indexed _spender, uint256 _value);
function() payable {
buyTokens(msg.sender);
}
///ERC20 Interface functions
function balanceOf(address _owner) constant returns (uint256 balanceOf) {
return balance[_owner];
}
function totalSupply() constant returns (uint256 totalSupply) {
return supply;
}
function allowance(address _owner, address _spender) constant returns (uint256 remaining) {
return allowed[_owner][_spender];
}
function transfer(address _to, uint256 _value) onlyBeforeMigration minimalPayloadSize(2 * 32) returns (bool success)
{
assert(_value > 0);
balance[msg.sender] = balance[msg.sender].sub(_value);
balance[_to] = balance[_to].add(_value);
Transfer(msg.sender,_to,_value);
return true;
}
function approve(address _spender, uint256 _value) onlyBeforeMigration returns (bool success) {
assert((_value == 0) || (allowed[msg.sender][_spender] == 0));
allowed[msg.sender][_spender] = _value;
Approval(msg.sender,_spender,_value);
return true;
}
function transferFrom(address _from, address _to, uint256 _value) onlyBeforeMigration minimalPayloadSize(3 * 32) returns (bool success) {
assert(_value > 0);
allowed[_from][msg.sender] = allowed[_from][msg.sender].sub(_value);
balance[_from] = balance[_from].sub(_value);
balance[_to] = balance[_to].add(_value);
Transfer(_from,_to,_value);
return true;
}
///Presale-specific functions
function buyTokens(address _buyer) payable
onlyWhileRunning
{
require(msg.value != 0);
require(priceTicker.getEnabled());
var centsPerETH = getCentsPerETH();
require(centsPerETH != 0);
var newTokens = msg.value.mul(centsPerETH).mul(uint(10)**decimals).div(priceCents.mul(1 ether / 1 wei));
assert(newTokens != 0);
if (supply.add(newTokens) > maxSupply) {
var remainder = maxSupply.sub(supply);
balance[_buyer] = balance[_buyer].add(remainder);
supply = supply.add(remainder);
lastBuyer = _buyer;
refundValue = newTokens.sub(remainder);
LogBuy(_buyer, remainder, centsPerETH);
}
else {
balance[_buyer] = balance[_buyer].add(newTokens);
supply = supply.add(newTokens);
LogBuy(_buyer, newTokens, centsPerETH);
}
if (supply == maxSupply) {
lastCentsPerETH = centsPerETH;
currentPhase = Phase.Finished;
LogPhaseSwitch(Phase.Finished);
}
}
function migrateTokens(address _owner)
onlyMigrationManager
onlyWhileMigrating
{
assert(balance[_owner] != 0);
var migratedValue = balance[_owner];
supply = supply.sub(migratedValue);
balance[_owner] = 0;
LogMigrate(_owner, migratedValue);
if(supply == 0) {
currentPhase = Phase.Migrated;
LogPhaseSwitch(Phase.Migrated);
}
}
function startPresale()
onlyTokenManager
onlyWhileCreated
{
assert(address(priceTicker) != 0x0);
currentPhase = Phase.Running;
LogPhaseSwitch(Phase.Running);
}
function finishPresale()
onlyTokenManager
onlyWhileRunning
{
lastCentsPerETH = priceTicker.getCentsPerETH();
currentPhase = Phase.Finished;
LogPhaseSwitch(Phase.Finished);
}
function finalizePresale()
onlyTokenManager
onlyWhileFinished
{
if (refundValue != 0) {
lastBuyer.transfer((refundValue.mul(priceCents).mul(1 ether / 1 wei)).div(lastCentsPerETH.mul(uint(10)**decimals)));
}
withdrawEther();
currentPhase = Phase.Finalized;
LogPhaseSwitch(Phase.Finalized);
}
function startMigration()
onlyTokenManager
onlyWhileFinalized
{
assert(migrationManager != 0x0);
currentPhase = Phase.Migrating;
LogPhaseSwitch(Phase.Migrating);
}
function withdrawEther() private
{
assert(this.balance > 0);
tokenManager.transfer(this.balance);
}
function setMigrationManager(address _mgr)
onlyTokenManager
onlyWhileFinalized
{
migrationManager = _mgr;
}
function raiseCap(uint _newCap)
onlyTokenManager
onlyWhileFinalized
{
assert(_newCap > maxSupply);
maxSupply = _newCap;
currentPhase = Phase.Running;
LogPhaseSwitch(Phase.Running);
}
function giveTokens(address _address, uint _value, string _reason)
onlyTokenManager
onlyBeforeMigration
{
balance[_address] = balance[_address].add(_value);
supply = supply.add(_value);
givenSupply = givenSupply.add(_value);
LogGive(_address, _value, _reason);
}
///Ticker interaction functions
function createTicker(uint256 _delay)
onlyTokenManager
{
priceTicker = new CMCEthereumTicker(tokenManager, _delay);
}
function attachTicker(address _tickerAddress)
onlyTokenManager
{
priceTicker = CMCEthereumTicker(_tickerAddress);
}
function enableTicker()
onlyTokenManager
{
priceTicker.enable();
}
function disableTicker()
onlyTokenManager
{
priceTicker.disable();
}
function sendToTicker() payable
onlyTokenManager
{
assert(address(priceTicker) != 0x0);
address(priceTicker).transfer(msg.value);
}
function withdrawFromTicker(uint _amount) {
assert(address(priceTicker) != 0x0);
priceTicker.payToManager(_amount);
}
function tickerAddress() constant returns (address) {
return address(priceTicker);
}
function getCentsPerETH() constant returns (uint) {
return priceTicker.getCentsPerETH();
}
}