Skip to content

Commit

Permalink
Transfer debt bugs (#2204)
Browse files Browse the repository at this point in the history
* Fix transfer debt functionality

* Fix cash transfer form
  • Loading branch information
sophialittlejohn authored Jun 6, 2024
1 parent adba9b0 commit 49abcb5
Showing 1 changed file with 14 additions and 30 deletions.
44 changes: 14 additions & 30 deletions centrifuge-app/src/pages/Loan/TransferDebtForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ type FormValues = {
targetLoan: string
amount: number | '' | Decimal
price: number | '' | Decimal
faceValue: number | ''
targetLoanFaceValue: number | '' | Decimal
quantity: number | ''
targetLoanQuantity: number | '' | Decimal
targetLoanPrice: number | '' | Decimal
}

Expand Down Expand Up @@ -54,9 +54,9 @@ export function TransferDebtForm({ loan }: { loan: LoanType }) {
targetLoan: '',
amount: '',
price: '',
faceValue: '',
quantity: '',
targetLoanPrice: '',
targetLoanFaceValue: '',
targetLoanQuantity: '',
},
onSubmit: (values, actions) => {
if (!selectedLoan) return
Expand All @@ -66,7 +66,7 @@ export function TransferDebtForm({ loan }: { loan: LoanType }) {
if (isExternalLoan(loan)) {
borrow = {
price: CurrencyBalance.fromFloat(values.price, pool.currency.decimals),
quantity: Price.fromFloat(Dec(values.faceValue).div(loan.pricing.notional.toDecimal())),
quantity: Price.fromFloat(values.quantity),
}
borrowAmount = borrow.quantity.mul(borrow.price).div(Price.fromFloat(1))
} else if (isExternalLoan(selectedLoan)) {
Expand All @@ -87,9 +87,7 @@ export function TransferDebtForm({ loan }: { loan: LoanType }) {
let repay: any = { principal, interest }
if (isExternalLoan(selectedLoan)) {
const repayPriceBN = CurrencyBalance.fromFloat(form.values.targetLoanPrice || 1, pool.currency.decimals)
const repayQuantityBN = Price.fromFloat(
Dec(values.targetLoanFaceValue || 0).div(selectedLoan.pricing.notional.toDecimal())
)
const repayQuantityBN = Price.fromFloat(Dec(values.targetLoanQuantity || 0))
repay = { quantity: repayQuantityBN, price: repayPriceBN, interest }
}

Expand All @@ -101,13 +99,9 @@ export function TransferDebtForm({ loan }: { loan: LoanType }) {
},
validate(values) {
const financeAmount = isExternalLoan(loan)
? Dec(values.price || 0)
.mul(Dec(values.faceValue || 0))
.div(loan.pricing.notional.toDecimal())
? Dec(values.price || 0).mul(Dec(values.quantity || 0))
: selectedLoan && isExternalLoan(selectedLoan)
? Dec(values.targetLoanPrice || 0)
.mul(Dec(values.targetLoanFaceValue || 0))
.div(selectedLoan.pricing.notional.toDecimal())
? Dec(values.targetLoanPrice || 0).mul(Dec(values.targetLoanQuantity || 0))
: Dec(values.amount || 0)

let errors: any = {}
Expand Down Expand Up @@ -152,13 +146,9 @@ export function TransferDebtForm({ loan }: { loan: LoanType }) {
if (availableFinancing.lte(0) || maturityDatePassed || !loans?.length) return null

const financeAmount = isExternalLoan(loan)
? Dec(form.values.price || 0)
.mul(Dec(form.values.faceValue || 0))
.div(loan.pricing.notional.toDecimal())
? Dec(form.values.price || 0).mul(Dec(form.values.quantity || 0))
: selectedLoan && isExternalLoan(selectedLoan)
? Dec(form.values.targetLoanPrice || 0)
.mul(Dec(form.values.targetLoanFaceValue || 0))
.div(selectedLoan.pricing.notional.toDecimal())
? Dec(form.values.targetLoanPrice || 0).mul(Dec(form.values.targetLoanQuantity || 0))
: Dec(form.values.amount || 0)

return (
Expand Down Expand Up @@ -196,13 +186,7 @@ export function TransferDebtForm({ loan }: { loan: LoanType }) {
<ExternalFinanceFields
loan={loan}
pool={pool}
validate={(val) =>
validate(
Dec(val)
.mul(form.values.faceValue || 1)
.div(loan.pricing.notional.toDecimal())
)
}
validate={(val) => validate(Dec(val).mul(form.values.quantity || 1))}
/>
<Shelf justifyContent="space-between">
<Text variant="emphasized">Total amount</Text>
Expand Down Expand Up @@ -231,15 +215,15 @@ export function TransferDebtForm({ loan }: { loan: LoanType }) {
) : null}
{selectedLoan && isExternalLoan(selectedLoan) && (
<>
<Field validate={combine(positiveNumber())} name="targetLoanFaceValue">
<Field validate={combine(positiveNumber())} name="targetLoanQuantity">
{({ field, meta, form }: FieldProps) => {
return (
<CurrencyInput
{...field}
label="Face value (settlement asset)"
label="Quantity (settlement asset)"
errorMessage={meta.touched ? meta.error : undefined}
decimals={8}
onChange={(value) => form.setFieldValue('targetLoanFaceValue', value)}
onChange={(value) => form.setFieldValue('targetLoanQuantity', value)}
currency={pool.currency.symbol}
/>
)
Expand Down

0 comments on commit 49abcb5

Please sign in to comment.