Skip to content

Commit

Permalink
fix: use settings key to sync reward snapshots (#297)
Browse files Browse the repository at this point in the history
  • Loading branch information
gowthamsundaresan authored Dec 12, 2024
1 parent 4ba28f9 commit 2ec4386
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
4 changes: 2 additions & 2 deletions packages/seeder/src/events/seedLogsRewardsSubmissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export async function seedLogsAVSRewardsSubmission(toBlock?: bigint, fromBlock?:
if (log.args.rewardsSubmission?.strategiesAndMultipliers) {
for (const strategyAndMultiplier of log.args.rewardsSubmission.strategiesAndMultipliers) {
strategies.push(strategyAndMultiplier.strategy.toLowerCase())
multipliers.push(String(strategyAndMultiplier.multiplier.toString()))
multipliers.push(strategyAndMultiplier.multiplier.toString())
}

logsAvsRewardsSubmissions.push({
Expand Down Expand Up @@ -103,6 +103,6 @@ export async function seedLogsAVSRewardsSubmission(toBlock?: bigint, fromBlock?:
dbTransactions,
`[Logs] AVS Rewards Submission from: ${fromBlock} to: ${toBlock} size: ${seedLength}`
)
} catch (error) {}
} catch {}
})
}
5 changes: 5 additions & 0 deletions packages/seeder/src/monitors/avsApy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ export async function monitorAvsApy() {
try {
// Fetch totalStakers, totalOperators & rewards data for all avs in this iteration
const avsMetrics = await prismaClient.avs.findMany({
where: {
rewardSubmissions: {
some: {}
}
},
include: {
operators: {
where: { isActive: true },
Expand Down
31 changes: 18 additions & 13 deletions packages/seeder/src/seedStakerRewardSnapshots.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import prisma from '@prisma/client'
import { getPrismaClient } from './utils/prismaClient'
import { getNetwork } from './utils/viemClient'
import { bulkUpdateDbTransactions } from './utils/seeder'
import { bulkUpdateDbTransactions, fetchLastSyncTime } from './utils/seeder'
import { fetchTokenPrices } from './utils/tokenPrices'

interface ClaimData {
Expand All @@ -11,12 +11,14 @@ interface ClaimData {
cumulative_amount: string
}

const timeSyncKey = 'lastSyncedTimestamp_stakerRewardSnapshot'

/**
* Seeds the StakerRewardSnapshot table to maintain latest state of all EL stakers
*
* @returns
*/
export async function seedStakerRewardSnapshots() {
export async function seedStakerRewardSnapshots(timestamp?: Date) {
const prismaClient = getPrismaClient()
const bucketUrl = getBucketUrl()
const BATCH_SIZE = 10_000
Expand All @@ -40,18 +42,11 @@ export async function seedStakerRewardSnapshots() {
.split('T')[0]

// Find snapshot date of existing data
const snapshotRecord = await prismaClient.stakerRewardSnapshot.findFirst({
select: {
timestamp: true
},
orderBy: {
timestamp: 'asc' // All snapshots should ideally have the same timestamp, but we check for earliest in case of sync issues
}
})

const snapshotTimestamp = snapshotRecord?.timestamp?.toISOString()?.split('T')[0]
const lastSyncedTimestamp = timestamp
? timestamp?.toISOString()?.split('T')[0]
: (await fetchLastSyncTime(timeSyncKey))?.toISOString()?.split('T')[0]

if (latestSnapshotTimestamp === snapshotTimestamp) {
if (latestSnapshotTimestamp === lastSyncedTimestamp) {
console.log('[In Sync] [Data] Staker Reward Snapshots')
return
}
Expand Down Expand Up @@ -168,6 +163,16 @@ export async function seedStakerRewardSnapshots() {
} finally {
reader.releaseLock()
}

// Update latest time sync key
await prismaClient.settings.upsert({
where: { key: timeSyncKey },
update: { value: Number(latestLog.rewardsCalculationEndTimestamp) * 1000 },
create: {
key: timeSyncKey,
value: Number(latestLog.rewardsCalculationEndTimestamp) * 1000
}
})
} catch {}
}

Expand Down

0 comments on commit 2ec4386

Please sign in to comment.