Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for getting a token price for a given currency #144

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading