'Ethereum - insufficient funds for intrinsic transaction cost
I'm trying to create my own NFT contract factory, but it fails in mainnet. I have ETH on my wallet. In test it works fine. Here's my contract:
//Contract based on [https://docs.openzeppelin.com/contracts/3.x/erc721](https://docs.openzeppelin.com/contracts/3.x/erc721)
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
contract NFT is ERC721URIStorage, Ownable {
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
constructor() ERC721("NFT", "NFT") {}
function mintNFT(address recipient, string memory tokenURI)
public onlyOwner
returns (uint256)
{
_tokenIds.increment();
uint256 newItemId = _tokenIds.current();
_mint(recipient, newItemId);
_setTokenURI(newItemId, tokenURI);
return newItemId;
}
}
Here's my contract deployment script
const MyNFT = await ethers.getContractFactory("NFT");
const gasPrice = await MyNFT.signer.getGasPrice();
const estimatedGas2 = await ethers.provider.estimateGas(MyNFT.getDeployTransaction().data)
const estGas = await MyNFT.signer.estimateGas({
maxFeePerGas: gasPrice + 1,
gasLimit: estimatedGas2,
maxPriorityFeePerGas: gasPrice + 1,
value: 0
});
console.log(estGas);
console.log (gasPrice * estimatedGas2);
console.log(await MyNFT.signer.getBalance());
const myNFT = await MyNFT.deploy();
await myNFT.deployed();
the results in console log are:
BigNumber { value: "53000" }
5060147051722000
BigNumber { value: "83341883765736405" }
and error
Error: insufficient funds for intrinsic transaction cost [ See: https://links.ethers.org/v5-errors-INSUFFICIENT_FUNDS ] (error={"name":"ProviderError","code":-32000,"_isProviderError":true}, method="sendTransaction", transaction=undefined, code=INSUFFICIENT_FUNDS, version=providers/5.6.2)
How it can be insufficient founds? 83341883765736405 is more than 5060147051722000
Solution 1:[1]
You underestimate the gas price. 53000
is too low. It should be about 2774584
on the mainnet for your contract. Try something like this in your deployment script:
const MyNFT = await ethers.getContractFactory('NFT');
const gasPrice = await MyNFT.signer.getGasPrice();
console.log(`Current gas price: ${gasPrice}`);
const estimatedGas = await MyNFT.signer.estimateGas(
MyNFT.getDeployTransaction(),
);
console.log(`Estimated gas: ${estimatedGas}`);
const deploymentPrice = gasPrice.mul(estimatedGas);
const deployerBalance = await MyNFT.signer.getBalance();
console.log(`Deployer balance: ${ethers.utils.formatEther(deployerBalance)}`);
console.log(`Deployment price: ${ethers.utils.formatEther(deploymentPrice)}`);
if (deployerBalance.lt(deploymentPrice)) {
throw new Error(
`Insufficient funds. Top up your account balance by ${ethers.utils.formatEther(
deploymentPrice.sub(deployerBalance),
)}`,
);
}
const myNFT = await MyNFT.deploy();
await myNFT.deployed();
and the output is:
Current gas price: 20850635414
Estimated gas: 2774584
Deployer balance: your-balance
Deployment price: 0.057851839409517776
An unexpected error occurred:
Error: Insufficient funds. Top up your account balance by ???
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |