From 8db45af8cface47f5c92be207de4b1f56c15db06 Mon Sep 17 00:00:00 2001 From: nick134 <76399455+nick134-bit@users.noreply.github.com> Date: Fri, 8 Nov 2024 17:24:45 +0100 Subject: [PATCH 1/3] chore: remove trailing zeros --- util/amountInputValidation.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/util/amountInputValidation.ts b/util/amountInputValidation.ts index b1e02788..927660ec 100644 --- a/util/amountInputValidation.ts +++ b/util/amountInputValidation.ts @@ -6,12 +6,13 @@ */ export const amountInputValidation = (input: string): string => { const amount = input. - // Limiting the number of characters to be 32 + // Limiting the number of characters to be 32 slice(0, 32). - // Disallowing leading decimal place and multiple zeros before decimal place + // Disallowing leading decimal place and multiple zeros before decimal place replace(/^(?:\.|0{2,}\.)/u, '0.'). - // Eliminating multiple leading zeros before the numbers between 1-9 - replace(/^0+(?=[0-9])/u, '') + // Eliminating multiple leading zeros before the numbers between 1-9 + replace(/^0+(?=[0-9])/u, ''). + replace(/\.?0+$/u, '') // Ensuring multiple decimal points can't be used return input.indexOf('.') !== amount.lastIndexOf('.') ? amount.slice(0, amount.indexOf('.') + 1) + amount.slice(amount.indexOf('.')).replaceAll('.', '') From ebd1ec9256ca5fa34b7ce708d26a6fef9e000d96 Mon Sep 17 00:00:00 2001 From: nick134 <76399455+nick134-bit@users.noreply.github.com> Date: Fri, 8 Nov 2024 18:13:53 +0100 Subject: [PATCH 2/3] chore: another one --- util/amountInputValidation.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/util/amountInputValidation.ts b/util/amountInputValidation.ts index 927660ec..fbefc56a 100644 --- a/util/amountInputValidation.ts +++ b/util/amountInputValidation.ts @@ -12,7 +12,8 @@ export const amountInputValidation = (input: string): string => { replace(/^(?:\.|0{2,}\.)/u, '0.'). // Eliminating multiple leading zeros before the numbers between 1-9 replace(/^0+(?=[0-9])/u, ''). - replace(/\.?0+$/u, '') + // Remove excess zeros after decimal point + replace(/\.(\d+?)0{2,}$/u, '.$10') // Ensuring multiple decimal points can't be used return input.indexOf('.') !== amount.lastIndexOf('.') ? amount.slice(0, amount.indexOf('.') + 1) + amount.slice(amount.indexOf('.')).replaceAll('.', '') From d62411d816bac4ba218b119f553725a9272b64df Mon Sep 17 00:00:00 2001 From: nick134 <76399455+nick134-bit@users.noreply.github.com> Date: Fri, 8 Nov 2024 18:40:37 +0100 Subject: [PATCH 3/3] chore: another one --- components/AssetInput/WhaleInput.tsx | 5 ++++- util/amountInputValidation.ts | 30 +++++++++++++++++----------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/components/AssetInput/WhaleInput.tsx b/components/AssetInput/WhaleInput.tsx index 345a4f55..6f708ed6 100644 --- a/components/AssetInput/WhaleInput.tsx +++ b/components/AssetInput/WhaleInput.tsx @@ -126,7 +126,10 @@ ref) => { onChange={({ target }) => { onChange({ ...token, - amount: amountInputValidation(target.value), + amount: amountInputValidation( + target.value, + tokenInfo?.decimals + ), }); }} /> diff --git a/util/amountInputValidation.ts b/util/amountInputValidation.ts index fbefc56a..e8e0d875 100644 --- a/util/amountInputValidation.ts +++ b/util/amountInputValidation.ts @@ -2,20 +2,26 @@ * General use validation function for when the user supplies a positive decimal value. * For example it is used with token and slippage amounts. * @param input The user supplied value + * @param decimals Optional number of decimals to respect * @returns Validated string that has invalid patterns removed */ -export const amountInputValidation = (input: string): string => { - const amount = input. - // Limiting the number of characters to be 32 +export const amountInputValidation = (input: string, decimals?: number): string => { + let amount = input. slice(0, 32). - // Disallowing leading decimal place and multiple zeros before decimal place replace(/^(?:\.|0{2,}\.)/u, '0.'). - // Eliminating multiple leading zeros before the numbers between 1-9 - replace(/^0+(?=[0-9])/u, ''). - // Remove excess zeros after decimal point - replace(/\.(\d+?)0{2,}$/u, '.$10') - // Ensuring multiple decimal points can't be used - return input.indexOf('.') !== amount.lastIndexOf('.') ? - amount.slice(0, amount.indexOf('.') + 1) + amount.slice(amount.indexOf('.')).replaceAll('.', '') - : amount; + replace(/^0+(?=[0-9])/u, '') + + // Handle multiple decimal points + if (input.indexOf('.') !== amount.lastIndexOf('.')) { + amount = amount.slice(0, amount.indexOf('.') + 1) + + amount.slice(amount.indexOf('.')).replaceAll('.', '') + } + + // If decimals is specified, just limit decimal places without padding + if (decimals !== undefined && amount.includes('.')) { + const [whole, fraction] = amount.split('.') + amount = `${whole}.${fraction.slice(0, decimals)}` + } + + return amount }