原因(handleRevert)"/>
使用 web3js 获取失败事务的还原原因(handleRevert)
我尝试了很多方法(实际上是所有方法),但仍然无法在 web3js 上获得恢复原因。我的 web3js 版本是 1.9.0,我有一个简单的 solidity 合约,它像这样投票:
function addVote(string memory voteName, string memory candidate, address addr) public returns(string memory){
require(candidateVote[voteName].length > 0, "Vote does not exist.");
// require(voteCount[voteName][candidate] == 0, "You have already voted for this candidate.");
require(!voters[voteName][addr], "Address has already voted!");
voteCount[voteName][candidate]++;
// voteCount[address][voteName][candidate]++
voters[voteName][addr] = true;
return "Vote successful casted!";
}
如您所见,我有 3 个要求,这是我在 nodejs 中的 web3js 调用:
export function addVote(web3:any, contract:any, abi: any,contractAddress:any,voteName:string, candidate:any, fromAccount:any){
return new Promise((resolve, reject) => {
contract.methods.addVote(voteName, candidate, fromAccount).send({from:fromAccount,gas:3000000})
.on('confirmation', function(confirmationNumber, receipt){
if(receipt.status){
resolve(receipt)
}else{
reject(receipt)
}
}).on('error', function(error, receipt) {
reject(error)
})
})
}
这是输出:
E:\Docker-project\dvote\node_modules\web3-utils\lib\index.js:49
error = new Error(error);
^
Error: Transaction has been reverted by the EVM:
{
"transactionHash": "0xd3dc4740bb91d16b94b7214914347531e494c46582dcb69c12bc488133e49d06",
"transactionIndex": 0,
"blockNumber": 20,
"blockHash": "0x3a50b40559ac7e6ae7dad11fe73ec36e8c9be536fa8e5e6b1353cbf7ca3a96fd",
"from": "0x5d256d7b2238958831d55d9d6e0d08e51e3dd1d8",
"to": "0x25fde715c938ac5a27cd06aa3887981d0457f330",
"cumulativeGasUsed": 29872,
"gasUsed": 29872,
"contractAddress": null,
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"status": false,
"effectiveGasPrice": 2570615365,
"type": "0x2",
"events": {}
}
at Object._fireError (E:\Docker-project\dvote\node_modules\web3-utils\lib\index.js:49:17)
at E:\Docker-project\dvote\node_modules\web3-core-method\lib\index.js:396:39
我必须提到我使用并设置了 web3.eth.handleRevert = true,没有任何反应!我还在 trxhash 上使用了 getTransactionReceipt,我希望该对象可能包含恢复原因,但它什么都没有。如果你能帮助我,我将不胜感激。
回答如下:更多推荐
使用 web3js 获取失败事务的还原原因(handleRevert)
发布评论