From 5ec32c019213bfa42412eb6325ae41d38a806881 Mon Sep 17 00:00:00 2001 From: Polybius93 Date: Wed, 20 Nov 2024 14:55:58 +0100 Subject: [PATCH] feat: modify confirmation checker return value and form input parsing --- .../transaction-screen.transaction-form.tsx | 15 ++++++--------- src/app/hooks/use-confirmation-checker.ts | 3 ++- src/shared/utils.ts | 13 +++++++++++++ 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/app/components/transaction-screen/transaction-screen.transaction-form/components/transaction-screen.transaction-form/transaction-screen.transaction-form.tsx b/src/app/components/transaction-screen/transaction-screen.transaction-form/components/transaction-screen.transaction-form/transaction-screen.transaction-form.tsx index 917c90c5..0c8b0f63 100644 --- a/src/app/components/transaction-screen/transaction-screen.transaction-form/components/transaction-screen.transaction-form/transaction-screen.transaction-form.tsx +++ b/src/app/components/transaction-screen/transaction-screen.transaction-form/components/transaction-screen.transaction-form/transaction-screen.transaction-form.tsx @@ -8,7 +8,8 @@ import { BitcoinTransactionConfirmationsContext } from '@providers/bitcoin-query import { BitcoinWalletContextState } from '@providers/bitcoin-wallet-context-provider'; import { useForm } from '@tanstack/react-form'; import Decimal from 'decimal.js'; -import { isEmpty } from 'ramda'; + +import { parseAssetAmount } from '@shared/utils'; import { TransactionFormNavigateButtonGroup } from './components/transaction-screen.transaction-form.navigate-button-group'; import { TransactionFormProgressStack } from './components/transaction-screen.transaction-form.progress-stack/components/transaction-screen.transaction-form.progress-stack'; @@ -141,16 +142,12 @@ export function VaultTransactionForm({ }, validators: { onChange: ({ value }) => { - const assetAmount = value.assetAmount; - setCurrentFieldValue(isEmpty(assetAmount) ? 0 : new Decimal(value.assetAmount).toNumber()); + const decimalValue = parseAssetAmount(value.assetAmount); + setCurrentFieldValue(decimalValue.toNumber()); + return { fields: { - assetAmount: validateFormAmount( - parseFloat(value.assetAmount), - flow, - depositLimit, - vault - ), + assetAmount: validateFormAmount(decimalValue.toNumber(), flow, depositLimit, vault), }, }; }, diff --git a/src/app/hooks/use-confirmation-checker.ts b/src/app/hooks/use-confirmation-checker.ts index 62054ee9..56cfb826 100644 --- a/src/app/hooks/use-confirmation-checker.ts +++ b/src/app/hooks/use-confirmation-checker.ts @@ -34,7 +34,8 @@ export function useConfirmationChecker(): [string, number][] { try { const bitcoinTransactionBlockHeight = await fetchBitcoinTransactionBlockHeight(vault); - return blockHeight - bitcoinTransactionBlockHeight; + const difference = blockHeight - bitcoinTransactionBlockHeight; + return Math.max(difference, 0); } catch (error) { return 0; } diff --git a/src/shared/utils.ts b/src/shared/utils.ts index 53f98c03..91f22573 100644 --- a/src/shared/utils.ts +++ b/src/shared/utils.ts @@ -51,6 +51,19 @@ export function formatEvent(event: DetailedEvent): FormattedEvent { }; } +export function parseAssetAmount(assetAmount: string): Decimal { + const isValidNumber = /^-?\d*\.?\d*$/.test(assetAmount); + + if (isValidNumber) { + try { + return new Decimal(assetAmount); + } catch { + return new Decimal(0); + } + } + return new Decimal(0); +} + export function formatToFourDecimals(value: number): number { return parseFloat(value.toFixed(4)); }