Reputation: 1
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);
}
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
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
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:
_tokenId
_tokenId
does not belong to the token sender_tokenId
does not existMind 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