用ethers.js获取回执status只有成功/失败(web3j也是一样)
我想获得交易失败的原因
例如这个交易在goerli链0x2c2a52b7f9887c2d5a67902d188b4a0aea575e3c2f74879ee08e0bca83a95aff
下面是我的代码和返回信息
const transaction = await hre.ethers.provider.getTransactionReceipt("0x2c2a52b7f9887c2d5a67902d188b4a0aea575e3c2f74879ee08e0bca83a95aff");
console.log(transaction);
{
to: '0xF983139a85b759dC0ea07c9d1Fcf7F87B16Dd056',
from: '0x58E36Aa3A8e234dD6285C41823dE4C17d2Fb425f',
contractAddress: null,
transactionIndex: 2,
gasUsed: BigNumber { value: "61440" },
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
blockHash: '0xae1550b498a8c7471d4e834872c9e503917a539d4b84bebf7d0002dacb476616',
transactionHash: '0x2c2a52b7f9887c2d5a67902d188b4a0aea575e3c2f74879ee08e0bca83a95aff',
logs: [],
blockNumber: 8260129,
confirmations: 3,
cumulativeGasUsed: BigNumber { value: "356693" },
effectiveGasPrice: BigNumber { value: "17437033389" },
status: 0,
type: 2,
byzantium: true
}
以上只有一个简单的状态
但是在区块浏览器中是可以看到这个失败信息的
with error 'ERC20: transfer amount exceeds balance'
有什么方式可以获取这个信息吗
这个报错信息其实是在
可以调用rpc的debug_traceTx方法获得上面的trace,然后解析出报错字符串。当然一般的公共rpc不会提供这个接口,大多数情况都是运行自己的节点。