Skip to content

Commit

Permalink
fix: pass selected addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
KedziaPawel committed Feb 27, 2024
1 parent 64a134f commit 6ca0201
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 32 deletions.
11 changes: 7 additions & 4 deletions balancer-js/src/modules/data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ export class Data implements BalancerDataRepositories {
networkConfig: BalancerNetworkConfig,
provider: Provider,
contracts: Contracts,
subgraphQuery?: GraphQLQuery
subgraphQuery?: GraphQLQuery,
coingeckoTokenAddresses?: string[]
) {
this.pools = new PoolsSubgraphRepository({
url: networkConfig.urls.subgraph,
Expand Down Expand Up @@ -160,9 +161,11 @@ export class Data implements BalancerDataRepositories {
});
}

const tokenAddresses = initialCoingeckoList
.filter((t) => t.chainId == networkConfig.chainId)
.map((t) => t.address);
const tokenAddresses =
coingeckoTokenAddresses ??
initialCoingeckoList
.filter((t) => t.chainId == networkConfig.chainId)
.map((t) => t.address);

const coingeckoRepository = new CoingeckoPriceRepository(
tokenAddresses,
Expand Down
56 changes: 29 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,10 +13,6 @@ 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 @@ -46,32 +42,38 @@ export class CoingeckoPriceRepository implements Findable<Price> {

const chunks = splitIntoChunks(addresses);

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 }) => 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}, for chunk ${i}`
);
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}, all addresses ${addresses.length}`
);
}
} else {
message.push(error);
}
} else {
message.push(error);
}
return Promise.reject(message.join(' '));
});
return Promise.reject(message.join(' '));
})
.finally(() => {
console.timeEnd(
`fetching coingecko for ${addresses.length} tokens`
);
})
)
);

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

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

return result;
Expand Down
3 changes: 2 additions & 1 deletion balancer-js/src/modules/sdk.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ export class BalancerSDK implements BalancerSDKRoot {
this.networkConfig,
sor.provider,
this.balancerContracts,
config.subgraphQuery
config.subgraphQuery,
config.coingeckoTokenAddresses
);

this.swaps = new Swaps(this.config);
Expand Down
1 change: 1 addition & 0 deletions balancer-js/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export interface BalancerSdkConfig {
//optionally overwrite parts of the standard SOR config
sor?: Partial<BalancerSdkSorConfig>;
tenderly?: BalancerTenderlyConfig;
coingeckoTokenAddresses?: string[];
enableLogging?: boolean;
}

Expand Down

0 comments on commit 6ca0201

Please sign in to comment.