diff --git a/nym-wallet/src/utils/index.ts b/nym-wallet/src/utils/index.ts index a630fc4b3e..1d4686558f 100644 --- a/nym-wallet/src/utils/index.ts +++ b/nym-wallet/src/utils/index.ts @@ -1,7 +1,7 @@ import { appWindow } from '@tauri-apps/api/window'; import bs58 from 'bs58'; import { valid } from 'semver'; -import { basicRawCoinValueValidation, MajorAmountString } from '@nymproject/types'; +import { isValidRawCoin, MajorAmountString } from '@nymproject/types'; import { getLockedCoins, getSpendableCoins, userBalance } from '../requests'; import { Console } from './console'; @@ -26,19 +26,14 @@ export const validateAmount = async ( return false; } - try { - if (!basicRawCoinValueValidation(majorAmountAsString)) { - return false; - } - - const majorValueFloat = parseInt(majorAmountAsString, Number(10)); - - return majorValueFloat >= parseInt(minimumAmountAsString, Number(10)); - } catch (e) { - Console.error(e as string); + if (!isValidRawCoin(majorAmountAsString)) { return false; } + const majorValueFloat = parseInt(majorAmountAsString, Number(10)); + + return majorValueFloat >= parseInt(minimumAmountAsString, Number(10)); + // this conversion seems really iffy but I'm not sure how to better approach it }; diff --git a/ts-packages/types/src/utils/coin/validation.ts b/ts-packages/types/src/utils/coin/validation.ts index 08e0d19b37..b26db8b611 100644 --- a/ts-packages/types/src/utils/coin/validation.ts +++ b/ts-packages/types/src/utils/coin/validation.ts @@ -1,16 +1,25 @@ -export const basicRawCoinValueValidation = (rawAmount: string): boolean => { +export const isValidRawCoin = (rawAmount: string): boolean => { const amountFloat = parseFloat(rawAmount); - // it cannot have more than 6 decimal places - if (amountFloat !== parseInt(amountFloat.toFixed(6), Number(10))) { - return false; + // if value is a decimal it cannot have more than 6 decimal places + if (amountFloat % 1 > 0) { + const [_, numsAfterDecimal] = rawAmount.split('.'); + + if (+numsAfterDecimal.length > 6) { + return false; + } } // it cannot be larger than the total supply - if (amountFloat > 1_000_000_000_000_000) { + if (amountFloat > 1_000_000_000) { return false; } + console.log(amountFloat); // it can't be lower than one micro coin - return amountFloat >= 0.000001; + if (amountFloat < 0.000001) { + return false; + } + + return true; };