forked from bokkypoobah/FxxxLandRush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FxxxLandRush_flattened.sol
434 lines (397 loc) · 19.7 KB
/
FxxxLandRush_flattened.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
pragma solidity ^0.4.25;
// ----------------------------------------------------------------------------
// Fxxx Land Rush Contract - Purchase land parcels with GZE and ETH
//
// Enjoy.
//
// (c) BokkyPooBah / Bok Consulting Pty Ltd for GazeCoin 2018. The MIT Licence.
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// Owned contract
// ----------------------------------------------------------------------------
contract Owned {
address public owner;
address public newOwner;
bool private initialised;
event OwnershipTransferred(address indexed _from, address indexed _to);
modifier onlyOwner {
require(msg.sender == owner);
_;
}
function initOwned(address _owner) internal {
require(!initialised);
owner = _owner;
initialised = true;
}
function transferOwnership(address _newOwner) public onlyOwner {
newOwner = _newOwner;
}
function acceptOwnership() public {
require(msg.sender == newOwner);
emit OwnershipTransferred(owner, newOwner);
owner = newOwner;
newOwner = address(0);
}
function transferOwnershipImmediately(address _newOwner) public onlyOwner {
emit OwnershipTransferred(owner, _newOwner);
owner = _newOwner;
}
}
// ----------------------------------------------------------------------------
// Safe maths
// ----------------------------------------------------------------------------
library SafeMath {
function add(uint a, uint b) internal pure returns (uint c) {
c = a + b;
require(c >= a);
}
function sub(uint a, uint b) internal pure returns (uint c) {
require(b <= a);
c = a - b;
}
function mul(uint a, uint b) internal pure returns (uint c) {
c = a * b;
require(a == 0 || c / a == b);
}
function div(uint a, uint b) internal pure returns (uint c) {
require(b > 0);
c = a / b;
}
function max(uint a, uint b) internal pure returns (uint c) {
c = a >= b ? a : b;
}
function min(uint a, uint b) internal pure returns (uint c) {
c = a <= b ? a : b;
}
}
// ----------------------------------------------------------------------------
// BokkyPooBah's Token Teleportation Service Interface v1.10
//
// https://github.com/bokkypoobah/BokkyPooBahsTokenTeleportationServiceSmartContract
//
// Enjoy. (c) BokkyPooBah / Bok Consulting Pty Ltd 2018. The MIT Licence.
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// ERC Token Standard #20 Interface
// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20-token-standard.md
// ----------------------------------------------------------------------------
contract ERC20Interface {
event Transfer(address indexed from, address indexed to, uint tokens);
event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
function totalSupply() public constant returns (uint);
function balanceOf(address tokenOwner) public constant returns (uint balance);
function allowance(address tokenOwner, address spender) public constant returns (uint remaining);
function transfer(address to, uint tokens) public returns (bool success);
function approve(address spender, uint tokens) public returns (bool success);
function transferFrom(address from, address to, uint tokens) public returns (bool success);
}
// ----------------------------------------------------------------------------
// Contracts that can have tokens approved, and then a function executed
// ----------------------------------------------------------------------------
contract ApproveAndCallFallBack {
function receiveApproval(address from, uint256 tokens, address token, bytes data) public;
}
// ----------------------------------------------------------------------------
// BokkyPooBah's Token Teleportation Service Interface v1.10
//
// Enjoy. (c) BokkyPooBah / Bok Consulting Pty Ltd 2018. The MIT Licence.
// ----------------------------------------------------------------------------
contract BTTSTokenInterface is ERC20Interface {
uint public constant bttsVersion = 110;
bytes public constant signingPrefix = "\x19Ethereum Signed Message:\n32";
bytes4 public constant signedTransferSig = "\x75\x32\xea\xac";
bytes4 public constant signedApproveSig = "\xe9\xaf\xa7\xa1";
bytes4 public constant signedTransferFromSig = "\x34\x4b\xcc\x7d";
bytes4 public constant signedApproveAndCallSig = "\xf1\x6f\x9b\x53";
event OwnershipTransferred(address indexed from, address indexed to);
event MinterUpdated(address from, address to);
event Mint(address indexed tokenOwner, uint tokens, bool lockAccount);
event MintingDisabled();
event TransfersEnabled();
event AccountUnlocked(address indexed tokenOwner);
function symbol() public view returns (string);
function name() public view returns (string);
function decimals() public view returns (uint8);
function approveAndCall(address spender, uint tokens, bytes data) public returns (bool success);
// ------------------------------------------------------------------------
// signed{X} functions
// ------------------------------------------------------------------------
function signedTransferHash(address tokenOwner, address to, uint tokens, uint fee, uint nonce) public view returns (bytes32 hash);
function signedTransferCheck(address tokenOwner, address to, uint tokens, uint fee, uint nonce, bytes sig, address feeAccount) public view returns (CheckResult result);
function signedTransfer(address tokenOwner, address to, uint tokens, uint fee, uint nonce, bytes sig, address feeAccount) public returns (bool success);
function signedApproveHash(address tokenOwner, address spender, uint tokens, uint fee, uint nonce) public view returns (bytes32 hash);
function signedApproveCheck(address tokenOwner, address spender, uint tokens, uint fee, uint nonce, bytes sig, address feeAccount) public view returns (CheckResult result);
function signedApprove(address tokenOwner, address spender, uint tokens, uint fee, uint nonce, bytes sig, address feeAccount) public returns (bool success);
function signedTransferFromHash(address spender, address from, address to, uint tokens, uint fee, uint nonce) public view returns (bytes32 hash);
function signedTransferFromCheck(address spender, address from, address to, uint tokens, uint fee, uint nonce, bytes sig, address feeAccount) public view returns (CheckResult result);
function signedTransferFrom(address spender, address from, address to, uint tokens, uint fee, uint nonce, bytes sig, address feeAccount) public returns (bool success);
function signedApproveAndCallHash(address tokenOwner, address spender, uint tokens, bytes _data, uint fee, uint nonce) public view returns (bytes32 hash);
function signedApproveAndCallCheck(address tokenOwner, address spender, uint tokens, bytes _data, uint fee, uint nonce, bytes sig, address feeAccount) public view returns (CheckResult result);
function signedApproveAndCall(address tokenOwner, address spender, uint tokens, bytes _data, uint fee, uint nonce, bytes sig, address feeAccount) public returns (bool success);
function mint(address tokenOwner, uint tokens, bool lockAccount) public returns (bool success);
function unlockAccount(address tokenOwner) public;
function disableMinting() public;
function enableTransfers() public;
// ------------------------------------------------------------------------
// signed{X}Check return status
// ------------------------------------------------------------------------
enum CheckResult {
Success, // 0 Success
NotTransferable, // 1 Tokens not transferable yet
AccountLocked, // 2 Account locked
SignerMismatch, // 3 Mismatch in signing account
InvalidNonce, // 4 Invalid nonce
InsufficientApprovedTokens, // 5 Insufficient approved tokens
InsufficientApprovedTokensForFees, // 6 Insufficient approved tokens for fees
InsufficientTokens, // 7 Insufficient tokens
InsufficientTokensForFees, // 8 Insufficient tokens for fees
OverflowError // 9 Overflow error
}
}
// ----------------------------------------------------------------------------
// PriceFeed Interface - _live is true if the rate is valid, false if invalid
// ----------------------------------------------------------------------------
contract PriceFeedInterface {
function name() public view returns (string);
function getRate() public view returns (uint _rate, bool _live);
}
// ----------------------------------------------------------------------------
// Bonus List interface
// ----------------------------------------------------------------------------
contract BonusListInterface {
function isInBonusList(address account) public view returns (bool);
}
// ----------------------------------------------------------------------------
// FxxxLandRush Contract
// ----------------------------------------------------------------------------
contract FxxxLandRush is Owned, ApproveAndCallFallBack {
using SafeMath for uint;
uint private constant TENPOW18 = 10 ** 18;
BTTSTokenInterface public parcelToken;
BTTSTokenInterface public gzeToken;
PriceFeedInterface public ethUsdPriceFeed;
PriceFeedInterface public gzeEthPriceFeed;
BonusListInterface public bonusList;
address public wallet;
uint public startDate;
uint public endDate;
uint public maxParcels;
uint public parcelUsd; // USD per parcel, e.g., USD 1,500 * 10^18
uint public usdLockAccountThreshold; // e.g., USD 7,000 * 10^18
uint public gzeBonusOffList; // e.g., 20 = 20% bonus
uint public gzeBonusOnList; // e.g., 30 = 30% bonus
uint public parcelsSold;
uint public contributedGze;
uint public contributedEth;
bool public finalised;
event WalletUpdated(address indexed oldWallet, address indexed newWallet);
event StartDateUpdated(uint oldStartDate, uint newStartDate);
event EndDateUpdated(uint oldEndDate, uint newEndDate);
event MaxParcelsUpdated(uint oldMaxParcels, uint newMaxParcels);
event ParcelUsdUpdated(uint oldParcelUsd, uint newParcelUsd);
event UsdLockAccountThresholdUpdated(uint oldUsdLockAccountThreshold, uint newUsdLockAccountThreshold);
event GzeBonusOffListUpdated(uint oldGzeBonusOffList, uint newGzeBonusOffList);
event GzeBonusOnListUpdated(uint oldGzeBonusOnList, uint newGzeBonusOnList);
event Purchased(address indexed addr, uint parcels, uint gzeToTransfer, uint ethToTransfer, uint parcelsSold, uint contributedGze, uint contributedEth, bool lockAccount);
constructor(address _parcelToken, address _gzeToken, address _ethUsdPriceFeed, address _gzeEthPriceFeed, address _bonusList, address _wallet, uint _startDate, uint _endDate, uint _maxParcels, uint _parcelUsd, uint _usdLockAccountThreshold, uint _gzeBonusOffList, uint _gzeBonusOnList) public {
require(_parcelToken != address(0) && _gzeToken != address(0));
require(_ethUsdPriceFeed != address(0) && _gzeEthPriceFeed != address(0) && _bonusList != address(0));
require(_wallet != address(0));
require(_startDate >= now && _endDate > _startDate);
require(_maxParcels > 0 && _parcelUsd > 0);
initOwned(msg.sender);
parcelToken = BTTSTokenInterface(_parcelToken);
gzeToken = BTTSTokenInterface(_gzeToken);
ethUsdPriceFeed = PriceFeedInterface(_ethUsdPriceFeed);
gzeEthPriceFeed = PriceFeedInterface(_gzeEthPriceFeed);
bonusList = BonusListInterface(_bonusList);
wallet = _wallet;
startDate = _startDate;
endDate = _endDate;
maxParcels = _maxParcels;
parcelUsd = _parcelUsd;
usdLockAccountThreshold = _usdLockAccountThreshold;
gzeBonusOffList = _gzeBonusOffList;
gzeBonusOnList = _gzeBonusOnList;
}
function setWallet(address _wallet) public onlyOwner {
require(!finalised);
require(_wallet != address(0));
emit WalletUpdated(wallet, _wallet);
wallet = _wallet;
}
function setStartDate(uint _startDate) public onlyOwner {
require(!finalised);
require(_startDate >= now);
emit StartDateUpdated(startDate, _startDate);
startDate = _startDate;
}
function setEndDate(uint _endDate) public onlyOwner {
require(!finalised);
require(_endDate > startDate);
emit EndDateUpdated(endDate, _endDate);
endDate = _endDate;
}
function setMaxParcels(uint _maxParcels) public onlyOwner {
require(!finalised);
require(_maxParcels >= parcelsSold);
emit MaxParcelsUpdated(maxParcels, _maxParcels);
maxParcels = _maxParcels;
}
function setParcelUsd(uint _parcelUsd) public onlyOwner {
require(!finalised);
require(_parcelUsd > 0);
emit ParcelUsdUpdated(parcelUsd, _parcelUsd);
parcelUsd = _parcelUsd;
}
function setUsdLockAccountThreshold(uint _usdLockAccountThreshold) public onlyOwner {
require(!finalised);
emit UsdLockAccountThresholdUpdated(usdLockAccountThreshold, _usdLockAccountThreshold);
usdLockAccountThreshold = _usdLockAccountThreshold;
}
function setGzeBonusOffList(uint _gzeBonusOffList) public onlyOwner {
require(!finalised);
emit GzeBonusOffListUpdated(gzeBonusOffList, _gzeBonusOffList);
gzeBonusOffList = _gzeBonusOffList;
}
function setGzeBonusOnList(uint _gzeBonusOnList) public onlyOwner {
require(!finalised);
emit GzeBonusOnListUpdated(gzeBonusOnList, _gzeBonusOnList);
gzeBonusOnList = _gzeBonusOnList;
}
function symbol() public view returns (string _symbol) {
_symbol = parcelToken.symbol();
}
function name() public view returns (string _name) {
_name = parcelToken.name();
}
// USD per ETH, e.g., 221.99 * 10^18
function ethUsd() public view returns (uint _rate, bool _live) {
return ethUsdPriceFeed.getRate();
}
// ETH per GZE, e.g., 0.00004366 * 10^18
function gzeEth() public view returns (uint _rate, bool _live) {
return gzeEthPriceFeed.getRate();
}
// USD per GZE, e.g., 0.0096920834 * 10^18
function gzeUsd() public view returns (uint _rate, bool _live) {
uint _ethUsd;
bool _ethUsdLive;
(_ethUsd, _ethUsdLive) = ethUsdPriceFeed.getRate();
uint _gzeEth;
bool _gzeEthLive;
(_gzeEth, _gzeEthLive) = gzeEthPriceFeed.getRate();
if (_ethUsdLive && _gzeEthLive) {
_live = true;
_rate = _ethUsd.mul(_gzeEth).div(TENPOW18);
}
}
// ETH per parcel, e.g., 6.757061128879679264 * 10^18
function parcelEth() public view returns (uint _rate, bool _live) {
uint _ethUsd;
(_ethUsd, _live) = ethUsd();
if (_live) {
_rate = parcelUsd.mul(TENPOW18).div(_ethUsd);
}
}
// GZE per parcel, without bonus, e.g., 154765.486231783766945298 * 10^18
function parcelGzeWithoutBonus() public view returns (uint _rate, bool _live) {
uint _gzeUsd;
(_gzeUsd, _live) = gzeUsd();
if (_live) {
_rate = parcelUsd.mul(TENPOW18).div(_gzeUsd);
}
}
// GZE per parcel, with bonus but not on bonus list, e.g., 128971.238526486472454415 * 10^18
function parcelGzeWithBonusOffList() public view returns (uint _rate, bool _live) {
uint _parcelGzeWithoutBonus;
(_parcelGzeWithoutBonus, _live) = parcelGzeWithoutBonus();
if (_live) {
_rate = _parcelGzeWithoutBonus.mul(100).div(gzeBonusOffList.add(100));
}
}
// GZE per parcel, with bonus and on bonus list, e.g., 119050.374024449051496383 * 10^18
function parcelGzeWithBonusOnList() public view returns (uint _rate, bool _live) {
uint _parcelGzeWithoutBonus;
(_parcelGzeWithoutBonus, _live) = parcelGzeWithoutBonus();
if (_live) {
_rate = _parcelGzeWithoutBonus.mul(100).div(gzeBonusOnList.add(100));
}
}
// Account contributes by:
// 1. calling GZE.approve(landRushAddress, tokens)
// 2. calling this.purchaseWithGze(tokens)
function purchaseWithGze(uint256 tokens) public {
require(gzeToken.allowance(msg.sender, this) >= tokens);
receiveApproval(msg.sender, tokens, gzeToken, "");
}
// Account contributes by calling GZE.approveAndCall(landRushAddress, tokens, "")
function receiveApproval(address from, uint256 tokens, address token, bytes /* data */) public {
require(now >= startDate && now <= endDate);
require(token == address(gzeToken));
uint _parcelGze;
bool _live;
if (bonusList.isInBonusList(from)) {
(_parcelGze, _live) = parcelGzeWithBonusOnList();
} else {
(_parcelGze, _live) = parcelGzeWithBonusOffList();
}
require(_live);
uint parcels = tokens.div(_parcelGze);
if (parcelsSold.add(parcels) >= maxParcels) {
parcels = maxParcels.sub(parcelsSold);
}
uint gzeToTransfer = parcels.mul(_parcelGze);
contributedGze = contributedGze.add(gzeToTransfer);
require(ERC20Interface(token).transferFrom(from, wallet, gzeToTransfer));
bool lock = mintParcelTokens(from, parcels);
emit Purchased(from, parcels, gzeToTransfer, 0, parcelsSold, contributedGze, contributedEth, lock);
}
// Account contributes by sending ETH
function () public payable {
require(now >= startDate && now <= endDate);
uint _parcelEth;
bool _live;
(_parcelEth, _live) = parcelEth();
require(_live);
uint parcels = msg.value.div(_parcelEth);
if (parcelsSold.add(parcels) >= maxParcels) {
parcels = maxParcels.sub(parcelsSold);
}
uint ethToTransfer = parcels.mul(_parcelEth);
contributedEth = contributedEth.add(ethToTransfer);
uint ethToRefund = msg.value.sub(ethToTransfer);
if (ethToRefund > 0) {
msg.sender.transfer(ethToRefund);
}
bool lock = mintParcelTokens(msg.sender, parcels);
emit Purchased(msg.sender, parcels, 0, ethToTransfer, parcelsSold, contributedGze, contributedEth, lock);
}
// Contract owner allocates parcels to tokenOwner for offline purchase
function offlinePurchase(address tokenOwner, uint parcels) public onlyOwner {
require(!finalised);
if (parcelsSold.add(parcels) >= maxParcels) {
parcels = maxParcels.sub(parcelsSold);
}
bool lock = mintParcelTokens(tokenOwner, parcels);
emit Purchased(tokenOwner, parcels, 0, 0, parcelsSold, contributedGze, contributedEth, lock);
}
// Internal function to mint tokens and disable minting if maxParcels sold
function mintParcelTokens(address account, uint parcels) internal returns (bool _lock) {
require(parcels > 0);
parcelsSold = parcelsSold.add(parcels);
_lock = parcelToken.balanceOf(account).add(parcelUsd.mul(parcels)) >= usdLockAccountThreshold;
require(parcelToken.mint(account, parcelUsd.mul(parcels), _lock));
if (parcelsSold >= maxParcels) {
parcelToken.disableMinting();
finalised = true;
}
}
// Contract owner finalises to disable parcel minting
function finalise() public onlyOwner {
require(!finalised);
require(now > endDate || parcelsSold >= maxParcels);
parcelToken.disableMinting();
finalised = true;
}
}