Skip to content

Commit

Permalink
use coingecko pro api for gecko terminal
Browse files Browse the repository at this point in the history
  • Loading branch information
jiaqi-aperture committed Jan 10, 2025
1 parent fd5d741 commit 3b3f1f8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aperture_finance/uniswap-v3-automation-sdk",
"version": "3.14.3",
"version": "3.14.3-jiaqi.4",
"description": "SDK for Aperture's CLMM automation platform",
"author": "Aperture Finance <engineering@aperture.finance>",
"license": "MIT",
Expand Down
8 changes: 5 additions & 3 deletions src/price.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export const Q192 = Q96.times(Q96);
// timestamp in milliseconds, and the second element is the price at that timestamp.
export type CoingeckoHistoricalPriceDatapoint = [number, number];

export const GECKO_TERMINAL_URL = 'https://api.geckoterminal.com/api/v2';
export const COINGECKO_PRO_URL = 'https://pro-api.coingecko.com/api/v3';
// The proxy server adds a Coingecko Pro API key to the request header, and forwards the request to pro-api.coingecko.com.
// This is intended to be used by the frontend to avoid exposing the API key.
Expand Down Expand Up @@ -168,6 +167,7 @@ export async function getTokenPriceListFromCoingeckoWithAddresses(
* Fetches tokens' current price from Gecko Terminal in a batch.
* @param chainId The chain id.
* @param tokens The checksum addresses of tokens to fetch price information for. All tokens must have the same chain id. The number of tokens cannot be too big, exact threshold unknown but 50 should be safe; otherwise only some tokens will be fetched.
* @param apiKey The Coingecko API key to use. Use the free api if not specified.
* @returns The tokens' current USD price. For example,
* {
* 0xbe9895146f7af43049ca1c1ae358b0541ea49704: 1783.17,
Expand All @@ -177,15 +177,16 @@ export async function getTokenPriceListFromCoingeckoWithAddresses(
export async function getTokenPriceListFromGeckoTerminalWithAddresses(
chainId: ApertureSupportedChainId,
tokens: string[],
apiKey?: string,
): Promise<{ [address: string]: number | null }> {
const { gecko_terminal_platform_id } = getChainInfo(chainId);
if (gecko_terminal_platform_id === undefined) return {};
const addresses = tokens.toString();
const priceResponse: AxiosResponse = await axios.get(
`${GECKO_TERMINAL_URL}/simple/networks/${gecko_terminal_platform_id}/token_price/${addresses}`,
`${apiKey ? COINGECKO_PRO_URL : COINGECKO_PROXY_URL}/onchain/simple/networks/${gecko_terminal_platform_id}/token_price/${addresses}`,
);
const responseData = priceResponse.data.data.attributes.token_prices;
// Coingecko call example: https://{GECKO_TERMINAL_URL}/simple/networks/eth/token_price/0x15D4c048F83bd7e37d49eA4C83a07267Ec4203dA,0xF433089366899D83a9f26A773D59ec7eCF30355e,0x04abEdA201850aC0124161F037Efd70c74ddC74C,0xA4EED63db85311E22dF4473f87CcfC3DaDCFA3E3,0x1985365e9f78359a9B6AD760e32412f4a445E862
// Coingecko call example: https://{COINGECKO_URL}/onchain/simple/networks/eth/token_price/0x15D4c048F83bd7e37d49eA4C83a07267Ec4203dA,0xF433089366899D83a9f26A773D59ec7eCF30355e,0x04abEdA201850aC0124161F037Efd70c74ddC74C
return Object.keys(responseData).reduce(
(obj: { [address: string]: number | null }, address: string) => {
obj[address] = responseData[address]
Expand Down Expand Up @@ -227,6 +228,7 @@ export async function getTokenPriceListWithAddresses(
await getTokenPriceListFromGeckoTerminalWithAddresses(
chainId,
noCoingeckoPriceTokens,
apiKey,
);
return Object.assign(coingeckoPriceList, geckoTerminalPriceList);
}
Expand Down

0 comments on commit 3b3f1f8

Please sign in to comment.