-
Notifications
You must be signed in to change notification settings - Fork 10
/
MarketplaceUniversalRouterZap.sol
804 lines (689 loc) · 26.1 KB
/
MarketplaceUniversalRouterZap.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
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
// SPDX-License-Identifier: MIT
pragma solidity =0.8.15;
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {ERC721Holder} from "@openzeppelin/contracts/token/ERC721/utils/ERC721Holder.sol";
import {ERC1155Holder} from "@openzeppelin/contracts/token/ERC1155/utils/ERC1155Holder.sol";
import {SafeCast} from "@uni-core/libraries/SafeCast.sol";
import {TransferLib} from "@src/lib/TransferLib.sol";
import {SafeERC20, IERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {IWETH9} from "@uni-periphery/interfaces/external/IWETH9.sol";
import {IERC1155} from "@openzeppelin/contracts/token/ERC1155/IERC1155.sol";
import {IERC2981} from "@openzeppelin/contracts/interfaces/IERC2981.sol";
import {INFTXVaultV3} from "@src/interfaces/INFTXVaultV3.sol";
import {INFTXVaultFactoryV3} from "@src/interfaces/INFTXVaultFactoryV3.sol";
import {INFTXFeeDistributorV3} from "@src/interfaces/INFTXFeeDistributorV3.sol";
import {IPermitAllowanceTransfer} from "@src/interfaces/external/IPermitAllowanceTransfer.sol";
/**
* @title Marketplace Universal Router Zap
* @author @apoorvlathey
*
* @notice Marketplace Zap that utilizes Universal Router to facilitate the require token swaps
* @dev This Zap must be excluded from the vault fees, as vault fees handled via custom logic here.
*/
contract MarketplaceUniversalRouterZap is Ownable, ERC721Holder, ERC1155Holder {
using SafeERC20 for IERC20;
// =============================================================
// CONSTANTS
// =============================================================
IWETH9 public immutable WETH;
IPermitAllowanceTransfer public immutable PERMIT2;
INFTXVaultFactoryV3 public immutable nftxVaultFactory;
address public immutable inventoryStaking;
bytes4 private constant _INTERFACE_ID_ERC2981 = 0x2a55205a;
uint256 constant BASE = 10 ** 18;
// =============================================================
// VARIABLES
// =============================================================
address public universalRouter;
/// @notice Allows zap to be paused
bool public paused = false;
/// @notice The vToken threshold below which dust is sent to InventoryStaking, else back to the user
uint256 public dustThreshold;
// =============================================================
// EVENTS
// =============================================================
/// @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
/// @param netRoyaltyAmount The royalty amount sent
/// @param wethFees Vault fees paid
event Sell(
uint256 vaultId,
uint256 count,
uint256 ethReceived,
address to,
uint256 netRoyaltyAmount,
uint256 wethFees
);
/// @param ethSpent The amount of ETH spent in the swap
/// @param to The user affected by the event
event Swap(
uint256 vaultId,
uint256[] idsIn,
uint256[] idsOut,
uint256 ethSpent,
address to
);
event Swap(
uint256 vaultId,
uint256[] idsIn,
uint256[] amounts,
uint256[] idsOut,
uint256 ethSpent,
address to
);
/// @param nftIds The nftIds bought
/// @param ethSpent The amount of ETH spent in the buy
/// @param to The user affected by the event
/// @param netRoyaltyAmount The royalty amount sent
event Buy(
uint256 vaultId,
uint256[] nftIds,
uint256 ethSpent,
address to,
uint256 netRoyaltyAmount
);
/// @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);
event Paused(bool status);
event NewUniversalRouter(address universalRouter);
event NewDustThreshold(uint256 dustThreshold);
// =============================================================
// ERRORS
// =============================================================
error ZapPaused();
error SwapFailed();
error UnableToSendETH();
error InsufficientWethForVaultFees();
error WrongVaultType();
error NotEnoughFundsForRedeem();
error ZeroAddress();
// =============================================================
// INIT
// =============================================================
constructor(
INFTXVaultFactoryV3 nftxVaultFactory_,
address universalRouter_,
IPermitAllowanceTransfer PERMIT2_,
address inventoryStaking_,
IWETH9 WETH_
) {
nftxVaultFactory = nftxVaultFactory_;
universalRouter = universalRouter_;
PERMIT2 = PERMIT2_;
inventoryStaking = inventoryStaking_;
WETH = WETH_;
}
// =============================================================
// PUBLIC / EXTERNAL WRITE
// =============================================================
/**
* @notice Sell idsIn to ETH
*
* @dev idsIn --{--mint-> [vault] -> vTokens --sell-> [UniversalRouter] --}-> ETH
*
* @param vaultId The ID of the NFTX vault
* @param idsIn An array of token IDs to be deposited
* @param executeCallData Encoded calldata for Universal Router's `execute` function
* @param to The recipient of ETH from the tx
*/
function sell721(
uint256 vaultId,
uint256[] calldata idsIn,
bytes calldata executeCallData,
address payable to,
bool deductRoyalty
) external onlyOwnerIfPaused {
// Mint (vault fees not deducted here, as zap is on exclusion list)
(address vault, address assetAddress) = _mint721(vaultId, idsIn);
// swap vTokens to WETH
(uint256 wethAmount, ) = _swapTokens(
vault,
address(WETH),
executeCallData
);
// distributing vault fees with the weth received
uint256 wethFees = _ethMintFees(INFTXVaultV3(vault), idsIn.length);
if (wethAmount < wethFees) revert InsufficientWethForVaultFees();
_distributeVaultFees(vaultId, wethFees, true);
uint256 netRoyaltyAmount;
if (deductRoyalty) {
netRoyaltyAmount = _deductRoyalty(assetAddress, idsIn, wethAmount);
}
wethAmount -= (wethFees + netRoyaltyAmount);
// convert WETH to ETH and send remaining ETH to `to`
_wethToETHResidue(to, wethAmount);
// Emit our sale event
emit Sell(
vaultId,
idsIn.length,
wethAmount,
to,
netRoyaltyAmount,
wethFees
);
}
/**
* @notice Swap idsIn to idsOut
* Send ETH along as well (via msg.value) to account for swap fees
*/
function swap721(
uint256 vaultId,
uint256[] calldata idsIn,
uint256[] calldata idsOut,
uint256 vTokenPremiumLimit,
address payable to
) external payable onlyOwnerIfPaused {
// Transfer tokens from the message sender to the vault
(address vault, ) = _transferSender721ToVault(vaultId, idsIn);
// Swap our tokens. Forcing to deduct vault fees
uint256[] memory emptyAmounts;
uint256 ethFees = INFTXVaultV3(vault).swap{value: msg.value}(
idsIn,
emptyAmounts,
idsOut,
msg.sender,
to,
vTokenPremiumLimit,
true
);
// send back remaining ETH
_sendETHResidue(to);
emit Swap(vaultId, idsIn, idsOut, ethFees, to);
}
/**
* @notice Buy idsOut with ETH
*
* @dev ETH --{-sell-> [UniversalRouter] -> vTokens + ETH --redeem-> [vault] --}-> idsOut
*
* @param vaultId The ID of the NFTX vault
* @param idsOut An array of any token IDs to be redeemed
* @param executeCallData Encoded calldata for Universal Router's `execute` function
* @param to The recipient of the token IDs from the tx
*/
function buyNFTsWithETH(
uint256 vaultId,
uint256[] calldata idsOut,
bytes calldata executeCallData,
address payable to,
uint256 vTokenPremiumLimit,
bool deductRoyalty
) external payable onlyOwnerIfPaused {
// Wrap ETH into WETH for our contract
WETH.deposit{value: msg.value}();
// swap WETH to vTokens
address vault = nftxVaultFactory.vault(vaultId);
uint256 wethSpent;
uint256 vTokenAmount;
{
uint256 iniWETHBal = WETH.balanceOf(address(this));
(vTokenAmount, ) = _swapTokens(
address(WETH),
vault,
executeCallData
);
if (vTokenAmount < idsOut.length * BASE)
revert NotEnoughFundsForRedeem();
wethSpent = iniWETHBal - WETH.balanceOf(address(this));
}
uint256 wethLeft = msg.value - wethSpent;
// redeem NFTs. Forcing to deduct vault fees
TransferLib.unSafeMaxApprove(address(WETH), vault, wethLeft);
uint256 wethFees = INFTXVaultV3(vault).redeem(
idsOut,
to,
wethLeft,
vTokenPremiumLimit,
true
);
// the (1 - idsOut.length * BASE / vTokenAmount) share of wethSpent is not spend,
// but swapped to vTokens and returned to the caller via _transferDust() below
wethSpent = (wethSpent * idsOut.length * BASE) / vTokenAmount;
uint256 netRoyaltyAmount;
if (deductRoyalty) {
address assetAddress = INFTXVaultV3(vault).assetAddress();
netRoyaltyAmount = _deductRoyalty(assetAddress, idsOut, wethSpent);
}
// transfer vToken dust and remaining WETH balance
_transferDust(vault, true);
emit Buy(
vaultId,
idsOut,
wethSpent + wethFees + netRoyaltyAmount,
to,
netRoyaltyAmount
);
}
struct BuyNFTsWithERC20Params {
IERC20 tokenIn; // Input ERC20 token
uint256 amountIn; // Input ERC20 amount
uint256 vaultId; // The ID of the NFTX vault
uint256[] idsOut; // An array of any token IDs to be redeemed
uint256 vTokenPremiumLimit;
bytes executeToWETHCallData; // Encoded calldata for "ERC20 to WETH swap" for Universal Router's `execute` function
bytes executeToVTokenCallData; // Encoded calldata for "WETH to vToken swap" for Universal Router's `execute` function
address payable to; // The recipient of the token IDs from the tx
bool deductRoyalty;
}
/**
* @notice Buy idsOut with ERC20
*
* @dev ERC20 --{-sell-> [UniversalRouter] -> ETH -> [UniversalRouter] -> vTokens + ETH --redeem-> [vault] --}-> idsOut
*/
function buyNFTsWithERC20(
BuyNFTsWithERC20Params calldata params
) external onlyOwnerIfPaused {
params.tokenIn.safeTransferFrom(
msg.sender,
address(this),
params.amountIn
);
return _buyNFTsWithERC20(params);
}
function buyNFTsWithERC20WithPermit2(
BuyNFTsWithERC20Params calldata params,
bytes calldata encodedPermit2
) external onlyOwnerIfPaused {
if (encodedPermit2.length > 0) {
(
address owner,
IPermitAllowanceTransfer.PermitSingle memory permitSingle,
bytes memory signature
) = abi.decode(
encodedPermit2,
(address, IPermitAllowanceTransfer.PermitSingle, bytes)
);
PERMIT2.permit(owner, permitSingle, signature);
}
PERMIT2.transferFrom(
msg.sender,
address(this),
SafeCast.toUint160(params.amountIn),
address(params.tokenIn)
);
return _buyNFTsWithERC20(params);
}
/**
* @notice Sell idsIn to ETH
*
* @dev idsIn --{--mint-> [vault] -> vTokens --sell-> [UniversalRouter] --}-> ETH
*
* @param vaultId The ID of the NFTX vault
* @param idsIn An array of token IDs to be deposited
* @param executeCallData Encoded calldata for Universal Router's `execute` function
* @param to The recipient of ETH from the tx
*/
function sell1155(
uint256 vaultId,
uint256[] calldata idsIn,
uint256[] calldata amounts,
bytes calldata executeCallData,
address payable to,
bool deductRoyalty
) external onlyOwnerIfPaused {
// Mint (vault fees not deducted here, as zap is on exclusion list)
(address vault, address assetAddress) = _mint1155(
vaultId,
idsIn,
amounts
);
uint256 totalAmount = _validate1155Ids(idsIn, amounts);
// swap vTokens to WETH
(uint256 wethAmount, ) = _swapTokens(
vault,
address(WETH),
executeCallData
);
// distributing vault fees with the weth received
uint256 wethFees = _ethMintFees(INFTXVaultV3(vault), totalAmount);
if (wethAmount < wethFees) revert InsufficientWethForVaultFees();
_distributeVaultFees(vaultId, wethFees, true);
uint256 netRoyaltyAmount;
if (deductRoyalty) {
netRoyaltyAmount = _deductRoyalty1155(
assetAddress,
idsIn,
amounts,
totalAmount,
wethAmount
);
}
wethAmount -= (wethFees + netRoyaltyAmount); // if underflow, then revert desired
// convert WETH to ETH and send remaining ETH to `to`
_wethToETHResidue(to, wethAmount);
// Emit our sale event
emit Sell(
vaultId,
totalAmount,
wethAmount,
to,
netRoyaltyAmount,
wethFees
);
}
/**
* @notice Swap idsIn to idsOut
* Send ETH along as well (via msg.value) to account for swap fees
*/
function swap1155(
uint256 vaultId,
uint256[] calldata idsIn,
uint256[] calldata amounts,
uint256[] calldata idsOut,
uint256 vTokenPremiumLimit,
address payable to
) external payable onlyOwnerIfPaused {
address vault = nftxVaultFactory.vault(vaultId);
if (!INFTXVaultV3(vault).is1155()) revert WrongVaultType();
address assetAddress = INFTXVaultV3(vault).assetAddress();
IERC1155(assetAddress).safeBatchTransferFrom(
msg.sender,
address(this),
idsIn,
amounts,
""
);
IERC1155(assetAddress).setApprovalForAll(vault, true);
// Swap our tokens. Forcing to deduct vault fees
uint256 ethFees = INFTXVaultV3(vault).swap{value: msg.value}(
idsIn,
amounts,
idsOut,
msg.sender,
to,
vTokenPremiumLimit,
true
);
// send back remaining ETH
_sendETHResidue(to);
emit Swap(vaultId, idsIn, amounts, idsOut, ethFees, to);
}
// =============================================================
// ONLY OWNER WRITE
// =============================================================
/**
* @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() {
if (paused && msg.sender != owner()) revert ZapPaused();
_;
}
/**
* @notice Allows our zap to be paused to prevent any processing.
*
* @param paused_ New pause state
*/
function pause(bool paused_) external onlyOwner {
paused = paused_;
emit Paused(paused_);
}
function setUniversalRouter(address universalRouter_) external onlyOwner {
universalRouter = universalRouter_;
emit NewUniversalRouter(universalRouter_);
}
function setDustThreshold(uint256 dustThreshold_) external onlyOwner {
dustThreshold = dustThreshold_;
emit NewDustThreshold(dustThreshold_);
}
function rescueTokens(IERC20 token) external onlyOwner {
if (address(token) != address(0)) {
uint256 balance = token.balanceOf(address(this));
token.safeTransfer(msg.sender, balance);
} else {
uint256 balance = address(this).balance;
TransferLib.transferETH(msg.sender, balance);
}
}
// =============================================================
// INTERNAL / PRIVATE
// =============================================================
function _buyNFTsWithERC20(
BuyNFTsWithERC20Params calldata params
) internal {
// swap tokenIn to WETH
_swapTokens(
address(params.tokenIn),
address(WETH),
params.executeToWETHCallData
);
// swap some WETH to vTokens
uint256 iniWETHBal = WETH.balanceOf(address(this));
address vault = nftxVaultFactory.vault(params.vaultId);
_swapTokens(address(WETH), vault, params.executeToVTokenCallData);
uint256 wethLeft = WETH.balanceOf(address(this));
uint256 wethSpent = iniWETHBal - wethLeft;
// redeem NFTs
TransferLib.unSafeMaxApprove(address(WETH), vault, wethLeft);
uint256 wethFees = INFTXVaultV3(vault).redeem(
params.idsOut,
params.to,
wethLeft,
params.vTokenPremiumLimit,
true
);
uint256 netRoyaltyAmount;
if (params.deductRoyalty) {
address assetAddress = INFTXVaultV3(vault).assetAddress();
netRoyaltyAmount = _deductRoyalty(
assetAddress,
params.idsOut,
wethSpent
);
}
// transfer vToken dust and remaining WETH balance
_transferDust(vault, true);
emit Buy(
params.vaultId,
params.idsOut,
wethSpent + wethFees + netRoyaltyAmount,
params.to,
netRoyaltyAmount
);
}
/**
* @param vaultId The ID of the NFTX vault
* @param ids An array of token IDs to be minted
*/
function _mint721(
uint256 vaultId,
uint256[] calldata ids
) internal returns (address vault, address assetAddress) {
// Transfer tokens from the message sender to the vault
(vault, assetAddress) = _transferSender721ToVault(vaultId, ids);
// Mint our tokens from the vault to this contract
uint256[] memory emptyAmounts;
INFTXVaultV3(vault).mint(ids, emptyAmounts, msg.sender, address(this));
}
function _mint1155(
uint256 vaultId,
uint256[] calldata ids,
uint256[] calldata amounts
) internal returns (address vault, address assetAddress) {
vault = nftxVaultFactory.vault(vaultId);
if (!INFTXVaultV3(vault).is1155()) revert WrongVaultType();
assetAddress = INFTXVaultV3(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
INFTXVaultV3(vault).mint(ids, amounts, msg.sender, address(this));
}
function _validate1155Ids(
uint256[] calldata ids,
uint256[] calldata amounts
) internal pure returns (uint256 totalAmount) {
// Sum the amounts for our emitted events
for (uint i; i < ids.length; ) {
unchecked {
// simultaneously verifies that lengths of `ids` and `amounts` match.
totalAmount += amounts[i];
++i;
}
}
}
function _transferSender721ToVault(
uint256 vaultId,
uint256[] calldata ids
) internal returns (address vault, address assetAddress) {
// Get our vault address information
vault = nftxVaultFactory.vault(vaultId);
assetAddress = INFTXVaultV3(vault).assetAddress();
// Transfer tokens from the message sender to the vault
TransferLib.transferFromERC721(assetAddress, address(vault), ids);
}
/**
* @notice Swaps ERC20->ERC20 tokens using Universal Router.
*
* @param executeCallData Encoded calldata for Universal Router's `execute` function
*/
function _swapTokens(
address sellToken,
address buyToken,
bytes calldata executeCallData
) internal returns (uint256 boughtAmount, uint256 finalBuyTokenBalance) {
// Track our balance of the buyToken to determine how much we've bought.
uint256 iniBuyTokenBalance = IERC20(buyToken).balanceOf(address(this));
_permit2ApproveToken(sellToken, address(universalRouter));
// execute swap
(bool success, ) = address(universalRouter).call(executeCallData);
if (!success) revert SwapFailed();
// Use our current buyToken balance to determine how much we've bought.
finalBuyTokenBalance = IERC20(buyToken).balanceOf(address(this));
boughtAmount = finalBuyTokenBalance - iniBuyTokenBalance;
}
function _permit2ApproveToken(address token, address spender) internal {
(uint160 permitAllowance, , ) = PERMIT2.allowance(
address(this),
token,
spender
);
if (
permitAllowance >=
uint160(0xf000000000000000000000000000000000000000) // sufficiently large value
) return;
IERC20(token).safeApprove(address(PERMIT2), type(uint256).max);
PERMIT2.approve(token, spender, type(uint160).max, type(uint48).max);
}
function _ethMintFees(
INFTXVaultV3 vToken,
uint256 nftCount
) internal view returns (uint256) {
(uint256 mintFee, , ) = vToken.vaultFees();
return vToken.vTokenToETH(mintFee * nftCount);
}
function _distributeVaultFees(
uint256 vaultId,
uint256 ethAmount,
bool isWeth
) internal {
if (ethAmount > 0) {
INFTXFeeDistributorV3 feeDistributor = INFTXFeeDistributorV3(
nftxVaultFactory.feeDistributor()
);
if (!isWeth) {
WETH.deposit{value: ethAmount}();
}
WETH.transfer(address(feeDistributor), ethAmount);
feeDistributor.distribute(vaultId);
}
}
function _deductRoyalty(
address nft,
uint256[] calldata idsIn,
uint256 netWethAmount
) internal returns (uint256 netRoyaltyAmount) {
bool success = IERC2981(nft).supportsInterface(_INTERFACE_ID_ERC2981);
if (success) {
uint256 salePrice = netWethAmount / idsIn.length;
for (uint256 i; i < idsIn.length; ) {
(address receiver, uint256 royaltyAmount) = IERC2981(nft)
.royaltyInfo(idsIn[i], salePrice);
netRoyaltyAmount += royaltyAmount;
if (royaltyAmount > 0) {
WETH.transfer(receiver, royaltyAmount);
}
unchecked {
++i;
}
}
}
}
function _deductRoyalty1155(
address nft,
uint256[] calldata idsIn,
uint256[] calldata amounts,
uint256 totalAmount,
uint256 netWethAmount
) internal returns (uint256 netRoyaltyAmount) {
bool success = IERC2981(nft).supportsInterface(_INTERFACE_ID_ERC2981);
if (success) {
uint256 salePrice = netWethAmount / totalAmount;
for (uint256 i; i < idsIn.length; ) {
(address receiver, uint256 royaltyAmount) = IERC2981(nft)
.royaltyInfo(idsIn[i], salePrice);
uint256 royaltyToPay = royaltyAmount * amounts[i];
netRoyaltyAmount += royaltyToPay;
if (royaltyToPay > 0) {
WETH.transfer(receiver, royaltyToPay);
}
unchecked {
++i;
}
}
}
}
function _allWethToETHResidue(
address to
) internal returns (uint256 wethAmount) {
wethAmount = WETH.balanceOf(address(this));
_wethToETHResidue(to, wethAmount);
}
function _wethToETHResidue(address to, uint256 wethAmount) internal {
if (wethAmount > 0) {
// Unwrap our WETH into ETH and transfer it to the recipient
WETH.withdraw(wethAmount);
}
_sendETHResidue(to);
}
function _sendETHResidue(address to) internal {
if (to == address(0)) revert ZeroAddress();
// sending entire ETH balance (hence accounting for the unused msg.value)
(bool success, ) = payable(to).call{value: address(this).balance}("");
if (!success) revert UnableToSendETH();
}
/**
* @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 hasWETHDust Checks and transfers WETH dust if boolean is true
*/
function _transferDust(address vault, bool hasWETHDust) internal {
uint256 wethRemaining;
if (hasWETHDust) {
wethRemaining = _allWethToETHResidue(msg.sender);
}
uint256 dustBalance = IERC20(vault).balanceOf(address(this));
address dustRecipient;
if (dustBalance > 0) {
if (dustBalance > dustThreshold) {
dustRecipient = msg.sender;
} else {
dustRecipient = inventoryStaking;
}
IERC20(vault).transfer(dustRecipient, dustBalance);
}
emit DustReturned(wethRemaining, dustBalance, dustRecipient);
}
receive() external payable {}
}