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 [.x/erc721](.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: ] (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
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./contracts/3.x/erc721](https://docs.openzeppelin./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/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
Share Improve this question edited May 3, 2022 at 20:22 TylerH 21.1k78 gold badges79 silver badges113 bronze badges asked May 1, 2022 at 15:48 KrzysztfKrzysztf 231 silver badge5 bronze badges 1- Your first number is larger than the largest integer supported by Javascript. – Tangentially Perpendicular Commented May 3, 2022 at 20:41
1 Answer
Reset to default 8You 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 ???
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742359758a4429156.html
评论列表(0条)