Fetching USD prices for an array of tokens


I am slowly becoming familiar with the REST API for moralis, which I current really like. However currently I’m running into an issue fetching the USD price of all tokens from an address. Right now I use the address balance endpoint to retrieve all the native balances, but haven’t found a great way to convert native balance -> usd price.

I thought of requesting the price of each individual token separately (in a loop), but felt as though requesting hundreds of times (or thousands) would be extremely inefficient. Is there any better way to leverage the moralis api/sdk where I can input an array of token addresses and get their corresponding usd price?

It looks like Moralis doesn’t have this API function now, I moved this thread to Suggestions and Improvements.

1 Like

Adding a voice to this one. The “get usd price” api is one where we call the API 6 times (for the 6 tokens we need to care about), but we often get 400 errors, I assume due to rate limit checking. Being able to grab a selection in one call would be really helpful.

1 Like

I beg your pardon. But this is just an upgrade to the existing https://docs.moralis.io/moralis-server/web3-sdk/token#gettokenprice to take arrays for token addresses.

I would add it should take an array of to_block as well.

May I ask for the server-side code to getTokenPrice()? This may not be a priority for the Moralis team but it is for several of us out in the wild. We’ll write the ‘extended edition’ ourselves if we could get the prototype to work with. Thank you.

I think that the main problem would be that it would be a more complicated api call with a list as parameter, it will have to check more blocks with an array of to_block for example

you can make now too a function in cloud functions code that receives a list and queries getTokenPrice multiple times to return the data for the entire list

One way or another if Moralis is to evolve into any kind of trading algorithm data source it will need to return arrays of pricing data. It seems to me that iterating over such arrays on the server would be far more efficient and reliable than iterating on the front end with an API call over the internet at each point. I’d expect Moralis to have token prices at DEX contracts indexed somewhere for easy access. Otherwise where does getTokenPrice() source its data from? In any case, can you point us to getTokenPrice()'s code so we can have a look? Thanks.

it may be something similar to how this post describes: Token Price Error | "No pools found with enough liquidity"

So…no pointers to the source code of Moralis.Web3API.token.getTokenPrice()? Is it not open source–or is it so open source that I should be staring at it on GitHub by now. :thinking:

I’d just use coinGecko, but they have a unique mapping between tokens, names, and symbols that’s become quite dangerous. I’d rather hit up Moralis and go directly from token contract addresses.

this particular api call is not open source, it is an internal api call that makes requests to an internal db and/or directly to nodes

you can cache the data so that you don’t query all the data every time, like making now 100 requests for today and tomorrow another 100 requests and still keeping the data for previous 100 requests in a db so that you have data from two days (for example)