-
Notifications
You must be signed in to change notification settings - Fork 0
/
EeseeRaribleRouter.sol
62 lines (51 loc) · 2.29 KB
/
EeseeRaribleRouter.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
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.21;
import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import "@openzeppelin/contracts/token/ERC721/utils/ERC721Holder.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "../../interfaces/IEeseeMarketplaceRouter.sol";
import "../../interfaces/IExchangeV2Core.sol";
import "../../libraries/LibDirectTransfer.sol";
contract EeseeRaribleRouter is IEeseeMarketplaceRouter, ERC721Holder {
using SafeERC20 for IERC20;
///@dev Main rarible marketplace contract's address.
IExchangeV2Core public immutable exchangeV2Core;
error ERC20NotSupported();
constructor(IExchangeV2Core _exchangeV2Core) {
exchangeV2Core = _exchangeV2Core;
}
receive() external payable {
//Reject deposits from EOA
if (msg.sender == tx.origin) revert EthDepositRejected();
}
/**
* @dev Buys NFT for {nftPrice} from Rarible marketplace and sends it to {recipient}.
* @param data - Encoded LibDirectTransfer.Purchase struct needed for rarible contracts.
* @param recipient - Address to send nft to.
*
* @return asset - Asset received.
* @return spent - Tokens spent.
*/
function purchaseAsset(bytes calldata data, address recipient) external payable returns (Asset memory asset, uint256 spent) {
LibDirectTransfer.Purchase memory purchase = abi.decode(data, (LibDirectTransfer.Purchase));
spent = (101 * purchase.sellOrderPaymentAmount) / 100;
if (address(purchase.paymentToken) != address(0)) revert ERC20NotSupported();
if (msg.value < spent) revert InsufficientFunds(); // Take fee into account
exchangeV2Core.directPurchase{value: spent}(purchase);
if(spent != msg.value){
unchecked {
(bool success, ) = msg.sender.call{value: msg.value - spent}("");
if(!success) revert TransferNotSuccessful();
}
}
(address token, uint256 tokenID) = abi.decode(purchase.nftData, (address, uint256));
asset = Asset({
token: token,
tokenID: tokenID,
amount: 1,
assetType: AssetType.ERC721,
data: ""
});
IERC721(token).safeTransferFrom(address(this), recipient, tokenID);
}
}