From a7483904f1184d72d3f6f908a05ff0f4e9f70fcb Mon Sep 17 00:00:00 2001 From: Jagoda Berry Rybacka Date: Tue, 14 Nov 2023 16:58:41 +0100 Subject: [PATCH] Fix max button precision by passing correct arguments --- src/shared/components/TokenAmountInput.tsx | 6 ++++-- src/shared/utils/numbers.ts | 14 ++++++++++---- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/shared/components/TokenAmountInput.tsx b/src/shared/components/TokenAmountInput.tsx index d1f671f29..ddd035ef9 100644 --- a/src/shared/components/TokenAmountInput.tsx +++ b/src/shared/components/TokenAmountInput.tsx @@ -71,7 +71,9 @@ export default function TokenAmountInput({
{label && (
{`${label} ${bigIntToDisplayUserAmount( - balance + balance, + 18, + 5 )} ${symbol}`}
)} { event.preventDefault() - onChange(bigIntToUserAmount(balance)) + onChange(bigIntToUserAmount(balance, 18, 18)) }} > Max diff --git a/src/shared/utils/numbers.ts b/src/shared/utils/numbers.ts index 19fa568fa..58982830d 100644 --- a/src/shared/utils/numbers.ts +++ b/src/shared/utils/numbers.ts @@ -4,10 +4,15 @@ import { FixedPointNumber } from "shared/types/stake" const FLOATING_POINT_REGEX = /^[^0-9]*([0-9,]+)(?:\.([0-9]*))?$/ export const separateThousandsByComma = ( - value: number | bigint | string + value: number | bigint | string, + decimals = 2 ): string => { const adjustedValue = typeof value === "string" ? +value : value - return adjustedValue.toLocaleString("en-US", { maximumFractionDigits: 2 }) + return adjustedValue.toLocaleString("en-US", { + // @ts-expect-error - `maximumFractionDigits` wants to get number less than 21, + // but as the tokens have 18 decimals have, we can safely pass a parameter + maximumFractionDigits: decimals < 21 ? decimals : 2, + }) } function parseToFixedPointNumber( @@ -81,7 +86,7 @@ export function bigIntToUserAmount( desiredDecimals = 2 ): string { const desiredDecimalsAmount = - amount / 10n ** BigInt(Math.max(1, decimals - desiredDecimals)) + amount / 10n ** BigInt(Math.max(0, decimals - desiredDecimals)) return ( Number(desiredDecimalsAmount) / @@ -98,6 +103,7 @@ export function bigIntToDisplayUserAmount( const amountBigInt = typeof amount === "string" ? BigInt(amount) : amount return separateThousandsByComma( - bigIntToUserAmount(amountBigInt, decimals, desiredDecimals) + bigIntToUserAmount(amountBigInt, decimals, desiredDecimals), + desiredDecimals ) }