Search code examples
ethereumsolidity

Solidity - transferring NFT from smart contract to address


I've sent an NFT to a IERC721Receiver implemented contract.

My withdraw function on the contract is as follows:

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;
import "@openzeppelin/contracts/token/ERC721/IERC721Receiver.sol";
import "@openzeppelin/contracts/token/ERC721/IERC721.sol";

contract Contract is IERC721Receiver {
  ..random..stuff..

  function triggerNFTWithdrawalToAddress(
        address nftContractAddress,
        uint256 tokenId,
        string calldata toAddress
    ) public onlyOwner {
        IERC721(nftContractAddress).safeTransferFrom(
            address(this),
            toAddress,
            tokenId,
            "0x"
        );
  }
}

Basically, I as the contract owner, would like to transfer the NFT held by the contract to some person's address.

The method above takes in the nftContractAddress of the NFT I'd like to send as well as the tokenId and the toAddress.

However, my compiler is complaining that

Member "safeTransferFrom" not found or not visible after argument-dependent lookup in contract IERC721. solidity(9582)

Not sure how to proceed from here.


Solution

  • The error message is a bit unfortunate, as it only shows the function name but not the function arguments.

    Your code tries to pass a string type toAddress, effectively trying to call a function safeTransferFrom(address,string,uint256,bytes) (mind the string as the second argument type). But this function doesn't exist.

    The correct function is safeTransferFrom(address,address,uint256,bytes) (second argument type is address).

    So all you need to do is to pass an address type instead of the string.

    function triggerNFTWithdrawalToAddress(
        address nftContractAddress,
        uint256 tokenId,
        address toAddress // `address` instead of `string`
    ) public {
        IERC721(nftContractAddress).safeTransferFrom(
            address(this),
            toAddress,
            tokenId,
            "0x"
        );
    }