以太坊-内部交易成本资金不足



我试图创建自己的NFT合同工厂,但它在主网失败。我钱包里有ETH。在测试中它工作得很好。这是我的合同:

//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;
}
}

这是我的合同部署脚本

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();

控制台日志的结果如下:

BigNumber { value: "53000" }
5060147051722000
BigNumber { value: "83341883765736405" }

和错误

错误:内部交易成本资金不足[参见: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)

怎么可能是资金不足?83341883765736405大于5060147051722000

你低估了油价。53000过低。应该是关于你合同中主网的2774584。在您的部署脚本中尝试这样做:

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();

,输出为:

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 ???

相关内容

  • 没有找到相关文章

最新更新