Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cash flow report investments/redemptions #2407

Merged
merged 2 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 45 additions & 16 deletions centrifuge-js/src/modules/pools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2509,55 +2509,77 @@ export function getPoolsModule(inst: Centrifuge) {
trancheStates[tid] = [entry]
}
})
const poolSeenDay = new Set<string>()

// One day may have multiple snapshots
// Fields ending with an ByPeriod are reset to 0 in between each snapshot
// So those fields need to be summed up
const snapshotByDay = new Map<string, any>()
return {
poolStates:
poolSnapshots?.flatMap((state) => {
const timestamp = state.timestamp.slice(0, 10)
if (poolSeenDay.has(timestamp)) return []
poolSeenDay.add(timestamp)
const snapshotToday = snapshotByDay.get(timestamp)
const poolState = {
id: state.id,
netAssetValue: new CurrencyBalance(state.netAssetValue, poolCurrency.decimals),
totalReserve: new CurrencyBalance(state.totalReserve, poolCurrency.decimals),
offchainCashValue: new CurrencyBalance(state.offchainCashValue, poolCurrency.decimals),
portfolioValuation: new CurrencyBalance(state.portfolioValuation, poolCurrency.decimals),
sumPoolFeesChargedAmountByPeriod: new CurrencyBalance(
state.sumPoolFeesChargedAmountByPeriod ?? 0,
BigInt(state.sumPoolFeesChargedAmountByPeriod) +
BigInt(snapshotToday?.sumPoolFeesChargedAmountByPeriod ?? 0),
poolCurrency.decimals
),
sumPoolFeesAccruedAmountByPeriod: new CurrencyBalance(
state.sumPoolFeesAccruedAmountByPeriod ?? 0,
BigInt(state.sumPoolFeesAccruedAmountByPeriod) +
BigInt(snapshotToday?.sumPoolFeesAccruedAmountByPeriod ?? 0),
poolCurrency.decimals
),
sumPoolFeesPaidAmountByPeriod: new CurrencyBalance(
state.sumPoolFeesPaidAmountByPeriod ?? 0,
BigInt(state.sumPoolFeesPaidAmountByPeriod) +
BigInt(snapshotToday?.sumPoolFeesPaidAmountByPeriod ?? 0),
poolCurrency.decimals
),
sumBorrowedAmountByPeriod: new CurrencyBalance(state.sumBorrowedAmountByPeriod, poolCurrency.decimals),
sumPrincipalRepaidAmountByPeriod: new CurrencyBalance(
state.sumPrincipalRepaidAmountByPeriod,
BigInt(state.sumPrincipalRepaidAmountByPeriod) +
BigInt(snapshotToday?.sumPrincipalRepaidAmountByPeriod ?? 0),
poolCurrency.decimals
),
sumInterestRepaidAmountByPeriod: new CurrencyBalance(
state.sumInterestRepaidAmountByPeriod,
BigInt(state.sumInterestRepaidAmountByPeriod) +
BigInt(snapshotToday?.sumInterestRepaidAmountByPeriod ?? 0),
poolCurrency.decimals
),
sumUnscheduledRepaidAmountByPeriod: new CurrencyBalance(
state.sumUnscheduledRepaidAmountByPeriod,
BigInt(state.sumUnscheduledRepaidAmountByPeriod) +
BigInt(snapshotToday?.sumUnscheduledRepaidAmountByPeriod ?? 0),
poolCurrency.decimals
),
sumRepaidAmountByPeriod: new CurrencyBalance(
BigInt(state.sumRepaidAmountByPeriod) + BigInt(snapshotToday?.sumRepaidAmountByPeriod ?? 0),
poolCurrency.decimals
),
sumInvestedAmountByPeriod: new CurrencyBalance(
BigInt(state.sumInvestedAmountByPeriod) + BigInt(snapshotToday?.sumInvestedAmountByPeriod ?? 0),
poolCurrency.decimals
),
sumRedeemedAmountByPeriod: new CurrencyBalance(
BigInt(state.sumRedeemedAmountByPeriod) + BigInt(snapshotToday?.sumRedeemedAmountByPeriod ?? 0),
poolCurrency.decimals
),
sumRepaidAmountByPeriod: new CurrencyBalance(state.sumRepaidAmountByPeriod, poolCurrency.decimals),
sumInvestedAmountByPeriod: new CurrencyBalance(state.sumInvestedAmountByPeriod, poolCurrency.decimals),
sumRedeemedAmountByPeriod: new CurrencyBalance(state.sumRedeemedAmountByPeriod, poolCurrency.decimals),
sumPoolFeesPendingAmount: new CurrencyBalance(state.sumPoolFeesPendingAmount, poolCurrency.decimals),
sumDebtWrittenOffByPeriod: new CurrencyBalance(state.sumDebtWrittenOffByPeriod, poolCurrency.decimals),
sumDebtWrittenOffByPeriod: new CurrencyBalance(
BigInt(state.sumDebtWrittenOffByPeriod) + BigInt(snapshotToday?.sumDebtWrittenOffByPeriod ?? 0),
poolCurrency.decimals
),
sumInterestAccruedByPeriod: new CurrencyBalance(
state.sumInterestAccruedByPeriod,
BigInt(state.sumInterestAccruedByPeriod) + BigInt(snapshotToday?.sumInterestAccruedByPeriod ?? 0),
poolCurrency.decimals
),
sumRealizedProfitFifoByPeriod: new CurrencyBalance(
state.sumRealizedProfitFifoByPeriod,
BigInt(state.sumRealizedProfitFifoByPeriod) +
BigInt(snapshotToday?.sumRealizedProfitFifoByPeriod ?? 0),
poolCurrency.decimals
),
sumUnrealizedProfitAtMarketPrice: new CurrencyBalance(
Expand All @@ -2569,10 +2591,17 @@ export function getPoolsModule(inst: Centrifuge) {
poolCurrency.decimals
),
sumUnrealizedProfitByPeriod: new CurrencyBalance(
state.sumUnrealizedProfitByPeriod,
BigInt(state.sumUnrealizedProfitByPeriod) + BigInt(snapshotToday?.sumUnrealizedProfitByPeriod ?? 0),
poolCurrency.decimals
),
}

snapshotByDay.set(timestamp, poolState)
if (snapshotToday) {
Object.assign(snapshotToday, poolState)
return []
}

const poolValue = new CurrencyBalance(new BN(state?.netAssetValue || '0'), poolCurrency.decimals)

// TODO: This is inefficient, would be better to construct a map indexed by the timestamp
Expand Down
6 changes: 3 additions & 3 deletions centrifuge-js/src/types/subquery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export type SubqueryPoolSnapshot = {
totalReserve: number
offchainCashValue: number
portfolioValuation: number
sumPoolFeesChargedAmountByPeriod: string | null
sumPoolFeesAccruedAmountByPeriod: string | null
sumPoolFeesPaidAmountByPeriod: string | null
sumPoolFeesChargedAmountByPeriod: string
sumPoolFeesAccruedAmountByPeriod: string
sumPoolFeesPaidAmountByPeriod: string
sumBorrowedAmountByPeriod: string
sumPrincipalRepaidAmountByPeriod: string
sumInterestRepaidAmountByPeriod: string
Expand Down
14 changes: 8 additions & 6 deletions centrifuge-js/src/utils/BN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ class BNSubType extends BN {
const n = Dec(number).mul(Dec(10).pow(this.decimals)).toDecimalPlaces(0).toString()
return new (this as typeof BNSubType)(n) as T
}
constructor(number: number | string | number[] | Uint8Array | Buffer | BN) {
super(BN.isBN(number) ? number.toString() : number)
constructor(number: number | string | number[] | Uint8Array | Buffer | BN | Codec | bigint) {
super(
(typeof number === 'object' && 'toPrimitive' in number) || typeof number === 'bigint' || BN.isBN(number)
? number.toString()
: number
)
}
getDecimals() {
return (this.constructor as typeof BNSubType).decimals
Expand All @@ -25,11 +29,9 @@ class BNSubType extends BN {

export class CurrencyBalance extends BN {
decimals: number
constructor(number: number | string | number[] | Uint8Array | Buffer | BN | Codec, decimals: number) {
constructor(number: number | string | number[] | Uint8Array | Buffer | BN | Codec | bigint, decimals: number) {
super(
typeof number === 'object' && 'toPrimitive' in number
? number.toString()
: BN.isBN(number)
(typeof number === 'object' && 'toPrimitive' in number) || typeof number === 'bigint' || BN.isBN(number)
? number.toString()
: number
)
Expand Down
Loading