Skip to content

Commit

Permalink
fix: missing poolFee values (#247)
Browse files Browse the repository at this point in the history
* fix: fix incorrect accrual asset transactions after payments
Fixes #234

* fix: 235-0-paid-sum-in-cashflow-report-pool-snapshots
  • Loading branch information
filo87 committed Aug 27, 2024
1 parent ec2dd6a commit c91fc4a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
20 changes: 14 additions & 6 deletions src/helpers/stateSnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends SnapshottableEntity, U extends SnapshottedEntityProps>(
Expand All @@ -24,6 +25,7 @@ async function stateSnapshotter<T extends SnapshottableEntity, U extends Snapsho
filterKey?: keyof T,
filterValue?: T[keyof T],
fkReferenceName?: ForeignKey,
resetPeriodStates = true,
blockchainId: T['blockchainId'] = '0'
): Promise<void[]> {
const entitySaves: Promise<void>[] = []
Expand All @@ -47,11 +49,13 @@ async function stateSnapshotter<T extends SnapshottableEntity, U extends Snapsho

const propNames = Object.getOwnPropertyNames(stateEntity)
const propNamesToReset = propNames.filter((propName) => 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)
Expand All @@ -64,7 +68,8 @@ export function evmStateSnapshotter<T extends SnapshottableEntity, U extends Sna
block: EthereumBlock,
filterKey?: keyof T,
filterValue?: T[keyof T],
fkReferenceName?: ForeignKey
fkReferenceName?: ForeignKey,
resetPeriodStates = true
): Promise<void[]> {
const formattedBlock = { number: block.number, timestamp: new Date(Number(block.timestamp) * 1000) }
return stateSnapshotter<T, U>(
Expand All @@ -76,6 +81,7 @@ export function evmStateSnapshotter<T extends SnapshottableEntity, U extends Sna
filterKey,
filterValue,
fkReferenceName,
resetPeriodStates,
'1'
)
}
Expand All @@ -88,7 +94,8 @@ export function substrateStateSnapshotter<T extends SnapshottableEntity, U exten
block: SubstrateBlock,
filterKey?: keyof T,
filterValue?: T[keyof T],
fkReferenceName?: ForeignKey
fkReferenceName?: ForeignKey,
resetPeriodStates = true
): Promise<void[]> {
const formattedBlock = { number: block.block.header.number.toNumber(), timestamp: block.timestamp }
return stateSnapshotter<T, U>(
Expand All @@ -100,6 +107,7 @@ export function substrateStateSnapshotter<T extends SnapshottableEntity, U exten
filterKey,
filterValue,
fkReferenceName,
resetPeriodStates,
'0'
)
}
Expand Down
12 changes: 11 additions & 1 deletion src/mappings/handlers/poolsHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,5 +305,15 @@ async function _handleEpochExecuted(event: SubstrateEvent<EpochClosedExecutedEve

await Promise.all(assetTransactionSaves)

await substrateStateSnapshotter('epochId', epoch.id, Pool, PoolSnapshot, event.block, 'isActive', true, 'poolId')
await substrateStateSnapshotter(
'epochId',
epoch.id,
Pool,
PoolSnapshot,
event.block,
'isActive',
true,
'poolId',
false
)
}
4 changes: 2 additions & 2 deletions src/mappings/services/poolFeeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class PoolFeeService extends PoolFee {

public pay(data: Omit<PoolFeeData, 'amount'> & Required<Pick<PoolFeeData, 'amount'>>) {
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
Expand All @@ -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
}
Expand Down

0 comments on commit c91fc4a

Please sign in to comment.