Skip to content

Commit

Permalink
fix oracle loan financing
Browse files Browse the repository at this point in the history
  • Loading branch information
onnovisser committed Aug 8, 2023
1 parent f20576f commit 53768ae
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 70 deletions.
3 changes: 2 additions & 1 deletion centrifuge-app/src/pages/IssuerPool/Assets/CreateLoan.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,12 @@ function IssuerCreateLoan() {
? {
valuationMethod: values.pricing.valuationMethod,
maxBorrowAmount: values.pricing.maxBorrowQuantity
? CurrencyBalance.fromFloat(values.pricing.maxBorrowQuantity, decimals).toString()
? CurrencyBalance.fromFloat(values.pricing.maxBorrowQuantity, decimals)
: null,
Isin: values.pricing.Isin || '',
maturityDate: new Date(values.pricing.maturityDate),
interestRate: Rate.fromPercent(values.pricing.interestRate),
notional: CurrencyBalance.fromFloat(values.pricing.notional, decimals),
}
: {
valuationMethod: values.pricing.valuationMethod,
Expand Down
50 changes: 31 additions & 19 deletions centrifuge-app/src/pages/Loan/ExternalFinanceForm.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CurrencyBalance, findBalance, Loan as LoanType, Rate } from '@centrifuge/centrifuge-js'
import { CurrencyBalance, ExternalPricingInfo, findBalance, Loan as LoanType } from '@centrifuge/centrifuge-js'
import { useBalances, useCentrifugeTransaction } from '@centrifuge/centrifuge-react'
import {
Button,
Expand Down Expand Up @@ -70,14 +70,20 @@ export function ExternalFinanceForm({ loan }: { loan: LoanType }) {
quantity: '',
},
onSubmit: (values, actions) => {
const price = Rate.fromFloat(values.price)
const amount = new CurrencyBalance(
price.muln(values.quantity || 0).div(new BN(10).pow(new BN(27 - pool.currency.decimals))),
pool.currency.decimals
const price = CurrencyBalance.fromFloat(values.price, pool.currency.decimals)
const quantity = CurrencyBalance.fromFloat(
values.quantity,
27 // TODO: Will be 18 decimals after next chain update
)

// @ts-expect-error
doFinanceTransaction([loan.poolId, loan.id, amount, price, loan.pricing.Isin, account.actingAddress])
doFinanceTransaction([
loan.poolId,
loan.id,
quantity,
price,
(loan.pricing as ExternalPricingInfo).Isin,
account.actingAddress,
])
actions.setSubmitting(false)
},
validateOnMount: true,
Expand All @@ -89,13 +95,19 @@ export function ExternalFinanceForm({ loan }: { loan: LoanType }) {
quantity: '',
},
onSubmit: (values, actions) => {
const price = Rate.fromFloat(values.price)
const amount = new CurrencyBalance(
price.muln(values.quantity || 0).div(new BN(10).pow(new BN(27 - pool.currency.decimals))),
pool.currency.decimals
)
// @ts-expect-error
doRepayTransaction([loan.poolId, loan.id, amount, new BN(0), price, loan.pricing.Isin, account.actingAddress])
const price = CurrencyBalance.fromFloat(values.price, pool.currency.decimals)
const quantity = CurrencyBalance.fromFloat(values.quantity, 27)

doRepayTransaction([
loan.poolId,
loan.id,
quantity,
new BN(0),
new BN(0),
price,
(loan.pricing as ExternalPricingInfo).Isin,
account.actingAddress,
])
actions.setSubmitting(false)
},
validateOnMount: true,
Expand Down Expand Up @@ -227,11 +239,11 @@ export function ExternalFinanceForm({ loan }: { loan: LoanType }) {
{/* outstandingDebt needs to be rounded down, b/c onSetMax displays the rounded down value as well */}
<Text variant="label1">
{'valuationMethod' in loan.pricing && loan.pricing.valuationMethod === 'oracle'
? `${
loan.pricing.outstandingQuantity
.div(new BN(10).pow(new BN(pool?.currency.decimals - 2)))
.toNumber() / 100
} @ ${formatBalance(loan.pricing.oracle.value.toDecimal(), pool?.currency.symbol, 2)}`
? `${loan.pricing.outstandingQuantity.toFloat()} @ ${formatBalance(
loan.pricing.oracle.value,
pool?.currency.symbol,
2
)}`
: ''}
</Text>
</Shelf>
Expand Down
4 changes: 1 addition & 3 deletions centrifuge-app/src/pages/Loan/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,7 @@ const Loan: React.FC<{ setShowOraclePricing?: () => void }> = ({ setShowOraclePr
},
{
label: 'Quantity',
value: formatBalance(
new CurrencyBalance(loan.pricing.outstandingQuantity, pool?.currency.decimals)
),
value: formatBalance(loan.pricing.outstandingQuantity),
},
{
label: 'Price',
Expand Down
35 changes: 35 additions & 0 deletions centrifuge-js/src/CentrifugeBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ const parachainTypes = {
Staking: 'StakingCurrency',
},
},
ActiveLoanInfo: {
interest_accrued: 'u128',
present_value: 'u128',
},
}

const parachainRpcMethods: Record<string, Record<string, DefinitionRpc>> = {
Expand Down Expand Up @@ -187,6 +191,37 @@ const parachainRuntimeApi: DefinitionsCall = {
version: 1,
},
],
LoansApi: [
{
methods: {
portfolio: {
description: 'Get active pool loan',
params: [
{
name: 'pool_id',
type: 'u64',
},
],
type: 'Vec<(u64, ActiveLoanInfo)>',
},
portfolio_loan: {
description: 'Get active pool loan',
params: [
{
name: 'pool_id',
type: 'u64',
},
{
name: 'loan_id',
type: 'u64',
},
],
type: 'Option<ActiveLoanInfo>',
},
},
version: 1,
},
],
}

type Events = ISubmittableResult['events']
Expand Down
Loading

0 comments on commit 53768ae

Please sign in to comment.