From 8c4731882668fe3aba7430c043c05b55a407cb2c Mon Sep 17 00:00:00 2001 From: Filippo Fontana Date: Wed, 13 Nov 2024 23:10:56 +0100 Subject: [PATCH] feat: add initialised at timestamp to tranche balances --- schema.graphql | 2 ++ src/mappings/handlers/evmHandlers.ts | 7 ++++- src/mappings/handlers/investmentsHandlers.ts | 28 ++++++++++++++++--- src/mappings/handlers/poolsHandlers.ts | 3 +- .../services/trancheBalanceService.ts | 9 +++--- 5 files changed, 39 insertions(+), 10 deletions(-) diff --git a/schema.graphql b/schema.graphql index c66b140..cf8a365 100644 --- a/schema.graphql +++ b/schema.graphql @@ -410,6 +410,8 @@ type TrancheBalance @entity { pool: Pool! @index tranche: Tranche! @index + initialisedAt: Date! + pendingInvestCurrency: BigInt! claimableTrancheTokens: BigInt! diff --git a/src/mappings/handlers/evmHandlers.ts b/src/mappings/handlers/evmHandlers.ts index 9a7bf1a..a8fba92 100644 --- a/src/mappings/handlers/evmHandlers.ts +++ b/src/mappings/handlers/evmHandlers.ts @@ -122,7 +122,12 @@ async function _handleEvmTransfer(event: TransferLog): Promise { const investLpCollect = InvestorTransactionService.collectLpInvestOrder({ ...orderData, address: toAccount!.id }) await investLpCollect.save() - const trancheBalance = await TrancheBalanceService.getOrInit(toAccount!.id, orderData.poolId, orderData.trancheId) + const trancheBalance = await TrancheBalanceService.getOrInit( + toAccount.id, + orderData.poolId, + orderData.trancheId, + timestamp + ) await trancheBalance.investCollect(orderData.amount) await trancheBalance.save() } diff --git a/src/mappings/handlers/investmentsHandlers.ts b/src/mappings/handlers/investmentsHandlers.ts index 7e1a37b..d5f45c4 100644 --- a/src/mappings/handlers/investmentsHandlers.ts +++ b/src/mappings/handlers/investmentsHandlers.ts @@ -74,7 +74,12 @@ async function _handleInvestOrderUpdated(event: SubstrateEvent 0) { const it = InvestorTransactionService.collectInvestOrder(orderData) @@ -248,7 +263,12 @@ async function _handleRedeemOrdersCollected(event: SubstrateEvent 0) { const it = InvestorTransactionService.collectRedeemOrder(orderData) diff --git a/src/mappings/handlers/poolsHandlers.ts b/src/mappings/handlers/poolsHandlers.ts index 3ea49ce..ca24748 100644 --- a/src/mappings/handlers/poolsHandlers.ts +++ b/src/mappings/handlers/poolsHandlers.ts @@ -240,7 +240,8 @@ async function _handleEpochExecuted(event: SubstrateEvent BigInt(0) && epochState.investFulfillmentPercentage! > BigInt(0)) { diff --git a/src/mappings/services/trancheBalanceService.ts b/src/mappings/services/trancheBalanceService.ts index 9cc45fb..9b34812 100644 --- a/src/mappings/services/trancheBalanceService.ts +++ b/src/mappings/services/trancheBalanceService.ts @@ -1,13 +1,14 @@ import { TrancheBalance } from '../../types/models/TrancheBalance' export class TrancheBalanceService extends TrancheBalance { - static init(address: string, poolId: string, trancheId: string) { + static init(address: string, poolId: string, trancheId: string, timestamp: Date) { logger.info(`Initialising new TrancheBalance: ${address}-${poolId}-${trancheId}`) const trancheBalance = new this( `${address}-${poolId}-${trancheId}`, address, poolId, `${poolId}-${trancheId}`, + timestamp, BigInt(0), BigInt(0), BigInt(0), @@ -24,10 +25,10 @@ export class TrancheBalanceService extends TrancheBalance { return trancheBalance as TrancheBalanceService | undefined } - static getOrInit = async (address: string, poolId: string, trancheId: string) => { + static getOrInit = async (address: string, poolId: string, trancheId: string, timestamp: Date) => { let trancheBalance = await this.getById(address, poolId, trancheId) - if (!trancheBalance) { - trancheBalance = this.init(address, poolId, trancheId) + if (trancheBalance === undefined) { + trancheBalance = this.init(address, poolId, trancheId, timestamp) await trancheBalance.save() } return trancheBalance as TrancheBalanceService