LSH
LSH

Reputation: 1

Gas Estimation Error when Transfer ERC721 token in Remix

I am currently implementing an ERC721 token staking function contract, but when I add the transfer code, a Gas Estimation Error occurs.

MarineBluesContract(_nftContract).transferFrom(msg.sender, address(this), _tokenId);

function stake(uint256 _tokenId, address _nftContract)
        external
        nonReentrant
    {
        require(ntfContractList[_nftContract], "Not allowed NFT contract");
        require(msg.sender != address(0), "Invalid staker address");
        require(_tokenId != 0, "Invalid token id");
        require(MarineBluesContract(_nftContract).ownerOf(_tokenId) == msg.sender, "Not token owner");

        // Staking start time
        uint48 timestamp = uint48(block.timestamp);

        // Staking to contract
        MarineBluesContract(_nftContract).transferFrom(msg.sender, address(this), _tokenId);

        // Save staking information
        stakedTokens.push(
            StakedToken(msg.sender, _tokenId, _nftContract, timestamp, false)
        );

        // Increase in staking count
        totalStaked++;

        emit Stake(msg.sender, _tokenId, _nftContract, timestamp);
    }

enter image description here

  1. Make sure I have enough Ethereum
  2. Make sure staking contract has enough Ethereum
  3. Authorize the staking contract using setApprovalForAll so that it can transfer my NFTs

Gas Estimation error came out even after taking the above steps. I'm not sure why, but if you can guess or if I'm doing something wrong, please tell me. thanks in advance!

Upvotes: 0

Views: 307

Answers (2)

Satnam Singh
Satnam Singh

Reputation: 1

I was faced with the same error: "Gas estimation error with the following message (see below). The transaction execution will likely fail. Do you want to force sending?" while I am trying to connect the ganache with the remix. So the problem is that I am not using the same compiler version. I set a compiler version like 0.8.25, and I use it in the contract code 0.8.19. The solution is to set the same version, like the compiler version 0.8.19, and use it in contract code: 0.8.19. In last, I refreshed both remix and ganache. Restart the ganache and remix. Sometimes restarting both tools can resolve synchronization issues

Upvotes: 0

Petr Hejda
Petr Hejda

Reputation: 43521

Based on the fact that the require() condition validating _nftContract.ownerOf() doesn't fail, I'm assuming that your contract is correctly deployed on the same network as _nftContract.

transferFrom() can fail for several reasons. Most common causes might be:

  • The token sender (specified in the first argument) didn't approve the transaction sender (in this case your contract) to operate this specific _tokenId
  • The _tokenId does not belong to the token sender
  • The _tokenId does not exist

Mind that you're executing the transferFrom() function from your contract - so the msg.sender (user executing stake() function) needs to give approval directly to yourContract.

Upvotes: 0

Related Questions