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

Feat: adding balance check logic #169

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions src/features/swap/SwapForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ export function SwapFormCard() {
}

function SwapForm() {
const balances = useAppSelector((s) => s.account.balances)
const { balances, lastUpdated } = useAppSelector((s) => s.account)
const { showSlippage } = useAppSelector((s) => s.swap)

const dispatch = useAppDispatch()
const onSubmit = (values: SwapFormValues) => {
dispatch(setFormValues(values))
dispatch(setConfirmView(true)) // Switch to confirm view
}
const validateForm = useFormValidator(balances)
const validateForm = useFormValidator(balances, lastUpdated)
const storedFormValues = useAppSelector((s) => s.swap.formValues) // Get stored form values
const initialFormValues = storedFormValues || initialValues // Use stored values if they exist

Expand Down
5 changes: 3 additions & 2 deletions src/features/swap/useFormValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import { areAmountsNearlyEqual, parseAmount, toWei } from 'src/utils/amount'
import { logger } from 'src/utils/logger'
import { useChainId } from 'wagmi'

export function useFormValidator(balances: AccountBalances) {
export function useFormValidator(balances: AccountBalances, lastUpdated: number | null) {
const chainId = useChainId()
return useCallback(
(values?: SwapFormValues): Promise<FormikErrors<SwapFormValues>> => {
return (async () => {
if (!lastUpdated) return { fromTokenId: 'Balance still loading' }
if (!values || !values.amount) return { amount: 'Amount Required' }
const parsedAmount = parseAmount(values.amount)
if (!parsedAmount) return { amount: 'Amount is Invalid' }
Expand All @@ -33,7 +34,7 @@ export function useFormValidator(balances: AccountBalances) {
return {}
})
},
[balances, chainId]
[balances, chainId, lastUpdated]
)
}

Expand Down
Loading