Skip to content

Commit

Permalink
Removing fetchChunk and putting limit on the debouncer;
Browse files Browse the repository at this point in the history
changing the console time label;
  • Loading branch information
Luiz Gustavo Abou Hatem De Liz committed Nov 30, 2023
1 parent e9cdc57 commit 330a452
Showing 1 changed file with 4 additions and 30 deletions.
34 changes: 4 additions & 30 deletions balancer-js/src/modules/data/token-prices/coingecko.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,46 +21,20 @@ export class CoingeckoPriceRepository implements Findable<Price> {
)}?vs_currencies=usd,eth`;
this.debouncer = new Debouncer<TokenPrices, string>(
this.fetch.bind(this),
200
200,
10
);
}

private async fetch(
addresses: string[],
{ signal }: { signal?: AbortSignal } = {}
): Promise<TokenPrices> {
const promises = [];
const maxAddressesAllowedByCoingecko = 10; // Coingecko is only allowing 10 tokens per time

const fetchChunk = async (chunk: string[]): Promise<TokenPrices> => {
const { data } = await axios.get<TokenPrices>(this.url(chunk), {
try {
const { data } = await axios.get<TokenPrices>(this.url(addresses), {
signal,
});
return data;
};

for (
let i = 0;
i < addresses.length / maxAddressesAllowedByCoingecko;
i += 1
) {
promises.push(
fetchChunk(
addresses.slice(
i * maxAddressesAllowedByCoingecko,
(i + 1) * maxAddressesAllowedByCoingecko
)
)
);
}
let tokenPrices: TokenPrices = {};
try {
console.time(`fetching coingecko for ${addresses.length} tokens`);
tokenPrices = (await Promise.all(promises)).reduce((acc, cur) => {
return { ...acc, ...cur };
}, {});
console.timeEnd(`fetching coingecko for ${addresses.length} tokens`);
return tokenPrices;
} catch (error) {
const message = ['Error fetching token prices from coingecko'];
if ((error as AxiosError).isAxiosError) {
Expand Down

0 comments on commit 330a452

Please sign in to comment.