0

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.

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.

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);
    }
}
Nayana
  • 1,513
  • 3
  • 24
  • 39

1 Answers1

0

Edit: I've just noticed your contract seemingly implements both the ERC721 and ERC20 standards. 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. 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);
  }
}
Tadej
  • 2,901
  • 1
  • 17
  • 23
  • @Tadeg, Thanks. But I need to separately implement both ERC20 and 721 standards as my dapp allows users to buy erc20 tokens with Eth and mint/trade 721 NFT tokens. – Nayana Jan 10 '22 at 01:22