Search code examples
transactionsethereumsoliditymetamask

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

  • 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)
    }