From e0a4f21ac077ac8f7b79fdb5ddcbbb8cb49d727c Mon Sep 17 00:00:00 2001 From: Filippo Fontana Date: Mon, 29 Jan 2024 22:16:43 +0100 Subject: [PATCH] fix: init tranche currency details on tranche creation --- src/mappings/handlers/evmHandlers.ts | 2 +- src/mappings/handlers/poolsHandlers.ts | 13 ++++++++++++- src/mappings/services/currencyService.ts | 16 ++++++++-------- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/mappings/handlers/evmHandlers.ts b/src/mappings/handlers/evmHandlers.ts index 5ca98579..a772d553 100644 --- a/src/mappings/handlers/evmHandlers.ts +++ b/src/mappings/handlers/evmHandlers.ts @@ -41,7 +41,7 @@ async function _handleEvmDeployTranche(event: DeployTrancheLog): Promise { const investmentManager = InvestmentManagerAbi__factory.connect(investmentManagerAddress, ethApi) const userEscrowAddress = await investmentManager.userEscrow() - await currency.initEvmDetails(tokenAddress, escrowAddress, userEscrowAddress, tranche.poolId, tranche.trancheId) + await currency.initTrancheDetails(tranche.poolId, tranche.trancheId, tokenAddress, escrowAddress, userEscrowAddress) await currency.save() await createTrancheTrackerDatasource({ address: tokenAddress }) diff --git a/src/mappings/handlers/poolsHandlers.ts b/src/mappings/handlers/poolsHandlers.ts index 4ffe5266..ed3a731b 100644 --- a/src/mappings/handlers/poolsHandlers.ts +++ b/src/mappings/handlers/poolsHandlers.ts @@ -52,6 +52,10 @@ async function _handlePoolCreated(event: SubstrateEvent): Prom await tranche.init(index, trancheData[tranche.trancheId].data) await tranche.updateSupply() await tranche.save() + + const currency = await CurrencyService.getOrInit(blockchain.id, 'Tranche', pool.id, tranche.trancheId) + await currency.initTrancheDetails(pool.id, tranche.trancheId) + await currency.save() } // Initialise Epoch @@ -65,8 +69,11 @@ async function _handlePoolUpdated(event: SubstrateEvent): Prom const [poolId] = event.event.data logger.info(`Pool ${poolId.toString()} updated on block ${event.block.block.header.number}`) + const chainId = await getNodeChainId() + const blockchain = await BlockchainService.getOrInit(chainId) + const pool = await PoolService.getById(poolId.toString()) - if (pool === undefined) throw missingPool + if (!pool) throw missingPool await pool.initData() await pool.save() @@ -88,6 +95,10 @@ async function _handlePoolUpdated(event: SubstrateEvent): Prom await trancheService.updateSupply() await trancheService.updateDebt(tranche.data.debt.toBigInt()) await trancheService.save() + + const currency = await CurrencyService.getOrInit(blockchain.id, 'Tranche', pool.id, trancheService.trancheId) + await currency.initTrancheDetails(pool.id, trancheService.trancheId) + await currency.save() } } diff --git a/src/mappings/services/currencyService.ts b/src/mappings/services/currencyService.ts index c88fd095..d269bab3 100644 --- a/src/mappings/services/currencyService.ts +++ b/src/mappings/services/currencyService.ts @@ -37,18 +37,18 @@ export class CurrencyService extends Currency { return currency as CurrencyService } - public initEvmDetails( - tokenAddress: string, - escrowAddress: string, - userEscrowAddress: string, + public initTrancheDetails( poolId: string, - trancheId: string + trancheId: string, + evmTokenAddress?: string, + evmEscrowAddress?: string, + evmUserEscrowAddress?: string ) { - this.tokenAddress = tokenAddress - this.escrowAddress = escrowAddress - this.userEscrowAddress = userEscrowAddress this.poolId = poolId this.trancheId = `${poolId}-${trancheId}` + this.tokenAddress = evmTokenAddress + this.escrowAddress = evmEscrowAddress + this.userEscrowAddress = evmUserEscrowAddress } }