Populating Token Lists

I’m building a wallet feature for users to view their tokens/nfts/transactions. Are there options for sorting other than by block number, and options such as limit and skip for pagination, and is there a way to include price during the first getTokenBalances() call rather than separately calling the getTokenPrice(). My goal is to layout the tokens in descending order of USD asset balance for the user.

So my thought is something like this: When calling Moralis.WEB3API.account.getTokenBalances() is there a way to include the .token.getTokenPrice() as an option? I’m currently calling getTokenBalances() and then running a for loop to add the price as a new key:value pair into each of the user’s tokens and to populate a list of 65 tokens is taking over 25seconds and that’s without running additional code to then sort by that asset balance in USD. Many thanks!

    const getTokens = async() => {
        const options = { chain: {currentChain}}
        const balances = await Moralis.Web3API.account.getTokenBalances(options);
        for(let i = 0; i < balances.length; ++i) {
            let token = balances[i];
            let tokenAddress = token.token_address;
            const options = {
                address: tokenAddress,
                chain: {currentChain},                
            let tokenPrice;
             tokenPrice = await Moralis.Web3API.token.getTokenPrice(options);
            } catch (error){
            token.price = tokenPrice.usdPrice.toFixed(5);
            token.usdAmount = (token.price*token.balance).toFixed(2);

As a work around for now you could:

  • keep track of what tokens all your users use
  • have the token price for all of those tokens saved in a data base table
  • have a job that updates that price in the database periodically
  • query database as a cache for token prices

Hi I wish I could but I don’t have a computer or a laptop I am from Uganda but I have neither of those things