From f218ed750ccbd3a34f6593d71dc83f31566c0888 Mon Sep 17 00:00:00 2001 From: JP Angelle Date: Tue, 19 Sep 2023 21:09:28 -0500 Subject: [PATCH] fix average settlement price --- .../src/pages/Loan/HoldingsValues.tsx | 53 +++++++++++-------- centrifuge-app/src/pages/Loan/index.tsx | 6 ++- centrifuge-app/src/utils/usePools.ts | 8 ++- 3 files changed, 43 insertions(+), 24 deletions(-) diff --git a/centrifuge-app/src/pages/Loan/HoldingsValues.tsx b/centrifuge-app/src/pages/Loan/HoldingsValues.tsx index 9dc23b12a1..0d6621fc71 100644 --- a/centrifuge-app/src/pages/Loan/HoldingsValues.tsx +++ b/centrifuge-app/src/pages/Loan/HoldingsValues.tsx @@ -1,20 +1,20 @@ 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 = { pool: Pool transactions?: BorrowerTransaction[] | null + currentFace: CurrencyBalance } -export function HoldingsValues({ pool, transactions }: Props) { +export function HoldingsValues({ pool, transactions, currentFace }: Props) { const netSpent = transactions?.reduce((sum, trx) => { if (trx.type === 'REPAID') { sum = new CurrencyBalance( - sum.sub( + sum.add( 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)) @@ -27,7 +27,7 @@ export function HoldingsValues({ pool, transactions }: Props) { if (trx.type === 'BORROWED') { sum = new CurrencyBalance( - sum.add( + sum.sub( 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)) @@ -41,22 +41,31 @@ export function HoldingsValues({ pool, transactions }: Props) { return sum }, 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 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 BN(trx.amount).mul(new BN(100)) : new CurrencyBalance(0, pool.currency.decimals)), - pool.currency.decimals - ) - } - return sum - }, new CurrencyBalance(0, pool.currency.decimals)) || new CurrencyBalance(0, pool.currency.decimals) + const getAverageSettlePrice = () => { + const settlementTransactions = + transactions?.reduce((sum, trx) => { + if (!new BN(trx.settlementPrice || 0).isZero()) { + sum = sum.add(new BN(1)) + } + return sum + }, new BN(0)) || new BN(0) + + if (settlementTransactions.isZero()) { + return new CurrencyBalance(0, pool.currency.decimals) + } + + return ( + transactions?.reduce((sum, trx) => { + if (!new BN(trx.settlementPrice || 0).isZero()) { + sum = new CurrencyBalance( + sum.add(trx.settlementPrice ? new BN(trx.settlementPrice) : new CurrencyBalance(0, pool.currency.decimals)), + pool.currency.decimals + ) + } + return sum + }, new CurrencyBalance(0, pool.currency.decimals)) || new CurrencyBalance(0, pool.currency.decimals) + ).div(settlementTransactions) + } return ( <> @@ -71,10 +80,10 @@ export function HoldingsValues({ pool, transactions }: Props) { void }> = ({ setShowOraclePr {'valuationMethod' in loan.pricing && loan.pricing.valuationMethod === 'oracle' && ( Holdings}> - + )} diff --git a/centrifuge-app/src/utils/usePools.ts b/centrifuge-app/src/utils/usePools.ts index 0f138d1173..f4a5d3aa1f 100644 --- a/centrifuge-app/src/utils/usePools.ts +++ b/centrifuge-app/src/utils/usePools.ts @@ -88,7 +88,13 @@ export function useAverageAmount(poolId: string) { const pool = usePool(poolId) const loans = useLoans(poolId) - if (!loans || !pool || !borrowerTransactions) return new BN(0) + if ( + !loans?.length || + !pool || + !borrowerTransactions || + !('valuationMethod' in loans[0].pricing && loans[0].pricing.valuationMethod === 'oracle') + ) + return new BN(0) const getLatestPrice = (assetId: string) => { const pricing = loans.find((loan) => loan.id === assetId)?.pricing as ExternalPricingInfo