# 调用智能合约报错分析小技巧 Error: execution reverted

By [Bot80926](https://paragraph.com/@bot80926) · 2023-05-04

---

调用智能合约报错分析小技巧 Error: execution reverted
---------------------------------------

Bot80926 于 2023-04-29 22:46:03 首次发布

版权声明：本文为博主原创文章，遵循 [CC 4.0 BY-SA](http://creativecommons.org/licenses/by-sa/4.0/) 版权协议，转载请附上原文出处链接和本声明。

> 前端在调试调用合约的时候经常会遇到合约revert的情况，大部分区块浏览器只会显示 execution reverted，并不会把具体原因告诉我们。这个时候一般会去找合约大哥帮忙分析错误，但是合约大哥是怎么分析报错的呢？ 这篇文章给你一个解题方法。

直接上代码
-----

    const main = async (txHash) => {
      const tx = await provider.getTransaction(txHash)
      if (!tx) {
        console.log('tx not found')
      } else {
        const code = await provider.call(tx)
        console.log('revert reason:', code)
      }
    }
    

测试：`main('0x5ac9e43a32a651e92cff884a38b910d4a1897578bd935430057a619f27b7a21d')`

![](https://storage.googleapis.com/papyrus_images/25f9716077f64d15eee6a000714458a43189de7cab14681b7da8adcea3eb221c.png)

可以看到测试txHash被revert的原因是 输入了无效的 B Amount，也就给我们提供的 debug 的思路。

该方法试用于所有evm链，只需修改 provider 对应链的chain信息即可。

完整代码地址：[仓库地址](https://github.com/Bot80926/ethers-scripts/blob/main/utils/error.js)

五一休假的第四篇产出。完结 🎉 ～

---

*Originally published on [Bot80926](https://paragraph.com/@bot80926/error-execution-reverted)*
