Skip to content

Commit

Permalink
fix decimals
Browse files Browse the repository at this point in the history
  • Loading branch information
JP Angelle committed Sep 19, 2023
1 parent 4b1daa3 commit 82a5696
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 24 deletions.
18 changes: 12 additions & 6 deletions centrifuge-app/src/pages/Loan/ExternalFinanceForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function ExternalFinanceForm({ loan }: { loan: LoanType }) {
faceValue: '',
},
onSubmit: (values, actions) => {
const price = CurrencyBalance.fromFloat(values.price, pool.currency.decimals)
const price = CurrencyBalance.fromFloat(values.price, pool.currency.decimals).div(new BN(100))
const quantity = Price.fromFloat((values.faceValue as number) / (values.price as number))

doFinanceTransaction([loan.poolId, loan.id, quantity, price, account.actingAddress])
Expand All @@ -75,7 +75,7 @@ export function ExternalFinanceForm({ loan }: { loan: LoanType }) {
faceValue: '',
},
onSubmit: (values, actions) => {
const price = CurrencyBalance.fromFloat(values.price, pool.currency.decimals)
const price = CurrencyBalance.fromFloat(values.price, pool.currency.decimals).div(new BN(100))
const quantity = Price.fromFloat((values.faceValue as number) / (values.price as number))

doRepayTransaction([loan.poolId, loan.id, quantity, new BN(0), new BN(0), price, account.actingAddress])
Expand Down Expand Up @@ -105,13 +105,19 @@ export function ExternalFinanceForm({ loan }: { loan: LoanType }) {
const currentFace =
borrowerAssetTransactions?.reduce((sum, trx) => {
if (trx.type === 'BORROWED') {
sum = new CurrencyBalance(sum.add(trx.amount || new CurrencyBalance(0, 27)), 27)
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 CurrencyBalance(0, 27)), 27)
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, 27)) || new CurrencyBalance(0, 27)
}, new CurrencyBalance(0, pool.currency.decimals)) || new CurrencyBalance(0, pool.currency.decimals)

return (
<Stack gap={3}>
Expand Down Expand Up @@ -222,7 +228,7 @@ export function ExternalFinanceForm({ loan }: { loan: LoanType }) {
{/* outstandingDebt needs to be rounded down, b/c onSetMax displays the rounded down value as well */}
<Text variant="label2">
{'valuationMethod' in loan.pricing && loan.pricing.valuationMethod === 'oracle'
? formatBalance(currentFace, pool.currency.symbol, 6, 2)
? formatBalance(currentFace, pool.currency.symbol, 2, 2)
: ''}
</Text>
</Shelf>
Expand Down
42 changes: 31 additions & 11 deletions centrifuge-app/src/pages/Loan/HoldingsValues.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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 @@ -14,51 +15,70 @@ export function HoldingsValues({ pool, transactions }: Props) {
if (trx.type === 'REPAID') {
sum = new CurrencyBalance(
sum.sub(
trx.amount && trx.settlementPrice ? trx.amount.mul(new BN(trx.settlementPrice)) : new CurrencyBalance(0, 27)
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))
)
: new CurrencyBalance(0, pool.currency.decimals)
),
27
pool.currency.decimals
)
}

if (trx.type === 'BORROWED') {
sum = new CurrencyBalance(
sum.add(
trx.amount && trx.settlementPrice ? trx.amount.mul(new BN(trx.settlementPrice)) : new CurrencyBalance(0, 27)
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))
)
: new CurrencyBalance(0, pool.currency.decimals)
),
27
pool.currency.decimals
)
}

return sum
}, new CurrencyBalance(0, 27)) || new CurrencyBalance(0, 27)
}, 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 CurrencyBalance(0, 27)), 27)
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 CurrencyBalance(0, 27)), 27)
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, 27)) || new CurrencyBalance(0, 27)
}, new CurrencyBalance(0, pool.currency.decimals)) || new CurrencyBalance(0, pool.currency.decimals)

return (
<>
<LabelValueStack
label="Current face"
value={`${formatBalance(new CurrencyBalance(currentFace, 24), pool.currency.symbol, 6, 2)}`}
value={`${formatBalance(new CurrencyBalance(currentFace, pool.currency.decimals), pool.currency.symbol, 2, 2)}`}
/>
<LabelValueStack
label="Net spent"
value={`${formatBalance(new CurrencyBalance(netSpent, 32), pool.currency.symbol, 6, 2)}`}
value={`${formatBalance(new CurrencyBalance(netSpent, pool.currency.decimals), pool.currency.symbol, 2, 2)}`} // fixdis
/>
<LabelValueStack
label="Average settle price"
value={
netSpent.isZero()
? '-'
: `${formatBalance(new CurrencyBalance(netSpent.div(currentFace), 6), pool.currency.symbol, 2, 2)}`
: `${formatBalance(
Dec(netSpent.toNumber()).div(Dec(currentFace.toNumber())).mul(100),
pool.currency.symbol,
2,
2
)}`
}
/>
</>
Expand Down
19 changes: 13 additions & 6 deletions centrifuge-app/src/pages/Loan/TransactionTable.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BorrowerTransaction, CurrencyBalance } from '@centrifuge/centrifuge-js'
import { BorrowerTransactionType } from '@centrifuge/centrifuge-js/dist/types/subquery'
import { StatusChip, Tooltip } from '@centrifuge/fabric'
import BN from 'bn.js'
import { useMemo } from 'react'
import { DataTable } from '../../components/DataTable'
import { formatDate } from '../../utils/date'
Expand Down Expand Up @@ -34,7 +35,9 @@ export const TransactionTable = ({ transactions, currency, loanType, decimals }:
return sortedTransactions.map((transaction, index, array) => ({
type: transaction.type,
transactionDate: transaction.timestamp,
settlePrice: transaction.settlementPrice ? new CurrencyBalance(transaction.settlementPrice, decimals) : null,
settlePrice: transaction.settlementPrice
? new CurrencyBalance(new BN(transaction.settlementPrice).mul(new BN(100)), decimals)
: null,
faceFlow: transaction.amount,
position: array.slice(0, index + 1).reduce((sum, trx) => {
if (trx.type === 'BORROWED') {
Expand Down Expand Up @@ -93,9 +96,9 @@ export const TransactionTable = ({ transactions, currency, loanType, decimals }:
cell: (row) =>
row.faceFlow
? `${row.type === 'REPAID' ? '-' : ''}${formatBalance(
new CurrencyBalance(row.faceFlow, 24),
new CurrencyBalance(new BN(row.faceFlow).mul(new BN(100)), decimals),
currency,
6,
2,
2
)}`
: '-',
Expand All @@ -113,9 +116,11 @@ export const TransactionTable = ({ transactions, currency, loanType, decimals }:
cell: (row) =>
row.faceFlow && row.settlePrice
? `${row.type === 'BORROWED' ? '-' : ''}${formatBalance(
new CurrencyBalance(row.faceFlow.mul(row.settlePrice), 32),
new CurrencyBalance(row.faceFlow, decimals)
.toDecimal()
.mul(new CurrencyBalance(row.settlePrice, decimals).toDecimal()),
currency,
6,
2,
2
)}`
: '-',
Expand All @@ -125,7 +130,9 @@ export const TransactionTable = ({ transactions, currency, loanType, decimals }:
align: 'left',
header: 'Position',
cell: (row) =>
row.position.isZero() ? '-' : formatBalance(new CurrencyBalance(row.position, 24), currency, 6, 2),
row.position.isZero()
? '-'
: formatBalance(new CurrencyBalance(new BN(row.position).mul(new BN(100)), decimals), currency, 2, 2),
flex: '3',
},
// TODO: add link to transaction
Expand Down
2 changes: 1 addition & 1 deletion centrifuge-app/src/pages/Loan/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ const Loan: React.FC<{ setShowOraclePricing?: () => void }> = ({ setShowOraclePr
? [
{
label: 'Current value',
value: `${formatBalance(loan.presentValue, pool.currency.symbol, 6, 2)}`,
value: `${formatBalance(loan.presentValue, pool.currency.symbol, 2, 2)}`,
},
]
: []),
Expand Down

0 comments on commit 82a5696

Please sign in to comment.