From 82a5696b5ba1bc4eb84008f649921f4eeea3700c Mon Sep 17 00:00:00 2001 From: JP Angelle Date: Fri, 15 Sep 2023 18:04:04 -0500 Subject: [PATCH] fix decimals --- .../src/pages/Loan/ExternalFinanceForm.tsx | 18 +++++--- .../src/pages/Loan/HoldingsValues.tsx | 42 ++++++++++++++----- .../src/pages/Loan/TransactionTable.tsx | 19 ++++++--- centrifuge-app/src/pages/Loan/index.tsx | 2 +- 4 files changed, 57 insertions(+), 24 deletions(-) diff --git a/centrifuge-app/src/pages/Loan/ExternalFinanceForm.tsx b/centrifuge-app/src/pages/Loan/ExternalFinanceForm.tsx index a3434ee503..559d31c52f 100644 --- a/centrifuge-app/src/pages/Loan/ExternalFinanceForm.tsx +++ b/centrifuge-app/src/pages/Loan/ExternalFinanceForm.tsx @@ -60,7 +60,7 @@ export function ExternalFinanceForm({ loan }: { loan: LoanType }) { faceValue: '', }, onSubmit: (values, actions) => { - const price = CurrencyBalance.fromFloat(values.price, pool.currency.decimals) + const price = CurrencyBalance.fromFloat(values.price, pool.currency.decimals).div(new BN(100)) const quantity = Price.fromFloat((values.faceValue as number) / (values.price as number)) doFinanceTransaction([loan.poolId, loan.id, quantity, price, account.actingAddress]) @@ -75,7 +75,7 @@ export function ExternalFinanceForm({ loan }: { loan: LoanType }) { faceValue: '', }, onSubmit: (values, actions) => { - const price = CurrencyBalance.fromFloat(values.price, pool.currency.decimals) + const price = CurrencyBalance.fromFloat(values.price, pool.currency.decimals).div(new BN(100)) 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]) @@ -105,13 +105,19 @@ export function ExternalFinanceForm({ loan }: { loan: LoanType }) { const currentFace = borrowerAssetTransactions?.reduce((sum, trx) => { if (trx.type === 'BORROWED') { - sum = new CurrencyBalance(sum.add(trx.amount || new CurrencyBalance(0, 27)), 27) + sum = new CurrencyBalance( + sum.add(trx.amount ? new BN(trx.amount).mul(new BN(100)) : new CurrencyBalance(0, pool.currency.decimals)), + pool.currency.decimals + ) } if (trx.type === 'REPAID') { - sum = new CurrencyBalance(sum.sub(trx.amount || new CurrencyBalance(0, 27)), 27) + sum = new CurrencyBalance( + sum.sub(trx.amount ? new BN(trx.amount).mul(new BN(100)) : new CurrencyBalance(0, pool.currency.decimals)), + pool.currency.decimals + ) } return sum - }, new CurrencyBalance(0, 27)) || new CurrencyBalance(0, 27) + }, new CurrencyBalance(0, pool.currency.decimals)) || new CurrencyBalance(0, pool.currency.decimals) return ( @@ -222,7 +228,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(currentFace, pool.currency.symbol, 6, 2) + ? formatBalance(currentFace, pool.currency.symbol, 2, 2) : ''} diff --git a/centrifuge-app/src/pages/Loan/HoldingsValues.tsx b/centrifuge-app/src/pages/Loan/HoldingsValues.tsx index 65c73d534f..9dc23b12a1 100644 --- a/centrifuge-app/src/pages/Loan/HoldingsValues.tsx +++ b/centrifuge-app/src/pages/Loan/HoldingsValues.tsx @@ -1,6 +1,7 @@ import { BorrowerTransaction, CurrencyBalance, Pool } from '@centrifuge/centrifuge-js' import BN from 'bn.js' import { LabelValueStack } from '../../components/LabelValueStack' +import { Dec } from '../../utils/Decimal' import { formatBalance } from '../../utils/formatting' type Props = { @@ -14,51 +15,70 @@ export function HoldingsValues({ pool, transactions }: Props) { if (trx.type === 'REPAID') { sum = new CurrencyBalance( sum.sub( - trx.amount && trx.settlementPrice ? trx.amount.mul(new BN(trx.settlementPrice)) : new CurrencyBalance(0, 27) + trx.amount && trx.settlementPrice + ? new BN(trx.amount.mul(new BN(trx.settlementPrice).mul(new BN(100)))).div( + new BN(10).pow(new BN(pool.currency.decimals)) + ) + : new CurrencyBalance(0, pool.currency.decimals) ), - 27 + pool.currency.decimals ) } if (trx.type === 'BORROWED') { sum = new CurrencyBalance( sum.add( - trx.amount && trx.settlementPrice ? trx.amount.mul(new BN(trx.settlementPrice)) : new CurrencyBalance(0, 27) + trx.amount && trx.settlementPrice + ? new BN(trx.amount.mul(new BN(trx.settlementPrice).mul(new BN(100)))).div( + new BN(10).pow(new BN(pool.currency.decimals)) + ) + : new CurrencyBalance(0, pool.currency.decimals) ), - 27 + pool.currency.decimals ) } return sum - }, new CurrencyBalance(0, 27)) || new CurrencyBalance(0, 27) + }, new CurrencyBalance(0, pool.currency.decimals)) || new CurrencyBalance(0, pool.currency.decimals) const currentFace = transactions?.reduce((sum, trx) => { if (trx.type === 'BORROWED') { - sum = new CurrencyBalance(sum.add(trx.amount || new CurrencyBalance(0, 27)), 27) + sum = new CurrencyBalance( + sum.add(trx.amount ? new BN(trx.amount).mul(new BN(100)) : new CurrencyBalance(0, pool.currency.decimals)), + pool.currency.decimals + ) } if (trx.type === 'REPAID') { - sum = new CurrencyBalance(sum.sub(trx.amount || new CurrencyBalance(0, 27)), 27) + sum = new CurrencyBalance( + sum.sub(trx.amount ? new BN(trx.amount).mul(new BN(100)) : new CurrencyBalance(0, pool.currency.decimals)), + pool.currency.decimals + ) } return sum - }, new CurrencyBalance(0, 27)) || new CurrencyBalance(0, 27) + }, new CurrencyBalance(0, pool.currency.decimals)) || new CurrencyBalance(0, pool.currency.decimals) return ( <> diff --git a/centrifuge-app/src/pages/Loan/TransactionTable.tsx b/centrifuge-app/src/pages/Loan/TransactionTable.tsx index fb3994ebd9..c568139a9a 100644 --- a/centrifuge-app/src/pages/Loan/TransactionTable.tsx +++ b/centrifuge-app/src/pages/Loan/TransactionTable.tsx @@ -1,6 +1,7 @@ import { BorrowerTransaction, CurrencyBalance } from '@centrifuge/centrifuge-js' import { BorrowerTransactionType } from '@centrifuge/centrifuge-js/dist/types/subquery' import { StatusChip, Tooltip } from '@centrifuge/fabric' +import BN from 'bn.js' import { useMemo } from 'react' import { DataTable } from '../../components/DataTable' import { formatDate } from '../../utils/date' @@ -34,7 +35,9 @@ export const TransactionTable = ({ transactions, currency, loanType, decimals }: return sortedTransactions.map((transaction, index, array) => ({ type: transaction.type, transactionDate: transaction.timestamp, - settlePrice: transaction.settlementPrice ? new CurrencyBalance(transaction.settlementPrice, decimals) : null, + settlePrice: transaction.settlementPrice + ? new CurrencyBalance(new BN(transaction.settlementPrice).mul(new BN(100)), decimals) + : null, faceFlow: transaction.amount, position: array.slice(0, index + 1).reduce((sum, trx) => { if (trx.type === 'BORROWED') { @@ -93,9 +96,9 @@ export const TransactionTable = ({ transactions, currency, loanType, decimals }: cell: (row) => row.faceFlow ? `${row.type === 'REPAID' ? '-' : ''}${formatBalance( - new CurrencyBalance(row.faceFlow, 24), + new CurrencyBalance(new BN(row.faceFlow).mul(new BN(100)), decimals), currency, - 6, + 2, 2 )}` : '-', @@ -113,9 +116,11 @@ export const TransactionTable = ({ transactions, currency, loanType, decimals }: cell: (row) => row.faceFlow && row.settlePrice ? `${row.type === 'BORROWED' ? '-' : ''}${formatBalance( - new CurrencyBalance(row.faceFlow.mul(row.settlePrice), 32), + new CurrencyBalance(row.faceFlow, decimals) + .toDecimal() + .mul(new CurrencyBalance(row.settlePrice, decimals).toDecimal()), currency, - 6, + 2, 2 )}` : '-', @@ -125,7 +130,9 @@ export const TransactionTable = ({ transactions, currency, loanType, decimals }: align: 'left', header: 'Position', cell: (row) => - row.position.isZero() ? '-' : formatBalance(new CurrencyBalance(row.position, 24), currency, 6, 2), + row.position.isZero() + ? '-' + : formatBalance(new CurrencyBalance(new BN(row.position).mul(new BN(100)), decimals), currency, 2, 2), flex: '3', }, // TODO: add link to transaction diff --git a/centrifuge-app/src/pages/Loan/index.tsx b/centrifuge-app/src/pages/Loan/index.tsx index c25ec6aac4..a94f5a84d2 100644 --- a/centrifuge-app/src/pages/Loan/index.tsx +++ b/centrifuge-app/src/pages/Loan/index.tsx @@ -186,7 +186,7 @@ const Loan: React.FC<{ setShowOraclePricing?: () => void }> = ({ setShowOraclePr ? [ { label: 'Current value', - value: `${formatBalance(loan.presentValue, pool.currency.symbol, 6, 2)}`, + value: `${formatBalance(loan.presentValue, pool.currency.symbol, 2, 2)}`, }, ] : []),