Skip to content

Commit

Permalink
fix: handling null usdValue for balance formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
yeager-eren committed Oct 29, 2024
1 parent 8aae1ea commit ab16aea
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
2 changes: 1 addition & 1 deletion widget/embedded/src/types/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface Wallet {
export type Balance = {
amount: string;
decimals: number;
usdValue: string;
usdValue: string | null;
};

export type Blockchain = string;
Expand Down
29 changes: 15 additions & 14 deletions widget/embedded/src/utils/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ export function resetConnectedWalletState(

export const calculateWalletUsdValue = (balances: BalanceState) => {
const total = Object.values(balances).reduce((prev, balance) => {
return prev.plus(balance.usdValue);
return balance.usdValue ? prev.plus(balance.usdValue) : prev;
}, new BigNumber(ZERO));

return numberWithThousandSeparator(total.toString());
Expand Down Expand Up @@ -331,23 +331,24 @@ export function formatBalance(balance: Balance | null): Balance | null {
const amount = new BigNumber(balance.amount)
.shiftedBy(-balance.decimals)
.toFixed();
const usdValue = new BigNumber(balance.usdValue)
.shiftedBy(-balance.decimals)
.toFixed();
const usdValue = balance.usdValue
? new BigNumber(balance.usdValue).shiftedBy(-balance.decimals).toFixed()
: null;
const formattedAmount = numberToString(
amount,
BALANCE_MIN_DECIMALS,
BALANCE_MAX_DECIMALS
);
// null is using for detecing uknown prices
const formattedUsdValue = usdValue
? numberToString(usdValue, USD_VALUE_MIN_DECIMALS, USD_VALUE_MAX_DECIMALS)
: null;

const formattedBalance: Balance | null = balance
? {
...balance,
amount: numberToString(
amount,
BALANCE_MIN_DECIMALS,
BALANCE_MAX_DECIMALS
),
usdValue: numberToString(
usdValue,
USD_VALUE_MIN_DECIMALS,
USD_VALUE_MAX_DECIMALS
),
amount: formattedAmount,
usdValue: formattedUsdValue,
}
: null;

Expand Down

0 comments on commit ab16aea

Please sign in to comment.