Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't round balances #2852

Merged
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
10 changes: 3 additions & 7 deletions wormhole-connect/src/hooks/useGetTokenBalances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@ import { useDispatch, useSelector } from 'react-redux';
import { RootState } from 'store';
import { TokenId } from 'sdklegacy';
import { useEffect, useState } from 'react';
import {
accessBalance,
Balances,
formatBalance,
updateBalances,
} from 'store/transferInput';
import { accessBalance, Balances, updateBalances } from 'store/transferInput';
import config, { getWormholeContextV2 } from 'config';
import { TokenConfig } from 'config/types';
import { formatAmount } from 'utils/amount';
import { chainToPlatform } from '@wormhole-foundation/sdk-base';
import { getTokenBridgeWrappedTokenAddress } from 'utils/sdkv2';
import { Chain, TokenAddress } from '@wormhole-foundation/sdk';
Expand Down Expand Up @@ -133,7 +129,7 @@ const useGetTokenBalances = (
const balance = result[tokenAddress];
let formatted: string | null = null;
if (balance !== null) {
formatted = formatBalance(chain, tokenConfig, BigInt(balance));
formatted = formatAmount(chain, tokenConfig, balance);
}
updatedBalances[tokenConfig.key] = {
balance: formatted,
Expand Down
21 changes: 1 addition & 20 deletions wormhole-connect/src/store/transferInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { Context } from 'sdklegacy';
import config from 'config';
import { TokenConfig } from 'config/types';
import { getTokenDecimals } from 'utils';
import {
switchChain,
TransferWallet,
Expand All @@ -16,7 +15,7 @@ import {
getEmptyDataWrapper,
receiveDataWrapper,
} from './helpers';
import { amount, Chain } from '@wormhole-foundation/sdk';
import { Chain } from '@wormhole-foundation/sdk';

export type Balance = {
lastUpdated: number;
Expand All @@ -30,24 +29,6 @@ export type BalancesCache = { [key in Chain]?: ChainBalances };
type WalletAddress = string;
export type WalletBalances = { [key: WalletAddress]: BalancesCache };

export const formatBalance = (
chain: Chain,
token: TokenConfig,
balance: string | bigint | null,
): string | null => {
if (!balance) {
return null;
}
const decimals = getTokenDecimals(chain, token.tokenId);
const balanceNum = amount.whole({
amount: balance.toString(),
decimals,
});
return balanceNum.toLocaleString('en', {
maximumFractionDigits: 6,
});
};

export const formatStringAmount = (amountStr = '0'): string => {
const amountNum = parseFloat(amountStr);
return amountNum.toLocaleString('en', {
Expand Down
24 changes: 24 additions & 0 deletions wormhole-connect/src/utils/amount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { getTokenDecimals } from 'utils';
import { amount, Chain } from '@wormhole-foundation/sdk';
import { TokenConfig } from 'config/types';

export const formatAmount = (
chain: Chain,
token: TokenConfig,
val: string | bigint | null,
truncate?: number,
): string | null => {
if (!val) {
return null;
}

const decimals = getTokenDecimals(chain, token.tokenId);

let balanceAmount = amount.fromBaseUnits(BigInt(val), decimals);

if (truncate) {
balanceAmount = amount.truncate(balanceAmount, truncate);
}

return amount.display(balanceAmount);
};
2 changes: 1 addition & 1 deletion wormhole-connect/src/utils/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function interpretTransferError(
uiErrorMessage = 'Transfer timed out, please try again';
internalErrorCode = ERR_TIMEOUT;
} else if (InsufficientFundsForGasError.MESSAGE_REGEX.test(e?.message)) {
uiErrorMessage = e.message;
uiErrorMessage = 'Insufficient funds';
internalErrorCode = ERR_INSUFFICIENT_GAS;
} else if (USER_REJECTED_REGEX.test(e?.message)) {
uiErrorMessage = 'Transfer rejected in wallet, please try again';
Expand Down
7 changes: 5 additions & 2 deletions wormhole-connect/src/views/v2/Bridge/AmountInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ const AmountInput = (props: Props) => {
);

const tokenBalance = useMemo(
() => balances?.[sourceToken]?.balance || '',
() => balances?.[sourceToken]?.balance || '0',
[balances, sourceToken],
);

Expand All @@ -151,12 +151,15 @@ const AmountInput = (props: Props) => {
{isFetching ? (
<CircularProgress size={14} />
) : (
// TODO AMOUNT HACK... fix amount formatting in amount.Amount balance refactor
<Typography
fontSize={14}
textAlign="right"
className={classes.balance}
>
{tokenBalance}
{parseFloat(tokenBalance).toLocaleString('en', {
maximumFractionDigits: 6,
})}
</Typography>
)}
</Stack>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,13 @@ function TokenItem(props: TokenItemProps) {
<Typography fontSize={14}>
{props.isFetchingBalance ? (
<CircularProgress size={24} />
) : props.balance ? (
// TODO AMOUNT HACK... fix amount formatting in amount.Amount balance refactor
parseFloat(props.balance).toLocaleString('en', {
maximumFractionDigits: 6,
})
) : (
props.balance
''
)}
</Typography>
</ListItemButton>
Expand Down
5 changes: 3 additions & 2 deletions wormhole-connect/src/views/v2/Bridge/Routes/SingleRoute.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {

import type { RouteData } from 'config/routes';
import type { RootState } from 'store';
import { formatBalance } from 'store/transferInput';
import { formatAmount } from 'utils/amount';
import { toFixedDecimals } from 'utils/balance';
import { TokenConfig } from 'config/types';
import FastestRoute from 'icons/FastestRoute';
Expand Down Expand Up @@ -356,10 +356,11 @@ const SingleRoute = (props: Props) => {

const receiveAmountTrunc = useMemo(() => {
return quote && destChain && destTokenConfig
? formatBalance(
? formatAmount(
destChain,
destTokenConfig,
quote.destinationToken.amount.amount,
6,
)
: undefined;
}, [quote]);
Expand Down
Loading