【问题标题】:How to get the failed status from solidity require statement如何从solidity require语句中获取失败状态
【发布时间】:2022-01-16 12:49:26
【问题描述】:

在一个可靠的方法中,有一个 require 语句正在失败,并且 web3j 中的事务由于“Gas value is not enough”而抛出错误。

示例:require(providedtimestamp > block.timestamp, "release time is before current time");

但在来自 Etherscan 的交易中,错误消息会正确显示。示例:https://rinkeby.etherscan.io/tx/0x2ed757feef430f1695dcbbad8d13ee8df5ba630409465dbf82688dc5543dc52e

如何在使用 web3j 的方法调用期间捕获此错误消息。

【问题讨论】:

    标签: blockchain solidity web3-java


    【解决方案1】:

    您可以轻松地调用该函数并执行以下操作:

    contract.function.call({from:accounts[1]}).then((result) => {
      console.log("Success! Got result: " + result);
    }).catch((err) => {
      console.log("Failed with error: " + err);
    });
    

    【讨论】:

    • 你提供的解决方案是web3js。但我正在寻找 web3-java。感谢您的解决方案。
    【解决方案2】:

    当使用 web3.java 发生异常时,您应该使用try/catch 获取错误描述。

    try {
      TransactionReceipt balanceOf = test.transfer(new Address(address), transferBalance).get(); 
    } catch (Exception e) {
      // Extract error string from exception
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-23
      • 1970-01-01
      • 1970-01-01
      • 2018-06-24
      • 2018-10-03
      • 1970-01-01
      • 2018-10-15
      相关资源
      最近更新 更多