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

feat: implement oracle UI #1476

Merged
merged 20 commits into from
Jun 30, 2023
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: 7 additions & 1 deletion centrifuge-app/src/components/LoanLabel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ export function getLoanLabelStatus(l: Loan | TinlakeLoan): [LabelStatus, string]
today.setUTCHours(0, 0, 0, 0)
if (l.status === 'Active' && (l as ActiveLoan).writeOffStatus) return ['critical', 'Write-off']
if (l.status === 'Closed') return ['ok', 'Repaid']
if (l.status === 'Active' && l.pricing.interestRate?.gtn(0) && l.totalBorrowed?.isZero()) return ['default', 'Ready']
if (
l.status === 'Active' &&
'interestRate' in l.pricing &&
l.pricing.interestRate?.gtn(0) &&
l.totalBorrowed?.isZero()
)
return ['default', 'Ready']
if (l.status === 'Created') return ['default', 'Created']

if (l.status === 'Active' && 'maturityDate' in l.pricing && l.pricing.maturityDate) {
Expand Down
3 changes: 2 additions & 1 deletion centrifuge-app/src/components/LoanList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export function LoanList({ loans }: Props) {
originationDateSortKey:
loan.status === 'Active' &&
loan?.originationDate &&
'interestRate' in loan.pricing &&
!loan?.pricing.interestRate?.isZero() &&
!loan?.totalBorrowed?.isZero()
? loan.originationDate
Expand Down Expand Up @@ -167,7 +168,7 @@ function Amount({ loan }: { loan: Row }) {
return formatBalance(l.totalRepaid, pool?.currency.symbol)

case 'Active':
if (l.pricing.interestRate?.gtn(0) && l.totalBorrowed?.isZero()) {
if ('interestRate' in l.pricing && l.pricing.interestRate?.gtn(0) && l.totalBorrowed?.isZero()) {
return formatBalance(current, pool?.currency.symbol)
}

Expand Down
22 changes: 9 additions & 13 deletions centrifuge-app/src/components/Report.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Loan, Pool, Rate } from '@centrifuge/centrifuge-js'
import { Pool, Rate } from '@centrifuge/centrifuge-js'
import { Stack, Text } from '@centrifuge/fabric'
import * as React from 'react'
import styled from 'styled-components'
Expand Down Expand Up @@ -254,25 +254,21 @@ export const ReportComponent: React.FC<Props> = ({ pool, report, exportRef, cust
value: [
loan.id,
loan.status === 'Created' ? 'New' : loan.status,
(loan as Loan).pricing.value ? formatBalance((loan as Loan).pricing.value.toDecimal()) : '-',
'value' in loan.pricing ? formatBalance(loan.pricing.value.toDecimal()) : '-',
'outstandingDebt' in loan ? formatBalance(loan.outstandingDebt.toDecimal()) : '-',
'totalBorrowed' in loan ? formatBalance(loan.totalBorrowed.toDecimal()) : '-',
'totalRepaid' in loan ? formatBalance(loan.totalRepaid.toDecimal()) : '-',
'originationDate' in loan ? formatDate(loan.originationDate) : '-',
formatDate(loan.pricing.maturityDate),
formatPercentage(loan.pricing.interestRate.toPercent()),
(loan as Loan).pricing.advanceRate
? formatPercentage((loan as Loan).pricing.advanceRate.toPercent())
'interestRate' in loan.pricing ? formatPercentage(loan.pricing.interestRate.toPercent()) : '-',
'advanceRate' in loan.pricing ? formatPercentage(loan.pricing.advanceRate.toPercent()) : '-',
'probabilityOfDefault' in loan.pricing
? formatPercentage((loan.pricing.probabilityOfDefault as Rate).toPercent())
: '-',
(loan as Loan).pricing.probabilityOfDefault
? formatPercentage(((loan as Loan).pricing.probabilityOfDefault as Rate).toPercent())
: '-',
(loan as Loan).pricing.lossGivenDefault
? formatPercentage(((loan as Loan).pricing.lossGivenDefault as Rate).toPercent())
: '-',
(loan as Loan).pricing.discountRate
? formatPercentage(((loan as Loan).pricing.discountRate as Rate).toPercent())
'lossGivenDefault' in loan.pricing
? formatPercentage((loan.pricing.lossGivenDefault as Rate).toPercent())
: '-',
'discountRate' in loan.pricing ? formatPercentage((loan.pricing.discountRate as Rate).toPercent()) : '-',
// formatDate(loan.maturityDate.toString()),
],
heading: false,
Expand Down
1 change: 1 addition & 0 deletions centrifuge-app/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ const CENTRIFUGE: EnvironmentConfig = {
'Residential Real Estate',
'Project Finance',
'Trade Finance',
'US Treasuries',
],
defaultAssetClass: 'Consumer Credit',
poolCreationType,
Expand Down
3 changes: 3 additions & 0 deletions centrifuge-app/src/pages/IssuerCreatePool/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
combineAsync,
imageFile,
integer,
isin,
max,
maxDecimals,
maxFileSize,
Expand Down Expand Up @@ -56,6 +57,8 @@ export const validate = {
probabilityOfDefault: combine(required(), nonNegativeNumber(), max(100)),
discountRate: combine(required(), nonNegativeNumber(), max(100)),
lossGivenDefault: combine(required(), nonNegativeNumber(), max(100)),
maxBorrowQuantity: combine(required(), nonNegativeNumber(), max(Number.MAX_SAFE_INTEGER)),
Isin: combine(required(), minLength(12), maxLength(12), isin()),

// write-off groups
days: combine(required(), integer(), nonNegativeNumber(), max(Number.MAX_SAFE_INTEGER)),
Expand Down
38 changes: 25 additions & 13 deletions centrifuge-app/src/pages/IssuerPool/Assets/CreateLoan.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export type CreateLoanFormValues = {
assetName: string
attributes: Record<string, string | number>
pricing: {
valuationMethod: 'discountedCashFlow' | 'outstandingDebt'
valuationMethod: 'discountedCashFlow' | 'outstandingDebt' | 'oracle'
maxBorrowAmount: 'upToTotalBorrowed' | 'upToOutstandingDebt'
value: number | ''
maturityDate: string
Expand All @@ -66,6 +66,8 @@ export type CreateLoanFormValues = {
probabilityOfDefault: number | ''
lossGivenDefault: number | ''
discountRate: number | ''
maxBorrowQuantity: number | ''
Isin: string
}
}

Expand Down Expand Up @@ -172,7 +174,7 @@ function IssuerCreateLoan() {
const { assetOriginators } = usePoolAccess(pid)
const collateralCollectionId = assetOriginators.find((ao) => ao.address === account?.actingAddress)
?.collateralCollections[0]?.id
const balances = useBalances(account.actingAddress)
const balances = useBalances(account?.actingAddress)

const { isAuthed, token } = usePodAuth(pid)

Expand Down Expand Up @@ -216,22 +218,32 @@ function IssuerCreateLoan() {
probabilityOfDefault: '',
lossGivenDefault: '',
discountRate: '',
maxBorrowQuantity: '',
Isin: '',
},
},
onSubmit: async (values, { setSubmitting }) => {
if (!podUrl || !collateralCollectionId || !account || !isAuthed || !token || !templateMetadata) return
const { decimals } = pool.currency
const pricingInfo = {
valuationMethod: values.pricing.valuationMethod,
maxBorrowAmount: values.pricing.maxBorrowAmount,
value: CurrencyBalance.fromFloat(values.pricing.value, decimals),
maturityDate: new Date(values.pricing.maturityDate),
advanceRate: Rate.fromPercent(values.pricing.advanceRate),
interestRate: Rate.fromPercent(values.pricing.interestRate),
probabilityOfDefault: Rate.fromPercent(values.pricing.probabilityOfDefault || 0),
lossGivenDefault: Rate.fromPercent(values.pricing.lossGivenDefault || 0),
discountRate: Rate.fromPercent(values.pricing.discountRate || 0),
} as const
const pricingInfo =
values.pricing.valuationMethod === 'oracle'
? {
valuationMethod: values.pricing.valuationMethod,
maxBorrowQuantity: CurrencyBalance.fromFloat(values.pricing.maxBorrowQuantity, decimals).toString(),
Isin: values.pricing.Isin || '',
maturityDate: new Date(values.pricing.maturityDate),
}
: {
valuationMethod: values.pricing.valuationMethod,
maxBorrowAmount: values.pricing.maxBorrowAmount,
value: CurrencyBalance.fromFloat(values.pricing.value, decimals),
maturityDate: new Date(values.pricing.maturityDate),
advanceRate: Rate.fromPercent(values.pricing.advanceRate),
interestRate: Rate.fromPercent(values.pricing.interestRate),
probabilityOfDefault: Rate.fromPercent(values.pricing.probabilityOfDefault || 0),
lossGivenDefault: Rate.fromPercent(values.pricing.lossGivenDefault || 0),
discountRate: Rate.fromPercent(values.pricing.discountRate || 0),
}

const txId = Math.random().toString(36).substring(2)

Expand Down
119 changes: 75 additions & 44 deletions centrifuge-app/src/pages/IssuerPool/Assets/PricingInput.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CurrencyInput, DateInput, Grid, NumberInput, Select } from '@centrifuge/fabric'
import { CurrencyInput, DateInput, Grid, NumberInput, Select, TextInput } from '@centrifuge/fabric'
import { Field, FieldProps, useFormikContext } from 'formik'
import { FieldWithErrorMessage } from '../../../components/FieldWithErrorMessage'
import { Tooltips } from '../../../components/Tooltips'
Expand All @@ -22,40 +22,65 @@ export function PricingInput({ poolId }: { poolId: string }) {
options={[
{ value: 'discountedCashFlow', label: 'Discounted cashflow' },
{ value: 'outstandingDebt', label: 'Outstanding debt' },
{ value: 'oracle', label: 'Oracle' },
]}
placeholder="Choose valuation method"
/>
)}
</Field>
<Field name="pricing.maxBorrowAmount">
{({ field, meta, form }: FieldProps) => (
<Select
{...field}
label="Borrow restriction"
onChange={(event) => form.setFieldValue('pricing.maxBorrowAmount', event.target.value, false)}
errorMessage={meta.touched && meta.error ? meta.error : undefined}
options={[
{ value: 'upToTotalBorrowed', label: 'Up to total borrowed' },
{ value: 'upToOutstandingDebt', label: 'Up to outstanding debt' },
]}
placeholder="Choose borrow restriction"
{values.pricing.valuationMethod === 'oracle' && (
<>
<FieldWithErrorMessage
as={NumberInput}
label={<Tooltips type="financingFee" variant="secondary" label="Max quantity*" />}
placeholder="0"
name="pricing.maxBorrowQuantity"
validate={validate.maxBorrowQuantity}
/>
)}
</Field>

<Field name="pricing.value" validate={combine(required(), positiveNumber(), max(Number.MAX_SAFE_INTEGER))}>
{({ field, meta, form }: FieldProps) => (
<CurrencyInput
{...field}
label="Collateral value*"
placeholder="0.00"
errorMessage={meta.touched ? meta.error : undefined}
currency={pool?.currency.symbol}
onChange={(value) => form.setFieldValue('pricing.value', value)}
variant="small"
<FieldWithErrorMessage
as={TextInput}
label={<Tooltips type="financingFee" variant="secondary" label="ISIN*" />}
placeholder="010101010000"
name="pricing.Isin"
validate={validate.Isin}
/>
)}
</Field>
</>
)}

{(values.pricing.valuationMethod === 'discountedCashFlow' ||
values.pricing.valuationMethod === 'outstandingDebt') && (
<>
<Field name="pricing.maxBorrowAmount">
{({ field, meta, form }: FieldProps) => (
<Select
{...field}
label="Borrow restriction"
onChange={(event) => form.setFieldValue('pricing.maxBorrowAmount', event.target.value, false)}
errorMessage={meta.touched && meta.error ? meta.error : undefined}
options={[
{ value: 'upToTotalBorrowed', label: 'Up to total borrowed' },
{ value: 'upToOutstandingDebt', label: 'Up to outstanding debt' },
]}
placeholder="Choose borrow restriction"
/>
)}
</Field>

<Field name="pricing.value" validate={combine(required(), positiveNumber(), max(Number.MAX_SAFE_INTEGER))}>
{({ field, meta, form }: FieldProps) => (
<CurrencyInput
{...field}
label="Collateral value*"
placeholder="0.00"
errorMessage={meta.touched ? meta.error : undefined}
currency={pool?.currency.symbol}
onChange={(value) => form.setFieldValue('pricing.value', value)}
variant="small"
/>
)}
</Field>
</>
)}
<FieldWithErrorMessage
as={DateInput}
validate={required()}
Expand All @@ -67,23 +92,29 @@ export function PricingInput({ poolId }: { poolId: string }) {
// Max 5 years from now
max={new Date(Date.now() + 5 * 365 * 24 * 60 * 60 * 1000).toISOString().slice(0, 10)}
/>
<FieldWithErrorMessage
as={NumberInput}
label={<Tooltips type="financingFee" variant="secondary" label="Financing fee*" />}
placeholder="0.00"
rightElement="%"
name="pricing.interestRate"
validate={validate.fee}
/>

<FieldWithErrorMessage
as={NumberInput}
label={<Tooltips type="advanceRate" variant="secondary" label="Advance rate*" />}
placeholder="0.00"
rightElement="%"
name="pricing.advanceRate"
validate={validate.advanceRate}
/>
{(values.pricing.valuationMethod === 'discountedCashFlow' ||
values.pricing.valuationMethod === 'outstandingDebt') && (
<>
<FieldWithErrorMessage
as={NumberInput}
label={<Tooltips type="financingFee" variant="secondary" label="Financing fee*" />}
placeholder="0.00"
rightElement="%"
name="pricing.interestRate"
validate={validate.fee}
/>

<FieldWithErrorMessage
as={NumberInput}
label={<Tooltips type="advanceRate" variant="secondary" label="Advance rate*" />}
placeholder="0.00"
rightElement="%"
name="pricing.advanceRate"
validate={validate.advanceRate}
/>
</>
)}
{values.pricing.valuationMethod === 'discountedCashFlow' && (
<>
<FieldWithErrorMessage
Expand Down
Loading
Loading