skip to Main Content

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


  1. I faced the same problem using send function.
    I solved that using callback function to send function. Like this.

    ethereum.send({
      method: 'eth_sendTransaction',
      params: [{"from": accounts[0],
      "to": smartContractaddress,
      "gas": "0x2DC6C0", // 30400
      "gasPrice": "0x2540BE400", 
      "value": weitohex, // 2441406250
      "data": inputhex}],
      from: accounts[0],
    },function(err, transactionHash) {
      if (!err){
        console.log(transactionHash); 
        if(transactionHash.result !== undefined){
          document.getElementById('span_metalink').innerText="https://kovan.etherscan.io/tx/"+transactionHash.result;
          document.getElementById('span_metalink').href="https://kovan.etherscan.io/tx/"+transactionHash.result;
          document.getElementById('span_success').innerText = "Transaction Successfully Done!!!";
        }
        else{
          document.getElementById('span_success').innerText = "User denied transaction signature.";
        }
      }
    })
    
    Login or Signup to reply.
  2. 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

    // Call the contract method helloWorld()
    const transaction = this.contract.methods
      .helloWrold('some parameter 1', 'some parameter 2')
      .send({ from: userAccount });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search