How to sync users Token Balances?

Hey there,

I’m a little confused on the most optimized way to do the following:


I would like to sync the balances of my linked users so I can subscribe to that class and get live updates on their balances. I only really care about saving the balances of my ERC20 token and my NFT’s.

  1. Should I add my contracts (ERC20 + NFTs) to the “Sync and Watch Address” plugin?
  2. Should I use the “Sync Historical” feature for this? And what limit of historical events should I set?

At https://v1docs.moralis.io/moralis-dapp/automatic-transaction-sync/historical-transactions#collection-schema it says “Moralis Server will populate the following set of collections for each blockchain synced by the Moralis Server.” (xxxTransactions, xxxTokenTransfers, xxxTokenBalances, xxxNFTTransfers, xxxNFTOwners)

But I can only see “xxxTokenTransfers” and “xxxTransactions” after adding my ERC20 contract to the “Sync and Watch Address” plugin with a “Sync Historical” of 500.

I’m also adding my user’s wallets with code:

await Moralis.Cloud.run("watchBscAddress", {
  address: lowerCaseAddress,
}, { useMasterKey: true });

Is there anything I’m doing wrong?

Thanks In Advance!

Some tables were synced only before using a nitro server.

For now what you could do could be to use web3api to get the balances when a transaction was identified for an address.

Then I should only sync the contracts and update the balances and NFT’s with code right? No need to sync each user address.

In the case of ERC20, when I get a transfer, I update receiver and sender balances.

Should I do that with a beforeFind on the xxxTransactions table?

you could do that in theory, but you will still need to sync the initial balances, it could depend on what type of contracts you want to monitor, depending on how many NFTs, tokens, wallets they have

NFT’s might reach the hundreds but not thousands.
Tokens only 1.

I can sync the initial balance by calling the API on those addresses when linking it manually.
Does that sound alright or am I forgetting something?

it doesn’t sound like it is too much data, it could work, it looks like you want to monitor specific contracts that don’t have too many wallets or transactions

NFT’s won’t have many activity for sure.

The token however, has +18k holders at the moment and I suspect that number will only increase.

Isn’t this the most optimized way tho? Even for scalability?

Syncing the addresses 1 by 1 is more resource consuming right? But that’s not really an option because it doesn’t retrieve the token balances.

Therefore listening to the transactions events is the only way, correct?

yes, for scalability that would be the way, if you don’t have too many users you could use web3api at the beginning to get the balance for a user when a new transfer is made

1 Like