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

Redemption flow fixes #2626

Merged
merged 2 commits into from
Feb 12, 2025
Merged
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
8 changes: 1 addition & 7 deletions centrifuge-app/src/components/InvestRedeem/Claim.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,7 @@ export function Claim({ type, onDismiss }: { type: 'invest' | 'redeem'; onDismis
: state.poolCurrency?.symbol
)}`}
>
Claim{' '}
{formatBalanceAbbreviated(
state.collectAmount,
['invest', 'cancelRedeem'].includes(state.collectType)
? state.trancheCurrency?.symbol
: state.poolCurrency?.symbol
)}
Claim
</Button>
)}
{!state.needsToCollectBeforeOrder && (
Expand Down
40 changes: 10 additions & 30 deletions centrifuge-app/src/components/InvestRedeem/RedeemForm.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Pool, TokenBalance } from '@centrifuge/centrifuge-js'
import { Box, Button, CurrencyInput, SelectInner, Stack, Text } from '@centrifuge/fabric'
import { Box, Button, CurrencyInput, Stack, Text } from '@centrifuge/fabric'
import Decimal from 'decimal.js-light'
import { Field, FieldProps, Form, FormikErrors, FormikProvider, useFormik } from 'formik'
import React from 'react'
Expand Down Expand Up @@ -31,8 +31,8 @@
const pendingRedeem = state.order?.remainingRedeemToken ?? Dec(0)

const maxRedeemTokens = state.trancheBalanceWithPending
const maxRedeemCurrency = maxRedeemTokens.mul(state.tokenPrice)

Check warning on line 34 in centrifuge-app/src/components/InvestRedeem/RedeemForm.tsx

View workflow job for this annotation

GitHub Actions / build-app

'maxRedeemCurrency' is assigned a value but never used

Check warning on line 34 in centrifuge-app/src/components/InvestRedeem/RedeemForm.tsx

View workflow job for this annotation

GitHub Actions / deploy-ff-prod / build-app

'maxRedeemCurrency' is assigned a value but never used
const tokenSymbol = state.trancheCurrency?.symbol

Check warning on line 35 in centrifuge-app/src/components/InvestRedeem/RedeemForm.tsx

View workflow job for this annotation

GitHub Actions / build-app

'tokenSymbol' is assigned a value but never used

Check warning on line 35 in centrifuge-app/src/components/InvestRedeem/RedeemForm.tsx

View workflow job for this annotation

GitHub Actions / deploy-ff-prod / build-app

'tokenSymbol' is assigned a value but never used

hooks.useActionSucceeded((action) => {
if (action === 'approveTrancheToken') {
Expand Down Expand Up @@ -60,20 +60,18 @@
amount: '',
},
onSubmit: (values, formActions) => {
const amountTokens =
values.amount instanceof Decimal ? values.amount : Dec(values.amount || 0).div(state.tokenPrice)
actions.redeem(TokenBalance.fromFloat(amountTokens, state.poolCurrency?.decimals ?? 18))
const amountTokens = values.amount instanceof Decimal ? values.amount : Dec(values.amount || 0)
actions.redeem(TokenBalance.fromFloat(amountTokens, state.trancheCurrency?.decimals ?? 18))
formActions.setSubmitting(false)
},
validate: (values) => {
const errors: FormikErrors<RedeemValues> = {}
const amountTokens =
values.amount instanceof Decimal ? values.amount : Dec(values.amount || 0).div(state.tokenPrice)
const amountTokens = values.amount instanceof Decimal ? values.amount : Dec(values.amount || 0)
if (validateNumberInput(amountTokens, 0, maxRedeemTokens)) {
errors.amount = validateNumberInput(amountTokens, 0, maxRedeemTokens)
} else if (hasPendingOrder && amountTokens.eq(pendingRedeem)) {
errors.amount = 'Equals current order'
} else if (Dec(values.amount || 0).lt(state.minOrder)) {
} else if (amountTokens.lt(state.minOrder)) {
errors.amount = 'Order amount too low'
}

Expand Down Expand Up @@ -117,34 +115,16 @@
{({ field, meta }: FieldProps) => (
<CurrencyInput
{...field}
// when the value is a decimal we assume the user clicked the max button
// it tracks the value in tokens and needs to be multiplied by price to get the value in pool currency
value={field.value instanceof Decimal ? field.value.mul(state.tokenPrice).toNumber() : field.value}
value={field.value}
errorMessage={meta.touched && (field.value !== 0 || form.submitCount > 0) ? meta.error : undefined}
label="Amount"
disabled={isRedeeming}
onSetMax={() => form.setFieldValue('amount', state.trancheBalanceWithPending)}
onChange={(value) => form.setFieldValue('amount', value)}
currency={
state?.poolCurrencies.length > 1 ? (
<SelectInner
{...field}
onChange={(e) => {
actions.selectPoolCurrency(e.target.value)
}}
value={state.poolCurrency?.symbol}
options={state?.poolCurrencies
.sort((_, b) => (b.displayName.toLowerCase().includes('usdc') ? 1 : -1))
.map((c) => ({ value: c.symbol, label: c.displayName }))}
style={{ textAlign: 'right' }}
/>
) : (
state.poolCurrency?.displayName
)
}
currency={state.trancheCurrency?.displayName}
secondaryLabel={`${formatBalance(
roundDown(maxRedeemCurrency),
state.poolCurrency?.displayName,
roundDown(maxRedeemTokens),
state.trancheCurrency?.displayName,
2
)} available`}
autoFocus={autoFocus}
Expand All @@ -161,7 +141,7 @@
form.values.amount instanceof Decimal
? form.values.amount
: Dec(form.values.amount).div(state.tokenPrice),
tokenSymbol
state.poolCurrency?.displayName
)}`}
</Text>
</Text>
Expand Down
Loading