【问题标题】:how to call a ERC20 contract fucntion inside ERC721 contract如何在 ERC721 合约中调用 ERC20 合约函数
【发布时间】:2022-01-07 07:50:34
【问题描述】:

我想要完成的是从 ERC721 合约调用 ERC20 合约中定义的函数,如下所示,特别是 ERC20 合约中的 transferFrom 函数在 ERC721 中的同一函数内。

这不会编译。 它最终的作用是,每次发生 erc721 NFT 转让时,版税都会以 erc20 代币的形式转让给创作者。

import "./myToken.sol" as erc20contract //This is my ERC20 contract

contract ERC721 is ERC721Interface, myToken {
    function transferFrom(address _from, address _to, uint256 _tokenId) public payable {

        address addr_owner = ownerOf(_tokenId);

        require(
            addr_owner == _from,
            "_from is NOT the owner of the token"
        );

        require(
            _to != address(0),
            "Transfer _to address 0x0"
        );

        address addr_allowed = allowance[_tokenId];
        bool isOp = operators[addr_owner][msg.sender];

        require(
            addr_owner == msg.sender || addr_allowed == msg.sender || isOp,
            "msg.sender does not have transferable token"
        );


        //transfer : change the owner of the token
        tokenOwners[_tokenId] = _to;
        balances[_from] = balances[_from].sub(1);
        balances[_to] = balances[_to].add(1);

        //reset approved address
        if (allowance[_tokenId] != address(0)) {
            delete allowance[_tokenId];
        }

        erc20contract.transferFrom(_to, nftInfo[_from].creator, 10); // This is what I'd like to achieve
        {
        emit Transfer(_from, _to, _tokenId);
    }
}

【问题讨论】:

    标签: blockchain ethereum solidity erc20 erc721


    【解决方案1】:

    编辑:我刚刚注意到您的合同似乎同时实现了ERC721ERC20 标准。您将need to decide on one,但使用组合(如下所示)可能足以提供您所追求的所有必要功能。


    除非我遗漏了什么,否则您似乎只是缺少 ERC20 代币的实际实例。例如:

    contract YourToken is ERC721 {
      ERC20 private immutable token;
    
      constructor(ERC20 t) {
        token = t;
      }
    
      function transferFrom(address _from, address _to, uint256 _tokenId) public payable {
        // Your code here.
        token.transferFrom(_from, _to, 10);
      }
    }
    

    【讨论】:

    • @Tadeg,谢谢。但我需要分别实现 ERC20 和 721 标准,因为我的 dapp 允许用户使用 Eth 购买 erc20 代币并铸造/交易 721 NFT 代币。
    猜你喜欢
    • 1970-01-01
    • 2021-12-13
    • 2021-11-14
    • 2021-11-24
    • 1970-01-01
    • 2021-11-08
    • 2022-12-12
    • 2022-07-31
    • 2021-10-08
    相关资源
    最近更新 更多