From 71781dfd7a51167a207e2c9bce14ca6f1a0aa683 Mon Sep 17 00:00:00 2001 From: Onno Visser <23527729+onnovisser@users.noreply.github.com> Date: Mon, 18 Sep 2023 11:02:17 +0200 Subject: [PATCH] fix validation --- .../components/InvestRedeem/InvestRedeem.tsx | 24 ++++++++++++------- centrifuge-app/src/utils/useLiquidityPools.ts | 9 +++---- centrifuge-js/src/modules/liquidityPools.ts | 15 ------------ 3 files changed, 20 insertions(+), 28 deletions(-) diff --git a/centrifuge-app/src/components/InvestRedeem/InvestRedeem.tsx b/centrifuge-app/src/components/InvestRedeem/InvestRedeem.tsx index 5372d401ea..bfbbbf4102 100644 --- a/centrifuge-app/src/components/InvestRedeem/InvestRedeem.tsx +++ b/centrifuge-app/src/components/InvestRedeem/InvestRedeem.tsx @@ -598,7 +598,8 @@ function RedeemForm({ onCancel, autoFocus }: RedeemFormProps) { const pendingRedeem = state.order?.remainingRedeemToken ?? Dec(0) - const maxRedeem = state.trancheBalanceWithPending.mul(state.tokenPrice) + const maxRedeemTokens = state.trancheBalanceWithPending + const maxRedeemCurrency = maxRedeemTokens.mul(state.tokenPrice) const tokenSymbol = state.trancheCurrency?.symbol hooks.useActionSucceeded((action) => { @@ -628,16 +629,16 @@ function RedeemForm({ onCancel, autoFocus }: RedeemFormProps) { amount: '', }, onSubmit: (values, formActions) => { - const amount = values.amount instanceof Decimal ? values.amount : Dec(values.amount).div(state.tokenPrice) - actions.redeem(TokenBalance.fromFloat(amount, state.poolCurrency?.decimals ?? 18)) + const amountTokens = values.amount instanceof Decimal ? values.amount : Dec(values.amount).div(state.tokenPrice) + actions.redeem(TokenBalance.fromFloat(amountTokens, state.poolCurrency?.decimals ?? 18)) formActions.setSubmitting(false) }, validate: (values) => { const errors: FormikErrors = {} - const amount = values.amount instanceof Decimal ? values.amount : Dec(values.amount).div(state.tokenPrice) - if (validateNumberInput(amount, 0, maxRedeem)) { - errors.amount = validateNumberInput(amount, 0, maxRedeem) - } else if (hasPendingOrder && amount.eq(pendingRedeem)) { + const amountTokens = values.amount instanceof Decimal ? values.amount : Dec(values.amount).div(state.tokenPrice) + if (validateNumberInput(amountTokens, 0, maxRedeemTokens)) { + errors.amount = validateNumberInput(amountTokens, 0, maxRedeemTokens) + } else if (hasPendingOrder && amountTokens.eq(pendingRedeem)) { errors.amount = 'Equals current order' } @@ -674,7 +675,7 @@ function RedeemForm({ onCancel, autoFocus }: RedeemFormProps) { onSetMax={() => form.setFieldValue('amount', state.trancheBalanceWithPending)} onChange={(value) => form.setFieldValue('amount', value)} currency={state.poolCurrency?.symbol} - secondaryLabel={`${formatBalance(roundDown(maxRedeem), state.poolCurrency?.symbol, 2)} available`} + secondaryLabel={`${formatBalance(roundDown(maxRedeemCurrency), state.poolCurrency?.symbol, 2)} available`} autoFocus={autoFocus} /> )} @@ -685,7 +686,12 @@ function RedeemForm({ onCancel, autoFocus }: RedeemFormProps) { Token amount {!state.tokenPrice.isZero() && - `~${formatBalance(Dec(form.values.amount).div(state.tokenPrice), tokenSymbol)}`} + `~${formatBalance( + form.values.amount instanceof Decimal + ? form.values.amount + : Dec(form.values.amount).div(state.tokenPrice), + tokenSymbol + )}`} diff --git a/centrifuge-app/src/utils/useLiquidityPools.ts b/centrifuge-app/src/utils/useLiquidityPools.ts index 3015cea6d3..ed1f9eddb6 100644 --- a/centrifuge-app/src/utils/useLiquidityPools.ts +++ b/centrifuge-app/src/utils/useLiquidityPools.ts @@ -1,18 +1,18 @@ import { useCentrifuge, useCentrifugeQuery, useWallet } from '@centrifuge/centrifuge-react' import { useQuery } from 'react-query' -export function useDomainRouters() { - const [data] = useCentrifugeQuery(['domainRouters'], (cent) => cent.liquidityPools.getDomainRouters()) +export function useDomainRouters(suspense?: boolean) { + const [data] = useCentrifugeQuery(['domainRouters'], (cent) => cent.liquidityPools.getDomainRouters(), { suspense }) return data } -export function useActiveDomains(poolId: string) { +export function useActiveDomains(poolId: string, suspense?: boolean) { const { evm: { getProvider }, } = useWallet() const cent = useCentrifuge() - const routers = useDomainRouters() + const routers = useDomainRouters(suspense) const query = useQuery( ['activeDomains', poolId, routers?.length], async () => { @@ -54,6 +54,7 @@ export function useActiveDomains(poolId: string) { { enabled: !!routers?.length && !poolId.startsWith('0x'), staleTime: Infinity, + suspense, } ) diff --git a/centrifuge-js/src/modules/liquidityPools.ts b/centrifuge-js/src/modules/liquidityPools.ts index 96fb55d14b..4cd0f1c385 100644 --- a/centrifuge-js/src/modules/liquidityPools.ts +++ b/centrifuge-js/src/modules/liquidityPools.ts @@ -218,21 +218,6 @@ export function getLiquidityPoolsModule(inst: Centrifuge) { return pool[0].toString() === poolId ? { isActive: true } : undefined } - // function getDomainCurrencyIds(args: [chainId: number]) { - // const [chainId] = args - // return inst.pools.getCurrencies().pipe( - // map((currencies) => { - // // TODO: for testing, remove - // return [1] - // return currencies - // .filter( - // (cur) => getCurrencyChainId(cur) === chainId && typeof cur.key === 'object' && 'ForeignAsset' in cur.key - // ) - // .map((cur) => (cur.key as any).ForeignAsset) - // }) - // ) - // } - function getDomainCurrencies(args: [chainId: number]) { const [chainId] = args return inst.pools.getCurrencies().pipe(