diff --git a/src/controllers/TokenStatsController.ts b/src/controllers/TokenStatsController.ts index bf98a98..8ea0dae 100644 --- a/src/controllers/TokenStatsController.ts +++ b/src/controllers/TokenStatsController.ts @@ -107,7 +107,9 @@ export class TokenStatsController extends ControllerBase implements IControllerB } */ try { - res.json(await this._statsService.getTokenStatsExtended(req.params.network as NetworkType)); + res.json( + await this._statsService.getTokenStatsExtended(req.params.network as NetworkType, ['usd', 'krw']), + ); } catch (err) { this.handleError(res, err as Error); } diff --git a/src/services/StatsService.ts b/src/services/StatsService.ts index 219d2da..0a8402b 100644 --- a/src/services/StatsService.ts +++ b/src/services/StatsService.ts @@ -31,7 +31,7 @@ export type ExtendedTokenStats = { export interface IStatsService { getTokenStats(network: NetworkType): Promise; - getTokenStatsExtended(network: NetworkType): Promise; + getTokenStatsExtended(network: NetworkType, currencies: string[]): Promise; getTotalSupply(network: NetworkType): Promise; getTotalIssuanceHistory(network: NetworkType): Promise; } @@ -82,44 +82,49 @@ export class StatsService extends DappStakingV3IndexerBase implements IStatsServ * Calculates token circulation supply by substracting sum of all token holder accounts * not in circulation from total token supply. * @param network NetworkType (astar or shiden) to calculate token supply for. - * @returns Token statistics including total supply and circulating supply. + * @param currencies + * @returns Token statistics array including total supply and circulating supply. */ - public async getTokenStatsExtended(network: NetworkType): Promise { + public async getTokenStatsExtended(network: NetworkType, currencies: string[]): Promise { if (network !== 'astar' && network !== 'shiden') { throw new Error(`This method is not supported for the network ${network}`); } try { - const currency = 'usd'; - const api = this._apiFactory.getApiInstance(network); const apiClient = await api.getApiPromise(); const chainTokens = apiClient.registry.chainTokens; const tokenSymbol = chainTokens[0]; + const priceRequests = currencies.map((currency) => { + return this._priceProvider.getPrice(tokenSymbol.toLowerCase(), currency); + }); - const [chainDecimals, totalSupply, balancesToExclude, price] = await Promise.all([ + const [chainDecimals, totalSupply, balancesToExclude] = await Promise.all([ api.getChainDecimals(), api.getTotalSupply(), api.getBalances(addressesToExclude), - this._priceProvider.getPrice(tokenSymbol.toLowerCase(), currency), ]); + const prices = await Promise.all(priceRequests); const totalBalancesToExclude = this.getTotalBalanceToExclude(balancesToExclude); const circulatingSupplyWei = totalSupply.sub(totalBalancesToExclude); const circulatingSupply = this.formatBalance(circulatingSupplyWei, chainDecimals); + const lastUpdatedTimestamp = Date.now(); - return { - symbol: tokenSymbol, - currencyCode: currency.toUpperCase(), - price, - marketCap: circulatingSupply * price, - accTradePrice24h: null, - circulatingSupply, - maxSupply: this.formatBalance(totalSupply, chainDecimals), - provider: 'Stake Technologies Pte Ltd', - lastUpdatedTimestamp: Date.now(), - }; + return currencies.map((currency, index) => { + return { + symbol: tokenSymbol, + currencyCode: currency.toUpperCase(), + price: prices[index], + marketCap: circulatingSupply * prices[index], + accTradePrice24h: null, + circulatingSupply, + maxSupply: this.formatBalance(totalSupply, chainDecimals), + provider: 'Stake Technologies Pte Ltd', + lastUpdatedTimestamp, + }; + }); } catch (e) { console.error(e); throw new Error('Unable to fetch token statistics from a node.');