Trouble sending NFT from smart contract: TokenID issue

I’m stuck trying to send an NFT from my smart contract. It’s a Zed.Run horse, but I can’t figure out how to get the tokenID or contract address. These seem to be needed for the transferFrom or safeTransferFrom functions.

My contract works fine for sending and getting regular funds on Polygon, Avalanche, and Klaytn. But NFTs are giving me a headache. I’ve looked everywhere and can’t find a way to get the tokenID from the NFT itself.

Has anyone dealt with this before? What am I missing? Is there a special way to handle NFTs in smart contracts that I don’t know about?

Here’s a dummy code snippet to show what I’m trying to do:

function transferNFT(address to) public {
    // How do I get these?
    address nftContract = ???;
    uint256 tokenId = ???;

    IERC721(nftContract).transferFrom(address(this), to, tokenId);
}

Any help would be awesome. I’m really scratching my head over this one!

oh hey ava61! nfts can be tricky, right? :sweat_smile: i’m curious, have you tried looking at the zed.run api or documentation? sometimes they have specific ways to interact with their nfts.

btw, how are you getting the nft into your contract in the first place? that might give us a clue about how to handle it.

oh, and another thought - are you sure the contract actually owns the nft? cuz if it doesn’t, transferFrom won’t work anyway.

keep us posted on what you find out! nft stuff is always interesting to learn about :nerd_face:

hey there! i’ve dealt with NFTs before. for Zed.Run horses, you need to know the contract address and tokenID beforehand. they’re not stored in the NFT itself. you gotta pass these as parameters to your function. something like:

function transferNFT(address to, address nftContract, uint256 tokenId) public {
IERC721(nftContract).transferFrom(address(this), to, tokenId);
}

hope this helps! lmk if u need more info

To address your NFT transfer issue, you need to know the contract address and tokenID beforehand. These aren’t inherent properties of the NFT that can be extracted automatically. For Zed.Run horses, you’ll need to obtain this information externally and pass it to your function.

Consider modifying your function to accept these parameters:

function transferNFT(address to, address nftContract, uint256 tokenId) public {
IERC721(nftContract).transferFrom(address(this), to, tokenId);
}

Ensure your contract has the necessary permissions to transfer the NFT. You might need to approve the transfer first if your contract isn’t the owner. Also, verify that your contract actually holds the NFT before attempting the transfer.

If you’re still facing issues, consider consulting Zed.Run’s specific documentation or API for any unique requirements they might have for interacting with their NFTs.