Skip to content

Commit

Permalink
fix validation
Browse files Browse the repository at this point in the history
  • Loading branch information
onnovisser committed Sep 18, 2023
1 parent 33ed728 commit 71781df
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 28 deletions.
24 changes: 15 additions & 9 deletions centrifuge-app/src/components/InvestRedeem/InvestRedeem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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<InvestValues> = {}
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'
}

Expand Down Expand Up @@ -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}
/>
)}
Expand All @@ -685,7 +686,12 @@ function RedeemForm({ onCancel, autoFocus }: RedeemFormProps) {
<Text variant="body3">Token amount</Text>
<Text variant="body3" width={12} variance={0}>
{!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
)}`}
</Text>
</Shelf>
</Stack>
Expand Down
9 changes: 5 additions & 4 deletions centrifuge-app/src/utils/useLiquidityPools.ts
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand Down Expand Up @@ -54,6 +54,7 @@ export function useActiveDomains(poolId: string) {
{
enabled: !!routers?.length && !poolId.startsWith('0x'),
staleTime: Infinity,
suspense,
}
)

Expand Down
15 changes: 0 additions & 15 deletions centrifuge-js/src/modules/liquidityPools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 71781df

Please sign in to comment.