Skip to content

Commit

Permalink
integrate settle price
Browse files Browse the repository at this point in the history
  • Loading branch information
JP Angelle committed Sep 11, 2023
1 parent 4be2503 commit e2f8c2b
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 15 deletions.
41 changes: 38 additions & 3 deletions centrifuge-app/src/pages/Loan/HoldingsValues.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BorrowerTransaction, CurrencyBalance, ExternalPricingInfo, Loan, Pool } from '@centrifuge/centrifuge-js'
import BN from 'bn.js'
import { LabelValueStack } from '../../components/LabelValueStack'
import { formatBalance } from '../../utils/formatting'

Expand All @@ -9,25 +10,59 @@ type Props = {
}

export function HoldingsValues({ loan: { pricing }, pool, transactions }: Props) {
const currentTotalFace =
const currentHolding =
transactions?.reduce((sum, trx) => {
if (trx.type === 'REPAID') {
sum = new CurrencyBalance(sum.sub(trx.amount || new CurrencyBalance(0, 27)), 27)
sum = new CurrencyBalance(
sum.sub(
trx.amount && trx.settlementPrice ? trx.amount.mul(new BN(trx.settlementPrice)) : new CurrencyBalance(0, 27)
),
27
)
}

if (trx.type === 'BORROWED') {
sum = new CurrencyBalance(sum.add(trx.amount || new CurrencyBalance(0, 27)), 27)
sum = new CurrencyBalance(
sum.add(
trx.amount && trx.settlementPrice ? trx.amount.mul(new BN(trx.settlementPrice)) : new CurrencyBalance(0, 27)
),
27
)
}

return sum
}, new CurrencyBalance(0, 27)) || new CurrencyBalance(0, 27)

const currentTotalFace =
transactions?.reduce((sum, trx) => {
if (trx.type === 'BORROWED') {
sum = new CurrencyBalance(sum.add(trx.amount || new CurrencyBalance(0, 27)), 27)
}
if (trx.type === 'REPAID') {
sum = new CurrencyBalance(sum.sub(trx.amount || new CurrencyBalance(0, 27)), 27)
}
return sum
}, new CurrencyBalance(0, 27)) || new CurrencyBalance(0, 27)

return (
<>
<LabelValueStack
label="Current total face"
value={`${formatBalance(new CurrencyBalance(currentTotalFace, 24), pool.currency.symbol, 6, 2)}`}
/>
<LabelValueStack
label="Current holding"
value={`${formatBalance(new CurrencyBalance(currentHolding, 32), pool.currency.symbol, 6, 2)}`}
/>
<LabelValueStack
label="Average price"
value={`${formatBalance(
new CurrencyBalance(currentHolding.div(currentTotalFace), 6),
pool.currency.symbol,
2,
2
)}`}
/>
<LabelValueStack
label="Current value"
value={`${formatBalance(
Expand Down
43 changes: 31 additions & 12 deletions centrifuge-app/src/pages/Loan/TransactionTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ export const TransactionTable = ({ transactions, currency }: Props) => {
return sortedTransactions.map((transaction, index, array) => ({
type: transaction.type,
transactionDate: transaction.timestamp,
// settlePrice: TODO: add subquery query for fetching settle price
netFlow: transaction.amount,
settlePrice: transaction.settlementPrice ? new CurrencyBalance(transaction.settlementPrice, 6) : null,
faceFlow: transaction.amount,
position: array.slice(0, index + 1).reduce((sum, trx) => {
if (trx.type === 'BORROWED') {
sum = new CurrencyBalance(sum.add(trx.amount || new CurrencyBalance(0, 27)), 27)
Expand Down Expand Up @@ -72,26 +72,45 @@ export const TransactionTable = ({ transactions, currency }: Props) => {
cell: (row) => formatDate(row.transactionDate),
flex: '3',
},
// TODO: add subquery query for fetching settle price
// {
// align: 'left',
// header: 'Settle price',
// cell: (row) => formatBalance(row.settlePrice, currency),
// flex: '3',
// },

{
align: 'left',
header: 'Face flow',
cell: (row) =>
row.netFlow
? `${row.type === 'REPAID' ? '-' : ''}${formatBalance(new CurrencyBalance(row.netFlow, 24), currency)}`
row.faceFlow
? `${row.type === 'REPAID' ? '-' : ''}${formatBalance(
new CurrencyBalance(row.faceFlow, 24),
currency,
6,
2
)}`
: '-',
flex: '3',
},
{
align: 'left',
header: 'Settle price',
cell: (row) => (row.settlePrice ? formatBalance(row.settlePrice, currency, 6, 2) : '-'),
flex: '3',
},
{
align: 'left',
header: 'Net cash flow',
cell: (row) =>
row.faceFlow && row.settlePrice
? `${row.type === 'BORROWED' ? '-' : ''}${formatBalance(
new CurrencyBalance(row.faceFlow.mul(row.settlePrice), 32),
currency,
6,
2
)}`
: '-',
flex: '3',
},
{
align: 'left',
header: 'Position',
cell: (row) => formatBalance(new CurrencyBalance(row.position, 24), currency),
cell: (row) => formatBalance(new CurrencyBalance(row.position, 24), currency, 6, 2),
flex: '3',
},
// TODO: add link to transaction
Expand Down
2 changes: 2 additions & 0 deletions centrifuge-js/src/modules/pools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,7 @@ export type BorrowerTransaction = {
loanId: string
type: BorrowerTransactionType
amount: CurrencyBalance | undefined
settlementPrice: string | null
}

export type Permissions = {
Expand Down Expand Up @@ -2089,6 +2090,7 @@ export function getPoolsModule(inst: Centrifuge) {
type
timestamp
amount
settlementPrice
}
}
}
Expand Down
1 change: 1 addition & 0 deletions centrifuge-js/src/types/subquery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export type SubqueryBorrowerTransaction = {
loanId: string
type: BorrowerTransactionType
amount?: number | null
settlementPrice: string | null
}

export type SubqueryEpoch = {
Expand Down

0 comments on commit e2f8c2b

Please sign in to comment.