Why there is so big difference in time between getTokenBalances and using query on a speednode?

Hi, I’m testing getTokenBalances() and ethers query using speedynodes and it is a big difference in getting information (time), why? my code in ethers :

const provider = new ethers.providers.JsonRpcProvider(NODE_URL);
for (let key in tokens) {
            if (tokens.hasOwnProperty(key)) {
                // console.log(tokens[key]);
                let contract_address_to_check = tokens[key].address;
                if (contract_address_to_check != "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee") {
                    const Contract = new ethers.Contract(contract_address_to_check, Abi, provider);
                    let balance_wei = await Contract.balanceOf(address);
                    
                    if (balance_wei > 0) {
                        let decimals = await Contract.decimals();
                        let balance = balance_wei / 10 ** decimals;
                        console.log(tokens[key].address);
                        console.log(tokens[key].decimals);
                        console.log(tokens[key].symbol);
                        console.log(balance);
                        // balance = balance / (10 ** tokens[key].decimals);
                        // console.log(balance);
                    }
                }
            }
        }

You mean that the data that you get with getTokenBalances looks to be from some time in the past, like some minutes in the past?

No, I meant that getTokenBalances gives response in few seconds max while checking all contracts, and when I try to do the same using ethers balanceOf call it takes 10x longer time

The reason is because nodes are primitive tech that should be avoided as much as possible

You should use our API and SDK instead as it’s more optimized for querying and is built for very high load and very high performance

RPC nodes are great for consensus - bad as database

For development purposes nodes should be avoided if possible especially for production

In some cases nodes can’t be avoided (for example if you want to upload a smart contract) and in that case you should use nodes

At the end of the day it comes down to number of requests - with Moralis SDK you make 1 request to get all info about user balances - with node you have to make many requests and ask about balance of each token at a time.

There are also many other reasons like caching, indexing etc that make SDK much more performing

1 Like

Thanks, I see the performance of API/SDK it is amazing, but in the end your API is also connected to some node right? Is it using some batchrequest on the chain?

I want to have a different method than getTokenBalances() because:

  • don’t want to make a Moralis a point of failure, if your server is down, it lead to chain reaction on any project using it (happens very rarely but happens) so I need another function in case of Moralis unavailability
  • getTokenBalances() gives a shitcoins in response, so if im querring with ethers only those tokens which are tradeable on 1inch I get 0-1% of shitcoins :slight_smile:

No SDK is not using nodes directly for each request - we have massive indexing infrastructure in place to save and index blockchain data in order to make the requests blazing fast

This way the node just feeds the data to our systems once and we can serve that data to millions of users FAST!!

We will add ways to filter data easily

I get it, so you in Realtime get all new chain data saved to your server and put into some database, then your SDK query easily over this data right?

Yes that’s how it works on the big picture