0

How does Metamask keep the updated balances of my ERC20 tokens?

What happens in the background that as soon as I receive an ERC20 transfer, the balance gets updated? Who calls balanceOf()?

2 Answers 2

1

Metamask relies on external data indexers - last time I checked it used Infura.

Whenever a new block arrives, its data is stored in database in various, queryable, formats. One of the formats is used to query "which tokens does address X own". Metamask then queries for that data whenever new blocks are received.

Metamask could also analyze its transactions to see token balance changes, but I doubt it actually does this, since it would only show part of token changes (for example one may transact with the same account outside Metamask).

3
  • If I understood correctly, you're saying that these data indexers are listening each chain for every block, which then Metamask would query to get the token balances?
    – dNyrM
    Mar 15 at 12:06
  • Basically yes. They analyze the data per each new block and organize it in easily-available databases for querying. Mar 15 at 13:23
  • Cool! Thanks a lot
    – dNyrM
    Mar 15 at 13:45
0

For tokens, the logic of monitoring token account updates is in assets-controller, TokenBalancesController to be more specific.

The token balance is updated when

  1. 3 mins intervals by default.
  2. There is a change in the tokens states

A typical token state looks like this:

const tokenState = {
      metamask: {
        addressBook: {},
        identities: {},
        blockGasLimit: '',
        selectedAddress: '',
        provider: {
          chainId: '0x1',
        },
      },
      send: {
        account: {
          balance: '',
        },
        asset: {
          type: AssetType.token,
          details: {},
        },
        gas: {
          gasPrice: '',
        },
        recipient: {
          address: '',
        },
        amount: {
          value: '',
        },
        userInputHexData: '',
      },
    };

Token state change can be triggered in several events. For example:

  • A transaction is finalized.
  • A new token is added.
  • Change of Chain.

Metamask monitors state changes and query blockchain information via Infura's RPC endpoint.

4
  • But who calls balanceOf()? Metamask? If so, where do the files that do the call live? In which server? Or in the browser?
    – dNyrM
    Mar 15 at 12:07
  • No one calls it mate. I guess the metamask backend listens for events. As soon as the transfer events is emitted it updates the balances Mar 15 at 13:21
  • That's the question. Who's the metamask backend? Are they third-parties that metamask connects to? Is it Infura (besides an RPC url)? If not, does metamask run offchain scripts for getting that data?
    – dNyrM
    Mar 15 at 22:14
  • But who calls balanceOf()? metamask. If so, where do the files that do the call live? browser. Who's the metamask backend? Infura, you can provide your own rpc actually. Are they third-parties that metamask connects to? I think it connects to other service for exchange rate. If not, does metamask run offchain scripts for getting that data? Yup, the controllers in taking care of this. Mar 16 at 5:02

Your Answer

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

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