The previous article Solidity - Custom Error mentioned how to use custom error in smart contracts. This article continues this topic and explains how to use Ethers.js to parse custom error when call, estimateGas and transaction failed. Ethers.js version 6.5.1 is used here, tested at Arbitrum Goerli.
Next is the processing after sending the transaction
1 2 3 4 5 6 7 8 9 10
// If the gasLimit is not set, it will execute estimateGas first, and can't send const tx = await myContract.sendEmptyError({ gasLimit: 1000000 });
// It is not known whether the transaction was successful until the transaction is confirmed, so it won't throw error. Wait for confirmation and check. const receipt = await tx.wait();
// if fail if (!receipt.status) { // start parsing... }
At this time, we will find that neither the tx object nor the receipt has any available data. At this time, we can only use a strange way to achieve.
if (!receipt.status) { try { // Specify the node to simulate the state of a block number constrequest: any = { blockTag: receipt.blockNumber }; ['to', 'from', 'nonce', 'gasLimit', 'gasPrice', 'maxPriorityFeePerGas', 'maxFeePerGas', 'data', 'value', 'chainId', 'type', 'accessList'].forEach((key) => { request[key] = tx[key]; }); // Execute a simulated transaction await provider.call(request); } catch (e) { // will give the same result as estimateGas // null console.log(e?.revert);
// 0x4f3d7def console.log(e.data);
// processing can be done in the above way } }
It should be noted that if blockTag is specified as a block number, there is a time limit. Bblocks that are too long ago cannot be executed.
Summarize
Organize the above code into functions as follows: