11import { useMemo } from "react" ;
22
33import { useQuery } from "@tanstack/react-query" ;
4+ import { formatUnits } from "viem" ;
45
56import chain from "@exactly/common/generated/chain" ;
67import { floatingDepositRates , withdrawLimit } from "@exactly/lib" ;
@@ -109,7 +110,7 @@ export default function usePortfolio(options?: { sortBy?: "usdcFirst" | "usdValu
109110 return tokens
110111 . filter ( ( token ) => ! marketAddresses . has ( token . address . toLowerCase ( ) ) )
111112 . flatMap ( ( token ) => {
112- const asset = toExternalAsset ( token , chain . id ) ;
113+ const asset = toExternalAsset ( token ) ;
113114 return asset ? [ asset ] : [ ] ;
114115 } ) ;
115116 } , [ balances , markets ] ) ;
@@ -121,7 +122,7 @@ export default function usePortfolio(options?: { sortBy?: "usdcFirst" | "usdValu
121122 const chainId = Number ( chainIdKey ) ;
122123 if ( ! Number . isInteger ( chainId ) || chainId === chain . id ) continue ;
123124 for ( const token of tokens ) {
124- const asset = toExternalAsset ( token , chainId ) ;
125+ const asset = toExternalAsset ( token ) ;
125126 if ( asset ) result . push ( asset ) ;
126127 }
127128 }
@@ -171,20 +172,9 @@ function compareAssets(sortBy: "usdcFirst" | "usdValue" | undefined) {
171172 } ;
172173}
173174
174- function toExternalAsset ( token : TokenAmount , chainId : number ) : ExternalAsset | undefined {
175- if ( ! token . amount || token . amount <= 0n ) return undefined ;
176- const rawUsd = ( Number ( token . priceUSD ) * Number ( token . amount ) ) / 10 ** token . decimals ;
175+ function toExternalAsset ( token : TokenAmount ) : ExternalAsset | undefined {
176+ if ( ! token . amount ) return undefined ;
177+ const rawUsd = Number ( formatUnits ( token . amount , token . decimals ) ) * Number ( token . priceUSD ) ;
177178 const usdValue = Number . isFinite ( rawUsd ) && rawUsd > 0 ? rawUsd : 0 ;
178- return {
179- address : token . address ,
180- amount : token . amount ,
181- chainId,
182- decimals : token . decimals ,
183- logoURI : token . logoURI ,
184- name : token . name ,
185- priceUSD : token . priceUSD ,
186- symbol : token . symbol ,
187- type : "external" ,
188- usdValue,
189- } ;
179+ return { ...token , type : "external" , usdValue } ;
190180}
0 commit comments