I have an NFT stored in my smart contract that implements IERC721Receiver. Now I want to create a function that lets me send this NFT to any wallet address.
Here’s my current withdrawal function:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/IERC721Receiver.sol";
import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
contract NFTVault is IERC721Receiver {
// other contract code here
function withdrawNFTToWallet(
address tokenContract,
uint256 nftId,
address recipient
) external onlyOwner {
IERC721(tokenContract).safeTransferFrom(
address(this),
recipient,
nftId,
""
);
}
}
The idea is simple - as the contract owner, I want to transfer an NFT that my contract holds to someone else’s address. I pass in the NFT contract address, token ID, and destination address.
But I’m getting this compilation error:
Member "safeTransferFrom" not found or not visible after argument-dependent lookup in contract IERC721. solidity(9582)
What am I doing wrong here? How can I fix this transfer issue?
hey there! interesting issue you’re running into. i’ve seen this exact problem before and it’s actually a pretty common gotcha with openzeppelin’s interfaces.
the thing is, the basic IERC721 interface doesn’t include the safeTransferFrom function with the data parameter - that’s part of the extended functionality. you’re probably thinking of the full ERC721 implementation, but the interface itself is more minimal.
what version of openzeppelin are you using btw? that might matter here. also, are you sure the NFT contract you’re trying to withdraw from actually supports the full safeTransferFrom signature?
you could try a couple different approaches - either use the basic transferFrom instead, or import the full ERC721 contract interface if you need the safe transfer functionality. but im curious, what specific NFT contract are you trying to move tokens from? some contracts have their own quirks with transfers.
also just wondering - have you tested that your contract is actually receiving the NFTs properly in the first place? sometimes the withdrawal issue is actually masking a deposit problem.
yea this is basicly because you’re mixing up the interface methods. IERC721 only has the 3-parameter safeTransferFrom but you’re calling the 4-parameter version. just drop that empty string and it shoudl work fine: safeTransferFrom(address(this), recipient, nftId) without the data param. works for me everytime
The compilation error occurs because the IERC721 interface only defines the basic safeTransferFrom(address from, address to, uint256 tokenId) function, not the overloaded version with the data parameter you are trying to use. Your function call includes an empty string as the fourth parameter, which the standard interface does not recognize. You can fix this by removing the empty string and using the basic safeTransferFrom instead: IERC721(tokenContract).safeTransferFrom(address(this), recipient, nftId); Alternatively, if the data parameter is crucial for your use case, consider casting to the full ERC721 implementation. In most cases, however, the basic call suffices for simple transfers.