首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从sendSignedTransaction获得还原原因

从sendSignedTransaction获得还原原因
EN

Stack Overflow用户
提问于 2021-05-19 19:43:01
回答 2查看 2.6K关注 0票数 0

我正在运行一个Hyperledger私人连锁店,并从一个快递服务器进行sendSignedTransaction调用。

代码语言:javascript
复制
        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),错误作为一个长字符串返回:

代码语言:javascript
复制
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调用的恢复原因。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-05-19 22:08:05

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

代码语言:javascript
复制
const reason = web3.utils.hexToAscii('0x08c379a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000204552433737373a2073656e6420746f20746865207a65726f2061646472657373');

返回

代码语言:javascript
复制
Ãy   ERC777: send to the zero address

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

票数 1
EN

Stack Overflow用户

发布于 2022-01-17 21:39:20

这是一项工作,因为revertReason似乎还没有使用sendSignedTransaction (至少从我使用炼金术的节点服务的经验来看)。

代码语言:javascript
复制
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) {
    this.contract.methods.method()
        .call({'from': fromAccount}).then(() => {
        throw Error ('reverted tx')})
        .catch(revertReason => console.log({revertReason})
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67609884

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档