Transfer NFT - cannot determine tokenID

50 Views Asked by At

How do I transfer an **NFT **(Zed.Run) from my smart contract? I can't seem to determine the tokenID or contract address from the NFT and it seems necessary to use the transferFrom or safeTransferFrom methods.

I'm able to receive and send funds via my smart contract (polygon, Avax & Klaytn), however, transferring an NFT seems to require a tokenID (which I cannot determine from any commands).

1

There are 1 best solutions below

1
Alon Ben Yaakov On

To make sure we are on the same page:

  1. By saying NFT I'm assuming you mean the ERC721 protocol.
  2. You are the owner of a smart contract that owns an NFT.
  3. You want to transfer that NFT to another wallet/smart contract.

To transfer an NFT from an existing smart contract these are the steps:

  1. Identify the NFT id you want to transfer. You can use the block explorer for that. The link I've provided is your smart contract, so you can check which NFTs that smart contract owns.
  2. Add a new function on your smart contract to transfer NFT(ERC721), Something like this
    function transferERC721(
        address tokenAddress,
        address target,
        uint256 tokenId
    ) external{
        IERC721(tokenAddress).safeTransferFrom(address(this), target, tokenId);
    }

And then initiate that transaction with data of the NFT(token address & id) you want to transfer.

A few important notes:

  • Your smart contract can't detect the incoming NFTs because there is no such built-in functionality.
  • You should add access control, to make sure only you can transfer that NFT. Examples by OpenZepplin
  • In case your smart contract doesn't have this kind of function already implemented, then the NFT is stuck. Because smart contracts are immutable and you can't update their code after deploying. Unless you defined the smart contract to be upgradeable (source) before deploying it.