Transaction Status – How to Check Transfer Status in Metamask using Javascript

javascriptmetamasktransactions

I am trying to set up a structure that will receive payment with the sendTransaction method. But there is a small problem. The sendTransaction method works fine, but has the user completed the transfer? Did the transfer go to the destination wallet? I can't control like Is there a way to do these checks?

You can think like this.

sendEthButton.addEventListener('click', () => {
  ethereum
    .request({
      method: 'eth_sendTransaction',
      params: [
        {
          from: accounts[0],
          to: {sellerAdress},
          value: {totalAmount}
        },
      ],
    })
    .then((txHash) => console.log(txHash))
    .catch((error) => console.error);
});

Thanks to this method, the transfer notification is sent to the customer's metamask. There is no problem here. But did the client approve the transfer request from Metamask? Is the transfer complete? Or did the customer not respond to the transfer notification? Is there any method to do such queries?

Briefly, how can I check the transfer status to see if the payment has been completed?

Best Answer

You just need to get the receipt using the txHash

https://www.quicknode.com/docs/ethereum/eth_getTransactionReceipt https://web3js.readthedocs.io/en/v1.7.1/web3-eth.html#gettransactionreceipt

Assume transactions have 3 steps

  1. Pending (tx sent but not mined): you won't get a lot of params in the call such as blockHash
  2. Approved (Mined and executed successfully): status param will be true
  3. Rejected (Mined but execution failed): status param will be false

I recommend you use a library to do those checks like ethers.js or web3js, they will automatically subscribe and notify you, you can even choose to accept the transaction only when a number of confirmations (Blocks on top of the block where your tx was mined) happens to protect against 51% attacks

https://web3js.readthedocs.io/en/v1.7.1/callbacks-promises-events.html?highlight=receipt

Best of luck!

Related Topic