1.0 OpenZeppelin ERC1155Burnable
OpenZeppelin有Extensions ERC1155Burnable。如果另一個智能合約需調用ERC1155Burnable時,必需要有IERC1155Burnable接口。
但OpenZeppelin沒有Burnable接口,所以要自己創建 IERC1155Burnable接口。
2.1 ERC1155 ContractA
import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol"; import "@openzeppelin/contracts/access/Ownable.sol"; import "@openzeppelin/contracts/token/ERC1155/extensions/ERC1155Burnable.sol"; contract ContractA is ERC1155, Ownable, ERC1155Burnable { constructor() ERC1155("") {} function setURI(string memory newuri) public onlyOwner { _setURI(newuri); } function mint(address account, uint256 id, uint256 amount, bytes memory data) public onlyOwner { _mint(account, id, amount, data); } function mintBatch(address to, uint256[] memory ids, uint256[] memory amounts, bytes memory data) public onlyOwner { _mintBatch(to, ids, amounts, data); } /* * Overrides burn 重載燒毀 */ function burn(address account, uint256 id, uint256 amount) public virtual { require(account == _msgSender() || isApprovedForAll(account, _msgSender()), "Ikasumi: caller is not token owner or approved"); _burn(account, id, amount); } }
Overrides burn 重載燒毀
2.2 ERC1155 ContractB call ContractA burn funcs()
import "@openzeppelin/contracts/token/ERC1155/IERC1155.sol"; import "@openzeppelin/contracts/access/Ownable.sol"; /* * 創建 IERC1155Burnable接口 */ interface IERC1155ContractA is IERC1155 { function burn(address account, uint256 id, uint256 value) external; } contract ContractB is Ownable { /* * 全域參數 IERC1155Burnable接口 */ IERC1155ContractA public erc1155TokenA; /* * set contract address */ constructor( IERC1155ContractA _erc1155TokenA { erc1155TokenA = _erc1155TokenA; } /* * call ContractA burn funcs() via IERC1155Burnable接口 */ function burn(uint256 tokenId) external nonReentrant { erc1155TokenA.burn(msg.sender, tokenId, qty); } }
ContractB constructor sets address of IERC1155ContractA, then call contractA burn funcs().
3.0 why we do it?
如果ERC1155是獨立NFT合約ContractA,而操作邏輯是ContractB,就需要使用IERC1155Burnable。