diff --git a/centrifuge-js/src/modules/pools.ts b/centrifuge-js/src/modules/pools.ts index d1752dcdb..37a497d14 100644 --- a/centrifuge-js/src/modules/pools.ts +++ b/centrifuge-js/src/modules/pools.ts @@ -2509,13 +2509,16 @@ export function getPoolsModule(inst: Centrifuge) { trancheStates[tid] = [entry] } }) - const poolSeenDay = new Set() + + // 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() 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), @@ -2523,41 +2526,60 @@ export function getPoolsModule(inst: Centrifuge) { 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( @@ -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 diff --git a/centrifuge-js/src/types/subquery.ts b/centrifuge-js/src/types/subquery.ts index a7abff3cc..5f781c7f0 100644 --- a/centrifuge-js/src/types/subquery.ts +++ b/centrifuge-js/src/types/subquery.ts @@ -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 diff --git a/centrifuge-js/src/utils/BN.ts b/centrifuge-js/src/utils/BN.ts index 8f915234c..3c43fe97b 100644 --- a/centrifuge-js/src/utils/BN.ts +++ b/centrifuge-js/src/utils/BN.ts @@ -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 @@ -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 )