Skip to content

Commit

Permalink
Adding example for bptPrice;
Browse files Browse the repository at this point in the history
Separating coingecko token prices requests by chunks of 10, coingecko is only allowing 10 tokens per request in the free plan, they have a paid plan for more token prices (https://www.coingecko.com/en/api/pricing);
  • Loading branch information
Luiz Gustavo Abou Hatem De Liz committed Nov 28, 2023
1 parent f89012c commit 37e995d
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 22 deletions.
19 changes: 19 additions & 0 deletions balancer-js/examples/pools/bpt-price.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { BalancerSDK } from '@balancer-labs/sdk';

const sdk = new BalancerSDK({
network: 1,
rpcUrl: 'https://rpc.ankr.com/eth',
});

const bptPriceExample = async () => {
const poolId =
'0x26cc136e9b8fd65466f193a8e5710661ed9a98270002000000000000000005ad';
const pool = await sdk.pools.find(poolId);
if (!pool) {
throw new Error('Pool not found');
}
const bptPrice = await sdk.pools.bptPrice(pool);
console.log('bpt price: ', bptPrice);
};

bptPriceExample().catch((error) => console.error(error));
65 changes: 43 additions & 22 deletions balancer-js/src/modules/data/token-prices/coingecko.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-empty-function */
import { Price, Findable, TokenPrices, Network } from '@/types';
import axios from 'axios';
import { Findable, Network, Price, TokenPrices } from '@/types';
import axios, { AxiosError } from 'axios';

Check warning on line 3 in balancer-js/src/modules/data/token-prices/coingecko.ts

View workflow job for this annotation

GitHub Actions / lint

'AxiosError' is defined but never used
import { TOKENS } from '@/lib/constants/tokens';
import { Debouncer, tokenAddressForPricing } from '@/lib/utils';

Expand All @@ -25,30 +25,51 @@ export class CoingeckoPriceRepository implements Findable<Price> {
);
}

private fetch(
private async fetch(
addresses: string[],
{ signal }: { signal?: AbortSignal } = {}
): Promise<TokenPrices> {
console.time(`fetching coingecko for ${addresses.length} tokens`);
return axios
.get<TokenPrices>(this.url(addresses), { 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);
}
return Promise.reject(message.join(' '));
})
.finally(() => {
console.timeEnd(`fetching coingecko for ${addresses.length} tokens`);
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), {
signal,
});
return data;
};

if (addresses.length > maxAddressesAllowedByCoingecko) {
for (let i = 0; i < 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: any) {

Check warning on line 62 in balancer-js/src/modules/data/token-prices/coingecko.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
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 as string);
}
return Promise.reject(message.join(' '));
}
}

private fetchNative({
Expand Down

0 comments on commit 37e995d

Please sign in to comment.