Skip to content

Commit 2db4dd6

Browse files
committed
🐛 app: avoid precision loss in external asset value
1 parent 21289c6 commit 2db4dd6

2 files changed

Lines changed: 12 additions & 17 deletions

File tree

.changeset/purple-hats-occur.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@exactly/mobile": patch
3+
---
4+
5+
🐛 avoid precision loss in external asset value

src/utils/usePortfolio.ts

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useMemo } from "react";
22

33
import { useQuery } from "@tanstack/react-query";
4+
import { formatUnits } from "viem";
45

56
import chain from "@exactly/common/generated/chain";
67
import { 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

Comments
 (0)