简体   繁体   English

如何在 ERC721 合约中调用 ERC20 合约函数

[英]how to call a ERC20 contract fucntion inside ERC721 contract

what I am trying to accomplish is calling a function defined in ERC20 contract from ERC721 contract as shown below, specifically transferFrom function from ERC20 contract inside the same function in ERC721.我想要完成的是从 ERC721 合约调用 ERC20 合约中定义的 function,如下所示,特别是transferFrom ERC20 合约中的 function 在同一个 ZC1C425268E48385D1AB507 中的 ERC20 合约中调用 function。

This does not compile.这不编译。 what it ultimately does is that everytime a erc721 NFT transfer occurs, royalty is tranferred to the creator in the form of erc20 tokens.它最终所做的是,每次发生 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);
    }
}

Edit: I've just noticed your contract seemingly implements both the ERC721 and ERC20 standards.编辑:我刚刚注意到您的合同似乎同时实现了ERC721ERC20标准。 You'll need to decide on one , but using composition (as I've shown below) might be sufficient to provide all the necessary functionality you're after.需要决定一个,但使用组合(如下所示)可能足以提供您所追求的所有必要功能。


Unless I'm missing something, you only seem to lack an actual instance of your ERC20 token.除非我遗漏了什么,否则您似乎只缺少 ERC20 代币的实际实例。 For example:例如:

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);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以在 ERC721 合约中调用 ERC20 function? - Is is possible to call a ERC20 function inside a ERC721 contract? ERC20 代币智能合约如何运作? - How does a ERC20 Token Smart Contract work? 在同一个合约中处理多个 ERC721 代币 - Handling multiple ERC721 Tokens in the same Contract 来自 web3 的 MintTo 合同给出错误 - ERC721:转移到非 ERC721Receiver 实施者 - MintTo contract from web3 gives error - ERC721: transfer to non ERC721Receiver implementer 关于实现 ERC20 代币交换合约的问题 - Questions about implementing ERC20 tokenswap contract ERC20代币合约的批准function问题 - Problem with the approve function of ERC20 token contract 使用智能合约存入和提取 erc20 代币 - deposite and withdraw erc20 token using smart contract 如何在我的 Solidity 合约中使用我的 erc20 自定义代币作为所需付款? - How can I use my erc20 custom token as required payment in my solidity contract? 与智能合约erc721中的输入反应中的大数字相关的错误 - Error while related to big number in react for input in smart contract erc721 是否可以在不使用 openzepplin 库的情况下创建 erc721 合约并将其呈现在 opensea 上? - Is it possible to create an erc721 contract without using openzepplin library and render it on opensea?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM