DTeam 技术日志

Doer、Delivery、Dream

排错:调用只读方法时,遇到 UNPREDICTABLE_GAS_LIMIT

胡键 Posted at — Mar 10, 2021 阅读

症状

开发以太坊应用时,调用只读方法时遇到类似以下内容的报错信息:

ERROR Error: Uncaught (in promise): Error: cannot estimate gas; transaction may fail or may require manual gas limit (error={"code":-32000,"message":"execution reverted"}, method="call", transaction={"to":"0xE6183d3094b9r360B121Ec1330afAE76A74d1cbF","data":"0xdb699765000000000000000000000000b2b7bedd7d7fc19804c7dd4a4e8174c4c73c220d0000000000000000000000000000000000000000000000000de0b6b3b7640000000000000000000000000000000000000000000000000000000000006c915aef"}, code=UNPREDICTABLE_GAS_LIMIT, version=providers/5.0.24)
    at d.makeError (main.a577643548eb15e6832e.js:1)
    at d.throwError (main.a577643548eb15e6832e.js:1)
    at Z (main.a577643548eb15e6832e.js:1)
    at $e.<anonymous> (main.a577643548eb15e6832e.js:1)
    at Generator.throw (<anonymous>)
    at a (main.a577643548eb15e6832e.js:1)
    at l.invoke (polyfills.797d13e303959ed8dd77.js:1)
    at Object.onInvoke (main.a577643548eb15e6832e.js:1)
    at l.invoke (polyfills.797d13e303959ed8dd77.js:1)
    at a.run (polyfills.797d13e303959ed8dd77.js:1)
    at Z (polyfills.797d13e303959ed8dd77.js:1)
    at polyfills.797d13e303959ed8dd77.js:1
    at a (main.a577643548eb15e6832e.js:1)
    at l.invoke (polyfills.797d13e303959ed8dd77.js:1)
    at Object.onInvoke (main.a577643548eb15e6832e.js:1)
    at l.invoke (polyfills.797d13e303959ed8dd77.js:1)
    at a.run (polyfills.797d13e303959ed8dd77.js:1)
    at polyfills.797d13e303959ed8dd77.js:1
    at l.invokeTask (polyfills.797d13e303959ed8dd77.js:1)
    at Object.onInvokeTask (main.a577643548eb15e6832e.js:1)

原因

看到上面这样的信息,十有八九会让开发者摸不着头脑,但最可能的原因是:调用了合约中不存在的方法。这种情况在 ethers.js 搭配 Human-Readable ABI 时尤其容易出现:比如因手抖而导致的拼写错误。

解决

检查调用地址(上文中的 to)对应的合约中是否有要调用的方法并进行修改。


友情链接


相关文章