From c91fc4a119f0f4520428245a49b03645c4f390e9 Mon Sep 17 00:00:00 2001 From: Filippo Date: Tue, 27 Aug 2024 09:47:03 +0200 Subject: [PATCH] fix: missing poolFee values (#247) * fix: fix incorrect accrual asset transactions after payments Fixes #234 * fix: 235-0-paid-sum-in-cashflow-report-pool-snapshots --- src/helpers/stateSnapshot.ts | 20 ++++++++++++++------ src/mappings/handlers/poolsHandlers.ts | 12 +++++++++++- src/mappings/services/poolFeeService.ts | 4 ++-- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/helpers/stateSnapshot.ts b/src/helpers/stateSnapshot.ts index 6bdf440d..c84732ab 100644 --- a/src/helpers/stateSnapshot.ts +++ b/src/helpers/stateSnapshot.ts @@ -13,6 +13,7 @@ import { SubstrateBlock } from '@subql/types' * blockNumber fields) * @param block - the correspondint substrateBlock to provide additional state values to the snapshot * @param fkReferenceName - (optional) name of the foreignKey to save a reference to the originating entity. + * @param resetPeriodStates - (optional) reset properties ending in ByPeriod to 0 after snapshot (defaults to true). * @returns A promise resolving when all state manipulations in the DB is completed */ async function stateSnapshotter( @@ -24,6 +25,7 @@ async function stateSnapshotter { const entitySaves: Promise[] = [] @@ -47,11 +49,13 @@ async function stateSnapshotter propName.endsWith('ByPeriod')) as ResettableKey[] - for (const propName of propNamesToReset) { - logger.debug(`resetting ${stateEntity._name.toLowerCase()}.${propName} to 0`) - stateEntity[propName] = BigInt(0) + if (resetPeriodStates) { + for (const propName of propNamesToReset) { + logger.debug(`resetting ${stateEntity._name.toLowerCase()}.${propName} to 0`) + stateEntity[propName] = BigInt(0) + } + entitySaves.push(stateEntity.save()) } - entitySaves.push(stateEntity.save()) entitySaves.push(snapshotEntity.save()) } return Promise.all(entitySaves) @@ -64,7 +68,8 @@ export function evmStateSnapshotter { const formattedBlock = { number: block.number, timestamp: new Date(Number(block.timestamp) * 1000) } return stateSnapshotter( @@ -76,6 +81,7 @@ export function evmStateSnapshotter { const formattedBlock = { number: block.block.header.number.toNumber(), timestamp: block.timestamp } return stateSnapshotter( @@ -100,6 +107,7 @@ export function substrateStateSnapshotter & Required>) { logger.info(`Paying PoolFee ${data.feeId} with amount ${data.amount.toString(10)}`) - if (!this.isActive) throw new Error('Unable to payinactive PolFee') + if (!this.isActive) throw new Error('Unable to pay inactive PolFee') this.sumPaidAmount += data.amount this.sumPaidAmountByPeriod += data.amount this.pendingAmount -= data.amount @@ -109,7 +109,7 @@ export class PoolFeeService extends PoolFee { this.pendingAmount = pending + disbursement const newAccruedAmount = this.pendingAmount - this.sumAccruedAmountByPeriod = newAccruedAmount - this.sumAccruedAmount + this.sumAccruedAmountByPeriod = newAccruedAmount - this.sumAccruedAmount + this.sumPaidAmountByPeriod this.sumAccruedAmount = newAccruedAmount return this }