Skip to content

Commit

Permalink
fix investments/redemptions
Browse files Browse the repository at this point in the history
  • Loading branch information
onnovisser committed Aug 26, 2024
1 parent 54b5ec0 commit 07c6c1c
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 25 deletions.
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

0 comments on commit 07c6c1c

Please sign in to comment.