Skip to content

Commit

Permalink
Support for getting a token price for a given currency (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
bobo-k2 authored Mar 7, 2024
1 parent 7a6168e commit 54e21fe
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/controllers/TokenStatsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions src/services/CoinGeckoPriceProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number> {
public async getPrice(symbol: string, currency = 'usd'): Promise<number> {
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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/DiaDataPriceProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number> {
public async getPrice(symbol: string): Promise<number> {
const url = `${DiaDataPriceProvider.BaseUrl}/${symbol}`;
const result = await axios.get(url);

Expand Down
2 changes: 1 addition & 1 deletion src/services/IPriceProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ export interface IPriceProvider {
* Gets current token price in USD.
* @param tokenInfo Token information.
*/
getUsdPrice(symbol: string): Promise<number>;
getPrice(symbol: string, currency: string | undefined): Promise<number>;
}
9 changes: 5 additions & 4 deletions src/services/PriceProviderWithFailover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number> {
public async getPrice(symbol: string, currency = 'usd'): Promise<number> {
Guard.ThrowIfUndefined('symbol', symbol);

const providers = container.getAll<IPriceProvider>(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;
}
Expand Down

0 comments on commit 54e21fe

Please sign in to comment.