Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix average settlement price
Browse files Browse the repository at this point in the history
JP Angelle committed Sep 20, 2023

Unverified

No user is associated with the committer email.
1 parent 4759c09 commit f218ed7
Showing 3 changed files with 43 additions and 24 deletions.
53 changes: 31 additions & 22 deletions centrifuge-app/src/pages/Loan/HoldingsValues.tsx
Original file line number Diff line number Diff line change
@@ -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) {
<LabelValueStack
label="Average settle price"
value={
netSpent.isZero()
getAverageSettlePrice().isZero()
? '-'
: `${formatBalance(
Dec(netSpent.toNumber()).div(Dec(currentFace.toNumber())).mul(100),
new CurrencyBalance(getAverageSettlePrice().mul(new BN(100)), pool.currency.decimals),
pool.currency.symbol,
2,
2
6 changes: 5 additions & 1 deletion centrifuge-app/src/pages/Loan/index.tsx
Original file line number Diff line number Diff line change
@@ -232,7 +232,11 @@ const Loan: React.FC<{ setShowOraclePricing?: () => void }> = ({ setShowOraclePr
{'valuationMethod' in loan.pricing && loan.pricing.valuationMethod === 'oracle' && (
<PageSection title={<Box>Holdings</Box>}>
<Shelf gap={6} flexWrap="wrap">
<HoldingsValues pool={pool as Pool} transactions={borrowerAssetTransactions} />
<HoldingsValues
pool={pool as Pool}
transactions={borrowerAssetTransactions}
currentFace={currentFace}
/>
</Shelf>
</PageSection>
)}
8 changes: 7 additions & 1 deletion centrifuge-app/src/utils/usePools.ts
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit f218ed7

Please sign in to comment.