-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replacing CoingeckoTokenPriceService by ApiTokenPriceService to fetch…
… 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
Showing
3 changed files
with
91 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
balancer-js/src/modules/sor/token-price/apiTokenPriceService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
} |