繁体   English   中英

Hardhat 在 ERC-721 的以太坊开发中的脚本中发出 UNPREDICTABLE_GAS_LIMIT

[英]Hardhat issue UNPREDICTABLE_GAS_LIMIT in a script within ethereum development of ERC-721

我有一个我真的很绝望的问题,我认为这可能与“等待确认?”有关。 问题,但我不知道如何激活它。

问题是,我在部署脚本中收到针对我的 ERC-721 合同的 function 消息的请求的结果:

原因:'无法估计气体; 交易可能会失败或可能需要手动 gas limit',代码:'UNPREDICTABLE_GAS_LIMIT',

我认为这可能与命令彼此快速发生有关,因为当我手动在 hardhad 控制台(npx hardhat console -.network rinkeby)中执行以下步骤时,一切正常,没有这个错误(即使有那个<>.confirmations 始终为 0,我不明白)

我在运行的 deploy.js 中有以下命令:

npx hardhat run -.network rinkeby scripts/deploy.js

async function main() {
    const BBA = await hre.ethers.getContractFactory("mycontract");
    const bba = await BBA.deploy();
    await bba.deployed();
    console.log("mycontract deployed to:", bba.address);

    await bba.safeMint("0x123...", 0, 'https://gateway.pinata.cloud/ipfs/url');

    await bba.lockToken(0, "1644598343");

    console.log(await bba.getLock(0));
    ...
}

通过调用 await bba.getLock(0) 我得到错误:无法估计气体; 交易可能会失败或可能需要手动 gas 限制 (error={"name":"ProviderError","code":-32000,"_isProviderError":true}, method="call", transaction=...

我基于 openzeppelin 合约向导https: //docs.openzeppelin.com/contracts/4.x/wizard 构建了一个 1:1 ERC-721。

在这份合约中,我添加了一个简单的 function:

    mapping(uint256 => uint256) private _locks;

    function lockToken(
        uint256 targetTokenId,
        uint256 since,
    ) public {
        _locks[targetTokenId] = since;
    }
    
   function getLock(uint256 targetTokenId) public view returns (uint256) {
        return _locks[targetTokenId];
    }

我将 INFURA 与以下 hardhat.config.js 一起使用:

const { projectId, mnemonic } = require('./secrets.json');

module.exports = {
    rinkeby: {
      url: "https://rinkeby.infura.io/v3/" + projectId,
      accounts: { mnemonic: mnemonic },
      confirmations: 2,
      gas: 2100000,
      gasPrice: 8000000000,
      saveDeployments: true
    }
  },
  solidity: {
    version: "0.8.4",
    settings: {
      evmVersion: "byzantium",
      optimizer: {
        enabled: true,
        runs: 1500,
      }
    }
  }
};

任何人都可以指导我解决这个问题的原因吗?

提前致谢。

经过额外的研究和测试,解决方案似乎非常简单。

我只需要为 function 调用声明一个 const,然后需要对它们调用 wait() 调用。

正如我所读,程序等待调用被挖掘。

我仍然不确定如何命令等待两次确认,但已经解决了 GAS LIMIT 问题。

const mint = await bba.safeMint("0x123...", 0, 'https://gateway.pinata.cloud/ipfs/url');

await mint.wait();

const locking= await bba.lockToken(0, "1644598343");

await locking.wait();

console.log(await bba.getLock(0));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM