Here is my code example:
const userAccount = '0x...';
// Call the contract method helloWorld()
const transaction = this.contract.methods
.helloWrold('some parameter 1', 'some parameter 2')
.send({ from: userAccount });
// Handle errors from the transaction
transaction.on('error', (error: any) => {
console.error('Transaction error:', error);
});
// Handle success
transaction.on('receipt', (receipt: any) => {
console.log('Transaction success:', receipt);
});
When the send()
method is executed then MetaMask
‘s popup appears.
And the problem is, if a user cancels the transaction by pressing cancel in that popup.
My error handler is not executed. I have tried to use different events, I have tried to use try...catch
block, but that doesn’t seems to work with an async functions.
Is there any solution on this?
All I can see – that is the error in the console, that comes from MetaMask, that the user has denied to sign the transaction.
2
Answers
I faced the same problem using send function.
I solved that using callback function to send function. Like this.
Without having tested this, I suspect this is because your are trying to catch transaction errors while the user rejecting the transaction might be handled differently.
Try adding a catch around