Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix average settlement price calculations #2113

Merged
merged 4 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions centrifuge-app/src/pages/Loan/HoldingsValues.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AssetTransaction, CurrencyBalance, External#Info, Pool } from '@centrifuge/centrifuge-js'
import Decimal from 'decimal.js-light'
import React from 'react'
import { LabelValueStack } from '../../components/LabelValueStack'
import { Dec } from '../../utils/Decimal'
import { formatBalance } from '../../utils/formatting'
Expand All @@ -25,7 +26,7 @@ export function HoldingsValues({ pool, transactions, currentFace, # }: Pro
return sum
}, Dec(0)) || Dec(0)

const getAverageSettlePrice = () => {
const averageSettlePrice = React.useMemo(() => {
if (!transactions?.length) return Dec(0)

const weightedSum = transactions.reduce((sum, trx) => {
Expand All @@ -39,12 +40,17 @@ export function HoldingsValues({ pool, transactions, currentFace, # }: Pro
}, Dec(0))

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

return weightedSum.div(sumOfAmounts)
}
}, [transactions])

return (
<>
Expand All @@ -55,11 +61,7 @@ export function HoldingsValues({ pool, transactions, currentFace, # }: Pro
<LabelValueStack label="Net spent" value={`${formatBalance(netSpent, pool.currency.symbol, 2, 2)}`} />
<LabelValueStack
label="Average settle price"
value={
getAverageSettlePrice().isZero()
? '-'
: `${formatBalance(getAverageSettlePrice(), pool.currency.symbol, 2, 2)}`
}
value={averageSettlePrice.isZero() ? '-' : `${formatBalance(averageSettlePrice, pool.currency.symbol, 2, 2)}`}
/>
<LabelValueStack label="Notional" value={`${formatBalance(#.notional, pool.currency.symbol, 2, 2)}`} />
<LabelValueStack label="Quantity" value={`${formatBalance(#.outstandingQuantity, undefined, 2, 0)}`} />
Expand Down
125 changes: 63 additions & 62 deletions centrifuge-app/src/pages/Loan/TransactionTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,74 +104,75 @@ export const TransactionTable = ({ transactions, currency, loanType, decimals, p
return `${type[0]}${type.slice(1).toLowerCase()}`
}

let columns: Column[] = [
{
align: 'left',
header: 'Type',
cell: (row: Row) => <StatusChip status={getStatusChipType(row.type)}>{getStatusText(row.type)}</StatusChip>,
},
{
align: 'left',
header: 'Transaction date',
cell: (row: Row) => (
<Tooltip
title="Transaction date"
body={formatDate(row.transactionDate, { hour: 'numeric', minute: 'numeric', second: 'numeric' })}
>
{formatDate(row.transactionDate)}
</Tooltip>
),
},
]

if (poolType === 'publicCredit') {
columns = [
...columns,
const columns = useMemo(() => {
return [
{
align: 'left',
header: `Face flow (${currency})`,
cell: (row: Row) =>
row.faceFlow ? `${row.type === 'REPAID' ? '-' : ''}${formatBalance(row.faceFlow, undefined, 2, 2)}` : '-',
header: 'Type',
cell: (row: Row) => <StatusChip status={getStatusChipType(row.type)}>{getStatusText(row.type)}</StatusChip>,
},
{
align: 'left',
header: 'Quantity',
cell: (row: Row) => (row.quantity ? formatBalance(row.quantity, undefined, 2, 0) : '-'),
header: 'Transaction date',
cell: (row: Row) => (
<Tooltip
title="Transaction date"
body={formatDate(row.transactionDate, { hour: 'numeric', minute: 'numeric', second: 'numeric' })}
>
{formatDate(row.transactionDate)}
</Tooltip>
),
},
{
align: 'left',
header: `Settle price (${currency})`,
cell: (row: Row) => (row.settlePrice ? formatBalance(row.settlePrice, undefined, 6, 2) : '-'),
},
{
align: 'left',
header: `Net cash flow (${currency})`,
cell: (row: Row) =>
row.amount ? `${row.type === 'BORROWED' ? '-' : ''}${formatBalance(row.amount, undefined, 2, 2)}` : '-',
},
{
align: 'left',
header: `Position (${currency})`,
cell: (row: Row) => (row.type === 'CREATED' ? '-' : formatBalance(row.position, undefined, 2, 2)),
},
]
} else {
columns = [
...columns,
{
align: 'left',
header: `Amount (${currency})`,
cell: (row: Row) =>
row.amount ? `${row.type === 'REPAID' ? '-' : ''}${formatBalance(row.amount, undefined, 2, 2)}` : '-',
},
{
align: 'left',
header: `Principal (${currency})`,
cell: (row: Row) =>
row.position ? `${row.type === 'REPAID' ? '-' : ''}${formatBalance(row.position, undefined, 2, 2)}` : '-',
},
]
}
...(poolType === 'publicCredit'
? [
{
align: 'left',
header: `Face flow (${currency})`,
cell: (row: Row) =>
row.faceFlow
? `${row.type === 'REPAID' ? '-' : ''}${formatBalance(row.faceFlow, undefined, 2, 2)}`
: '-',
},
{
align: 'left',
header: 'Quantity',
cell: (row: Row) => (row.quantity ? formatBalance(row.quantity, undefined, 2, 0) : '-'),
},
{
align: 'left',
header: `Settle price (${currency})`,
cell: (row: Row) => (row.settlePrice ? formatBalance(row.settlePrice, undefined, 6, 2) : '-'),
},
{
align: 'left',
header: `Net cash flow (${currency})`,
cell: (row: Row) =>
row.amount ? `${row.type === 'BORROWED' ? '-' : ''}${formatBalance(row.amount, undefined, 2, 2)}` : '-',
},
{
align: 'left',
header: `Position (${currency})`,
cell: (row: Row) => (row.type === 'CREATED' ? '-' : formatBalance(row.position, undefined, 2, 2)),
},
]
: [
{
align: 'left',
header: `Amount (${currency})`,
cell: (row: Row) =>
row.amount ? `${row.type === 'REPAID' ? '-' : ''}${formatBalance(row.amount, undefined, 2, 2)}` : '-',
},
{
align: 'left',
header: `Principal (${currency})`,
cell: (row: Row) =>
row.position
? `${row.type === 'REPAID' ? '-' : ''}${formatBalance(row.position, undefined, 2, 2)}`
: '-',
},
]),
] as Column[]
}, [])

return <DataTable data={assetTransactions.reverse()} columns={columns} />
}
Loading