3

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) {
                console.log('ok')
            } else {
                if (error.message.includes("User denied")) {
                    alert("You rejected the transaction on Metamask!")
                } else {
                    alert(error)
                }
            }

        })
    })

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.

1

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

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

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
  }
  console.log(ethereum.isMetaMask);

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

    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.";
        }
      }
    })

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

Hope this will help you.

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.