Skip to content
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
4 changes: 3 additions & 1 deletion src/controllers/TokenStatsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
41 changes: 23 additions & 18 deletions src/services/StatsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export type ExtendedTokenStats = {

export interface IStatsService {
getTokenStats(network: NetworkType): Promise<TokenStats>;
getTokenStatsExtended(network: NetworkType): Promise<ExtendedTokenStats>;
getTokenStatsExtended(network: NetworkType, currencies: string[]): Promise<ExtendedTokenStats[]>;
getTotalSupply(network: NetworkType): Promise<number>;
getTotalIssuanceHistory(network: NetworkType): Promise<TotalSupply[]>;
}
Expand Down Expand Up @@ -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
Copy link

Copilot AI May 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enhance the JSDoc comment for the 'currencies' parameter by specifying the expected currency codes and explaining that the function returns an array of statistics, one per currency.

Copilot uses AI. Check for mistakes.
* @returns Token statistics array including total supply and circulating supply.
*/
public async getTokenStatsExtended(network: NetworkType): Promise<ExtendedTokenStats> {
public async getTokenStatsExtended(network: NetworkType, currencies: string[]): Promise<ExtendedTokenStats[]> {
Copy link

Copilot AI May 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update the documentation comment for getTokenStatsExtended to include details about the new 'currencies' parameter and the updated return type.

Copilot uses AI. Check for mistakes.
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.');
Expand Down