-
Notifications
You must be signed in to change notification settings - Fork 0
/
NFTXMarketplace0xZap.sol
690 lines (539 loc) · 21.9 KB
/
NFTXMarketplace0xZap.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "./interface/INFTXVault.sol";
import "./interface/INFTXVaultFactory.sol";
import "./testing/IERC1155.sol";
import "./testing/ERC721Holder.sol";
import "./testing/ERC1155Holder.sol";
import "./util/Ownable.sol";
import "./util/ReentrancyGuard.sol";
import "./util/SafeERC20.sol";
/**
* @notice A partial WETH interface.
*/
interface IWETH {
function deposit() external payable;
function transfer(address to, uint value) external returns (bool);
function withdraw(uint) external;
function balanceOf(address to) external view returns (uint256);
}
/**
* @notice Sets up a marketplace zap to interact with the 0x protocol. The 0x contract that
* is hit later on handles the token conversion based on parameters that are sent from the
* frontend.
*
* @author Twade
*/
contract NFTXMarketplace0xZap is Ownable, ReentrancyGuard, ERC721Holder, ERC1155Holder {
using SafeERC20 for IERC20;
/// @notice Allows zap to be paused
bool public paused = false;
/// @notice Sets our 0x swap target
address payable private immutable swapTarget;
/// @notice An interface for the WETH contract
IWETH public immutable WETH;
/// @notice An interface for the NFTX Vault Factory contract
INFTXVaultFactory public immutable nftxFactory;
address public immutable feeDistributor;
/// @notice The vToken threshold below which dust is sent to feeDistributor, else back to the user
uint256 public dustThreshold;
/// @notice A mapping of NFTX Vault IDs to their address corresponding vault contract address
mapping(uint256 => address) public nftxVaultAddresses;
// Set a constant address for specific contracts that need special logic
address constant CRYPTO_PUNKS = 0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB;
/// @notice Emitted by the `buyAndRedeem` function.
/// @param count The number of tokens affected by the event
/// @param ethSpent The amount of ETH spent in the buy
/// @param to The user affected by the event
event Buy(uint256 count, uint256 ethSpent, address to);
/// @notice Emitted by the `mintAndSell` functions.
/// @param count The number of tokens affected by the event
/// @param ethReceived The amount of ETH received in the sell
/// @param to The user affected by the event
event Sell(uint256 count, uint256 ethReceived, address to);
/// @notice Emitted by the `buyAndSwap` functions.
/// @param count The number of tokens affected by the event
/// @param ethSpent The amount of ETH spent in the swap
/// @param to The user affected by the event
event Swap(uint256 count, uint256 ethSpent, address to);
/// @notice Emitted when dust is returned after a transaction.
/// @param ethAmount Amount of ETH returned to user
/// @param vTokenAmount Amount of vToken returned to user
/// @param to The user affected by the event
event DustReturned(uint256 ethAmount, uint256 vTokenAmount, address to);
/**
* @notice Initialises our zap by setting contract addresses onto their
* respective interfaces.
*
* @param _nftxFactory NFTX Vault Factory contract address
* @param _WETH WETH contract address
* @param _swapTarget The swap target specified by the 0x protocol
*/
constructor(address _nftxFactory, address _WETH, address payable _swapTarget, uint256 _dustThreshold) Ownable() ReentrancyGuard() {
nftxFactory = INFTXVaultFactory(_nftxFactory);
WETH = IWETH(_WETH);
swapTarget = _swapTarget;
feeDistributor = INFTXVaultFactory(_nftxFactory).feeDistributor();
dustThreshold = _dustThreshold;
}
/**
* @notice Mints tokens from our NFTX vault and sells them on 0x.
*
* @param vaultId The ID of the NFTX vault
* @param ids An array of token IDs to be minted
* @param swapCallData The `data` field from the API response
* @param to The recipient of ETH from the tx
*/
function mintAndSell721(
uint256 vaultId,
uint256[] calldata ids,
bytes calldata swapCallData,
address payable to
) external nonReentrant onlyOwnerIfPaused {
// Check that we aren't burning tokens or sending to ourselves
require(to != address(0) && to != address(this), 'Invalid recipient');
// Check that we have been provided IDs
require(ids.length != 0, 'Must send IDs');
// Mint our 721s against the vault
address vault = _mint721(vaultId, ids);
// Sell our vault token for WETH
uint256 amount = _fillQuote(vault, address(WETH), swapCallData);
// convert WETH to ETH and send to `to`
_transferAllWETH(to);
// Emit our sale event
emit Sell(ids.length, amount, to);
// Handle vault token dust
_transferDust(vault, false);
}
/**
* @notice Purchases vault tokens from 0x with WETH and then swaps the tokens for
* either random or specific token IDs from the vault. The specified recipient will
* receive the ERC721 tokens, as well as any WETH dust that is left over from the tx.
*
* @param vaultId The ID of the NFTX vault
* @param idsIn An array of random token IDs to be minted
* @param specificIds An array of any specific token IDs to be minted
* @param swapCallData The `data` field from the API response
* @param to The recipient of the token IDs from the tx
*/
function buyAndSwap721(
uint256 vaultId,
uint256[] calldata idsIn,
uint256[] calldata specificIds,
bytes calldata swapCallData,
address payable to
) external payable nonReentrant onlyOwnerIfPaused {
// Check that we aren't burning tokens or sending to ourselves
require(to != address(0) && to != address(this), 'Invalid recipient');
// Check that we have been provided IDs
require(idsIn.length != 0, 'Must send IDs');
// Check that we have a message value sent
require(msg.value > 0, 'Invalid amount');
// Wrap ETH into WETH for our contract from the sender
WETH.deposit{value: msg.value}();
// Get our NFTX vault
address vault = _vaultAddress(vaultId);
// Buy enough vault tokens to fuel our buy
uint256 amount = _fillQuote(address(WETH), vault, swapCallData);
// Swap our tokens for the IDs requested
_swap721(vaultId, idsIn, specificIds, to);
emit Swap(idsIn.length, amount, to);
// Transfer dust ETH to sender and handle vault token dust
_transferDust(vault, true);
}
/**
* @notice Purchases vault tokens from 0x with WETH and then redeems the tokens for
* either random or specific token IDs from the vault. The specified recipient will
* receive the ERC721 tokens, as well as any WETH dust that is left over from the tx.
*
* @param vaultId The ID of the NFTX vault
* @param amount The number of tokens to buy
* @param specificIds An array of any specific token IDs to be minted
* @param swapCallData The `data` field from the API response
* @param to The recipient of the token IDs from the tx
*/
function buyAndRedeem(
uint256 vaultId,
uint256 amount,
uint256[] calldata specificIds,
bytes calldata swapCallData,
address payable to
) external payable nonReentrant onlyOwnerIfPaused {
// Check that we aren't burning tokens or sending to ourselves
require(to != address(0) && to != address(this), 'Invalid recipient');
// Check that we have an amount specified
require(amount > 0, 'Must send amount');
// Wrap ETH into WETH for our contract from the sender
WETH.deposit{value: msg.value}();
// Get our vault address information
address vault = _vaultAddress(vaultId);
// Buy vault tokens that will cover our transaction
uint256 quoteAmount = _fillQuote(address(WETH), vault, swapCallData);
// check if received sufficient vault tokens
require(quoteAmount >= amount * 1e18, 'Insufficient vault tokens');
// Redeem token IDs from the vault
_redeem(vaultId, amount, specificIds, to);
emit Buy(amount, quoteAmount, to);
// Transfer dust ETH to sender and handle vault token dust
_transferDust(vault, true);
}
/**
* @notice Mints tokens from our NFTX vault and sells them on 0x.
*
* @param vaultId The ID of the NFTX vault
* @param ids An array of token IDs to be minted
* @param amounts The number of the corresponding ID to be minted
* @param swapCallData The `data` field from the API response
* @param to The recipient of ETH from the tx
*/
function mintAndSell1155(
uint256 vaultId,
uint256[] calldata ids,
uint256[] calldata amounts,
bytes calldata swapCallData,
address payable to
) external nonReentrant onlyOwnerIfPaused {
// Check that we aren't burning tokens or sending to ourselves
require(to != address(0) && to != address(this), 'Invalid recipient');
// Get a sum of the total number of IDs we have sent up, and validate that
// the data sent through is valid.
(, uint totalAmount) = _validate1155Ids(ids, amounts);
// Mint our 1155s against the vault
address vault = _mint1155(vaultId, ids, amounts);
// Sell our vault token for WETH
uint256 amount = _fillQuote(vault, address(WETH), swapCallData);
// convert WETH to ETH and send to `to`
_transferAllWETH(to);
// Emit our sale event
emit Sell(totalAmount, amount, to);
// Handle vault token dust
_transferDust(vault, false);
}
/**
* @notice Purchases vault tokens from 0x with WETH and then swaps the tokens for
* either random or specific token IDs from the vault. The specified recipient will
* receive the ERC1155 tokens, as well as any WETH dust that is left over from the tx.
*
* @param vaultId The ID of the NFTX vault
* @param idsIn An array of random token IDs to be minted
* @param specificIds An array of any specific token IDs to be minted
* @param swapCallData The `data` field from the API response
* @param to The recipient of token IDs from the tx
*/
function buyAndSwap1155(
uint256 vaultId,
uint256[] calldata idsIn,
uint256[] calldata amounts,
uint256[] calldata specificIds,
bytes calldata swapCallData,
address payable to
) external payable nonReentrant onlyOwnerIfPaused {
// Check that we aren't burning tokens or sending to ourselves
require(to != address(0) && to != address(this), 'Invalid recipient');
// Check that we have a message value sent
require(msg.value > 0, 'Invalid amount');
// Get a sum of the total number of IDs we have sent up, and validate that
// the data sent through is valid.
(, uint totalAmount) = _validate1155Ids(idsIn, amounts);
// Wrap ETH into WETH for our contract from the sender
WETH.deposit{value: msg.value}();
// Get our NFTX vault
address vault = _vaultAddress(vaultId);
// Buy enough vault tokens to fuel our buy
uint256 amount = _fillQuote(address(WETH), vault, swapCallData);
// Swap our tokens for the IDs requested
_swap1155(vaultId, idsIn, amounts, specificIds, to);
emit Swap(totalAmount, amount, to);
// Transfer dust ETH to sender and handle vault token dust
_transferDust(vault, true);
}
/**
* @param vaultId The ID of the NFTX vault
* @param ids An array of token IDs to be minted
*/
function _mint721(uint256 vaultId, uint256[] memory ids) internal returns (address) {
// Get our vault address information
address vault = _vaultAddress(vaultId);
// Transfer tokens from the message sender to the vault
address assetAddress = INFTXVault(vault).assetAddress();
uint256 length = ids.length;
for (uint256 i; i < length;) {
transferFromERC721(assetAddress, ids[i], vault);
if (assetAddress == CRYPTO_PUNKS) {
_approveERC721(assetAddress, ids[i], vault);
}
unchecked { ++i; }
}
// Mint our tokens from the vault to this contract
uint256[] memory emptyIds;
INFTXVault(vault).mint(ids, emptyIds);
return vault;
}
/**
* @param vaultId The ID of the NFTX vault
* @param ids An array of token IDs to be minted
* @param amounts An array of amounts whose indexes map to the ids array
*/
function _mint1155(uint256 vaultId, uint256[] memory ids, uint256[] memory amounts) internal returns (address) {
// Get our vault address information
address vault = _vaultAddress(vaultId);
// Transfer tokens from the message sender to the vault
address assetAddress = INFTXVault(vault).assetAddress();
IERC1155(assetAddress).safeBatchTransferFrom(msg.sender, address(this), ids, amounts, "");
IERC1155(assetAddress).setApprovalForAll(vault, true);
// Mint our tokens from the vault to this contract
INFTXVault(vault).mint(ids, amounts);
return vault;
}
/**
*
* @param vaultId The ID of the NFTX vault
* @param idsIn An array of token IDs to be minted
* @param idsOut An array of token IDs to be redeemed
* @param to The recipient of the idsOut from the tx
*/
function _swap721(
uint256 vaultId,
uint256[] memory idsIn,
uint256[] memory idsOut,
address to
) internal returns (address) {
// Get our vault address information
address vault = _vaultAddress(vaultId);
// Transfer tokens to zap
address assetAddress = INFTXVault(vault).assetAddress();
uint256 length = idsIn.length;
for (uint256 i; i < length;) {
transferFromERC721(assetAddress, idsIn[i], vault);
if (assetAddress == CRYPTO_PUNKS) {
_approveERC721(assetAddress, idsIn[i], vault);
}
unchecked { ++i; }
}
// Swap our tokens
uint256[] memory emptyIds;
INFTXVault(vault).swapTo(idsIn, emptyIds, idsOut, to);
return vault;
}
/**
* @notice Swaps 1155 tokens, transferring them from the recipient to this contract, and
* then sending them to the NFTX vault, that sends them to the recipient.
*
* @param vaultId The ID of the NFTX vault
* @param idsIn The IDs owned by the sender to be swapped
* @param amounts The number of each corresponding ID being swapped
* @param idsOut The requested IDs to be swapped for
* @param to The recipient of the swapped tokens
*
* @return address The address of the NFTX vault
*/
function _swap1155(
uint256 vaultId,
uint256[] memory idsIn,
uint256[] memory amounts,
uint256[] memory idsOut,
address to
) internal returns (address) {
// Get our vault address information
address vault = _vaultAddress(vaultId);
// Transfer tokens to zap and mint to NFTX.
address assetAddress = INFTXVault(vault).assetAddress();
IERC1155(assetAddress).safeBatchTransferFrom(msg.sender, address(this), idsIn, amounts, "");
IERC1155(assetAddress).setApprovalForAll(vault, true);
INFTXVault(vault).swapTo(idsIn, amounts, idsOut, to);
return vault;
}
/**
* @notice Redeems tokens from a vault to a recipient.
*
* @param vaultId The ID of the NFTX vault
* @param amount The number of tokens to be redeemed
* @param specificIds Specified token IDs if desired, otherwise will be _random_
* @param to The recipient of the token
*/
function _redeem(uint256 vaultId, uint256 amount, uint256[] memory specificIds, address to) internal {
INFTXVault(_vaultAddress(vaultId)).redeemTo(amount, specificIds, to);
}
/**
* @notice Transfers our ERC721 tokens to a specified recipient.
*
* @param assetAddr Address of the asset being transferred
* @param tokenId The ID of the token being transferred
* @param to The address the token is being transferred to
*/
function transferFromERC721(address assetAddr, uint256 tokenId, address to) internal virtual {
bytes memory data;
if (assetAddr == CRYPTO_PUNKS) {
// Fix here for frontrun attack.
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 {
// 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));
}
/**
* @notice Approves our ERC721 tokens to be transferred.
*
* @dev This is only required to provide special logic for Cryptopunks.
*
* @param assetAddr Address of the asset being transferred
* @param tokenId The ID of the token being transferred
* @param to The address the token is being transferred to
*/
function _approveERC721(address assetAddr, uint256 tokenId, address to) internal virtual {
if (assetAddr != CRYPTO_PUNKS) {
return;
}
bytes memory data = abi.encodeWithSignature("offerPunkForSaleToAddress(uint256,uint256,address)", tokenId, 0, to);
(bool success, bytes memory resultData) = address(assetAddr).call(data);
require(success, string(resultData));
}
/**
* @notice Swaps ERC20->ERC20 tokens held by this contract using a 0x-API quote.
*
* @dev Must attach ETH equal to the `value` field from the API response.
*
* @param sellToken The `sellTokenAddress` field from the API response
* @param buyToken The `buyTokenAddress` field from the API response
* @param swapCallData The `data` field from the API response
*/
function _fillQuote(
address sellToken,
address buyToken,
bytes calldata swapCallData
) internal returns (uint256) {
// Track our balance of the buyToken to determine how much we've bought.
uint256 boughtAmount = IERC20(buyToken).balanceOf(address(this));
// Give `swapTarget` an infinite allowance to spend this contract's `sellToken`.
// Note that for some tokens (e.g., USDT, KNC), you must first reset any existing
// allowance to 0 before being able to update it.
require(IERC20(sellToken).approve(swapTarget, type(uint256).max), 'Unable to approve contract');
// Call the encoded swap function call on the contract at `swapTarget`
(bool success,) = swapTarget.call(swapCallData);
require(success, 'SWAP_CALL_FAILED');
// Use our current buyToken balance to determine how much we've bought.
return IERC20(buyToken).balanceOf(address(this)) - boughtAmount;
}
/**
* @notice Transfers remaining ETH to msg.sender.
* And transfers vault token dust to feeDistributor if below dustThreshold, else to msg.sender
*
* @param vault Address of the vault token
* @param isWETHDust Checks and transfers WETH dust if boolean is true
*/
function _transferDust(address vault, bool isWETHDust) internal {
uint256 remaining;
if(isWETHDust) {
remaining = _transferAllWETH(msg.sender);
}
uint256 dustBalance = IERC20(vault).balanceOf(address(this));
address dustRecipient;
if(dustBalance > 0) {
if (dustBalance > dustThreshold) {
dustRecipient = msg.sender;
} else {
dustRecipient = feeDistributor;
}
IERC20(vault).transfer(dustRecipient, dustBalance);
}
emit DustReturned(remaining, dustBalance, dustRecipient);
}
function _transferAllWETH(address recipient) internal returns(uint256 amount) {
amount = WETH.balanceOf(address(this));
if (amount > 0) {
// Unwrap our WETH into ETH and transfer it to the recipient
WETH.withdraw(amount);
(bool success, ) = payable(recipient).call{value: amount}("");
require(success, "Unable to send unwrapped WETH");
}
}
/**
* @notice Allows 1155 IDs and amounts to be validated.
*
* @param ids The IDs of the 1155 tokens.
* @param amounts The number of each corresponding token to process.
*
* @return totalIds The number of different IDs being sent.
* @return totalAmount The total number of IDs being processed.
*/
function _validate1155Ids(
uint[] calldata ids,
uint[] calldata amounts
) internal pure returns (
uint totalIds,
uint totalAmount
) {
totalIds = ids.length;
// Check that we have been provided IDs
require(totalIds != 0, 'Must send IDs');
require(totalIds <= amounts.length, 'Must define amounts against IDs');
// Sum the amounts for our emitted events
for (uint i; i < totalIds;) {
require(amounts[i] > 0, 'Invalid 1155 amount');
unchecked {
totalAmount += amounts[i];
++i;
}
}
}
/**
* @notice Maps a cached NFTX vault address against a vault ID.
*
* @param vaultId The ID of the NFTX vault
*/
function _vaultAddress(uint256 vaultId) internal returns (address) {
if (nftxVaultAddresses[vaultId] == address(0)) {
nftxVaultAddresses[vaultId] = nftxFactory.vault(vaultId);
}
require(nftxVaultAddresses[vaultId] != address(0), 'Vault does not exist');
return nftxVaultAddresses[vaultId];
}
/**
* @notice Allows our zap to be paused to prevent any processing.
*
* @param _paused New pause state
*/
function pause(bool _paused) external onlyOwner {
paused = _paused;
}
/**
* @notice Allows owner to modify dustThreshold value
*
* @param _dustThreshold New dustThreshold
*/
function setDustThreshold(uint256 _dustThreshold) external onlyOwner {
dustThreshold = _dustThreshold;
}
/**
* @notice Allows our owner to withdraw and tokens in the contract.
*
* @param token The address of the token to be rescued
*/
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");
} else {
IERC20(token).safeTransfer(msg.sender, IERC20(token).balanceOf(address(this)));
}
}
/**
* @notice A modifier that only allows the owner to interact with the function
* if the contract is paused. If the contract is not paused then anyone can
* interact with the function.
*/
modifier onlyOwnerIfPaused() {
require(!paused || msg.sender == owner(), "Zap is paused");
_;
}
/**
* @notice Allows our contract to receive any assets.
*/
receive() external payable {}
}