'How to capture scaffold-eth Transactor error
I am using scaffold-eth's Transactor to send transactions. But I am not able to capture if an error (or revert) occurs after the transaction is send. Instead I am getting Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'wait') because the tx is undefined if an error occurs. How can I capture the transaction error? Below is my code:
// scaffold-eth's Transactor helper gives us a nice UI popup when a transaction is sent. Transactor is a function.
const transactor = Transactor(provider, gasPrice);
const tx = await transactor(contract.redeem(ownerAddress, 0, voucher, {value: amount}));
//Here the tx is undefined since the transaction reverted (failed)
// Wait for the transaction to be confirmed, then get the token ID out of the emitted Transfer event.
const receipt = await tx.wait();
Solution 1:[1]
Can't you wrap your code in a try catch? Something like this:
try {
const tx = await transactor(contract.redeem(ownerAddress, 0, voucher, {value: amount}));
const receipt = tw.wait(1)
} catch(err) {
console.log(err.message)
}
Solution 2:[2]
Transactor takes a callback function, passing the callback will catch any sort of transaction error.
const tx = await transactor(contract.redeem(ownerAddress, 0, voucher, {value: amount}),(error)=>console.log(error);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Niccolò Fant |
| Solution 2 | shafa.yeat |
