Skip to content

Commit

Permalink
dynamic query batching
Browse files Browse the repository at this point in the history
  • Loading branch information
waynebruce0x committed Jun 14, 2024
1 parent bf4d857 commit bea2f68
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,10 +354,10 @@ async function computeTVL(balances, timestamp) {
let tokenData = []
readKeys.forEach(i => unknownTokens[i] = true)

const queries = buildPricesGetQueries(readKeys)
const { errors } = await PromisePool.withConcurrency(5)
.for(sliceIntoChunks(readKeys, 100))
.process(async (keys) => {
tokenData.push((await axios.get(`https://coins.llama.fi/prices/current/${keys.join(',')}`)).data.coins)
.for(queries).process(async (query) => {
tokenData.push((await axios.get(query)).data.coins)
})

if (errors && errors.length)
Expand Down Expand Up @@ -423,7 +423,22 @@ setTimeout(() => {
process.exit(1);
}, 10 * 60 * 1000) // 10 minutes

function buildPricesGetQueries(readKeys) {
const burl = 'https://coins.llama.fi/prices/current/'
const queries = []
let query = burl

for (key of readKeys) {
if (query.length + key.length > 2000) {
queries.push(query.slice(0, -1))
query = burl
}
query += `${key},`
}

queries.push(query.slice(0, -1))
return queries
}

async function initCache() {
let currentCache = await sdk.cache.readCache(INTERNAL_CACHE_FILE)
Expand Down

0 comments on commit bea2f68

Please sign in to comment.