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

fix(suite-common): resolve formatAmount TODO in try catch block #16058

Merged
merged 1 commit into from
Jan 22, 2025
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
11 changes: 8 additions & 3 deletions packages/suite/src/components/wallet/Fees/Fees.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,16 @@ export const Fees = <TFieldValues extends FormState>({
const error = errors.selectedFee;
const selectedLevel = feeInfo.levels.find(level => level.label === selectedOption)!;
const transactionInfo = composedLevels?.[selectedOption];
const hasTransactionInfo = transactionInfo !== undefined && transactionInfo.type !== 'error';
// Solana has only `normal` fee level, so we do not display any feeOptions since there is nothing to choose from
const feeOptions = networkType === 'solana' ? [] : buildFeeOptions(feeInfo.levels);

const shouldAnimateNormalFee = !isCustomLevel;

const networkAmount = hasTransactionInfo
? formatNetworkAmount(transactionInfo.fee, symbol)
: null;

return (
<Column gap={spacings.xs}>
<InfoItem
Expand All @@ -114,17 +119,17 @@ export const Fees = <TFieldValues extends FormState>({
)
}
>
{transactionInfo !== undefined && transactionInfo.type !== 'error' && (
{networkAmount && (
<Row gap={spacings.md} alignItems="baseline">
<FormattedCryptoAmount
disableHiddenPlaceholder
value={formatNetworkAmount(transactionInfo.fee, symbol)}
value={networkAmount}
symbol={symbol}
/>
<Text variant="tertiary" typographyStyle="label">
<FiatValue
disableHiddenPlaceholder
amount={formatNetworkAmount(transactionInfo.fee, symbol)}
amount={networkAmount}
symbol={symbol}
showApproximationIndicator
/>
Expand Down
3 changes: 2 additions & 1 deletion packages/suite/src/views/wallet/send/TotalSent/TotalSent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const TotalSent = () => {
const selectedFee = getValues().selectedFee || 'normal';
const transactionInfo = composedLevels ? composedLevels[selectedFee] : undefined;
const isTokenTransfer = networkType === 'ethereum' && !!getValues('outputs.0.token');
const hasTransactionInfo = transactionInfo && transactionInfo.type !== 'error';
const hasTransactionInfo = transactionInfo !== undefined && transactionInfo.type !== 'error';
const tokenInfo = hasTransactionInfo ? transactionInfo.token : undefined;

return (
Expand Down Expand Up @@ -53,6 +53,7 @@ export const TotalSent = () => {
/>
)}
</InfoItem>

<InfoItem
label={<Translation id={isTokenTransfer ? 'FEE' : 'INCLUDING_FEE'} />}
direction="row"
Expand Down
5 changes: 4 additions & 1 deletion suite-common/wallet-utils/src/__tests__/accountUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ describe('account utils', () => {
expect(formatNetworkAmount('1', 'eth')).toEqual('0.000000000000000001');
expect(formatNetworkAmount('1', 'btc', true)).toEqual('0.00000001 BTC');
expect(formatNetworkAmount('1', 'btc', true, true)).toEqual('1 sat');
expect(formatNetworkAmount('aaa', 'eth')).toEqual('-1');
expect(formatNetworkAmount('', 'btc')).toEqual('0');
expect(formatNetworkAmount('', 'btc', true)).toEqual('0 BTC');
expect(formatNetworkAmount('', 'btc', true, true)).toEqual('0 sat');
expect(() => formatNetworkAmount('aaa', 'eth')).toThrow();
});

it('format amount to satoshi', () => {
Expand Down
18 changes: 9 additions & 9 deletions suite-common/wallet-utils/src/accountUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,16 +287,16 @@ export const stripNetworkAmount = (amount: string, decimals: number) =>
new BigNumber(amount).toFixed(decimals, 1);

export const formatAmount = (amount: BigNumberValue, decimals: number) => {
try {
const bAmount = new BigNumber(amount);
if (bAmount.isNaN()) {
throw new Error('Amount is not a number');
}
const safeAmount = amount || '0';
const bAmount = new BigNumber(safeAmount);

return bAmount.div(new BigNumber(10).exponentiatedBy(decimals)).toString(10);
} catch {
return '-1'; // TODO: this is definitely not correct return value
if (bAmount.isNaN()) {
throw new Error('Amount is not a number');
}

const factor = new BigNumber(10).exponentiatedBy(decimals);

return bAmount.div(factor).toString(10);
};

export const amountToSmallestUnit = (amount: BigNumberValue, decimals: number) => {
Expand Down Expand Up @@ -353,7 +353,7 @@ export const formatNetworkAmount = (
let formattedSymbol = getNetworkDisplaySymbol(symbol);

if (isSatoshis) {
formattedAmount = amount;
formattedAmount = amount || '0';
formattedSymbol = symbol === 'btc' ? 'sat' : `sat ${getNetworkDisplaySymbol(symbol)}`;
}

Expand Down
Loading