Skip to content

Commit

Permalink
Use CurrencyBalance instead of Dec and convert settlement price to Cu…
Browse files Browse the repository at this point in the history
…rrencyBalance
  • Loading branch information
sophialittlejohn committed Sep 15, 2023
1 parent 532f334 commit 22b8887
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
58 changes: 42 additions & 16 deletions centrifuge-app/src/pages/Loan/HoldingsValues.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
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 = {
Expand All @@ -12,41 +12,67 @@ export function HoldingsValues({ pool, transactions }: Props) {
const netSpent =
transactions?.reduce((sum, trx) => {
if (trx.type === 'REPAID') {
return sum.sub(
trx.amount && trx.settlementPrice
? trx.amount.toDecimal().mul(new CurrencyBalance(trx.settlementPrice, pool.currency.decimals).toDecimal())
: Dec(0)
return new CurrencyBalance(
sum.sub(
trx.amount && trx.settlementPrice
? trx.amount.mul(
new CurrencyBalance(
trx.settlementPrice.div(new BN(10).pow(new BN(pool.currency.decimals))).toString(),
pool.currency.decimals
)
)
: CurrencyBalance.fromFloat(0, pool.currency.decimals)
),
pool.currency.decimals
)
}

if (trx.type === 'BORROWED') {
return sum.add(
trx.amount && trx.settlementPrice
? trx.amount.toDecimal().mul(new CurrencyBalance(trx.settlementPrice, pool.currency.decimals).toDecimal())
: Dec(0)
return new CurrencyBalance(
sum.add(
trx.amount && trx.settlementPrice
? trx.amount.mul(
new CurrencyBalance(
trx.settlementPrice.div(new BN(10).pow(new BN(pool.currency.decimals))).toString(),
pool.currency.decimals
)
)
: CurrencyBalance.fromFloat(0, pool.currency.decimals)
),
pool.currency.decimals
)
}
return sum
}, Dec(0)) || Dec(0)
}, CurrencyBalance.fromFloat(0, pool.currency.decimals)) || CurrencyBalance.fromFloat(0, pool.currency.decimals)

const currentFace =
transactions?.reduce((sum, trx) => {
if (trx.type === 'BORROWED') {
return sum.add(trx.amount?.toDecimal() || Dec(0))
return new CurrencyBalance(sum.add(trx?.amount || new BN(0)), pool.currency.decimals)
}
if (trx.type === 'REPAID') {
return sum.sub(trx.amount?.toDecimal() || Dec(0))
return new CurrencyBalance(sum.sub(trx?.amount || new BN(0)), pool.currency.decimals)
}
return sum
}, Dec(0)) || Dec(0)
}, CurrencyBalance.fromFloat(0, pool.currency.decimals)) || CurrencyBalance.fromFloat(0, pool.currency.decimals)

return (
<>
<LabelValueStack label="Current face" value={`${formatBalance(currentFace, pool.currency.symbol, 6, 2)}`} />
<LabelValueStack label="Net spent" value={`${formatBalance(netSpent.div(100), pool.currency.symbol, 6, 2)}`} />
<LabelValueStack
label="Current face"
value={`${formatBalance(currentFace.toDecimal(), pool.currency.symbol, 6, 2)}`}
/>
<LabelValueStack
label="Net spent"
value={`${formatBalance(netSpent.toDecimal().div(100), pool.currency.symbol, 6, 2)}`}
/>
<LabelValueStack
label="Average settle price"
value={netSpent.isZero() ? '-' : `${formatBalance(netSpent.div(currentFace), pool.currency.symbol, 2, 2)}`}
value={
netSpent.isZero()
? '-'
: `${formatBalance(netSpent.toDecimal().div(currentFace.toDecimal()), pool.currency.symbol, 2, 2)}`
}
/>
</>
)
Expand Down
1 change: 1 addition & 0 deletions centrifuge-js/src/modules/pools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2111,6 +2111,7 @@ export function getPoolsModule(inst: Centrifuge) {
map(([data, currency]) => {
return data!.borrowerTransactions.nodes.map((tx) => ({
...tx,
settlementPrice: tx.settlementPrice ? new CurrencyBalance(tx.settlementPrice, currency.decimals) : undefined,
amount: tx.amount ? new CurrencyBalance(tx.amount, currency.decimals) : undefined,
timestamp: new Date(`${tx.timestamp}+00:00`),
})) as unknown as BorrowerTransaction[]
Expand Down

0 comments on commit 22b8887

Please sign in to comment.