From ed7006133c001028ea41e76a9b9d811daef004dd Mon Sep 17 00:00:00 2001 From: JP Angelle Date: Thu, 14 Sep 2023 12:12:48 -0500 Subject: [PATCH] address pr feedback --- .../pages/IssuerPool/Assets/CreateLoan.tsx | 4 +- .../src/pages/Loan/ExternalFinanceForm.tsx | 41 +++++++++++-------- .../src/pages/Loan/OraclePriceForm.tsx | 7 ++-- .../src/pages/Loan/TransactionTable.tsx | 7 ++-- centrifuge-app/src/pages/Loan/index.tsx | 1 + 5 files changed, 34 insertions(+), 26 deletions(-) diff --git a/centrifuge-app/src/pages/IssuerPool/Assets/CreateLoan.tsx b/centrifuge-app/src/pages/IssuerPool/Assets/CreateLoan.tsx index fa7c549591..49ec4609e1 100644 --- a/centrifuge-app/src/pages/IssuerPool/Assets/CreateLoan.tsx +++ b/centrifuge-app/src/pages/IssuerPool/Assets/CreateLoan.tsx @@ -1,4 +1,4 @@ -import { CurrencyBalance, Rate } from '@centrifuge/centrifuge-js' +import { CurrencyBalance, Price, Rate } from '@centrifuge/centrifuge-js' import { formatBalance, Transaction, @@ -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, diff --git a/centrifuge-app/src/pages/Loan/ExternalFinanceForm.tsx b/centrifuge-app/src/pages/Loan/ExternalFinanceForm.tsx index 5e5b0ed194..a3434ee503 100644 --- a/centrifuge-app/src/pages/Loan/ExternalFinanceForm.tsx +++ b/centrifuge-app/src/pages/Loan/ExternalFinanceForm.tsx @@ -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' @@ -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) @@ -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) @@ -145,11 +145,13 @@ 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, @@ -157,11 +159,12 @@ export function ExternalFinanceForm({ loan }: { loan: LoanType }) { )})` : '' }, - (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)})` : '' } @@ -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 */} {'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) : ''} @@ -252,11 +255,13 @@ export function ExternalFinanceForm({ loan }: { loan: LoanType }) { { + (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, @@ -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" diff --git a/centrifuge-app/src/pages/Loan/OraclePriceForm.tsx b/centrifuge-app/src/pages/Loan/OraclePriceForm.tsx index f16e050916..1784419876 100644 --- a/centrifuge-app/src/pages/Loan/OraclePriceForm.tsx +++ b/centrifuge-app/src/pages/Loan/OraclePriceForm.tsx @@ -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' @@ -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) => { @@ -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) }, diff --git a/centrifuge-app/src/pages/Loan/TransactionTable.tsx b/centrifuge-app/src/pages/Loan/TransactionTable.tsx index 5d8f341b45..fb3994ebd9 100644 --- a/centrifuge-app/src/pages/Loan/TransactionTable.tsx +++ b/centrifuge-app/src/pages/Loan/TransactionTable.tsx @@ -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) { @@ -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') { @@ -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' diff --git a/centrifuge-app/src/pages/Loan/index.tsx b/centrifuge-app/src/pages/Loan/index.tsx index 2533b47e91..c25ec6aac4 100644 --- a/centrifuge-app/src/pages/Loan/index.tsx +++ b/centrifuge-app/src/pages/Loan/index.tsx @@ -275,6 +275,7 @@ const Loan: React.FC<{ setShowOraclePricing?: () => void }> = ({ setShowOraclePr ? 'external' : 'internal' } + decimals={pool.currency.decimals} /> ) : null}