Skip to content

Commit

Permalink
address pr feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
JP Angelle committed Sep 14, 2023
1 parent eca5c79 commit ed70061
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 26 deletions.
4 changes: 2 additions & 2 deletions centrifuge-app/src/pages/IssuerPool/Assets/CreateLoan.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CurrencyBalance, Rate } from '@centrifuge/centrifuge-js'
import { CurrencyBalance, Price, Rate } from '@centrifuge/centrifuge-js'
import {
formatBalance,
Transaction,
Expand Down Expand Up @@ -242,7 +242,7 @@ function IssuerCreateLoan() {
Isin: values.pricing.Isin || '',
maturityDate: new Date(values.pricing.maturityDate),
interestRate: Rate.fromPercent(values.pricing.interestRate),
notional: CurrencyBalance.fromFloat(values.pricing.notional, 18),
notional: Price.fromFloat(values.pricing.notional),
}
: {
valuationMethod: values.pricing.valuationMethod,
Expand Down
41 changes: 24 additions & 17 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 } from '@centrifuge/centrifuge-js'
import { CurrencyBalance, findBalance, Loan as LoanType, Price } from '@centrifuge/centrifuge-js'
import { roundDown, useBalances, useCentrifugeTransaction } from '@centrifuge/centrifuge-react'
import { Box, Button, Card, CurrencyInput, Shelf, Stack, Text } from '@centrifuge/fabric'
import BN from 'bn.js'
Expand Down Expand Up @@ -61,7 +61,7 @@ export function ExternalFinanceForm({ loan }: { loan: LoanType }) {
},
onSubmit: (values, actions) => {
const price = CurrencyBalance.fromFloat(values.price, pool.currency.decimals)
const quantity = CurrencyBalance.fromFloat((values.faceValue as number) / (values.price as number), 18)
const quantity = Price.fromFloat((values.faceValue as number) / (values.price as number))

doFinanceTransaction([loan.poolId, loan.id, quantity, price, account.actingAddress])
actions.setSubmitting(false)
Expand All @@ -76,7 +76,7 @@ export function ExternalFinanceForm({ loan }: { loan: LoanType }) {
},
onSubmit: (values, actions) => {
const price = CurrencyBalance.fromFloat(values.price, pool.currency.decimals)
const quantity = CurrencyBalance.fromFloat((values.faceValue as number) / (values.price as number), 18)
const quantity = Price.fromFloat((values.faceValue as number) / (values.price as number))

doRepayTransaction([loan.poolId, loan.id, quantity, new BN(0), new BN(0), price, account.actingAddress])
actions.setSubmitting(false)
Expand Down Expand Up @@ -145,23 +145,26 @@ export function ExternalFinanceForm({ loan }: { loan: LoanType }) {
name="price"
validate={combine(
settlementPrice(),
(val: any) => {
(val) => {
const num = val instanceof Decimal ? val.toNumber() : val
const financeAmount = (num * (financeForm.values.faceValue || 1)) / 100
const financeAmount = Dec(num)
.mul(financeForm.values.faceValue || 1)
.div(100)

return financeAmount > availableFinancing.toNumber()
return financeAmount.gt(availableFinancing)
? `Amount exceeds available reserve (${formatBalance(
availableFinancing,
pool?.currency.symbol,
2
)})`
: ''
},
(val: any) => {
const num = val instanceof Decimal ? val.toNumber() : val
const financeAmount = (num * (financeForm.values.faceValue || 1)) / 100
(val) => {
const financeAmount = Dec(val)
.mul(financeForm.values.faceValue || 1)
.div(100)

return financeAmount > maxBorrow.toNumber()
return financeAmount.gt(maxBorrow)
? `Amount exceeds max borrow (${formatBalance(maxBorrow, pool?.currency.symbol, 2)})`
: ''
}
Expand Down Expand Up @@ -219,7 +222,7 @@ export function ExternalFinanceForm({ loan }: { loan: LoanType }) {
{/* outstandingDebt needs to be rounded down, b/c onSetMax displays the rounded down value as well */}
<Text variant="label2">
{'valuationMethod' in loan.pricing && loan.pricing.valuationMethod === 'oracle'
? formatBalance(new CurrencyBalance(currentFace, 24), pool.currency.symbol, 6, 2)
? formatBalance(currentFace, pool.currency.symbol, 6, 2)
: ''}
</Text>
</Shelf>
Expand Down Expand Up @@ -252,11 +255,13 @@ export function ExternalFinanceForm({ loan }: { loan: LoanType }) {
<Field
validate={combine(
settlementPrice(),
(val: any) => {
(val) => {
const num = val instanceof Decimal ? val.toNumber() : val
const repayAmount = (num * (repayForm.values.faceValue || 1)) / 100
const repayAmount = Dec(num)
.mul(repayForm.values.faceValue || 1)
.div(100)

return repayAmount > balance.toNumber()
return repayAmount.gt(balance)
? `Your wallet balance (${formatBalance(
roundDown(balance),
pool?.currency.symbol,
Expand All @@ -265,11 +270,13 @@ export function ExternalFinanceForm({ loan }: { loan: LoanType }) {
the outstanding balance.`
: ''
},
(val: any) => {
(val) => {
const num = val instanceof Decimal ? val.toNumber() : val
const repayAmount = (num * (repayForm.values.faceValue || 1)) / 100
const repayAmount = Dec(num)
.mul(repayForm.values.faceValue || 1)
.div(100)

return repayAmount > debt.toNumber() ? 'Amount exceeds outstanding' : ''
return repayAmount.gt(debt) ? 'Amount exceeds outstanding' : ''
}
)}
name="price"
Expand Down
7 changes: 3 additions & 4 deletions centrifuge-app/src/pages/Loan/OraclePriceForm.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { CurrencyBalance, Loan as LoanType, Rate } from '@centrifuge/centrifuge-js'
import { CurrencyBalance, Loan as LoanType, Price } from '@centrifuge/centrifuge-js'
import { useAddress, useCentrifugeTransaction } from '@centrifuge/centrifuge-react'
import { Box, Button, Card, CurrencyInput, Flex, IconArrowDown, Stack, Text } from '@centrifuge/fabric'
import BN from 'bn.js'
import Decimal from 'decimal.js-light'
import { Field, FieldProps, Form, FormikProvider, useFormik } from 'formik'
import * as React from 'react'
Expand Down Expand Up @@ -29,7 +28,7 @@ export function OraclePriceForm({

const { execute: doOraclePriceTransaction, isLoading: isOraclePriceLoading } = useCentrifugeTransaction(
'Set oracle price',
(cent) => (args: [price: string], options) => {
(cent) => (args: [price: Price], options) => {
const [price] = args
return cent.getApi().pipe(
switchMap((api) => {
Expand Down Expand Up @@ -57,7 +56,7 @@ export function OraclePriceForm({
newPrice: '',
},
onSubmit: (values, actions) => {
const newPrice = new BN(Rate.fromFloat(values.newPrice).toString()).div(new BN(10).pow(new BN(9))).toString()
const newPrice = Price.fromFloat(values.newPrice)
doOraclePriceTransaction([newPrice])
actions.setSubmitting(false)
},
Expand Down
7 changes: 4 additions & 3 deletions centrifuge-app/src/pages/Loan/TransactionTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import { formatBalance } from '../../utils/formatting'
type Props = {
transactions: BorrowerTransaction[]
currency: string
decimals: number
loanType: 'external' | 'internal'
}

export const TransactionTable = ({ transactions, currency, loanType }: Props) => {
export const TransactionTable = ({ transactions, currency, loanType, decimals }: Props) => {
const assetTransactions = useMemo(() => {
const sortedTransactions = transactions.sort((a, b) => {
if (a.timestamp > b.timestamp) {
Expand All @@ -33,7 +34,7 @@ export const TransactionTable = ({ transactions, currency, loanType }: Props) =>
return sortedTransactions.map((transaction, index, array) => ({
type: transaction.type,
transactionDate: transaction.timestamp,
settlePrice: transaction.settlementPrice ? new CurrencyBalance(transaction.settlementPrice, 6) : null,
settlePrice: transaction.settlementPrice ? new CurrencyBalance(transaction.settlementPrice, decimals) : null,
faceFlow: transaction.amount,
position: array.slice(0, index + 1).reduce((sum, trx) => {
if (trx.type === 'BORROWED') {
Expand All @@ -45,7 +46,7 @@ export const TransactionTable = ({ transactions, currency, loanType }: Props) =>
return sum
}, new CurrencyBalance(0, 27)),
}))
}, [transactions])
}, [transactions, decimals])

const getStatusChipType = (type: BorrowerTransactionType) => {
if (type === 'BORROWED' || type === 'CREATED' || type === 'PRICED') return 'info'
Expand Down
1 change: 1 addition & 0 deletions centrifuge-app/src/pages/Loan/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ const Loan: React.FC<{ setShowOraclePricing?: () => void }> = ({ setShowOraclePr
? 'external'
: 'internal'
}
decimals={pool.currency.decimals}
/>
</PageSection>
) : null}
Expand Down

0 comments on commit ed70061

Please sign in to comment.