From 05cbfe846dfc28172171081dee96dbb83b9440aa Mon Sep 17 00:00:00 2001 From: Filippo Fontana Date: Mon, 26 Aug 2024 16:02:49 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20missing=20=E2=80=9Cwithdrawal=20for=20fe?= =?UTF-8?q?es=E2=80=9D=20asset=20tx=20in=20onchain=20reserve=20Fixes=20#23?= =?UTF-8?q?3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/mappings/handlers/poolFeesHandlers.ts | 37 ++++++++++++++--------- src/mappings/handlers/poolsHandlers.ts | 4 +++ 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/mappings/handlers/poolFeesHandlers.ts b/src/mappings/handlers/poolFeesHandlers.ts index 1b306e7c..ae7a2d2b 100644 --- a/src/mappings/handlers/poolFeesHandlers.ts +++ b/src/mappings/handlers/poolFeesHandlers.ts @@ -21,13 +21,13 @@ async function _handleFeeProposed(event: SubstrateEvent): `on block ${event.block.block.header.number.toNumber()}` ) const pool = await PoolService.getOrSeed(poolId.toString(10), true, true) - const currentEpoch = await EpochService.getById(pool.id, pool.currentEpoch) + const epoch = await epochFetcher(pool) const poolFeeData: PoolFeeData = { poolId: pool.id, feeId: feeId.toString(10), blockNumber: event.block.block.header.number.toNumber(), timestamp: event.block.timestamp, - epochId: currentEpoch.id, + epochId: epoch.id, hash: event.hash.toString(), } const type = fee.feeType.type @@ -53,13 +53,13 @@ async function _handleFeeAdded(event: SubstrateEvent): Promi `on block ${event.block.block.header.number.toNumber()}` ) const pool = await PoolService.getOrSeed(poolId.toString(10), true, true) - const currentEpoch = await EpochService.getById(pool.id, pool.currentEpoch) + const epoch = await epochFetcher(pool) const poolFeeData: PoolFeeData = { poolId: pool.id, feeId: feeId.toString(10), blockNumber: event.block.block.header.number.toNumber(), timestamp: event.block.timestamp, - epochId: currentEpoch.id, + epochId: epoch.id, hash: event.hash.toString(), } const type = fee.feeType.type @@ -86,13 +86,13 @@ async function _handleFeeRemoved(event: SubstrateEvent): P ) const pool = await PoolService.getById(poolId.toString(10)) if (!pool) throw missingPool - const currentEpoch = await EpochService.getById(pool.id, pool.currentEpoch) + const epoch = await epochFetcher(pool) const poolFeeData: PoolFeeData = { poolId: pool.id, feeId: feeId.toString(10), blockNumber: event.block.block.header.number.toNumber(), timestamp: event.block.timestamp, - epochId: currentEpoch.id, + epochId: epoch.id, hash: event.hash.toString(), } @@ -112,13 +112,13 @@ async function _handleFeeCharged(event: SubstrateEvent): P ) const pool = await PoolService.getById(poolId.toString(10)) if (!pool) throw missingPool - const currentEpoch = await EpochService.getById(pool.id, pool.currentEpoch) + const epoch = await epochFetcher(pool) const poolFeeData = { poolId: pool.id, feeId: feeId.toString(10), blockNumber: event.block.block.header.number.toNumber(), timestamp: event.block.timestamp, - epochId: currentEpoch.id, + epochId: epoch.id, hash: event.hash.toString(), amount: amount.toBigInt(), pending: pending.toBigInt(), @@ -145,13 +145,13 @@ async function _handleFeeUncharged(event: SubstrateEvent ) const pool = await PoolService.getById(poolId.toString(10)) if (!pool) throw missingPool - const currentEpoch = await EpochService.getById(pool.id, pool.currentEpoch) + const epoch = await epochFetcher(pool) const poolFeeData = { poolId: pool.id, feeId: feeId.toString(10), blockNumber: event.block.block.header.number.toNumber(), timestamp: event.block.timestamp, - epochId: currentEpoch.id, + epochId: epoch.id, hash: event.hash.toString(), amount: amount.toBigInt(), pending: pending.toBigInt(), @@ -173,18 +173,18 @@ export const handleFeePaid = errorHandler(_handleFeePaid) async function _handleFeePaid(event: SubstrateEvent): Promise { const [poolId, feeId, amount, _destination] = event.event.data logger.info( - `Fee with id ${feeId.toString(10)} uncharged for pool ${poolId.toString(10)} ` + + `Fee with id ${feeId.toString(10)} paid for pool ${poolId.toString(10)} ` + `on block ${event.block.block.header.number.toNumber()}` ) const pool = await PoolService.getById(poolId.toString(10)) if (!pool) throw missingPool - const currentEpoch = await EpochService.getById(pool.id, pool.currentEpoch) + const epoch = await epochFetcher(pool) const poolFeeData = { poolId: pool.id, feeId: feeId.toString(10), blockNumber: event.block.block.header.number.toNumber(), timestamp: event.block.timestamp, - epochId: currentEpoch.id, + epochId: epoch.id, hash: event.hash.toString(), amount: amount.toBigInt(), } @@ -197,11 +197,18 @@ async function _handleFeePaid(event: SubstrateEvent): Promise await pool.increasePaidFees(poolFeeData.amount) await pool.save() - const epoch = await EpochService.getById(pool.id, pool.currentEpoch) - if (!poolFee) throw new Error(`Current epoch for pool ${pool.id} not found`) await epoch.increasePaidFees(poolFeeData.amount) await epoch.save() const poolFeeTransaction = PoolFeeTransactionService.pay(poolFeeData) await poolFeeTransaction.save() } + +function epochFetcher(pool: PoolService) { + const { lastEpochClosed, lastEpochExecuted, currentEpoch } = pool + if (lastEpochClosed === lastEpochExecuted) { + return EpochService.getById(pool.id, currentEpoch) + } else { + return EpochService.getById(pool.id, lastEpochClosed) + } +} diff --git a/src/mappings/handlers/poolsHandlers.ts b/src/mappings/handlers/poolsHandlers.ts index 63bfe8c0..ff44bf30 100644 --- a/src/mappings/handlers/poolsHandlers.ts +++ b/src/mappings/handlers/poolsHandlers.ts @@ -289,6 +289,8 @@ async function _handleEpochExecuted(event: SubstrateEvent BigInt(0)) { @@ -297,6 +299,8 @@ async function _handleEpochExecuted(event: SubstrateEvent