Hello!
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;
try{
tokenPrice = await Moralis.Web3API.token.getTokenPrice(options);
} catch (error){
console.log(error)
continue;
}
token.price = tokenPrice.usdPrice.toFixed(5);
token.usdAmount = (token.price*token.balance).toFixed(2);
}
setTokens(balances);
}