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

Ethereum - insufficient funds for intrinsic transaction cost

我正在尝试创建自己的 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" }

和错误

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)

怎么会发现不足呢? 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 ???