Using openZepplin, how do I use the safeTransferFrom(from, to, tokenId, "")?

Kindly bare with me as I am new to Solidity and the Web.30 space.

The intention of my DApp is to be able to delay/schedule the transfers of your NFT’s from ones own account to someone else’s.

The idea is that a user should be able to find most of his NFT details via the opensea platform and their MetaMask wallet…

Find below my simple solidity code in the Remix IDE:

// SPDX-License-Identifier: MIT
pragma solidity ^0.6.2;

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v3.4.0/contracts/token/ERC721/ERC721.sol";

contract CryptoGift is ERC721 {
    address public owner;
    uint256 private birtday = block.timestamp +30; 

     constructor()  ERC721("Crypto Gift", "CGT") public {
        owner = msg.sender;       
    }

    function GiftAFriend (address from, address to, uint256 tokenId) public virtual {

       require( block.timestamp >= birtday, "Its NOT your birthday yet!" );
       safeTransferFrom(from, to, tokenId, ""); 
    }

}

The settings I used in my Remix IDE are:

ENVIRONMENT: Web3 Provider

I was able to successfully Deploy to the Rinkeby Test Network this means I am also able to see all the interfaces in Remix as illustrated below

When running my GiftAFriend function, I used the details found in my NFT contract/address from my https://testnets.opensea.io/assets/0x1a2795bbdbc6b6fc1d777b47b9c1516a642ca7f7/1 and for the to, I used one of my fictitious MetaMask addresses.

For the tokenId, I used the tokenId at the end of my opensea address https://testnets.opensea.io/assets/0x1a2795bbdbc6b6fc1d777b47b9c1516a642ca7f7/1

When I click the transact for my GiftAFriend function I am met with the following error message in Remix:

Kindly tell me where/ why am going wrong?

Not so sure, did you try and see what error did you get from etherscan? could be from that require line but I can’t be so sure since I don’t know when did you call GiftAFriend

Hi YosephKS!

I really appreciate the help.

Nothing gets sent to Etherscan at all, so am not able to show you any errors from etherscan, however, what I should have thought of adding to this post, are the error messages I get from the Remix console.

To answer your question, when I call the GiftAFriend function… and I ignore the
Gas estimation failed message as seen in the image below, by clicking on the Send Transaction anyway.

Below are the error messages I get in the Remix console:

transact to CryptoGift.GiftAFriend pending …
transact to CryptoGift.GiftAFriend errored: Returned error: VM Exception while processing transaction: revert ERC721: operator query for nonexistent token

But as you can see from the screenshot taken from my testnets.opensea.io account: https://testnets.opensea.io/assets/0x1a2795bbdbc6b6fc1d777b47b9c1516a642ca7f7/1

the tokenId for the NFT I am trying to transfer is actually number: 1

1

Where am I going wrong?

hmmm I see, did you by any chance approve the transfer yet for this contract to take the NFT from your wallet? coz I see safeTransferFrom

No I haven’t gotten them approved, how do I get them approved?

you need to call the approve function in the NFT here https://docs.openzeppelin.com/contracts/2.x/api/token/erc721#ERC721-approve-address-uint256-

it needs the to and tokenId to approve

Thanks for the prompt response.

I updated my code to this:

function GiftAFriend (address _from, address _to, uint256 _tokenId) public virtual {
    approve( _to, _tokenId);
    safeTransferFrom(_from, _to, _tokenId, ""); 
}

But getting this error message in the Remix console:

transact to CryptoGift.GiftAFriend pending …
transact to CryptoGift.GiftAFriend errored: Returned error: VM Exception while processing transaction: revert ERC721: owner query for nonexistent token

Could the issue be that I am filling in the fields with the wrong values?
1

Kindly observe that in the from field, I use the NFT address found in the testnets.opensea.io which here is: 0x1a2795bbdbc6b6fc1d777b47b9c1516a642ca7f7

In the to field, I use one of the many addresses provided by the Remix
1

and for the tokenId off course I use the tokenId 1 found in testnets.opensea.io account
1

Am I getting the values to fill in the GiftAFriend correctly?

Hey sorry for the late reply, but the approve shouldn’t be called in the smart contract, instead you should call it directly from the wallet :raised_hands:

1 Like