|
| 1 | +import { isNativeError } from "util/types"; |
| 2 | +import axios, { AxiosInstance, isAxiosError } from "axios"; |
| 3 | + |
| 4 | +import { Address, isNativeToken } from "@grants-stack-indexer/shared"; |
| 5 | + |
| 6 | +import { IPricingProvider } from "../interfaces/index.js"; |
| 7 | +import { |
| 8 | + CoingeckoPlatformId, |
| 9 | + CoingeckoPriceChartData, |
| 10 | + CoingeckoSupportedChainId, |
| 11 | + CoingeckoTokenId, |
| 12 | + NetworkException, |
| 13 | + TokenPrice, |
| 14 | + UnknownPricingException, |
| 15 | + UnsupportedChainException, |
| 16 | +} from "../internal.js"; |
| 17 | + |
| 18 | +type CoingeckoOptions = { |
| 19 | + apiKey: string; |
| 20 | + apiType: "demo" | "pro"; |
| 21 | +}; |
| 22 | + |
| 23 | +const getApiTypeConfig = (apiType: "demo" | "pro"): { baseURL: string; authHeader: string } => |
| 24 | + apiType === "demo" |
| 25 | + ? { baseURL: "https://api.coingecko.com/api/v3", authHeader: "x-cg-demo-api-key" } |
| 26 | + : { baseURL: "https://pro-api.coingecko.com/api/v3/", authHeader: "x-cg-pro-api-key" }; |
| 27 | + |
| 28 | +const platforms: { [key in CoingeckoSupportedChainId]: CoingeckoPlatformId } = { |
| 29 | + 1: "ethereum" as CoingeckoPlatformId, |
| 30 | + 10: "optimistic-ethereum" as CoingeckoPlatformId, |
| 31 | + 100: "xdai" as CoingeckoPlatformId, |
| 32 | + 250: "fantom" as CoingeckoPlatformId, |
| 33 | + 42161: "arbitrum-one" as CoingeckoPlatformId, |
| 34 | + 43114: "avalanche" as CoingeckoPlatformId, |
| 35 | + 713715: "sei-network" as CoingeckoPlatformId, |
| 36 | + 1329: "sei-network" as CoingeckoPlatformId, |
| 37 | + 42: "lukso" as CoingeckoPlatformId, |
| 38 | + 42220: "celo" as CoingeckoPlatformId, |
| 39 | + 1088: "metis" as CoingeckoPlatformId, |
| 40 | +}; |
| 41 | + |
| 42 | +const nativeTokens: { [key in CoingeckoSupportedChainId]: CoingeckoTokenId } = { |
| 43 | + 1: "ethereum" as CoingeckoTokenId, |
| 44 | + 10: "ethereum" as CoingeckoTokenId, |
| 45 | + 100: "xdai" as CoingeckoTokenId, |
| 46 | + 250: "fantom" as CoingeckoTokenId, |
| 47 | + 42161: "ethereum" as CoingeckoTokenId, |
| 48 | + 43114: "avalanche-2" as CoingeckoTokenId, |
| 49 | + 713715: "sei-network" as CoingeckoTokenId, |
| 50 | + 1329: "sei-network" as CoingeckoTokenId, |
| 51 | + 42: "lukso-token" as CoingeckoTokenId, |
| 52 | + 42220: "celo" as CoingeckoTokenId, |
| 53 | + 1088: "metis-token" as CoingeckoTokenId, |
| 54 | +}; |
| 55 | + |
| 56 | +/** |
| 57 | + * The Coingecko provider is a pricing provider that uses the Coingecko API to get the price of a token. |
| 58 | + */ |
| 59 | +export class CoingeckoProvider implements IPricingProvider { |
| 60 | + private readonly axios: AxiosInstance; |
| 61 | + |
| 62 | + /** |
| 63 | + * @param options.apiKey - Coingecko API key. |
| 64 | + * @param options.apiType - Coingecko API type (demo or pro). |
| 65 | + */ |
| 66 | + constructor(options: CoingeckoOptions) { |
| 67 | + const { apiKey, apiType } = options; |
| 68 | + const { baseURL, authHeader } = getApiTypeConfig(apiType); |
| 69 | + |
| 70 | + this.axios = axios.create({ |
| 71 | + baseURL, |
| 72 | + headers: { |
| 73 | + common: { |
| 74 | + [authHeader]: apiKey, |
| 75 | + Accept: "application/json", |
| 76 | + }, |
| 77 | + }, |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + /* @inheritdoc */ |
| 82 | + async getTokenPrice( |
| 83 | + chainId: number, |
| 84 | + tokenAddress: Address, |
| 85 | + startTimestampMs: number, |
| 86 | + endTimestampMs: number, |
| 87 | + ): Promise<TokenPrice | undefined> { |
| 88 | + if (!this.isSupportedChainId(chainId)) { |
| 89 | + throw new UnsupportedChainException(chainId); |
| 90 | + } |
| 91 | + |
| 92 | + if (startTimestampMs > endTimestampMs) { |
| 93 | + return undefined; |
| 94 | + } |
| 95 | + |
| 96 | + const startTimestampSecs = Math.floor(startTimestampMs / 1000); |
| 97 | + const endTimestampSecs = Math.floor(endTimestampMs / 1000); |
| 98 | + |
| 99 | + const path = this.getApiPath(chainId, tokenAddress, startTimestampSecs, endTimestampSecs); |
| 100 | + |
| 101 | + //TODO: handle retries |
| 102 | + try { |
| 103 | + const { data } = await this.axios.get<CoingeckoPriceChartData>(path); |
| 104 | + |
| 105 | + const closestEntry = data.prices.at(0); |
| 106 | + if (!closestEntry) { |
| 107 | + return undefined; |
| 108 | + } |
| 109 | + |
| 110 | + return { |
| 111 | + timestampMs: closestEntry[0], |
| 112 | + priceUsd: closestEntry[1], |
| 113 | + }; |
| 114 | + } catch (error: unknown) { |
| 115 | + //TODO: notify |
| 116 | + if (isAxiosError(error)) { |
| 117 | + if (error.status! >= 400 && error.status! < 500) { |
| 118 | + console.error(`Coingecko API error: ${error.message}. Stack: ${error.stack}`); |
| 119 | + return undefined; |
| 120 | + } |
| 121 | + |
| 122 | + if (error.status! >= 500 || error.message === "Network Error") { |
| 123 | + throw new NetworkException(error.message, error.status!); |
| 124 | + } |
| 125 | + } |
| 126 | + console.error(error); |
| 127 | + throw new UnknownPricingException( |
| 128 | + isNativeError(error) ? error.message : JSON.stringify(error), |
| 129 | + isNativeError(error) ? error.stack : undefined, |
| 130 | + ); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + /* |
| 135 | + * @returns Whether the given chain ID is supported by the Coingecko API. |
| 136 | + */ |
| 137 | + private isSupportedChainId(chainId: number): chainId is CoingeckoSupportedChainId { |
| 138 | + return chainId in platforms; |
| 139 | + } |
| 140 | + |
| 141 | + /* |
| 142 | + * @returns The API endpoint path for the given parameters. |
| 143 | + */ |
| 144 | + private getApiPath( |
| 145 | + chainId: CoingeckoSupportedChainId, |
| 146 | + tokenAddress: Address, |
| 147 | + startTimestampSecs: number, |
| 148 | + endTimestampSecs: number, |
| 149 | + ): string { |
| 150 | + const platform = platforms[chainId]; |
| 151 | + const nativeTokenId = nativeTokens[chainId]; |
| 152 | + |
| 153 | + return isNativeToken(tokenAddress) |
| 154 | + ? `/coins/${nativeTokenId}/market_chart/range?vs_currency=usd&from=${startTimestampSecs}&to=${endTimestampSecs}&precision=full` |
| 155 | + : `/coins/${platform}/contract/${tokenAddress.toLowerCase()}/market_chart/range?vs_currency=usd&from=${startTimestampSecs}&to=${endTimestampSecs}&precision=full`; |
| 156 | + } |
| 157 | +} |
0 commit comments