Skip to content

Commit

Permalink
Replacing CoingeckoTokenPriceService by ApiTokenPriceService to fetch…
Browse files Browse the repository at this point in the history
… token prices from the API instead of from coingecko;
  • Loading branch information
Luiz Gustavo Abou Hatem De Liz committed Jan 24, 2024
1 parent 59900fb commit 3a52bf3
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 3 deletions.
25 changes: 25 additions & 0 deletions balancer-js/examples/data/api-token-price-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Display APRs for pool ids hardcoded under `const ids`
* Run command: yarn example ./examples/data/token-prices.ts
*/
import { ApiTokenPriceService } from '@/modules/sor/token-price/apiTokenPriceService';

const dai = '0x6b175474e89094c44da98b954eedeac495271d0f';
const weth = '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2';
const ohm = '0X64AA3364F17A4D01C6F1751FD97C2BD3D7E7F1D5';

(async () => {
const apiTokenPriceService = new ApiTokenPriceService(1);
const daiPriceInEth = await apiTokenPriceService.getNativeAssetPriceInToken(
dai
);
console.log('Dai Price In ETH: ' + daiPriceInEth);
const wethPriceInEth = await apiTokenPriceService.getNativeAssetPriceInToken(
weth
);
console.log('WETH Price In ETH: ' + wethPriceInEth);
const ohmPriceInEth = await apiTokenPriceService.getNativeAssetPriceInToken(
ohm
);
console.log('OHM Price In ETH: ' + ohmPriceInEth);
})();
5 changes: 2 additions & 3 deletions balancer-js/src/modules/sor/sor.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { SOR, SorConfig, TokenPriceService } from '@balancer-labs/sor';
import { Provider, JsonRpcProvider } from '@ethersproject/providers';
import { SubgraphPoolDataService } from './pool-data/subgraphPoolDataService';
import { CoingeckoTokenPriceService } from './token-price/coingeckoTokenPriceService';
import {
SubgraphClient,
createSubgraphClient,
Expand All @@ -14,6 +13,7 @@ import {
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';

export class Sor extends SOR {
constructor(sdkConfig: BalancerSdkConfig) {
Expand Down Expand Up @@ -99,7 +99,6 @@ export class Sor extends SOR {
network.addresses.tokens.wrappedNativeAsset
);
}

return new CoingeckoTokenPriceService(network.chainId);
return new ApiTokenPriceService(network.chainId);
}
}
64 changes: 64 additions & 0 deletions balancer-js/src/modules/sor/token-price/apiTokenPriceService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { TokenPriceService } from '@balancer-labs/sor';
import { gql, request } from 'graphql-request';
import { Network } from '@/types';

export class ApiTokenPriceService implements TokenPriceService {
private chainKey: string;

private balancerApiUrl = 'https://api-v3.balancer.fi/';

private tokenPriceQuery = gql`
query queryTokenPrices($chainKey: GqlChain!) {
tokenGetCurrentPrices(chains: [$chainKey]) {
address
price
}
}
`;

constructor(private readonly chainId: number) {
this.chainKey = Network[chainId];
}
async getNativeAssetPriceInToken(tokenAddress: string): Promise<string> {
const { tokenGetCurrentPrices: tokenPrices } = await request(
this.balancerApiUrl,
this.tokenPriceQuery,
{
chainKey: this.chainKey,
}
);
const tokenPriceUsd = (
tokenPrices as { address: string; price: number }[]
).find(
({ address }) => address.toLowerCase() === tokenAddress.toLowerCase()
);
if (!tokenPriceUsd) {
throw new Error('Token Price not found in the API');
}
const nativeAssetPriceUsd = (
tokenPrices as { address: string; price: number }[]
).find(
({ address }) =>
address.toLowerCase() ===
NativeAssetAddress[this.chainKey as keyof typeof NativeAssetAddress]
);
if (!nativeAssetPriceUsd) {
throw new Error('Native Token Price not found in the API');
}
const tokenPriceInNativeAsset =
tokenPriceUsd.price / nativeAssetPriceUsd.price;
return String(tokenPriceInNativeAsset);
}
}

enum NativeAssetAddress {
MAINNET = '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee',
POLYGON = '0x0000000000000000000000000000000000001010',
ARBITRUM = '0x912ce59144191c1204e64559fe8253a0e49e6548',
AVALANCHE = '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee',
BASE = '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee',
FANTOM = '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee',
GNOSIS = '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee',
OPTIMISM = '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee',
ZKEVM = '0xa2036f0538221a77a3937f1379699f44945018d0',
}

0 comments on commit 3a52bf3

Please sign in to comment.