Skip to content

Commit

Permalink
Adding coingecko config to SDK initialization;
Browse files Browse the repository at this point in the history
  • Loading branch information
Luiz Gustavo Abou Hatem De Liz committed Dec 19, 2023
1 parent b41f83b commit 2290bbb
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 10 deletions.
4 changes: 4 additions & 0 deletions balancer-js/examples/pools/bpt-price.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { BalancerSDK } from '@balancer-labs/sdk';
const sdk = new BalancerSDK({
network: 1,
rpcUrl: 'https://rpc.ankr.com/eth',
coingecko: {
coingeckoApiKey: 'CG-ViHyrfvtLz2WSCJzm59TfGow',
isDemoApiKey: true,
},
});

const bptPriceExample = async () => {
Expand Down
9 changes: 6 additions & 3 deletions balancer-js/src/modules/data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
BalancerNetworkConfig,
BalancerDataRepositories,
GraphQLQuery,
CoingeckoConfig,
} from '@/types';
import { PoolsSubgraphRepository } from './pool/subgraph';
import { SubgraphPoolDataService } from '../sor/pool-data/subgraphPoolDataService';
Expand Down Expand Up @@ -75,7 +76,8 @@ export class Data implements BalancerDataRepositories {
networkConfig: BalancerNetworkConfig,
provider: Provider,
contracts: Contracts,
subgraphQuery?: GraphQLQuery
subgraphQuery?: GraphQLQuery,
coingecko?: CoingeckoConfig
) {
this.pools = new PoolsSubgraphRepository({
url: networkConfig.urls.subgraph,
Expand Down Expand Up @@ -167,7 +169,8 @@ export class Data implements BalancerDataRepositories {

const coingeckoRepository = new CoingeckoPriceRepository(
tokenAddresses,
networkConfig.chainId
networkConfig.chainId,
coingecko
);

const subgraphPriceRepository = new SubgraphPriceRepository(
Expand All @@ -187,7 +190,7 @@ export class Data implements BalancerDataRepositories {
);

const coingeckoHistoricalRepository =
new CoingeckoHistoricalPriceRepository(networkConfig.chainId);
new CoingeckoHistoricalPriceRepository(networkConfig.chainId, coingecko);

this.tokenHistoricalPrices = new HistoricalPriceProvider(
coingeckoHistoricalRepository,
Expand Down
14 changes: 11 additions & 3 deletions balancer-js/src/modules/data/token-prices/coingecko-historical.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
TokenPrices,
Network,
HistoricalPrices,
CoingeckoConfig,
} from '@/types';
import axios, { AxiosError } from 'axios';
import { tokenAddressForPricing } from '@/lib/utils';
Expand All @@ -18,11 +19,15 @@ export class CoingeckoHistoricalPriceRepository implements Findable<Price> {
prices: TokenPrices = {};
nativePrice?: Promise<Price>;
urlBase: string;
apiKey?: string;

constructor(private chainId: Network = 1) {
this.urlBase = `https://api.coingecko.com/api/v3/coins/${this.platform(
constructor(private chainId: Network = 1, coingecko?: CoingeckoConfig) {
this.urlBase = `https://${
coingecko?.coingeckoApiKey && !coingecko.isDemoApiKey ? 'pro-' : ''
}api.coingecko.com/api/v3/coins/${this.platform(
chainId
)}/contract/%TOKEN_ADDRESS%/market_chart/range?vs_currency=usd`;
this.apiKey = coingecko?.coingeckoApiKey;
}

private async fetch(
Expand All @@ -33,7 +38,10 @@ export class CoingeckoHistoricalPriceRepository implements Findable<Price> {
const url = this.urlRange(address, timestamp);
console.time(`fetching coingecko historical for ${address}`);
try {
const { data } = await axios.get<HistoricalPrices>(url, { signal });
const { data } = await axios.get<HistoricalPrices>(url, {
signal,
headers: { 'x-cg-pro-api-key': this.apiKey ?? '' },
});
console.timeEnd(`fetching coingecko historical for ${address}`);
console.log(data);
return data;
Expand Down
13 changes: 10 additions & 3 deletions balancer-js/src/modules/data/token-prices/coingecko.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-empty-function */
import { Findable, Network, Price, TokenPrices } from '@/types';
import { CoingeckoConfig, Findable, Network, Price, TokenPrices } from '@/types';

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

View workflow job for this annotation

GitHub Actions / lint

Replace `·CoingeckoConfig,·Findable,·Network,·Price,·TokenPrices·` with `⏎··CoingeckoConfig,⏎··Findable,⏎··Network,⏎··Price,⏎··TokenPrices,⏎`
import axios, { AxiosError } from 'axios';
import { TOKENS } from '@/lib/constants/tokens';
import { Debouncer, tokenAddressForPricing } from '@/lib/utils';
Expand All @@ -13,16 +13,22 @@ export class CoingeckoPriceRepository implements Findable<Price> {
urlBase: string;
baseTokenAddresses: string[];
debouncer: Debouncer<TokenPrices, string>;
apiKey?: string;

constructor(tokenAddresses: string[], private chainId: Network = 1) {
constructor(
tokenAddresses: string[],
private chainId: Network = 1,
coingecko?: CoingeckoConfig
) {
this.baseTokenAddresses = tokenAddresses.map(tokenAddressForPricing);
this.urlBase = `https://api.coingecko.com/api/v3/simple/token_price/${this.platform(
chainId
)}?vs_currencies=usd,eth`;
this.apiKey = coingecko?.coingeckoApiKey;
this.debouncer = new Debouncer<TokenPrices, string>(
this.fetch.bind(this),
200,
10
coingecko?.tokensPerPriceRequest ?? 10
);
}

Expand All @@ -33,6 +39,7 @@ export class CoingeckoPriceRepository implements Findable<Price> {
try {
const { data } = await axios.get<TokenPrices>(this.url(addresses), {
signal,
headers: { ApiKey: this.apiKey ?? '' },
});
return data;
} catch (error) {
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.coingecko
);

this.swaps = new Swaps(this.config);
Expand Down
7 changes: 7 additions & 0 deletions balancer-js/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export interface BalancerSdkConfig {
sor?: Partial<BalancerSdkSorConfig>;
tenderly?: BalancerTenderlyConfig;
enableLogging?: boolean;
coingecko?: CoingeckoConfig;
}

export interface BalancerTenderlyConfig {
Expand Down Expand Up @@ -460,3 +461,9 @@ export interface GraphQLQuery {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
attrs: any;
}

export type CoingeckoConfig = {
coingeckoApiKey: string;
tokensPerPriceRequest?: number;
isDemoApiKey?: boolean;
};

0 comments on commit 2290bbb

Please sign in to comment.