-
Notifications
You must be signed in to change notification settings - Fork 0
/
NFTXStakingZap.sol
746 lines (679 loc) · 23.4 KB
/
NFTXStakingZap.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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "./util/SafeERC20Upgradeable.sol";
import "./interface/INFTXLPStaking.sol";
import "./interface/INFTXInventoryStaking.sol";
import "./interface/INFTXVaultFactory.sol";
import "./interface/INFTXVault.sol";
import "./interface/INFTXSimpleFeeDistributor.sol";
import "./interface/IUniswapV2Router01.sol";
import "./interface/ITimelockExcludeList.sol";
import "./token/ERC721HolderUpgradeable.sol";
import "./token/ERC1155HolderUpgradeable.sol";
import "./token/IERC1155Upgradeable.sol";
// Authors: @0xKiwi_.
interface IWETH {
function deposit() external payable;
function transfer(address to, uint256 value) external returns (bool);
function withdraw(uint256) external;
}
/**
* @dev Contract module that helps prevent reentrant calls to a function.
*
* Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier
* available, which can be applied to functions to make sure there are no nested
* (reentrant) calls to them.
*
* Note that because there is a single `nonReentrant` guard, functions marked as
* `nonReentrant` may not call one another. This can be worked around by making
* those functions `private`, and then adding `external` `nonReentrant` entry
* points to them.
*
* TIP: If you would like to learn more about reentrancy and alternative ways
* to protect against it, check out our blog post
* https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
*/
abstract contract ReentrancyGuard {
// Booleans are more expensive than uint256 or any type that takes up a full
// word because each write operation emits an extra SLOAD to first read the
// slot's contents, replace the bits taken up by the boolean, and then write
// back. This is the compiler's defense against contract upgrades and
// pointer aliasing, and it cannot be disabled.
// The values being non-zero value makes deployment a bit more expensive,
// but in exchange the refund on every call to nonReentrant will be lower in
// amount. Since refunds are capped to a percentage of the total
// transaction's gas, it is best to keep them low in cases like this one, to
// increase the likelihood of the full refund coming into effect.
uint256 private constant _NOT_ENTERED = 1;
uint256 private constant _ENTERED = 2;
uint256 private _status;
constructor() {
_status = _NOT_ENTERED;
}
/**
* @dev Prevents a contract from calling itself, directly or indirectly.
* Calling a `nonReentrant` function from another `nonReentrant`
* function is not supported. It is possible to prevent this from happening
* by making the `nonReentrant` function external, and make it call a
* `private` function that does the actual work.
*/
modifier nonReentrant() {
// On the first call to nonReentrant, _notEntered will be true
require(_status != _ENTERED, "ReentrancyGuard: reentrant call");
// Any calls to nonReentrant after this point will fail
_status = _ENTERED;
_;
// By storing the original value once again, a refund is triggered (see
// https://eips.ethereum.org/EIPS/eip-2200)
_status = _NOT_ENTERED;
}
}
/**
* @dev Contract module which provides a basic access control mechanism, where
* there is an account (an owner) that can be granted exclusive access to
* specific functions.
*
* By default, the owner account will be the one that deploys the contract. This
* can later be changed with {transferOwnership}.
*
* This module is used through inheritance. It will make available the modifier
* `onlyOwner`, which can be applied to your functions to restrict their use to
* the owner.
*/
abstract contract Ownable {
address private _owner;
event OwnershipTransferred(
address indexed previousOwner,
address indexed newOwner
);
/**
* @dev Initializes the contract setting the deployer as the initial owner.
*/
constructor() {
_setOwner(msg.sender);
}
/**
* @dev Returns the address of the current owner.
*/
function owner() public view virtual returns (address) {
return _owner;
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
require(owner() == msg.sender, "Ownable: caller is not the owner");
_;
}
/**
* @dev Leaves the contract without owner. It will not be possible to call
* `onlyOwner` functions anymore. Can only be called by the current owner.
*
* NOTE: Renouncing ownership will leave the contract without an owner,
* thereby removing any functionality that is only available to the owner.
*/
function renounceOwnership() public virtual onlyOwner {
_setOwner(address(0));
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/
function transferOwnership(address newOwner) public virtual onlyOwner {
require(
newOwner != address(0),
"Ownable: new owner is the zero address"
);
_setOwner(newOwner);
}
function _setOwner(address newOwner) private {
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}
contract NFTXStakingZap is
Ownable,
ReentrancyGuard,
ERC721HolderUpgradeable,
ERC1155HolderUpgradeable
{
using SafeERC20Upgradeable for IERC20Upgradeable;
IWETH public immutable WETH;
INFTXLPStaking public lpStaking;
INFTXInventoryStaking public inventoryStaking;
INFTXVaultFactory public immutable nftxFactory;
IUniswapV2Router01 public immutable sushiRouter;
ITimelockExcludeList public timelockExcludeList;
uint256 public lpLockTime = 48 hours;
uint256 public inventoryLockTime = 7 days;
uint256 constant BASE = 1e18;
event UserStaked(
uint256 vaultId,
uint256 count,
uint256 lpBalance,
uint256 timelockUntil,
address sender
);
constructor(address _nftxFactory, address _sushiRouter)
Ownable()
ReentrancyGuard()
{
nftxFactory = INFTXVaultFactory(_nftxFactory);
sushiRouter = IUniswapV2Router01(_sushiRouter);
WETH = IWETH(IUniswapV2Router01(_sushiRouter).WETH());
IERC20Upgradeable(address(IUniswapV2Router01(_sushiRouter).WETH()))
.safeApprove(_sushiRouter, type(uint256).max);
}
function assignStakingContracts() public {
require(
address(lpStaking) == address(0) ||
address(inventoryStaking) == address(0),
"not zero"
);
lpStaking = INFTXLPStaking(
INFTXSimpleFeeDistributor(
INFTXVaultFactory(nftxFactory).feeDistributor()
).lpStaking()
);
inventoryStaking = INFTXInventoryStaking(
INFTXSimpleFeeDistributor(
INFTXVaultFactory(nftxFactory).feeDistributor()
).inventoryStaking()
);
}
function setTimelockExcludeList(address addr) external onlyOwner {
timelockExcludeList = ITimelockExcludeList(addr);
}
function setLPLockTime(uint256 newLPLockTime) external onlyOwner {
require(newLPLockTime <= 7 days, "Lock too long");
lpLockTime = newLPLockTime;
}
function setInventoryLockTime(uint256 newInventoryLockTime)
external
onlyOwner
{
require(newInventoryLockTime <= 14 days, "Lock too long");
inventoryLockTime = newInventoryLockTime;
}
function isAddressTimelockExcluded(address addr, uint256 vaultId)
public
view
returns (bool)
{
if (address(timelockExcludeList) == address(0)) {
return false;
} else {
return timelockExcludeList.isExcluded(addr, vaultId);
}
}
function provideInventory721(uint256 vaultId, uint256[] calldata tokenIds)
external
{
uint256 count = tokenIds.length;
INFTXVault vault = INFTXVault(nftxFactory.vault(vaultId));
uint256 timelockTime = isAddressTimelockExcluded(msg.sender, vaultId)
? 0
: inventoryLockTime;
inventoryStaking.timelockMintFor(
vaultId,
count * BASE,
msg.sender,
timelockTime
);
address xToken = inventoryStaking.vaultXToken(vaultId);
uint256 oldBal = IERC20Upgradeable(vault).balanceOf(xToken);
uint256[] memory amounts = new uint256[](0);
address assetAddress = vault.assetAddress();
uint256 length = tokenIds.length;
for (uint256 i; i < length; ++i) {
transferFromERC721(assetAddress, tokenIds[i], address(vault));
approveERC721(assetAddress, address(vault), tokenIds[i]);
}
vault.mintTo(tokenIds, amounts, address(xToken));
uint256 newBal = IERC20Upgradeable(vault).balanceOf(xToken);
require(newBal == oldBal + count * BASE, "Incorrect vtokens minted");
uint256 lockEndTime = block.timestamp + timelockTime;
emit UserStaked(vaultId, tokenIds.length, 0, lockEndTime, msg.sender);
}
function provideInventory1155(
uint256 vaultId,
uint256[] calldata tokenIds,
uint256[] calldata amounts
) external {
uint256 length = tokenIds.length;
require(length == amounts.length, "Not equal length");
uint256 count;
for (uint256 i; i < length; ++i) {
count += amounts[i];
}
INFTXVault vault = INFTXVault(nftxFactory.vault(vaultId));
uint256 timelockTime = isAddressTimelockExcluded(msg.sender, vaultId)
? 0
: inventoryLockTime;
inventoryStaking.timelockMintFor(
vaultId,
count * BASE,
msg.sender,
timelockTime
);
address xToken = inventoryStaking.vaultXToken(vaultId);
uint256 oldBal = IERC20Upgradeable(vault).balanceOf(address(xToken));
IERC1155Upgradeable nft = IERC1155Upgradeable(vault.assetAddress());
nft.safeBatchTransferFrom(
msg.sender,
address(this),
tokenIds,
amounts,
""
);
nft.setApprovalForAll(address(vault), true);
vault.mintTo(tokenIds, amounts, address(xToken));
uint256 newBal = IERC20Upgradeable(vault).balanceOf(address(xToken));
require(newBal == oldBal + count * BASE, "Incorrect vtokens minted");
uint256 lockEndTime = block.timestamp + timelockTime;
emit UserStaked(vaultId, tokenIds.length, 0, lockEndTime, msg.sender);
}
function addLiquidity721ETH(
uint256 vaultId,
uint256[] calldata ids,
uint256 minWethIn
) external payable returns (uint256) {
return addLiquidity721ETHTo(vaultId, ids, minWethIn, msg.sender);
}
function addLiquidity721ETHTo(
uint256 vaultId,
uint256[] memory ids,
uint256 minWethIn,
address to
) public payable nonReentrant returns (uint256) {
require(to != address(0) && to != address(this));
WETH.deposit{value: msg.value}();
(, uint256 amountEth, uint256 liquidity) = _addLiquidity721WETH(
vaultId,
ids,
minWethIn,
msg.value,
to
);
// Return extras.
uint256 remaining = msg.value - amountEth;
if (remaining != 0) {
WETH.withdraw(remaining);
(bool success, ) = payable(to).call{value: remaining}("");
require(
success,
"Address: unable to send value, recipient may have reverted"
);
}
return liquidity;
}
function addLiquidity1155ETH(
uint256 vaultId,
uint256[] calldata ids,
uint256[] calldata amounts,
uint256 minEthIn
) external payable returns (uint256) {
return
addLiquidity1155ETHTo(vaultId, ids, amounts, minEthIn, msg.sender);
}
function addLiquidity1155ETHTo(
uint256 vaultId,
uint256[] memory ids,
uint256[] memory amounts,
uint256 minEthIn,
address to
) public payable nonReentrant returns (uint256) {
require(to != address(0) && to != address(this));
WETH.deposit{value: msg.value}();
// Finish this.
(, uint256 amountEth, uint256 liquidity) = _addLiquidity1155WETH(
vaultId,
ids,
amounts,
minEthIn,
msg.value,
to
);
// Return extras.
uint256 remaining = msg.value - amountEth;
if (remaining != 0) {
WETH.withdraw(remaining);
(bool success, ) = payable(to).call{value: remaining}("");
require(
success,
"Address: unable to send value, recipient may have reverted"
);
}
return liquidity;
}
function addLiquidity721(
uint256 vaultId,
uint256[] calldata ids,
uint256 minWethIn,
uint256 wethIn
) external returns (uint256) {
return addLiquidity721To(vaultId, ids, minWethIn, wethIn, msg.sender);
}
function addLiquidity721To(
uint256 vaultId,
uint256[] memory ids,
uint256 minWethIn,
uint256 wethIn,
address to
) public nonReentrant returns (uint256) {
require(to != address(0) && to != address(this));
IERC20Upgradeable(address(WETH)).safeTransferFrom(
msg.sender,
address(this),
wethIn
);
(, uint256 amountEth, uint256 liquidity) = _addLiquidity721WETH(
vaultId,
ids,
minWethIn,
wethIn,
to
);
// Return extras.
uint256 remaining = wethIn - amountEth;
if (remaining != 0) {
WETH.transfer(to, remaining);
}
return liquidity;
}
function addLiquidity1155(
uint256 vaultId,
uint256[] memory ids,
uint256[] memory amounts,
uint256 minWethIn,
uint256 wethIn
) public returns (uint256) {
return
addLiquidity1155To(
vaultId,
ids,
amounts,
minWethIn,
wethIn,
msg.sender
);
}
function addLiquidity1155To(
uint256 vaultId,
uint256[] memory ids,
uint256[] memory amounts,
uint256 minWethIn,
uint256 wethIn,
address to
) public nonReentrant returns (uint256) {
require(to != address(0) && to != address(this));
IERC20Upgradeable(address(WETH)).safeTransferFrom(
msg.sender,
address(this),
wethIn
);
(, uint256 amountEth, uint256 liquidity) = _addLiquidity1155WETH(
vaultId,
ids,
amounts,
minWethIn,
wethIn,
to
);
// Return extras.
uint256 remaining = wethIn - amountEth;
if (remaining != 0) {
WETH.transfer(to, remaining);
}
return liquidity;
}
function _addLiquidity721WETH(
uint256 vaultId,
uint256[] memory ids,
uint256 minWethIn,
uint256 wethIn,
address to
)
internal
returns (
uint256,
uint256,
uint256
)
{
require(nftxFactory.excludedFromFees(address(this)));
address vault = nftxFactory.vault(vaultId);
// Transfer tokens to zap and mint to NFTX.
address assetAddress = INFTXVault(vault).assetAddress();
uint256 length = ids.length;
for (uint256 i; i < length; i++) {
transferFromERC721(assetAddress, ids[i], vault);
approveERC721(assetAddress, vault, ids[i]);
}
uint256[] memory emptyIds;
INFTXVault(vault).mint(ids, emptyIds);
uint256 balance = length * BASE; // We should not be experiencing fees.
return
_addLiquidityAndLock(
vaultId,
vault,
balance,
minWethIn,
wethIn,
to
);
}
function _addLiquidity1155WETH(
uint256 vaultId,
uint256[] memory ids,
uint256[] memory amounts,
uint256 minWethIn,
uint256 wethIn,
address to
)
internal
returns (
uint256,
uint256,
uint256
)
{
require(nftxFactory.excludedFromFees(address(this)));
address vault = nftxFactory.vault(vaultId);
// Transfer tokens to zap and mint to NFTX.
address assetAddress = INFTXVault(vault).assetAddress();
IERC1155Upgradeable(assetAddress).safeBatchTransferFrom(
msg.sender,
address(this),
ids,
amounts,
""
);
IERC1155Upgradeable(assetAddress).setApprovalForAll(vault, true);
uint256 count = INFTXVault(vault).mint(ids, amounts);
uint256 balance = (count * BASE); // We should not be experiencing fees.
return
_addLiquidityAndLock(
vaultId,
vault,
balance,
minWethIn,
wethIn,
to
);
}
function _addLiquidityAndLock(
uint256 vaultId,
address vault,
uint256 minTokenIn,
uint256 minWethIn,
uint256 wethIn,
address to
)
internal
returns (
uint256,
uint256,
uint256
)
{
// Provide liquidity.
IERC20Upgradeable(vault).safeApprove(address(sushiRouter), minTokenIn);
(
uint256 amountToken,
uint256 amountEth,
uint256 liquidity
) = sushiRouter.addLiquidity(
address(vault),
address(WETH),
minTokenIn,
wethIn,
minTokenIn,
minWethIn,
address(this),
block.timestamp
);
// Stake in LP rewards contract
IERC20Upgradeable(pairFor(vault, address(WETH))).safeApprove(
address(lpStaking),
liquidity
);
uint256 timelockTime = isAddressTimelockExcluded(msg.sender, vaultId)
? 0
: lpLockTime;
lpStaking.timelockDepositFor(vaultId, to, liquidity, timelockTime);
uint256 remaining = minTokenIn - amountToken;
if (remaining != 0) {
IERC20Upgradeable(vault).safeTransfer(to, remaining);
}
uint256 lockEndTime = block.timestamp + timelockTime;
emit UserStaked(vaultId, minTokenIn, liquidity, lockEndTime, to);
return (amountToken, amountEth, liquidity);
}
function transferFromERC721(
address assetAddr,
uint256 tokenId,
address to
) internal virtual {
address kitties = 0x06012c8cf97BEaD5deAe237070F9587f8E7A266d;
address punks = 0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB;
bytes memory data;
if (assetAddr == kitties) {
// Cryptokitties.
data = abi.encodeWithSignature(
"transferFrom(address,address,uint256)",
msg.sender,
to,
tokenId
);
} else if (assetAddr == punks) {
// CryptoPunks.
// Fix here for frontrun attack. Added in v1.0.2.
bytes memory punkIndexToAddress = abi.encodeWithSignature(
"punkIndexToAddress(uint256)",
tokenId
);
(bool checkSuccess, bytes memory result) = address(assetAddr)
.staticcall(punkIndexToAddress);
address nftOwner = abi.decode(result, (address));
require(
checkSuccess && nftOwner == msg.sender,
"Not the NFT owner"
);
data = abi.encodeWithSignature("buyPunk(uint256)", tokenId);
} else {
// Default.
// We push to the vault to avoid an unneeded transfer.
data = abi.encodeWithSignature(
"safeTransferFrom(address,address,uint256)",
msg.sender,
to,
tokenId
);
}
(bool success, bytes memory resultData) = address(assetAddr).call(data);
require(success, string(resultData));
}
function approveERC721(
address assetAddr,
address to,
uint256 tokenId
) internal virtual {
address kitties = 0x06012c8cf97BEaD5deAe237070F9587f8E7A266d;
address punks = 0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB;
bytes memory data;
if (assetAddr == kitties) {
// Cryptokitties.
// data = abi.encodeWithSignature("approve(address,uint256)", to, tokenId);
// No longer needed to approve with pushing.
return;
} else if (assetAddr == punks) {
// CryptoPunks.
data = abi.encodeWithSignature(
"offerPunkForSaleToAddress(uint256,uint256,address)",
tokenId,
0,
to
);
} else {
// No longer needed to approve with pushing.
return;
}
(bool success, bytes memory resultData) = address(assetAddr).call(data);
require(success, string(resultData));
}
// calculates the CREATE2 address for a pair without making any external calls
function pairFor(address tokenA, address tokenB)
internal
view
returns (address pair)
{
(address token0, address token1) = sortTokens(tokenA, tokenB);
pair = address(
uint160(
uint256(
keccak256(
abi.encodePacked(
hex"ff",
sushiRouter.factory(),
keccak256(abi.encodePacked(token0, token1)),
hex"e18a34eb0e04b04f7a0ac29a6e80748dca96319b42c54d679cb821dca90c6303" // init code hash
)
)
)
)
);
}
// returns sorted token addresses, used to handle return values from pairs sorted in this order
function sortTokens(address tokenA, address tokenB)
internal
pure
returns (address token0, address token1)
{
require(tokenA != tokenB, "UniswapV2Library: IDENTICAL_ADDRESSES");
(token0, token1) = tokenA < tokenB
? (tokenA, tokenB)
: (tokenB, tokenA);
require(token0 != address(0), "UniswapV2Library: ZERO_ADDRESS");
}
receive() external payable {
require(msg.sender == address(WETH), "Only WETH");
}
function rescue(address token) external onlyOwner {
if (token == address(0)) {
(bool success, ) = payable(msg.sender).call{
value: address(this).balance
}("");
require(
success,
"Address: unable to send value, recipient may have reverted"
);
} else {
IERC20Upgradeable(token).safeTransfer(
msg.sender,
IERC20Upgradeable(token).balanceOf(address(this))
);
}
}
}