Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: missing “withdrawal for fees” asset tx in onchain reserve #246

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 22 additions & 15 deletions src/mappings/handlers/poolFeesHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ async function _handleFeeProposed(event: SubstrateEvent<PoolFeesProposedEvent>):
`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
Expand All @@ -53,13 +53,13 @@ async function _handleFeeAdded(event: SubstrateEvent<PoolFeesAddedEvent>): 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
Expand All @@ -86,13 +86,13 @@ async function _handleFeeRemoved(event: SubstrateEvent<PoolFeesRemovedEvent>): 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(),
}

Expand All @@ -112,13 +112,13 @@ async function _handleFeeCharged(event: SubstrateEvent<PoolFeesChargedEvent>): 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(),
Expand All @@ -145,13 +145,13 @@ async function _handleFeeUncharged(event: SubstrateEvent<PoolFeesUnchargedEvent>
)
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(),
Expand All @@ -173,18 +173,18 @@ export const handleFeePaid = errorHandler(_handleFeePaid)
async function _handleFeePaid(event: SubstrateEvent<PoolFeesPaidEvent>): Promise<void> {
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(),
}
Expand All @@ -197,11 +197,18 @@ async function _handleFeePaid(event: SubstrateEvent<PoolFeesPaidEvent>): 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)
}
}
4 changes: 4 additions & 0 deletions src/mappings/handlers/poolsHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@ async function _handleEpochExecuted(event: SubstrateEvent<EpochClosedExecutedEve
amount: epoch.sumRedeemedAmount,
})
assetTransactionSaves.push(withdrawalRedemptions.save())
} else {
logger.info(`No withdrawal redemptions for pool ${pool.id}`)
}

if (epoch.sumPoolFeesPaidAmount > BigInt(0)) {
Expand All @@ -297,6 +299,8 @@ async function _handleEpochExecuted(event: SubstrateEvent<EpochClosedExecutedEve
amount: epoch.sumPoolFeesPaidAmount,
})
assetTransactionSaves.push(withdrawalFees.save())
} else {
logger.info(`No withdrawal for fees for pool ${pool.id}`)
}

await Promise.all(assetTransactionSaves)
Expand Down
Loading