From 22b888758ec8db112cf99dae3315da599c526bc5 Mon Sep 17 00:00:00 2001 From: sophian Date: Fri, 15 Sep 2023 12:08:41 -0400 Subject: [PATCH] Use CurrencyBalance instead of Dec and convert settlement price to CurrencyBalance --- .../src/pages/Loan/HoldingsValues.tsx | 58 ++++++++++++++----- centrifuge-js/src/modules/pools.ts | 1 + 2 files changed, 43 insertions(+), 16 deletions(-) diff --git a/centrifuge-app/src/pages/Loan/HoldingsValues.tsx b/centrifuge-app/src/pages/Loan/HoldingsValues.tsx index 4ddef47d36..389ac28dd9 100644 --- a/centrifuge-app/src/pages/Loan/HoldingsValues.tsx +++ b/centrifuge-app/src/pages/Loan/HoldingsValues.tsx @@ -1,6 +1,6 @@ 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 = { @@ -12,41 +12,67 @@ export function HoldingsValues({ pool, transactions }: Props) { const netSpent = transactions?.reduce((sum, trx) => { if (trx.type === 'REPAID') { - return sum.sub( - trx.amount && trx.settlementPrice - ? trx.amount.toDecimal().mul(new CurrencyBalance(trx.settlementPrice, pool.currency.decimals).toDecimal()) - : Dec(0) + return new CurrencyBalance( + sum.sub( + trx.amount && trx.settlementPrice + ? trx.amount.mul( + new CurrencyBalance( + trx.settlementPrice.div(new BN(10).pow(new BN(pool.currency.decimals))).toString(), + pool.currency.decimals + ) + ) + : CurrencyBalance.fromFloat(0, pool.currency.decimals) + ), + pool.currency.decimals ) } if (trx.type === 'BORROWED') { - return sum.add( - trx.amount && trx.settlementPrice - ? trx.amount.toDecimal().mul(new CurrencyBalance(trx.settlementPrice, pool.currency.decimals).toDecimal()) - : Dec(0) + return new CurrencyBalance( + sum.add( + trx.amount && trx.settlementPrice + ? trx.amount.mul( + new CurrencyBalance( + trx.settlementPrice.div(new BN(10).pow(new BN(pool.currency.decimals))).toString(), + pool.currency.decimals + ) + ) + : CurrencyBalance.fromFloat(0, pool.currency.decimals) + ), + pool.currency.decimals ) } return sum - }, Dec(0)) || Dec(0) + }, CurrencyBalance.fromFloat(0, pool.currency.decimals)) || CurrencyBalance.fromFloat(0, pool.currency.decimals) const currentFace = transactions?.reduce((sum, trx) => { if (trx.type === 'BORROWED') { - return sum.add(trx.amount?.toDecimal() || Dec(0)) + return new CurrencyBalance(sum.add(trx?.amount || new BN(0)), pool.currency.decimals) } if (trx.type === 'REPAID') { - return sum.sub(trx.amount?.toDecimal() || Dec(0)) + return new CurrencyBalance(sum.sub(trx?.amount || new BN(0)), pool.currency.decimals) } return sum - }, Dec(0)) || Dec(0) + }, CurrencyBalance.fromFloat(0, pool.currency.decimals)) || CurrencyBalance.fromFloat(0, pool.currency.decimals) return ( <> - - + + ) diff --git a/centrifuge-js/src/modules/pools.ts b/centrifuge-js/src/modules/pools.ts index 55eef8eb13..df8451b9d0 100644 --- a/centrifuge-js/src/modules/pools.ts +++ b/centrifuge-js/src/modules/pools.ts @@ -2111,6 +2111,7 @@ export function getPoolsModule(inst: Centrifuge) { map(([data, currency]) => { return data!.borrowerTransactions.nodes.map((tx) => ({ ...tx, + settlementPrice: tx.settlementPrice ? new CurrencyBalance(tx.settlementPrice, currency.decimals) : undefined, amount: tx.amount ? new CurrencyBalance(tx.amount, currency.decimals) : undefined, timestamp: new Date(`${tx.timestamp}+00:00`), })) as unknown as BorrowerTransaction[]