科技美学

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  90 随笔 :: 0 文章 :: 1 评论 :: 69570 阅读

1.0 OpenZeppelin ERC1155Burnable

OpenZeppelin有Extensions ERC1155Burnable。如果另一個智能合約需調用ERC1155Burnable時,必需要有IERC1155Burnable接口。
但OpenZeppelin沒有Burnable接口,所以要自己創建 IERC1155Burnable接口。

 

2.1 ERC1155 ContractA

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
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()

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
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。

posted on   chankuang  阅读(33)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示