Skip to content

Commit

Permalink
Cash Flow report: Fix fees (#2416)
Browse files Browse the repository at this point in the history
  • Loading branch information
onnovisser authored Aug 30, 2024
1 parent 3804c6e commit b049704
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 43 deletions.
52 changes: 23 additions & 29 deletions centrifuge-app/src/components/Report/CashflowStatement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -202,37 +202,31 @@ export function CashflowStatement({ pool }: { pool: Pool }) {

const netCashflowRecords: Row[] = React.useMemo(() => {
return [
...(poolFeeStates
?.map((poolFeeStateByPeriod) => {
return Object.values(poolFeeStateByPeriod)
?.map((feeState) => {
// some fee data may be incomplete since fees may have been added sometime after pool creation
// this fill the nonexistant fee data with zero values
let missingStates: {
timestamp: string
sumPaidAmountByPeriod: CurrencyBalance
}[] = []
if (feeState.length !== poolStates?.length) {
const missingTimestamps = poolStates
?.map((state) => state.timestamp)
.filter((timestamp) => !feeState.find((state) => state.timestamp === timestamp))
missingStates =
missingTimestamps?.map((timestamp) => {
return {
timestamp,
sumPaidAmountByPeriod: CurrencyBalance.fromFloat(0, pool.currency.decimals),
}
}) || []
}
...(Object.entries(poolFeeStates || {})?.flatMap(([, feeState]) => {
// some fee data may be incomplete since fees may have been added sometime after pool creation
// this fill the nonexistant fee data with zero values
let missingStates: {
timestamp: string
sumPaidAmountByPeriod: CurrencyBalance
}[] = []
if (feeState.length !== poolStates?.length) {
const missingTimestamps = poolStates
?.map((state) => state.timestamp)
.filter((timestamp) => !feeState.find((state) => state.timestamp.slice(0, 10) === timestamp.slice(0, 10)))
missingStates =
missingTimestamps?.map((timestamp) => {
return {
name: feeState[0].poolFee.name,
value: [...missingStates, ...feeState].map((state) => state.sumPaidAmountByPeriod.toDecimal().neg()),
formatter: (v: any) => `${formatBalance(v, pool.currency.displayName, 2)}`,
timestamp,
sumPaidAmountByPeriod: CurrencyBalance.fromFloat(0, pool.currency.decimals),
}
})
.flat()
})
.flat() || []),
}) || []
}
return {
name: feeState[0].poolFee.name,
value: [...missingStates, ...feeState].map((state) => state.sumPaidAmountByPeriod.toDecimal().neg()),
formatter: (v: any) => `${formatBalance(v, pool.currency.displayName, 2)}`,
}
}) || []),
{
name: 'Net cash flow after fees',
value:
Expand Down
50 changes: 36 additions & 14 deletions centrifuge-js/src/modules/pools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2301,7 +2301,7 @@ export function getPoolsModule(inst: Centrifuge) {
poolSnapshots(
orderBy: BLOCK_NUMBER_ASC,
filter: {
id: { startsWith: $poolId },
poolId: { equalTo: $poolId },
timestamp: { greaterThan: $from, lessThan: $to }
}
after: $poolCursor
Expand Down Expand Up @@ -2791,23 +2791,45 @@ export function getPoolsModule(inst: Centrifuge) {
)
}

function getPoolFeeStatesByGroup(args: [poolId: string, from?: Date, to?: Date], groupBy: GroupBy = 'month') {
function getPoolFeeStatesByGroup(
args: [poolId: string, from?: Date, to?: Date],
groupBy: GroupBy = 'month'
): Observable<Record<string, DailyPoolFeesState[]>> {
return getDailyPoolFeeStates(args).pipe(
map((poolFees) => {
return Object.entries(poolFees).map(([feeId, feeStates]) => {
if (!feeStates.length) return []
const poolStatesByGroup: { [period: string]: DailyPoolFeesState } = {}
return Object.fromEntries(
Object.entries(poolFees).map(([feeId, feeStates]) => {
if (!feeStates.length) return []
const poolStatesByGroup: { [period: string]: DailyPoolFeesState } = {}

feeStates.forEach((feeState) => {
const date = new Date(feeState.timestamp)
const period = getGroupByPeriod(date, groupBy)
if (!poolStatesByGroup[period]) {
poolStatesByGroup[period] = feeState
} else {
const existing = poolStatesByGroup[period]
poolStatesByGroup[period] = {
...feeState,
sumAccruedAmountByPeriod: new CurrencyBalance(
feeState.sumAccruedAmountByPeriod.add(existing?.sumAccruedAmountByPeriod ?? new BN(0)),
feeState.sumAccruedAmountByPeriod.decimals
),
sumChargedAmountByPeriod: new CurrencyBalance(
feeState.sumChargedAmountByPeriod.add(existing?.sumChargedAmountByPeriod ?? new BN(0)),
feeState.sumChargedAmountByPeriod.decimals
),
sumPaidAmountByPeriod: new CurrencyBalance(
feeState.sumPaidAmountByPeriod.add(existing?.sumPaidAmountByPeriod ?? new BN(0)),
feeState.sumPaidAmountByPeriod.decimals
),
}
}
})

feeStates.forEach((feeState) => {
const date = new Date(feeState.timestamp)
const period = getGroupByPeriod(date, groupBy)
if (!poolStatesByGroup[period] || new Date(poolStatesByGroup[period].timestamp) < date) {
poolStatesByGroup[period] = feeState
}
return [feeId, Object.values(poolStatesByGroup)]
})

return { [feeId]: Object.values(poolStatesByGroup) }
})
)
})
)
}
Expand Down

0 comments on commit b049704

Please sign in to comment.