Skip to content

Commit

Permalink
Add to reports
Browse files Browse the repository at this point in the history
  • Loading branch information
kattylucy committed Jan 27, 2025
1 parent 20e6a86 commit 3b36d12
Show file tree
Hide file tree
Showing 10 changed files with 293 additions and 408 deletions.
67 changes: 39 additions & 28 deletions centrifuge-app/src/components/Report/AssetList.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { Pool } from '@centrifuge/centrifuge-js'
import { Box, Text } from '@centrifuge/fabric'
import { AssetListReport } from '@centrifuge/sdk/dist/types/reports'
import { useContext, useEffect, useMemo } from 'react'
import { useBasePath } from '../../../src/utils/useBasePath'
import { formatDate } from '../../utils/date'
import { formatBalance, formatPercentage } from '../../utils/formatting'
import { formatBalance, formatDecimal, formatPercentage } from '../../utils/formatting'
import { getCSVDownloadUrl } from '../../utils/getCSVDownloadUrl'
import { useAllPoolAssetSnapshots, usePoolMetadata } from '../../utils/usePools'
import { usePoolMetadata } from '../../utils/usePools'
import { DataTable, SortableTableHeader } from '../DataTable'
import { Spinner } from '../Spinner'
import { RouterTextLink } from '../TextLink'
import { ReportContext } from './ReportContext'
import { UserFeedback } from './UserFeedback'
import type { TableDataRow } from './index'
import { useReport } from './useReportsQuery'

const noop = (v: any) => v

Expand All @@ -21,7 +23,12 @@ const valuationLabels = {
oracle: 'Fungible asset - external pricing',
}

function getColumnConfig(isPrivate: boolean, symbol: string) {
type AssetSnapshot = AssetListReport & {
transactionType: 'ACTIVE' | string
name: string
}

function getColumnConfig(isPrivate: boolean, symbol: string, decimals: number) {
if (isPrivate) {
return [
{ header: 'Name', align: 'left', csvOnly: false, formatter: noop },
Expand Down Expand Up @@ -127,28 +134,28 @@ function getColumnConfig(isPrivate: boolean, symbol: string) {
align: 'left',
csvOnly: false,
sortable: true,
formatter: (v: any) => (v ? formatBalance(v, symbol, 2) : '-'),
formatter: (v: any) => (v ? formatDecimal(v, 2, symbol) : '-'),
},
{
header: 'Face value',
align: 'left',
csvOnly: false,
sortable: true,
formatter: (v: any) => (v ? formatBalance(v, symbol, 2) : '-'),
formatter: (v: any) => (v ? formatDecimal(v, 2, symbol) : '-'),
},
{
header: 'Quantity',
align: 'left',
csvOnly: false,
sortable: true,
formatter: (v: any) => (v ? formatBalance(v, undefined, 2) : '-'),
formatter: (v: any) => (v ? formatDecimal(v, 2, '') : '-'),
},
{
header: 'Market price',
align: 'left',
csvOnly: false,
sortable: true,
formatter: (v: any) => (v ? formatBalance(v, symbol, 2) : '-'),
formatter: (v: any) => (v ? formatDecimal(v, 2, symbol) : '-'),
},
{
header: 'Maturity date',
Expand All @@ -162,14 +169,14 @@ function getColumnConfig(isPrivate: boolean, symbol: string) {
align: 'left',
csvOnly: false,
sortable: true,
formatter: (v: any) => (v ? formatBalance(v, symbol, 2) : '-'),
formatter: (v: any) => (v ? formatDecimal(v, 2, symbol) : '-'),
},
{
header: 'Realized profit',
align: 'left',
csvOnly: false,
sortable: true,
formatter: (v: any) => (v ? formatBalance(v, symbol, 2) : '-'),
formatter: (v: any) => (v ? formatDecimal(v, 2, symbol) : '-'),
},
]
}
Expand All @@ -179,11 +186,16 @@ export function AssetList({ pool }: { pool: Pool }) {
const basePath = useBasePath()
const { loanStatus, startDate, setCsvData } = useContext(ReportContext)
const { data: poolMetadata } = usePoolMetadata(pool)
const { symbol } = pool.currency
const { symbol, decimals } = pool.currency
const poolCreditType = poolMetadata?.pool?.asset.class || 'privateCredit'
const { data: snapshots } = useAllPoolAssetSnapshots(pool.id, startDate)
const isPrivate = poolCreditType === 'Private credit' || poolCreditType === 'privateCredit'
const columnConfig = getColumnConfig(isPrivate, symbol)
const columnConfig = getColumnConfig(isPrivate, symbol, decimals)

const { data: snapshots = [], isLoading } = useReport('assetList', pool, new Date(startDate), undefined, undefined, {

Check warning on line 194 in centrifuge-app/src/components/Report/AssetList.tsx

View workflow job for this annotation

GitHub Actions / ff-prod / build-app

'isLoading' is assigned a value but never used

Check warning on line 194 in centrifuge-app/src/components/Report/AssetList.tsx

View workflow job for this annotation

GitHub Actions / build-app

'isLoading' is assigned a value but never used
...(loanStatus && { status: loanStatus }),
})

console.log(snapshots, isPrivate)

const columns = useMemo(
() =>
Expand Down Expand Up @@ -213,16 +225,14 @@ export function AssetList({ pool }: { pool: Pool }) {
const data = useMemo((): any[] => {
if (!snapshots) return []

return snapshots
return (snapshots as AssetSnapshot[])
.filter((snapshot) => snapshot?.valuationMethod?.toLowerCase() !== 'cash')
.filter((snapshot) => {
const isMaturityDatePassed = snapshot?.actualMaturityDate
? new Date() > new Date(snapshot.actualMaturityDate)
: false
const isDebtZero = snapshot?.outstandingDebt?.isZero()
const isMaturityDatePassed = snapshot?.maturityDate ? new Date() > new Date(snapshot.maturityDate) : false
const isDebtZero = 'outstandingQuantity' in snapshot ? snapshot.outstandingQuantity?.isZero() : false

if (loanStatus === 'ongoing') {
return snapshot.status === 'ACTIVE' && !isMaturityDatePassed && !isDebtZero
return snapshot.transactionType === 'ACTIVE' && !isMaturityDatePassed && !isDebtZero
} else if (loanStatus === 'repaid') {
return isMaturityDatePassed && isDebtZero
} else if (loanStatus === 'overdue') {
Expand All @@ -231,12 +241,13 @@ export function AssetList({ pool }: { pool: Pool }) {
})
.sort((a, b) => {
// Sort by actualMaturityDate in descending order
const dateA = new Date(a.actualMaturityDate || 0).getTime()
const dateB = new Date(b.actualMaturityDate || 0).getTime()
const dateA = new Date(a.maturityDate || 0).getTime()
const dateB = new Date(b.maturityDate || 0).getTime()
return dateB - dateA
})
.map((snapshot) => {
const valuationMethod = snapshot?.valuationMethod as keyof typeof valuationLabels
const valuationMethod =
'valuationMethod' in snapshot ? (snapshot.valuationMethod as keyof typeof valuationLabels) : ''
if (isPrivate) {
return {
name: '',
Expand All @@ -245,10 +256,10 @@ export function AssetList({ pool }: { pool: Pool }) {
snapshot?.presentValue,
snapshot?.outstandingPrincipal,
snapshot?.outstandingInterest,
snapshot?.totalRepaidPrincipal,
snapshot?.totalRepaidInterest,
snapshot?.totalRepaidUnscheduled,
snapshot?.actualOriginationDate,
snapshot?.repaidPrincipal,
snapshot?.repaidInterest,
snapshot?.repaidUnscheduled,
snapshot?.originationDate,
snapshot?.actualMaturityDate,
valuationMethod || snapshot?.valuationMethod,
snapshot?.advanceRate,
Expand All @@ -269,9 +280,9 @@ export function AssetList({ pool }: { pool: Pool }) {
snapshot?.faceValue,
snapshot?.outstandingQuantity,
snapshot?.currentPrice,
snapshot?.actualMaturityDate,
snapshot?.unrealizedProfitAtMarketPrice,
snapshot?.sumRealizedProfitFifo,
snapshot?.maturityDate,
snapshot?.unrealizedProfit,
snapshot?.realizedProfit,
],
heading: false,
id: snapshot?.assetId,
Expand Down
38 changes: 24 additions & 14 deletions centrifuge-app/src/components/Report/AssetTransactions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import { formatBalance } from '../../../src/utils/formatting'
import { getCSVDownloadUrl } from '../../../src/utils/getCSVDownloadUrl'
import { useBasePath } from '../../../src/utils/useBasePath'
import { formatDate } from '../../utils/date'
import { useAssetTransactions } from '../../utils/usePools'
import { DataTable } from '../DataTable'
import { getLabelAndAmount } from '../PoolOverview/TransactionHistory'
import { Spinner } from '../Spinner'
import { RouterTextLink } from '../TextLink'
import { ReportContext } from './ReportContext'
import { UserFeedback } from './UserFeedback'
import { useReport } from './useReportsQuery'
import { getTransactionLabelAndAmount } from './utils'

type Row = {
type: string
Expand All @@ -32,11 +32,21 @@ type Row = {
}

export function AssetTransactions({ pool }: { pool: Pool }) {
const { startDate, endDate, setCsvData, txType, loan: loanId } = React.useContext(ReportContext)
const transactions = useAssetTransactions(pool.id, new Date(startDate), new Date(endDate))
const { startDate, endDate, setCsvData, loan: loanId } = React.useContext(ReportContext)
const explorer = useGetExplorerUrl()
const basePath = useBasePath()

const { data: transactions = [], isLoading } = useReport(
'assetTransactions',
pool,
new Date(startDate),
new Date(endDate),
undefined,
{
...(loanId && { assetId: loanId }),
}
)

const columns = [
{
header: 'Transaction date',
Expand Down Expand Up @@ -107,21 +117,21 @@ export function AssetTransactions({ pool }: { pool: Pool }) {

const data = transactions
?.map((transaction) => {
const { label, sublabel } = getLabelAndAmount(transaction)
const { label, sublabel } = getTransactionLabelAndAmount(transaction)

return {
transactionDate: transaction.timestamp.toISOString(),
assetId: transaction.asset.id.split('-')[1],
assetName: transaction.asset.name,
transactionDate: transaction.timestamp,
assetId: transaction.assetId.split('-')[1],
assetName: transaction.asset?.name,
fromAssetId: transaction.fromAsset?.id,
fromAssetName: transaction.fromAsset?.name,
toAssetId: transaction.toAsset?.id,
toAssetName: transaction.toAsset?.name,
amount: transaction.amount?.toFloat() ?? '',
hash: transaction.hash,
hash: transaction.transactionHash,
label,
sublabel,
epochId: transaction.epochId.split('-').at(-1)!,
epochId: transaction.epoch.split('-').at(-1)!,
}
})
.filter((row) => {
Expand All @@ -132,7 +142,7 @@ export function AssetTransactions({ pool }: { pool: Pool }) {
React.useEffect(() => {
if (transactions) {
const csvData = transactions.map((transaction) => {
const { amount } = getLabelAndAmount(transaction)
const { amount } = getTransactionLabelAndAmount(transaction)
return {
'Transaction Date': `"${formatDate(transaction.timestamp, {
year: 'numeric',
Expand All @@ -143,9 +153,9 @@ export function AssetTransactions({ pool }: { pool: Pool }) {
second: 'numeric',
timeZoneName: 'short',
})}"`,
Transaction: explorer.tx(transaction.hash),
Transaction: explorer.tx(transaction.transactionHash),
Amount: amount ? `"${formatBalance(amount, 'USD', 2, 2)}"` : '-',
epoch: transaction.epochId,
epoch: transaction.epoch,
}
})

Expand All @@ -159,7 +169,7 @@ export function AssetTransactions({ pool }: { pool: Pool }) {
}
}, [transactions, setCsvData])

Check warning on line 170 in centrifuge-app/src/components/Report/AssetTransactions.tsx

View workflow job for this annotation

GitHub Actions / ff-prod / build-app

React Hook React.useEffect has a missing dependency: 'explorer'. Either include it or remove the dependency array

Check warning on line 170 in centrifuge-app/src/components/Report/AssetTransactions.tsx

View workflow job for this annotation

GitHub Actions / build-app

React Hook React.useEffect has a missing dependency: 'explorer'. Either include it or remove the dependency array

if (!transactions) {
if (isLoading) {
return <Spinner mt={2} />
}

Expand Down
Loading

0 comments on commit 3b36d12

Please sign in to comment.