Skip to content

Commit

Permalink
fix average settlement price
Browse files Browse the repository at this point in the history
  • Loading branch information
JP Angelle committed Sep 20, 2023
1 parent 4759c09 commit c110ab6
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 25 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))
Expand All @@ -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))
Expand All @@ -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 (
<>
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion centrifuge-app/src/pages/Loan/TransactionTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export const TransactionTable = ({ transactions, currency, loanType, decimals }:
align: 'left',
header: 'Position',
cell: (row) =>
row.position.isZero()
row.type === 'CREATED'
? '-'
: formatBalance(new CurrencyBalance(new BN(row.position).mul(new BN(100)), decimals), currency, 2, 2),
flex: '3',
Expand Down
6 changes: 5 additions & 1 deletion centrifuge-app/src/pages/Loan/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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>
)}
Expand Down
8 changes: 7 additions & 1 deletion centrifuge-app/src/utils/usePools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c110ab6

Please sign in to comment.