Skip to content

Commit

Permalink
fix: add debouncer
Browse files Browse the repository at this point in the history
  • Loading branch information
KedziaPawel committed Feb 27, 2024
1 parent 17a0eef commit 32a32db
Showing 1 changed file with 30 additions and 27 deletions.
57 changes: 30 additions & 27 deletions balancer-js/src/modules/data/token-prices/coingecko.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ function splitIntoChunks<TItem>(items: TItem[]): TItem[][] {
return chunks;
}

function wait(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

/**
* Simple coingecko price source implementation. Configurable by network and token addresses.
*/
Expand Down Expand Up @@ -42,36 +46,35 @@ export class CoingeckoPriceRepository implements Findable<Price> {

const chunks = splitIntoChunks(addresses);

const response = await Promise.all(
chunks.map((chunk) =>
axios
.get<TokenPrices>(this.url(chunk), { signal })
.then(({ data }) => {
return data;
})
.catch((error) => {
const message = ['Error fetching token prices from coingecko'];
if (error.isAxiosError) {
if (error.response?.status) {
message.push(`with status ${error.response.status}`);
}
} else {
message.push(error);
let result: TokenPrices = {};

for (let i = 0; i < chunks.length; i++) {
const chunk = chunks[i];
const repsonse = await axios
.get<TokenPrices>(this.url(chunk), { signal })
.then(({ data }) => {
return data;
})
.catch((error) => {
const message = ['Error fetching token prices from coingecko'];
if (error.isAxiosError) {
if (error.response?.status) {
message.push(`with status ${error.response.status}`);
}
return Promise.reject(message.join(' '));
})
.finally(() => {
console.timeEnd(
`fetching coingecko for ${addresses.length} tokens`
);
})
)
);
} else {
message.push(error);
}
return Promise.reject(message.join(' '));
})
.finally(() => {
console.timeEnd(`fetching coingecko for ${addresses.length} tokens`);
});

let result: TokenPrices = {};
result = { ...result, ...repsonse };

for (const chunk of response) {
result = { ...result, ...chunk };
if (i < chunks.length - 1) {
await wait(200);
}
}

return result;
Expand Down

0 comments on commit 32a32db

Please sign in to comment.