Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removing fetchChunk and putting limit on the debouncer; #550

Merged
merged 1 commit into from
Nov 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading