【问题标题】:Get revert reason from sendSignedTransaction从 sendSignedTransaction 获取还原原因
【发布时间】:2021-08-09 02:06:56
【问题描述】:

我正在运行一个 Hyperledger Besu 私有链,并从 Express 服务器发出 sendSignedTransaction 调用。

        try {
            let tx = {
                from: fromAccount,
                to: this.contract.options.address,
                gas: 2000000,
                gasPrice: "0",
                value: 0,
                data: await this.contract.methods
                    .method().encodeABI()
            };
    
            console.log(tx);
            console.log("signing transaction");
            let signedTx = await this.web3.eth.accounts.signTransaction(tx, privateKey);
            console.log("transaction signed");
            let result = await this.web3.eth.sendSignedTransaction(signedTx.rawTransaction)
            console.log(result);
        }
        catch (e) {
            console.log(e);
        }

交易正在还原并被捕获,但我不确定如何获取还原原因。我已经尝试设置contract.handleRevert 以及来自搜索引擎的其他解决方案,但所有其他解决方案都假定您从前端使用sendTransactioncallsend。根据 web3.js 文档,handleRevert 不适用于 sendSignedTransaction (https://web3js.readthedocs.io/en/v1.2.11/web3-eth.html#handlerevert),并且错误以一个长字符串形式返回:

Error: Transaction has been reverted by the EVM:
{
  "blockHash": "0xcb93d98a8d6f7c329dfd0cdb7d2fc421147ae077765e63263c794eb43aaa6263",
  "blockNumber": 560179,
  "contractAddress": null,
  "cumulativeGasUsed": 35348,
  "from": fromAddress,
  "gasUsed": 35348,
  "logs": [],
  "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  "status": false,
  "to": contractAddress,
  "transactionHash": "0xfbd9b755aa71d823640c0f719d358ef9c7d81362a901ec2901fba5f188a4a310",
  "transactionIndex": 0,
  "revertReason": "0x08c379a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000204552433737373a2073656e6420746f20746865207a65726f2061646472657373"
}
    at Object.TransactionError (/home/blockchain-dev/Documents/blockchain-app/server/node_modules/web3-core-helpers/src/errors.js:96:21)
    at Object.TransactionRevertedWithoutReasonError (/home/blockchain-dev/Documents/blockchain-app/server/node_modules/web3-core-helpers/src/errors.js:108:21)
    at /home/blockchain-dev/Documents/blockchain-app/server/node_modules/web3-core-method/src/index.js:482:48
    at processTicksAndRejections (internal/process/task_queues.js:93:5) {
  receipt: {
    blockHash: '0xcb93d98a8d6f7c329dfd0cdb7d2fc421147ae077765e63263c794eb43aaa6263',
    blockNumber: 560179,
    contractAddress: null,
    cumulativeGasUsed: 35348,
    from: fromAccount,
    gasUsed: 35348,
    logs: [],
    logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
    status: false,
    to: contractAddress,
    transactionHash: '0xfbd9b755aa71d823640c0f719d358ef9c7d81362a901ec2901fba5f188a4a310',
    transactionIndex: 0,
    revertReason: '0x08c379a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000204552433737373a2073656e6420746f20746865207a65726f2061646472657373'
  }
}

我还尝试在 revertReason 十六进制代码上运行十六进制到 ascii 转换器,但该值不可读。

我希望能够获得 sendSignedTransaction 调用的恢复原因。

【问题讨论】:

    标签: node.js express ethereum web3js


    【解决方案1】:

    您可以使用web3.utils.hexToAscii() 解码revertReason

    const reason = web3.utils.hexToAscii('0x08c379a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000204552433737373a2073656e6420746f20746865207a65726f2061646472657373');
    

    返回

    Ãy   ERC777: send to the zero address
    

    注意:前两个字符看起来像 UTF BOM,但 hexToUtf8() 无法解码字符串(字符串的其余部分实际上是 ASCII,而不是 UTF)。

    【讨论】:

    • 虽然TX被还原了,但是receipt中没有revertReason,为什么会发生以及如何从receipt中获取原因,而不需要额外的eth.call(tx, tx.blockNumber)
    猜你喜欢
    • 1970-01-01
    • 2010-09-11
    • 1970-01-01
    • 2016-07-01
    • 2012-10-26
    • 2021-07-29
    • 2022-10-14
    • 1970-01-01
    • 2015-06-12
    相关资源
    最近更新 更多