Skip to content

Commit

Permalink
weighted average
Browse files Browse the repository at this point in the history
  • Loading branch information
JP Angelle committed Sep 29, 2023
1 parent 167f3c7 commit e921a8a
Showing 1 changed file with 17 additions and 28 deletions.
45 changes: 17 additions & 28 deletions centrifuge-app/src/pages/Loan/HoldingsValues.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { BorrowerTransaction, CurrencyBalance, ExternalPricingInfo, Pool, PricingInfo } from '@centrifuge/centrifuge-js'
import BN from 'bn.js'
import Decimal from 'decimal.js-light'
import { LabelValueStack } from '../../components/LabelValueStack'
import { Dec } from '../../utils/Decimal'
Expand Down Expand Up @@ -27,29 +26,24 @@ export function HoldingsValues({ pool, transactions, currentFace, pricing }: Pro
}, Dec(0)) || Dec(0)

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 (!transactions?.length) return Dec(0)

if (settlementTransactions.isZero()) {
return new CurrencyBalance(0, pool.currency.decimals)
}
const weightedSum = transactions.reduce((sum, trx) => {
if (trx.settlementPrice && trx.amount) {
return sum.add(
new CurrencyBalance(trx.settlementPrice, pool.currency.decimals).toDecimal().mul(trx.amount.toDecimal())
)
}

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 sum
}, Dec(0))

const sumOfAmounts = transactions.reduce(
(sum, trx) => sum.add(trx.amount ? new CurrencyBalance(trx.amount, pool.currency.decimals).toDecimal() : Dec(0)),
Dec(0)
)

return weightedSum.div(sumOfAmounts)
}

return (
Expand All @@ -64,12 +58,7 @@ export function HoldingsValues({ pool, transactions, currentFace, pricing }: Pro
value={
getAverageSettlePrice().isZero()
? '-'
: `${formatBalance(
new CurrencyBalance(getAverageSettlePrice(), pool.currency.decimals),
pool.currency.symbol,
2,
2
)}`
: `${formatBalance(getAverageSettlePrice(), pool.currency.symbol, 2, 2)}`
}
/>
<LabelValueStack
Expand Down

0 comments on commit e921a8a

Please sign in to comment.