diff --git a/src/controllers/TokenStatsController.ts b/src/controllers/TokenStatsController.ts index 61645a6..fbb18cf 100644 --- a/src/controllers/TokenStatsController.ts +++ b/src/controllers/TokenStatsController.ts @@ -63,7 +63,8 @@ export class TokenStatsController extends ControllerBase implements IControllerB } */ try { - res.json(await this._priceProvider.getUsdPrice(req.params.symbol)); + const currency = req.query.currency as string | undefined; + res.json(await this._priceProvider.getPrice(req.params.symbol, currency)); } catch (err) { this.handleError(res, err as Error); } diff --git a/src/services/CoinGeckoPriceProvider.ts b/src/services/CoinGeckoPriceProvider.ts index 64b7f30..949664e 100644 --- a/src/services/CoinGeckoPriceProvider.ts +++ b/src/services/CoinGeckoPriceProvider.ts @@ -14,15 +14,15 @@ export class CoinGeckoPriceProvider implements IPriceProvider { public static BaseUrl = 'https://api.coingecko.com/api/v3'; private static tokens: CoinGeckoTokenInfo[]; - public async getUsdPrice(symbol: string): Promise { + public async getPrice(symbol: string, currency = 'usd'): Promise { const tokenSymbol = await this.getTokenId(symbol); if (tokenSymbol) { - const url = `${CoinGeckoPriceProvider.BaseUrl}/simple/price?ids=${tokenSymbol}&vs_currencies=usd`; + const url = `${CoinGeckoPriceProvider.BaseUrl}/simple/price?ids=${tokenSymbol}&vs_currencies=${currency}`; const result = await axios.get(url); if (result.data[tokenSymbol]) { - const price = result.data[tokenSymbol].usd; + const price = result.data[tokenSymbol][currency]; return Number(price); } } diff --git a/src/services/DiaDataPriceProvider.ts b/src/services/DiaDataPriceProvider.ts index 08d6f36..84ebd46 100644 --- a/src/services/DiaDataPriceProvider.ts +++ b/src/services/DiaDataPriceProvider.ts @@ -9,7 +9,7 @@ import { IPriceProvider } from './IPriceProvider'; export class DiaDataPriceProvider implements IPriceProvider { public static BaseUrl = 'https://api.diadata.org/v1/quotation'; - public async getUsdPrice(symbol: string): Promise { + public async getPrice(symbol: string): Promise { const url = `${DiaDataPriceProvider.BaseUrl}/${symbol}`; const result = await axios.get(url); diff --git a/src/services/IPriceProvider.ts b/src/services/IPriceProvider.ts index d0523e8..a44b713 100644 --- a/src/services/IPriceProvider.ts +++ b/src/services/IPriceProvider.ts @@ -6,5 +6,5 @@ export interface IPriceProvider { * Gets current token price in USD. * @param tokenInfo Token information. */ - getUsdPrice(symbol: string): Promise; + getPrice(symbol: string, currency: string | undefined): Promise; } diff --git a/src/services/PriceProviderWithFailover.ts b/src/services/PriceProviderWithFailover.ts index 7c4015b..a2ed109 100644 --- a/src/services/PriceProviderWithFailover.ts +++ b/src/services/PriceProviderWithFailover.ts @@ -18,18 +18,19 @@ export class PriceProviderWithFailover implements IPriceProvider { * @param tokenInfo Token information. * @returns Token price or 0 if unable to fetch price. */ - public async getUsdPrice(symbol: string): Promise { + public async getPrice(symbol: string, currency = 'usd'): Promise { Guard.ThrowIfUndefined('symbol', symbol); const providers = container.getAll(ContainerTypes.PriceProvider); + const cacheKey = `${symbol}-${currency}`; for (const provider of providers) { try { - const cacheItem = this.priceCache.getItem(symbol); + const cacheItem = this.priceCache.getItem(cacheKey); if (cacheItem) { return cacheItem; } else { - const price = await provider.getUsdPrice(symbol); - this.priceCache.setItem(symbol, price); + const price = await provider.getPrice(symbol, currency); + this.priceCache.setItem(cacheKey, price); return price; }