文件 1 的 4:IERC1155.sol
pragma solidity ^0.8.0;
import "../../utils/introspection/IERC165.sol";
interface IERC1155 is IERC165 {
event TransferSingle(address indexed operator, address indexed from, address indexed to, uint256 id, uint256 value);
event TransferBatch(
address indexed operator,
address indexed from,
address indexed to,
uint256[] ids,
uint256[] values
);
event ApprovalForAll(address indexed account, address indexed operator, bool approved);
event URI(string value, uint256 indexed id);
function balanceOf(address account, uint256 id) external view returns (uint256);
function balanceOfBatch(address[] calldata accounts, uint256[] calldata ids)
external
view
returns (uint256[] memory);
function setApprovalForAll(address operator, bool approved) external;
function isApprovedForAll(address account, address operator) external view returns (bool);
function safeTransferFrom(
address from,
address to,
uint256 id,
uint256 amount,
bytes calldata data
) external;
function safeBatchTransferFrom(
address from,
address to,
uint256[] calldata ids,
uint256[] calldata amounts,
bytes calldata data
) external;
}
文件 2 的 4:IERC165.sol
pragma solidity ^0.8.0;
interface IERC165 {
function supportsInterface(bytes4 interfaceId) external view returns (bool);
}
文件 3 的 4:IMarketplaceTokenReceiver.sol
pragma solidity ^0.8.0;
interface IMarketplaceTokenReceiver {
function decrementERC1155(address owner, address tokenAddress, uint256 tokenId, uint256 value) external;
function transferERC1155(address tokenAddress, uint256 tokenId, uint256 value, address to) external;
function withdrawERC1155(address tokenAddress, uint256 tokenId, uint256 value) external;
function onERC1155Received(address, address from, uint256 id, uint256 value, bytes calldata) external returns(bytes4);
}
文件 4 的 4:MarketplaceTokenReceiver.sol
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC1155/IERC1155.sol";
import "./IMarketplaceTokenReceiver.sol";
contract MarketplaceTokenReceiver is IMarketplaceTokenReceiver {
address private _marketplace;
mapping (address => mapping(uint256 => mapping(address => uint256))) private _erc1155balances;
constructor(address marketplace) {
_marketplace = marketplace;
}
function decrementERC1155(address owner, address tokenAddress, uint256 tokenId, uint256 value) external virtual override {
require(msg.sender == _marketplace, "Invalid caller");
require(_erc1155balances[tokenAddress][tokenId][owner] >= value, "Invalid token amount");
_erc1155balances[tokenAddress][tokenId][owner] -= value;
}
function transferERC1155(address tokenAddress, uint256 tokenId, uint256 value, address to) external virtual override {
require(msg.sender == _marketplace, "Invalid caller");
IERC1155(tokenAddress).safeTransferFrom(address(this), to, tokenId, value, "");
}
function withdrawERC1155(address tokenAddress, uint256 tokenId, uint256 value) external virtual override {
require(_erc1155balances[tokenAddress][tokenId][msg.sender] >= value, "Invalid token amount");
_erc1155balances[tokenAddress][tokenId][msg.sender] -= value;
IERC1155(tokenAddress).safeTransferFrom(address(this), msg.sender, tokenId, value, "");
}
function onERC1155Received(address, address from, uint256 id, uint256 value, bytes calldata) external virtual override returns(bytes4) {
_erc1155balances[msg.sender][id][from] += value;
return this.onERC1155Received.selector;
}
}
{
"compilationTarget": {
"contracts/MarketplaceTokenReceiver.sol": "MarketplaceTokenReceiver"
},
"evmVersion": "istanbul",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 1500
},
"remappings": []
}
[{"inputs":[{"internalType":"address","name":"marketplace","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"tokenAddress","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"},{"internalType":"uint256","name":"value","type":"uint256"}],"name":"decrementERC1155","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"address","name":"from","type":"address"},{"internalType":"uint256","name":"id","type":"uint256"},{"internalType":"uint256","name":"value","type":"uint256"},{"internalType":"bytes","name":"","type":"bytes"}],"name":"onERC1155Received","outputs":[{"internalType":"bytes4","name":"","type":"bytes4"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"tokenAddress","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"},{"internalType":"uint256","name":"value","type":"uint256"},{"internalType":"address","name":"to","type":"address"}],"name":"transferERC1155","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"tokenAddress","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"},{"internalType":"uint256","name":"value","type":"uint256"}],"name":"withdrawERC1155","outputs":[],"stateMutability":"nonpayable","type":"function"}]