Skip to content

Commit

Permalink
Chore/MGX-1255/fix token portfolio (#239)
Browse files Browse the repository at this point in the history
* chore: fix token network portfolio endpoint

* chore: cleanup
  • Loading branch information
olgakonsta authored Sep 16, 2024
1 parent d1b7083 commit 16f1673
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions stash/src/controller/tokenNetworkPortfolioController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { BN } from '@polkadot/util'
import { priceDiscovery } from '../service/PriceDiscoveryService.js'
import { Decimal } from 'decimal.js'
import { fromBN } from 'gasp-sdk'
import logger from '../util/Logger.js'

export const tokenNetworkPortfolio = async (req: Request, res: Response) => {
try {
Expand All @@ -14,9 +15,10 @@ export const tokenNetworkPortfolio = async (req: Request, res: Response) => {
)
const portfolioBalance = accountBalances.map(
async ([storageKey, value]) => {
const free = JSON.parse(JSON.stringify(value)).free.toString()
const free = JSON.parse(JSON.stringify(value.toHuman())).free.toString()
const freeWithoutCommas = free.replace(/,/g, '')
const frozen = JSON.parse(JSON.stringify(value)).frozen.toString()
const freeTokens = new BN(free)
const freeTokens = new BN(freeWithoutCommas)
const frozenTokens = new BN(frozen)
const freeBalance = freeTokens.sub(frozenTokens)
const tokenId = storageKey.args[1].toString()
Expand All @@ -25,8 +27,15 @@ export const tokenNetworkPortfolio = async (req: Request, res: Response) => {
).toHuman() as {
symbol: string
}
const tokenBalanceInUsd = (await priceDiscovery(tokenInfo.symbol))
.current_price['usd']

let tokenBalanceInUsd: string
try {
tokenBalanceInUsd = (await priceDiscovery(tokenInfo.symbol))
.current_price['usd']
} catch (error) {
logger.error('Error fetching token balance in USD:', error)
tokenBalanceInUsd = '0'
}

new Decimal(freeBalance.toString()).mul(new Decimal(tokenBalanceInUsd))
return {
Expand Down

0 comments on commit 16f1673

Please sign in to comment.