soliditysmartcontractserc721

How to call a function from smart contract A on a token ID received from smart contract B?


I have two ERC721 smart contracts A and B. I have successfully minted a token ID on contract A and transferred it to contract B address (to transfer it to a contract address and not a wallet address I used IERC721Receiver). From here, is there a way for contract's B functions, which take a token ID as argument, to be called on the token ID received from A that now belongs to B?

For example, if Contract A was:

contract ContractA is ERC721 {

   ...
   function mint(address _to, uint256 _mintAmount) public payable {
        for (uint256 i = 1; i <= _mintAmount; i++) {
            _safeMint(_to, supply + i);
        }
    }

}

and contract B was:

contract ContractB is ERC721 {

   ...
   function exampleFunction(uint256 tokenId) public payable {
        
        // Do something with tokenId

    }

}

How can I call exampleFunction(6) on ContractB if token ID #6 was transferred from ContractA to ContractB (and not minted on ContractB)?

It just seems to me like there is no way to use methods from ERC721 contracts on token IDs that are not minted from the same contract where the methods are implemented.

Anything helps, so thank you in advance!

I am able to see that ContractB owns the token transferred to it by ContractA, but only on ContractA's ownerOf() method. I cannot do anything with that token on ContractB's methods, even though it now belongs to it.


Solution

  • You can call your exampleFunction() from your onERC721Received() implementation.

    However you will not be able to do anything with the token as it will not have been transferred to you yet. The onERC721Received is purely to check that the contract supports receiving ERC721 tokens.