【问题标题】:Set gas limit on contract method in ethers.js在 ethers.js 中设置合约方法的气体限制
【发布时间】:2022-01-07 13:38:03
【问题描述】:

问题

我正在尝试使用测试网络中的合约方法(ropsten),但由于此错误而失败:

原因:'无法估算气体;交易可能会失败或可能需要手动限制气体”, 代码:'UNPREDICTABLE_GAS_LIMIT'

代码

我创建了一个智能合约的实例并想调用它的注册方法:

const registrationContract = new ethers.Contract(ADDRESS, abi, signer);
const hashedDomain = utils.keccak256(utils.toUtf8Bytes(domain));

const register = await registrationContract.register(hashedDomain, walletAddress);

ethers.js 是否提供了设置合约限制的功能?或者这可以通过其他方式完成吗?我在documentation 中没有找到。

【问题讨论】:

  • 我认为 gasLimit 通常是在 message 参数中传递的。我不知道是否总是这样。你能提供你正在尝试使用的合同吗?例如,使用 uniswap,您可以将 gas 限制设置为 xuniswap.swapExactTokensForETH(tokenAmount, ethAmount, etc., {gasLimit: x})

标签: javascript ethereum ethers.js ether


【解决方案1】:

你可以用一个对象作为最后一个参数来设置gas limit,对于一个简单的转账交易,你可以这样做:

const tx = {
  to: toAddress,
  value: ethers.utils.parseEther(value),
  gasLimit: 50000,
  nonce: nonce || undefined,
};
await signer.sendTransaction(tx);

如果您正在对智能合约进行交易,想法是相同的,但请确保在您的 abi 方法之一中设置最后一个参数,例如:

const tx = await contract.safeTransferFrom(from, to, tokenId, amount, [], {
  gasLimit: 100000,
  nonce: nonce || undefined,
});

这可以修复 UNPREDICTABLE_GAS_LIMIT 错误,因为手动通知它 ethers 会跳过请求计算的 gas_limit 的提供程序的 rpc 方法调用。

【讨论】:

  • 我已经尝试在合约上设置gas限制,但错误仍然存​​在。
  • 同样的错误信息?
  • 现在工作。真的不知道发生了什么变化,但我尽我所能。
  • 你解决了我最大的问题之一。谢谢你:)
猜你喜欢
  • 2022-01-12
  • 2022-06-18
  • 1970-01-01
  • 1970-01-01
  • 2013-04-04
  • 1970-01-01
  • 2022-08-20
  • 1970-01-01
  • 2021-12-17
相关资源
最近更新 更多