-
Notifications
You must be signed in to change notification settings - Fork 89
/
AxiePresaleExtended.sol
472 lines (392 loc) · 12.7 KB
/
AxiePresaleExtended.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
pragma solidity ^0.4.19;
import "zeppelin/contracts/lifecycle/Pausable.sol";
import "zeppelin/contracts/math/SafeMath.sol";
import "zeppelin/contracts/ownership/HasNoContracts.sol";
import "./AxiePresale.sol";
contract AxiePresaleExtended is HasNoContracts, Pausable {
using SafeMath for uint256;
// No Axies can be adopted after this end date: Monday, April 16, 2018 11:59:59 PM GMT.
uint256 constant public PRESALE_END_TIMESTAMP = 1523923199;
// The total number of adopted Axies will be capped at 5250,
// so the number of Axies which have Mystic parts will be capped roughly at 2000.
uint256 constant public MAX_TOTAL_ADOPTED_AXIES = 5250;
uint8 constant public CLASS_BEAST = 0;
uint8 constant public CLASS_AQUATIC = 2;
uint8 constant public CLASS_PLANT = 4;
// The initial price increment and the initial price are for reference only
uint256 constant public INITIAL_PRICE_INCREMENT = 1600 szabo; // 0.0016 Ether
uint256 constant public INITIAL_PRICE = INITIAL_PRICE_INCREMENT;
uint256 constant public REF_CREDITS_PER_AXIE = 5;
AxiePresale public presaleContract;
address public redemptionAddress;
mapping (uint8 => uint256) public currentPrice;
mapping (uint8 => uint256) public priceIncrement;
mapping (uint8 => uint256) private _totalAdoptedAxies;
mapping (uint8 => uint256) private _totalDeductedAdoptedAxies;
mapping (address => mapping (uint8 => uint256)) private _numAdoptedAxies;
mapping (address => mapping (uint8 => uint256)) private _numDeductedAdoptedAxies;
mapping (address => uint256) private _numRefCredits;
mapping (address => uint256) private _numDeductedRefCredits;
uint256 public numBountyCredits;
uint256 private _totalRewardedAxies;
uint256 private _totalDeductedRewardedAxies;
mapping (address => uint256) private _numRewardedAxies;
mapping (address => uint256) private _numDeductedRewardedAxies;
event AxiesAdopted(
address indexed _adopter,
uint8 indexed _class,
uint256 _quantity,
address indexed _referrer
);
event AxiesRewarded(address indexed _receiver, uint256 _quantity);
event AdoptedAxiesRedeemed(address indexed _receiver, uint8 indexed _class, uint256 _quantity);
event RewardedAxiesRedeemed(address indexed _receiver, uint256 _quantity);
event RefCreditsMinted(address indexed _receiver, uint256 _numMintedCredits);
function AxiePresaleExtended() public payable {
require(msg.value == 0);
paused = true;
numBountyCredits = 300;
}
function () external payable {
require(msg.sender == address(presaleContract));
}
modifier whenNotInitialized {
require(presaleContract == address(0));
_;
}
modifier whenInitialized {
require(presaleContract != address(0));
_;
}
modifier onlyRedemptionAddress {
require(msg.sender == redemptionAddress);
_;
}
function reclaimEther() external onlyOwner whenInitialized {
presaleContract.reclaimEther();
owner.transfer(this.balance);
}
/**
* @dev This must be called only once after the owner of the presale contract
* has been updated to this contract.
*/
function initialize(address _presaleAddress) external onlyOwner whenNotInitialized {
// Set the presale address.
presaleContract = AxiePresale(_presaleAddress);
presaleContract.pause();
// Restore price increments from the old contract.
priceIncrement[CLASS_BEAST] = presaleContract.priceIncrements(CLASS_BEAST);
priceIncrement[CLASS_AQUATIC] = presaleContract.priceIncrements(CLASS_AQUATIC);
priceIncrement[CLASS_PLANT] = presaleContract.priceIncrements(CLASS_PLANT);
// Restore current prices from the old contract.
currentPrice[CLASS_BEAST] = presaleContract.currentPrices(CLASS_BEAST);
currentPrice[CLASS_AQUATIC] = presaleContract.currentPrices(CLASS_AQUATIC);
currentPrice[CLASS_PLANT] = presaleContract.currentPrices(CLASS_PLANT);
paused = false;
}
function setRedemptionAddress(address _redemptionAddress) external onlyOwner whenInitialized {
redemptionAddress = _redemptionAddress;
}
function totalAdoptedAxies(
uint8 _class,
bool _deduction
)
external
view
whenInitialized
returns (uint256 _number)
{
_number = _totalAdoptedAxies[_class]
.add(presaleContract.totalAxiesAdopted(_class));
if (_deduction) {
_number = _number.sub(_totalDeductedAdoptedAxies[_class]);
}
}
function numAdoptedAxies(
address _owner,
uint8 _class,
bool _deduction
)
external
view
whenInitialized
returns (uint256 _number)
{
_number = _numAdoptedAxies[_owner][_class]
.add(presaleContract.axiesAdopted(_owner, _class));
if (_deduction) {
_number = _number.sub(_numDeductedAdoptedAxies[_owner][_class]);
}
}
function numRefCredits(
address _owner,
bool _deduction
)
external
view
whenInitialized
returns (uint256 _number)
{
_number = _numRefCredits[_owner]
.add(presaleContract.referralCredits(_owner));
if (_deduction) {
_number = _number.sub(_numDeductedRefCredits[_owner]);
}
}
function totalRewardedAxies(
bool _deduction
)
external
view
whenInitialized
returns (uint256 _number)
{
_number = _totalRewardedAxies
.add(presaleContract.totalAxiesRewarded());
if (_deduction) {
_number = _number.sub(_totalDeductedRewardedAxies);
}
}
function numRewardedAxies(
address _owner,
bool _deduction
)
external
view
whenInitialized
returns (uint256 _number)
{
_number = _numRewardedAxies[_owner]
.add(presaleContract.axiesRewarded(_owner));
if (_deduction) {
_number = _number.sub(_numDeductedRewardedAxies[_owner]);
}
}
function axiesPrice(
uint256 _beastQuantity,
uint256 _aquaticQuantity,
uint256 _plantQuantity
)
external
view
whenInitialized
returns (uint256 _totalPrice)
{
uint256 price;
(price,,) = _sameClassAxiesPrice(CLASS_BEAST, _beastQuantity);
_totalPrice = _totalPrice.add(price);
(price,,) = _sameClassAxiesPrice(CLASS_AQUATIC, _aquaticQuantity);
_totalPrice = _totalPrice.add(price);
(price,,) = _sameClassAxiesPrice(CLASS_PLANT, _plantQuantity);
_totalPrice = _totalPrice.add(price);
}
function adoptAxies(
uint256 _beastQuantity,
uint256 _aquaticQuantity,
uint256 _plantQuantity,
address _referrer
)
external
payable
whenInitialized
whenNotPaused
{
require(now <= PRESALE_END_TIMESTAMP);
require(_beastQuantity <= 3 && _aquaticQuantity <= 3 && _plantQuantity <= 3);
uint256 _totalAdopted = this.totalAdoptedAxies(CLASS_BEAST, false)
.add(this.totalAdoptedAxies(CLASS_AQUATIC, false))
.add(this.totalAdoptedAxies(CLASS_PLANT, false))
.add(_beastQuantity)
.add(_aquaticQuantity)
.add(_plantQuantity);
require(_totalAdopted <= MAX_TOTAL_ADOPTED_AXIES);
address _adopter = msg.sender;
address _actualReferrer = 0x0;
// An adopter cannot be his/her own referrer.
if (_referrer != _adopter) {
_actualReferrer = _referrer;
}
uint256 _value = msg.value;
uint256 _price;
if (_beastQuantity > 0) {
_price = _adoptSameClassAxies(
_adopter,
CLASS_BEAST,
_beastQuantity,
_actualReferrer
);
require(_value >= _price);
_value -= _price;
}
if (_aquaticQuantity > 0) {
_price = _adoptSameClassAxies(
_adopter,
CLASS_AQUATIC,
_aquaticQuantity,
_actualReferrer
);
require(_value >= _price);
_value -= _price;
}
if (_plantQuantity > 0) {
_price = _adoptSameClassAxies(
_adopter,
CLASS_PLANT,
_plantQuantity,
_actualReferrer
);
require(_value >= _price);
_value -= _price;
}
msg.sender.transfer(_value);
// The current referral is ignored if the referrer's address is 0x0.
if (_actualReferrer != 0x0) {
_applyRefCredits(
_actualReferrer,
_beastQuantity.add(_aquaticQuantity).add(_plantQuantity)
);
}
}
function mintRefCredits(
address _receiver,
uint256 _numMintedCredits
)
external
onlyOwner
whenInitialized
returns (uint256)
{
require(_receiver != address(0));
numBountyCredits = numBountyCredits.sub(_numMintedCredits);
_applyRefCredits(_receiver, _numMintedCredits);
RefCreditsMinted(_receiver, _numMintedCredits);
return numBountyCredits;
}
function redeemAdoptedAxies(
address _receiver,
uint256 _beastQuantity,
uint256 _aquaticQuantity,
uint256 _plantQuantity
)
external
onlyRedemptionAddress
whenInitialized
returns (
uint256 /* remainingBeastQuantity */,
uint256 /* remainingAquaticQuantity */,
uint256 /* remainingPlantQuantity */
)
{
return (
_redeemSameClassAdoptedAxies(_receiver, CLASS_BEAST, _beastQuantity),
_redeemSameClassAdoptedAxies(_receiver, CLASS_AQUATIC, _aquaticQuantity),
_redeemSameClassAdoptedAxies(_receiver, CLASS_PLANT, _plantQuantity)
);
}
function redeemRewardedAxies(
address _receiver,
uint256 _quantity
)
external
onlyRedemptionAddress
whenInitialized
returns (uint256 _remainingQuantity)
{
_remainingQuantity = this.numRewardedAxies(_receiver, true).sub(_quantity);
if (_quantity > 0) {
_numDeductedRewardedAxies[_receiver] = _numDeductedRewardedAxies[_receiver].add(_quantity);
_totalDeductedRewardedAxies = _totalDeductedRewardedAxies.add(_quantity);
RewardedAxiesRedeemed(_receiver, _quantity);
}
}
/**
* @notice Calculate price of Axies from the same class.
* @param _class The class of Axies.
* @param _quantity Number of Axies to be calculated.
*/
function _sameClassAxiesPrice(
uint8 _class,
uint256 _quantity
)
private
view
returns (
uint256 _totalPrice,
uint256 /* should be _subsequentIncrement */ _currentIncrement,
uint256 /* should be _subsequentPrice */ _currentPrice
)
{
_currentIncrement = priceIncrement[_class];
_currentPrice = currentPrice[_class];
uint256 _nextPrice;
for (uint256 i = 0; i < _quantity; i++) {
_totalPrice = _totalPrice.add(_currentPrice);
_nextPrice = _currentPrice.add(_currentIncrement);
if (_nextPrice / 100 finney != _currentPrice / 100 finney) {
_currentIncrement >>= 1;
}
_currentPrice = _nextPrice;
}
}
/**
* @notice Adopt some Axies from the same class.
* @dev The quantity MUST be positive.
* @param _adopter Address of the adopter.
* @param _class The class of adopted Axies.
* @param _quantity Number of Axies to be adopted.
* @param _referrer Address of the referrer.
*/
function _adoptSameClassAxies(
address _adopter,
uint8 _class,
uint256 _quantity,
address _referrer
)
private
returns (uint256 _totalPrice)
{
(_totalPrice, priceIncrement[_class], currentPrice[_class]) = _sameClassAxiesPrice(_class, _quantity);
_numAdoptedAxies[_adopter][_class] = _numAdoptedAxies[_adopter][_class].add(_quantity);
_totalAdoptedAxies[_class] = _totalAdoptedAxies[_class].add(_quantity);
AxiesAdopted(
_adopter,
_class,
_quantity,
_referrer
);
}
function _applyRefCredits(address _receiver, uint256 _numAppliedCredits) private {
_numRefCredits[_receiver] = _numRefCredits[_receiver].add(_numAppliedCredits);
uint256 _numCredits = this.numRefCredits(_receiver, true);
uint256 _numRewards = _numCredits / REF_CREDITS_PER_AXIE;
if (_numRewards > 0) {
_numDeductedRefCredits[_receiver] = _numDeductedRefCredits[_receiver]
.add(_numRewards.mul(REF_CREDITS_PER_AXIE));
_numRewardedAxies[_receiver] = _numRewardedAxies[_receiver].add(_numRewards);
_totalRewardedAxies = _totalRewardedAxies.add(_numRewards);
AxiesRewarded(_receiver, _numRewards);
}
}
/**
* @notice Redeem adopted Axies from the same class.
* @dev Emit the `AdoptedAxiesRedeemed` event if the quantity is positive.
* @param _receiver The address of the receiver.
* @param _class The class of adopted Axies.
* @param _quantity The number of adopted Axies to be redeemed.
*/
function _redeemSameClassAdoptedAxies(
address _receiver,
uint8 _class,
uint256 _quantity
)
private
returns (uint256 _remainingQuantity)
{
_remainingQuantity = this.numAdoptedAxies(_receiver, _class, true).sub(_quantity);
if (_quantity > 0) {
_numDeductedAdoptedAxies[_receiver][_class] = _numDeductedAdoptedAxies[_receiver][_class].add(_quantity);
_totalDeductedAdoptedAxies[_class] = _totalDeductedAdoptedAxies[_class].add(_quantity);
AdoptedAxiesRedeemed(_receiver, _class, _quantity);
}
}
}