Skip to content

Commit

Permalink
fix: pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
0xnigir1 committed Jul 29, 2024
1 parent b290492 commit 48b22a0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions libs/pricing/src/services/coingecko.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ describe("CoingeckoService", () => {
params: {
vs_currencies: currency,
ids: tokenIds.join(","),
precision: service["DECIMALS_PRECISION"].toString(),
},
headers: {
"x-cg-pro-api-key": apiKey,
Expand Down
29 changes: 27 additions & 2 deletions libs/pricing/src/services/coingecko.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,56 @@ import { ApiNotAvailable, RateLimitExceeded } from "@zkchainhub/pricing/exceptio
import { IPricingService } from "@zkchainhub/pricing/interfaces";
import { TokenPrices } from "@zkchainhub/pricing/types/tokenPrice.type";

/**
* Service for fetching token prices from Coingecko API.
*/
@Injectable()
export class CoingeckoService implements IPricingService {
private readonly logger = new Logger(CoingeckoService.name);

private readonly AUTH_HEADER = "x-cg-pro-api-key";
private readonly DECIMALS_PRECISION = 3;

/**
*
* @param apiKey * @param apiKey - Coingecko API key.
* @param apiBaseUrl - Base URL for Coingecko API. If you have a Pro account, you can use the Pro API URL.
*/
constructor(
private readonly apiKey: string,
private readonly apiBaseUrl: string = "https://api.coingecko.com/api/v3/",
private readonly httpService: HttpService,
) {}

/**
* @param tokenIds - An array of Coingecko Tokens IDs.
* @param config.currency - The currency in which the prices should be returned (default: "usd").
*/
async getTokenPrices(
tokenIds: string[],
config: { currency: string } = { currency: "usd" },
): Promise<Record<string, number>> {
const { currency } = config;
return this.get<TokenPrices>("/simple/price", {
return this.httpGet<TokenPrices>("/simple/price", {
vs_currencies: currency,
ids: tokenIds.join(","),
precision: this.DECIMALS_PRECISION.toString(),
}).then((data) => {
return Object.fromEntries(Object.entries(data).map(([key, value]) => [key, value.usd]));
});
}

private async get<ResponseType>(endpoint: string, params: Record<string, string> = {}) {
/**
* HTTP GET wrapper to perform a GET request to the specified endpoint with optional parameters.
* Also injects the API key and sets the Accept header to "application/json".
* @param endpoint - The endpoint to send the GET request to.
* @param params - Optional parameters to include in the request.
* @returns A promise that resolves to the response data.
* @throws {ApiNotAvailable} If the Coingecko API is not available (status code >= 500).
* @throws {RateLimitExceeded} If the rate limit for the API is exceeded (status code 429).
* @throws {Error} If an error occurs while fetching data or a non-network related error occurs.
*/
private async httpGet<ResponseType>(endpoint: string, params: Record<string, string> = {}) {
try {
const response = await this.httpService.axiosRef.get<ResponseType>(
`${this.apiBaseUrl}${endpoint}`,
Expand Down

0 comments on commit 48b22a0

Please sign in to comment.