Skip to content

Commit

Permalink
clean ups
Browse files Browse the repository at this point in the history
  • Loading branch information
JP Angelle committed Sep 29, 2023
1 parent 7a6bf91 commit 30b7661
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 246 deletions.
11 changes: 6 additions & 5 deletions centrifuge-app/src/components/LoanLabel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ import { ActiveLoan, Loan, TinlakeLoan } from '@centrifuge/centrifuge-js'
import { StatusChip } from '@centrifuge/fabric'
import * as React from 'react'
import { daysBetween } from '../utils/date'
import { useBorrowerAssetTransactions } from '../utils/usePools'

type LabelStatus = 'default' | 'info' | 'ok' | 'warning' | 'critical'

interface Props {
loan: Loan | TinlakeLoan
}

export function getLoanLabelStatus(l: Loan | TinlakeLoan, isExternalAssetRepaid: boolean): [LabelStatus, string] {
export function getLoanLabelStatus(l: Loan | TinlakeLoan, isExternalAssetRepaid?: boolean): [LabelStatus, string] {
const today = new Date()
today.setUTCHours(0, 0, 0, 0)
if (l.status === 'Active' && (l as ActiveLoan).writeOffStatus) return ['critical', 'Write-off']
Expand Down Expand Up @@ -39,10 +38,12 @@ export function getLoanLabelStatus(l: Loan | TinlakeLoan, isExternalAssetRepaid:
}

const LoanLabel: React.FC<Props> = ({ loan }) => {
const { currentFace, borrowerAssetTransactions } = useBorrowerAssetTransactions(loan.poolId, loan.id)
const currentFace =
loan.pricing && 'outstandingQuantity' in loan.pricing
? loan.pricing.outstandingQuantity.toDecimal().mul(loan.pricing.notional.toDecimal())
: null

const isExternalAssetRepaid =
currentFace.isZero() && !!borrowerAssetTransactions?.find((trx) => trx.type === 'BORROWED')
const isExternalAssetRepaid = currentFace?.isZero() && loan.status === 'Active'
const [status, text] = getLoanLabelStatus(loan, isExternalAssetRepaid)
return <StatusChip status={status}>{text}</StatusChip>
}
Expand Down
3 changes: 2 additions & 1 deletion centrifuge-app/src/pages/Loan/HoldingsValues.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { BorrowerTransaction, CurrencyBalance, ExternalPricingInfo, Pool, PricingInfo } from '@centrifuge/centrifuge-js'
import BN from 'bn.js'
import Decimal from 'decimal.js-light'
import { LabelValueStack } from '../../components/LabelValueStack'
import { Dec } from '../../utils/Decimal'
import { formatBalance } from '../../utils/formatting'

type Props = {
pool: Pool
transactions?: BorrowerTransaction[] | null
currentFace: CurrencyBalance | null
currentFace: Decimal | null
pricing: PricingInfo
}

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 @@ -90,7 +90,7 @@ const Loan: React.FC<{ setShowOraclePricing?: () => void }> = ({ setShowOraclePr
const metadataIsLoading = poolMetadataIsLoading || nftMetadataIsLoading
const address = useAddress()
const canOraclePrice = useCanSetOraclePrice(address)
const { borrowerAssetTransactions } = useBorrowerAssetTransactions(poolId, assetId)
const borrowerAssetTransactions = useBorrowerAssetTransactions(poolId, assetId)

const currentFace =
loan?.pricing && 'outstandingQuantity' in loan.pricing
Expand Down
37 changes: 1 addition & 36 deletions centrifuge-app/src/utils/usePools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,42 +213,7 @@ export function useBorrowerAssetTransactions(poolId: string, assetId: string, fr
}
)

const currentFace =
result?.reduce((sum, trx) => {
if (trx.type === 'BORROWED') {
sum = new CurrencyBalance(
sum.add(
trx.quantity
? new CurrencyBalance(
new BN(trx.quantity)
.mul((loan!.pricing as ExternalPricingInfo).notional)
.div(new BN(10).pow(new BN(18))),
18
)
: new CurrencyBalance(0, pool.currency.decimals)
),
pool.currency.decimals
)
}
if (trx.type === 'REPAID') {
sum = new CurrencyBalance(
sum.sub(
trx.quantity
? new CurrencyBalance(
new BN(trx.quantity)
.mul((loan!.pricing as ExternalPricingInfo).notional)
.div(new BN(10).pow(new BN(18))),
18
)
: new CurrencyBalance(0, pool.currency.decimals)
),
pool.currency.decimals
)
}
return sum
}, new CurrencyBalance(0, pool.currency.decimals)) || new CurrencyBalance(0, pool.currency.decimals)

return { borrowerAssetTransactions: result, currentFace }
return result
}

export function useDailyPoolStates(poolId: string, from?: Date, to?: Date) {
Expand Down
Loading

0 comments on commit 30b7661

Please sign in to comment.