Skip to content

Commit

Permalink
Rename external repay form to sell and use source loan max interest
Browse files Browse the repository at this point in the history
  • Loading branch information
sophialittlejohn committed Jul 31, 2024
1 parent 3aac9d1 commit c13b845
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
12 changes: 5 additions & 7 deletions centrifuge-app/src/pages/Loan/RepayForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { combineLatest, switchMap } from 'rxjs'
import { Dec, max as maxDec, min } from '../../utils/Decimal'
import { formatBalance, roundDown } from '../../utils/formatting'
import { useFocusInvalidInput } from '../../utils/useFocusInvalidInput'
import { useAvailableFinancing, useLoans } from '../../utils/useLoans'
import { useLoans } from '../../utils/useLoans'
import { useBorrower } from '../../utils/usePermissions'
import { usePool } from '../../utils/usePools'
import {
Expand All @@ -18,7 +18,7 @@ import {
positiveNumberNotRequired,
} from '../../utils/validation'
import { useChargePoolFees } from './ChargeFeesFields'
import { ExternalRepayForm } from './ExternalRepayForm'
import { SellForm } from './SellForm'
import { SourceSelect } from './SourceSelect'
import { isExternalLoan } from './utils'

Expand All @@ -37,7 +37,7 @@ export function RepayForm({ loan }: { loan: ActiveLoan }) {
<Text variant="heading2">{isExternalLoan(loan) ? 'Sell' : 'Repay'}</Text>
<SourceSelect loan={loan} value={destination} onChange={(newSource) => setDestination(newSource)} type="repay" />
{isExternalLoan(loan) ? (
<ExternalRepayForm loan={loan as ExternalLoan} destination={destination} />
<SellForm loan={loan as ExternalLoan} destination={destination} />
) : (
<InternalRepayForm loan={loan} destination={destination} />
)}
Expand All @@ -50,8 +50,6 @@ function InternalRepayForm({ loan, destination }: { loan: ActiveLoan; destinatio
const account = useBorrower(loan.poolId, loan.id)
const balances = useBalances(account?.actingAddress)
const balance = (balances && findBalance(balances.currencies, pool.currency.key)?.balance.toDecimal()) || Dec(0)
console.log('🚀 ~ balance:', balance.toString())
const { debtWithMargin } = useAvailableFinancing(loan.poolId, loan.id)
const poolFees = useChargePoolFees(loan.poolId, loan.id)
const loans = useLoans(loan.poolId)
const destinationLoan = loans?.find((l) => l.id === destination) as ActiveLoan
Expand Down Expand Up @@ -133,7 +131,7 @@ function InternalRepayForm({ loan, destination }: { loan: ActiveLoan; destinatio
if (destination !== 'reserve') {
maxAvailable = destinationLoan.outstandingDebt?.toDecimal()
maxPrincipal = destinationLoan.outstandingDebt.toDecimal()
maxInterest = destinationLoan.outstandingInterest.toDecimal()
maxInterest = loan.outstandingInterest.toDecimal()
}
const totalRepay = Dec(principal || 0)
.add(Dec(interest || 0))
Expand Down Expand Up @@ -187,7 +185,7 @@ function InternalRepayForm({ loan, destination }: { loan: ActiveLoan; destinatio
value={field.value instanceof Decimal ? field.value.toNumber() : field.value}
label="Interest"
secondaryLabel={`${formatBalance(
destination === 'reserve' ? loan.outstandingInterest : destinationLoan.outstandingInterest,
loan.outstandingInterest,
pool?.currency.symbol,
2
)} interest accrued`}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ type RepayValues = {
quantity: number | ''
fees: { id: string; amount: number | '' | Decimal }[]
}

export function ExternalRepayForm({ loan, destination }: { loan: ExternalLoan; destination: string }) {
/**
* Repay form for loans with `valuationMethod === oracle
*/
export function SellForm({ loan, destination }: { loan: ExternalLoan; destination: string }) {
const pool = usePool(loan.poolId)
const account = useBorrower(loan.poolId, loan.id)
const balances = useBalances(account?.actingAddress)
Expand Down Expand Up @@ -122,7 +124,7 @@ export function ExternalRepayForm({ loan, destination }: { loan: ExternalLoan; d
let maxInterest = min(balance, outstandingInterest)
if (destination !== 'reserve') {
maxAvailable = destinationLoan.outstandingDebt?.toDecimal() || Dec(0)
maxInterest = destinationLoan.outstandingInterest.toDecimal() || Dec(0)
maxInterest = outstandingInterest || Dec(0)
}
return {
maxAvailable,
Expand Down Expand Up @@ -207,7 +209,7 @@ export function ExternalRepayForm({ loan, destination }: { loan: ExternalLoan; d
label="Interest"
errorMessage={meta.touched ? meta.error : undefined}
secondaryLabel={`${formatBalance(
destination === 'reserve' ? loan.outstandingInterest : destinationLoan.outstandingInterest,
loan.outstandingInterest,
pool?.currency.symbol,
2
)} interest accrued`}
Expand Down

0 comments on commit c13b845

Please sign in to comment.