I need to know how to do do the following tasks, after I triggered a Metamask payment with Web3 and the user paid:

  • how to get the exact public address the user used to make that payment? I need that address because my dApp is a game and if the user wins, I will use that address to pay the user their winnings.

  • how to listen for confirmation that their payment has been written to the Ethereum ledger (has been confirmed), from the same web page Javascript that initiated the transaction with Metamask?

A good gist or code sample would be incredibly useful if you know of one.

1 Answer 1


To get the current user's active Metamask account:

let currentAccount;
web3.eth.getAccounts((err, accounts) => {
    if (err) reject(err);
    else if (!accounts.length) reject('No Metamask accounts found');
    else {
       currentAccount = accounts[0];
       ... do whetever with it ....

Confirmation is a little more involved - you need to either listen to the event emitted by the smart contract function (if it emits one) or you need to wait and check when the transaction gets on the blockchain.

You can take a look how I ran transactions in smart-contract-executor (currently I no longer use that function as I replaced it with truffle library, but you can still use it): https://github.com/aleybovich/smart-contract-executor/blob/master/src/js/util/ProcessTransaction.js

UPDATE: I think I misunderstood your second question. Did you ask how to detect that the user has sent an ETH payment via Metamask? Or via a call to a smart contract?

  • Is that method bulletproof? Here's my one concern. Suppose the user makes a Metamask payment and then for some odd timing reason, use Metamask again and switches accounts, before my code that makes the Web3 account query runs? Also, I assume from your code that Metamask moves accounts around so that the last used account is moved to slot 0? Commented Jun 21, 2018 at 2:27
  • 1
    accounts[0] is always the active account. If the user changes Metamask account to another one, that other one will become accounts[0]. Yes, it's bulletproof, it's how any DApp that works with MetaMask detects the current account.
    – Andrey
    Commented Jun 21, 2018 at 2:28
  • @RobertOschler - I also updated my answer to show you how to detect that a transaction has completed.
    – Andrey
    Commented Jun 21, 2018 at 2:29
  • Thanks. As far as my second question is concerned. You got it. If I remember correctly, when you call Metamask to trigger a payment, it tells you if the user accepted or rejected the payment, so I'm covered there. It's the listening for the payment confirmation to the contract the user is paying that I needed to know, and you answered it. When you say you "replaced it with the Truffle library", can you add that to your answer? What contract did you use to do that and where is it located? I use Open Zeppelin's library. Commented Jun 21, 2018 at 11:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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