首页 > 其他分享 >ERC1155 OpenZeppelin burnable interface from another contract 不能外部调用burnable合约接口

ERC1155 OpenZeppelin burnable interface from another contract 不能外部调用burnable合约接口

时间:2022-12-06 17:46:51浏览次数:91  
标签:account uint256 burn burnable contract IERC1155Burnable ERC1155 address

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。

标签:account,uint256,burn,burnable,contract,IERC1155Burnable,ERC1155,address
From: https://www.cnblogs.com/chenkuang/p/16954407.html

相关文章

  • 根据MediatR的Contract Messages自动生成Minimal WebApi接口
    大家好,我是失业在家,正在找工作的博主Jerry。今天给大家介绍一个能大大减少ASP.NetMinimalWebApi编码量的方法。我们一般会把微服务的VO和DTO封装成消息类,并作为WebApi的......
  • 《Design by Contract for Embedded Software》 翻译
    DesignbyContract isthesinglemosteffectiveprogrammingtechniquefordeliveringhigh-qualitycode.HereyoucanlearnwhattheDesignbyContractprogram......
  • contract
    late14c.,"todrawintoasmallercompass[范围],becomesmaller,shrink"(intransitive);early15c."makeanagreement,enterintoacontract,agreeorestab......