Skip to content

Commit

Permalink
Fix margin buffer and when to use it
Browse files Browse the repository at this point in the history
  • Loading branch information
sophialittlejohn committed Aug 9, 2024
1 parent 368da6c commit b42647c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
21 changes: 9 additions & 12 deletions centrifuge-app/src/pages/Loan/ExternalRepayForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ export function ExternalRepayForm({ loan, destination }: { loan: ExternalLoan; d
const destinationLoan = loans?.find((l) => l.id === destination) as ActiveLoan
const displayCurrency = destination === 'reserve' ? pool.currency.symbol : 'USD'
const utils = useCentrifugeUtils()
const [usedMaxInterest, setUsedMaxInterest] = React.useState(false)

const { execute: doRepayTransaction, isLoading: isRepayLoading } = useCentrifugeTransaction(
'Sell asset',
Expand Down Expand Up @@ -109,8 +108,7 @@ export function ExternalRepayForm({ loan, destination }: { loan: ExternalLoan; d
const amountAdditional = CurrencyBalance.fromFloat(values.amountAdditional || 0, pool.currency.decimals)
const quantity = Price.fromFloat(values.quantity || 0)

if (usedMaxInterest) {
const time = Date.now() - loan.fetchedAt.getTime()
if (interest.toDecimal().eq(maxInterest) && quantity.toDecimal().eq(maxQuantity.toDecimal())) {
const outstandingInterest =
'outstandingInterest' in loan
? loan.outstandingInterest
Expand All @@ -120,6 +118,8 @@ export function ExternalRepayForm({ loan, destination }: { loan: ExternalLoan; d
? loan.outstandingPrincipal
: CurrencyBalance.fromFloat(0, pool.currency.decimals)

const fiveMinuteBuffer = 5 * 60 * 1000
const time = Date.now() + fiveMinuteBuffer - loan.fetchedAt.getTime()
const mostUpToDateInterest = CurrencyBalance.fromFloat(
outstandingPrincipal
.toDecimal()
Expand All @@ -130,7 +130,7 @@ export function ExternalRepayForm({ loan, destination }: { loan: ExternalLoan; d
)
interest = mostUpToDateInterest
console.log(
`Repaying with the most up to date outstanding interest: ${mostUpToDateInterest.toDecimal()} instead of ${outstandingInterest.toDecimal()}`,
`Repaying with interest including buffer ${mostUpToDateInterest.toDecimal()} instead of ${outstandingInterest.toDecimal()}`,
loan.pricing.interestRate.toDecimal().toString()
)
}
Expand All @@ -146,15 +146,15 @@ export function ExternalRepayForm({ loan, destination }: { loan: ExternalLoan; d
const repayFormRef = React.useRef<HTMLFormElement>(null)
useFocusInvalidInput(repayForm, repayFormRef)

const { maxAvailable, maxInterest, totalRepay, maxQuantity, principalAmount } = React.useMemo(() => {
const { maxAvailable, maxInterest, totalRepay, maxQuantity, principal } = React.useMemo(() => {
const outstandingInterest = 'outstandingInterest' in loan ? loan.outstandingInterest.toDecimal() : Dec(0)
const { quantity, interest, price, amountAdditional } = repayForm.values
const totalRepay = Dec(price || 0)
.mul(quantity || 0)
.add(interest || 0)
.add(amountAdditional || 0)

const principalAmount = Dec(price || 0).mul(quantity || 0)
const principal = Dec(price || 0).mul(quantity || 0)

const maxInterest = outstandingInterest
let maxQuantity = loan.pricing.outstandingQuantity
Expand All @@ -170,7 +170,7 @@ export function ExternalRepayForm({ loan, destination }: { loan: ExternalLoan; d
maxInterest,
maxQuantity,
totalRepay,
principalAmount,
principal,
}
}, [loan, balance, repayForm.values, destination])

Expand Down Expand Up @@ -229,7 +229,7 @@ export function ExternalRepayForm({ loan, destination }: { loan: ExternalLoan; d
label="Principal"
disabled={true}
currency={displayCurrency}
value={principalAmount.toNumber()}
value={principal.toNumber()}
/>
</Box>
</Shelf>
Expand All @@ -250,10 +250,7 @@ export function ExternalRepayForm({ loan, destination }: { loan: ExternalLoan; d
disabled={isRepayLoading}
currency={displayCurrency}
onChange={(value) => form.setFieldValue('interest', value)}
onSetMax={() => {
setUsedMaxInterest(true)
form.setFieldValue('interest', maxInterest.toNumber())
}}
onSetMax={() => form.setFieldValue('interest', maxInterest.toNumber())}
/>
)
}}
Expand Down
13 changes: 5 additions & 8 deletions centrifuge-app/src/pages/Loan/RepayForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ function InternalRepayForm({ loan, destination }: { loan: ActiveLoan | CreatedLo
const destinationLoan = loans?.find((l) => l.id === destination) as Loan
const displayCurrency = destination === 'reserve' ? pool.currency.symbol : 'USD'
const utils = useCentrifugeUtils()
const [usedMaxInterest, setUsedMaxInterest] = React.useState(false)

const { execute: doRepayTransaction, isLoading: isRepayLoading } = useCentrifugeTransaction(
isCashLoan(loan) ? 'Withdraw funds' : 'Repay asset',
Expand Down Expand Up @@ -143,8 +142,7 @@ function InternalRepayForm({ loan, destination }: { loan: ActiveLoan | CreatedLo
const additionalAmount = CurrencyBalance.fromFloat(values.amountAdditional || 0, pool.currency.decimals)
const principal = CurrencyBalance.fromFloat(values.principal || 0, pool.currency.decimals)

if (usedMaxInterest) {
const time = Date.now() - loan.fetchedAt.getTime()
if (interest.toDecimal().eq(maxInterest) && principal.toDecimal().eq(maxPrincipal)) {
const outstandingInterest =
'outstandingInterest' in loan
? loan.outstandingInterest
Expand All @@ -154,6 +152,8 @@ function InternalRepayForm({ loan, destination }: { loan: ActiveLoan | CreatedLo
? loan.outstandingPrincipal
: CurrencyBalance.fromFloat(0, pool.currency.decimals)

const fiveMinuteBuffer = 5 * 60 * 1000
const time = Date.now() + fiveMinuteBuffer - loan.fetchedAt.getTime()
const mostUpToDateInterest = CurrencyBalance.fromFloat(
outstandingPrincipal
.toDecimal()
Expand All @@ -164,7 +164,7 @@ function InternalRepayForm({ loan, destination }: { loan: ActiveLoan | CreatedLo
)
interest = mostUpToDateInterest
console.log(
`Repaying with the most up to date outstanding interest: ${mostUpToDateInterest.toDecimal()} instead of ${outstandingInterest.toDecimal()}`,
`Repaying with interest including buffer ${mostUpToDateInterest.toDecimal()} instead of ${outstandingInterest.toDecimal()}`,
loan.pricing.interestRate.toDecimal().toString()
)
}
Expand Down Expand Up @@ -257,10 +257,7 @@ function InternalRepayForm({ loan, destination }: { loan: ActiveLoan | CreatedLo
disabled={isRepayLoading}
currency={displayCurrency}
onChange={(value) => form.setFieldValue('interest', value)}
onSetMax={() => {
setUsedMaxInterest(true)
form.setFieldValue('interest', maxInterest.gte(0) ? maxInterest : 0)
}}
onSetMax={() => form.setFieldValue('interest', maxInterest.gte(0) ? maxInterest : 0)}
/>
)
}}
Expand Down

0 comments on commit b42647c

Please sign in to comment.