I want to detect if the user has clicked a button that will make metamask popup asking them to submit, but then rejects the transaction. I have something like the following:

Contract.deployed().then(function (contractInstance) {
        contractInstance.function({ from: web3.eth.accounts[0]}).then(function (error, result) {

            if (!error) {
            } else {
                if (error.message.includes("User denied")) {
                    alert("You rejected the transaction on Metamask!")
                } else {


This doesn't work as intended. How should this be done? The error printed if the user rejects the transaction is:

Uncaught (in promise) Error: Error: MetaMask Tx Signature: User denied transaction signature.

So presumably this should be simple by checking if that's the error message.


I was looking for a solution for this too. I am using truffle and looked up their documentation. http://truffleframework.com/docs/getting_started/contracts

  contractInstance.function({from: web3.eth.accounts[0]}).then(function(result){             
      alert('transaction success')}).catch(function(e){ 

I was facing the same problem. Following is my code to connect and sign a transaction using Metamask API:

async function connectMetamask(){
  if (typeof window.ethereum !== 'undefined'
      || (typeof window.web3 !== 'undefined')) {
      const provider = window['ethereum'] || window.web3.currentProvider

  try {
    const accounts = await ethereum.enable();
    console.log("account: " +  accounts[0]);

      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){
        if(transactionHash.result !== undefined){
          document.getElementById('span_success').innerText = "Transaction Successfully Done!!!";
          document.getElementById('span_success').innerText = "User denied transaction signature.";

  } catch (error) {
    console.log(error === "User rejected provider access")

Hope this will help you.


To handle Confirm/Cancel response of user on your contract functions, you can use the transactionHash event & error event from send function.

"transactionHash" returns transactionHash: String: Fired when the transaction hash is available.

"error" returns error: Error: Fired if an error occurs during sending. If the transaction was rejected by the network with a receipt, the receipt will be available as a property on the error object.enter image description here

You can see detail here.

When you click on Confirm button then you can handle this case in the transactionHash event.

When you click on Cancel button then you can handle this case in the error event.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.