Skip to content

Commit

Permalink
Adding 'api' tokenPriceService type;
Browse files Browse the repository at this point in the history
Making 'api' the default token price service;
Applying the coingecko config to the coingeckoTokenPriceService;
bugfixing the token-prices/coingecko module to work correctly with the coingeckoConfig;
  • Loading branch information
Luiz Gustavo Abou Hatem De Liz committed Jan 26, 2024
1 parent 3a52bf3 commit 124e3dd
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 14 deletions.
18 changes: 12 additions & 6 deletions balancer-js/src/modules/data/token-prices/coingecko.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export class CoingeckoPriceRepository implements Findable<Price> {
prices: { [key: string]: Promise<Price> } = {};
nativePrice?: Promise<Price>;
urlBase: string;
urlBaseNative: string;
baseTokenAddresses: string[];
debouncer: Debouncer<TokenPrices, string>;
apiKey?: string;
Expand All @@ -27,9 +28,14 @@ export class CoingeckoPriceRepository implements Findable<Price> {
coingecko?: CoingeckoConfig
) {
this.baseTokenAddresses = tokenAddresses.map(tokenAddressForPricing);
this.urlBase = `https://api.coingecko.com/api/v3/simple/token_price/${this.platform(
this.urlBase = `https://${
coingecko?.coingeckoApiKey && !coingecko.isDemoApiKey ? 'pro-' : ''
}api.coingecko.com/api/v3/simple/token_price/${this.platform(
chainId
)}?vs_currencies=usd,eth`;
this.urlBaseNative = `https://${
coingecko?.coingeckoApiKey && !coingecko.isDemoApiKey ? 'pro-' : ''
}api.coingecko.com/api/v3/simple/price/?vs_currencies=eth,usd&ids=`;
this.apiKey = coingecko?.coingeckoApiKey;
this.debouncer = new Debouncer<TokenPrices, string>(
this.fetch.bind(this),
Expand All @@ -45,7 +51,7 @@ export class CoingeckoPriceRepository implements Findable<Price> {
try {
const { data } = await axios.get<TokenPrices>(this.url(addresses), {
signal,
headers: { ApiKey: this.apiKey ?? '' },
headers: { 'x-cg-pro-api-key': this.apiKey ?? '' },
});
return data;
} catch (error) {
Expand Down Expand Up @@ -74,10 +80,10 @@ export class CoingeckoPriceRepository implements Findable<Price> {
if (this.chainId === 137) assetId = Assets.MATIC;
if (this.chainId === 100) assetId = Assets.XDAI;
return axios
.get<{ [key in Assets]: Price }>(
`https://api.coingecko.com/api/v3/simple/price/?vs_currencies=eth,usd&ids=${assetId}`,
{ signal }
)
.get<{ [key in Assets]: Price }>(`${this.urlBaseNative}${assetId}`, {
signal,
headers: { 'x-cg-pro-api-key': this.apiKey ?? '' },
})
.then(({ data }) => {
return data[assetId];
})
Expand Down
15 changes: 11 additions & 4 deletions balancer-js/src/modules/sor/sor.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import {
BalancerNetworkConfig,
BalancerSdkConfig,
BalancerSdkSorConfig,
CoingeckoConfig,
} from '@/types';
import { SubgraphTokenPriceService } from './token-price/subgraphTokenPriceService';
import { getNetworkConfig } from '@/modules/sdk.helpers';
import { POOLS_TO_IGNORE } from '@/lib/constants/poolsToIgnore';
import { ApiTokenPriceService } from '@/modules/sor/token-price/apiTokenPriceService';
import { CoingeckoTokenPriceService } from '@/modules/sor/token-price/coingeckoTokenPriceService';

export class Sor extends SOR {
constructor(sdkConfig: BalancerSdkConfig) {
Expand All @@ -36,15 +38,16 @@ export class Sor extends SOR {
const tokenPriceService = Sor.getTokenPriceService(
network,
sorConfig,
subgraphClient
subgraphClient,
sdkConfig.coingecko
);

super(provider, sorNetworkConfig, poolDataService, tokenPriceService);
}

private static getSorConfig(config: BalancerSdkConfig): BalancerSdkSorConfig {
return {
tokenPriceService: 'coingecko',
tokenPriceService: 'api',
poolDataService: 'subgraph',
fetchOnChainBalances: true,
...config.sor,
Expand Down Expand Up @@ -89,12 +92,16 @@ export class Sor extends SOR {
private static getTokenPriceService(
network: BalancerNetworkConfig,
sorConfig: BalancerSdkSorConfig,
subgraphClient: SubgraphClient
subgraphClient: SubgraphClient,
coingeckoConfig?: CoingeckoConfig
): TokenPriceService {
if (sorConfig.tokenPriceService === 'coingecko' && coingeckoConfig) {
return new CoingeckoTokenPriceService(network.chainId, coingeckoConfig);
}
if (typeof sorConfig.tokenPriceService === 'object') {
return sorConfig.tokenPriceService;
} else if (sorConfig.tokenPriceService === 'subgraph') {
new SubgraphTokenPriceService(
return new SubgraphTokenPriceService(
subgraphClient,
network.addresses.tokens.wrappedNativeAsset
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import { TokenPriceService } from '@balancer-labs/sor';
import axios from 'axios';
import { BALANCER_NETWORK_CONFIG } from '@/lib/constants/config';
import { Network, BalancerNetworkConfig } from '@/types';
import { Network, BalancerNetworkConfig, CoingeckoConfig } from '@/types';

export class CoingeckoTokenPriceService implements TokenPriceService {
constructor(private readonly chainId: number) {}
private urlBase: string;
private apiKey: string;
constructor(private readonly chainId: number, coingecko: CoingeckoConfig) {
this.urlBase = `https://${
coingecko?.coingeckoApiKey && !coingecko.isDemoApiKey ? 'pro-' : ''
}api.coingecko.com/api/v3/simple/token_price/${
this.platformId
}?vs_currencies=${this.nativeAssetId}`;
this.apiKey = coingecko.coingeckoApiKey;
}

public async getNativeAssetPriceInToken(
tokenAddress: string
Expand All @@ -22,12 +31,13 @@ export class CoingeckoTokenPriceService implements TokenPriceService {
* @returns the price of 1 ETH in terms of the token base units
*/
async getTokenPriceInNativeAsset(tokenAddress: string): Promise<string> {
const endpoint = `https://api.coingecko.com/api/v3/simple/token_price/${this.platformId}?contract_addresses=${tokenAddress}&vs_currencies=${this.nativeAssetId}`;
const endpoint = `${this.urlBase}&contract_addresses=${tokenAddress}`;

const { data } = await axios.get(endpoint, {
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
'x-cg-pro-api-key': this.apiKey ?? '',
},
});

Expand Down
2 changes: 1 addition & 1 deletion balancer-js/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export interface BalancerTenderlyConfig {
export interface BalancerSdkSorConfig {
//use a built-in service or provide a custom implementation of a TokenPriceService
//defaults to coingecko
tokenPriceService: 'coingecko' | 'subgraph' | TokenPriceService;
tokenPriceService: 'api' | 'coingecko' | 'subgraph' | TokenPriceService;
//use a built-in service or provide a custom implementation of a PoolDataService
//defaults to subgraph
poolDataService: 'subgraph' | PoolDataService;
Expand Down

0 comments on commit 124e3dd

Please sign in to comment.