Skip to content

Commit 8b98a0e

Browse files
committed
fix: only update accountsOverThreshold for the day matching batch end for near-EOD accuracy
1 parent 2e1142c commit 8b98a0e

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

src/templates/otoken/otoken-daily-stats.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ export const processOTokenDailyStats = async (
4848
) => {
4949
// Daily Stats
5050
// Whatever days we've just crossed over, let's update their respective daily stat entry using the last block seen at that time.
51+
// params.balances reflects addressMap state at end-of-batch — only meaningful for the day matching the batch's last block.
52+
// For older days in this batch (batch spanning a UTC boundary), skip accountsOverThreshold so the prior batch's
53+
// near-EOD value isn't overwritten with a snapshot from the next day.
54+
const lastBatchDate = new Date(ctx.blocks[ctx.blocks.length - 1].header.timestamp).toISOString().substring(0, 10)
5155
for (const { block, entity } of params.dailyStats.values()) {
5256
if (block.header.height < params.from) continue
5357
const blockDate = new Date(block.header.timestamp)
@@ -237,9 +241,15 @@ export const processOTokenDailyStats = async (
237241
entity.marketCapUSD = +formatUnits(entity.totalSupply * entity.rateUSD, 18)
238242
entity.wrappedSupply = wrappedSupply
239243
entity.rateWrapped = wrappedRate
240-
entity.accountsOverThreshold = Array.from(params.balances.values()).filter(
241-
(balance) => balance >= params.accountsOverThresholdMinimum,
242-
).length
244+
// Only refresh accountsOverThreshold when the entity's day matches the batch's
245+
// last block — otherwise params.balances reflects a snapshot from a later day.
246+
// Exception: if the entity is uninitialized (e.g., created earlier in this same
247+
// multi-day batch), set an initial value rather than leaving it at 0.
248+
if (entity.date === lastBatchDate || needsInit) {
249+
entity.accountsOverThreshold = Array.from(params.balances.values()).filter(
250+
(balance) => balance >= params.accountsOverThresholdMinimum,
251+
).length
252+
}
243253
ctx.log.info(`Updated OTokenDailyStat: ${entity.id}`)
244254
}
245255
}

0 commit comments

Comments
 (0)