@@ -42,7 +42,7 @@ const deallocationQueueShareSnapshotsQuery = `
4242 from operator_allocations oa
4343 inner join blocks b on oa.block_number = b.number
4444 where
45- oa.effective_date is not null -- Only Sabine fork records
45+ oa.effective_date is not null
4646 -- Allocation already recorded before or on snapshot
4747 and date(b.block_time) <= '{{.snapshotDate}}'
4848 -- Effective date is in the future relative to snapshot
@@ -85,8 +85,6 @@ const deallocationQueueShareSnapshotsQuery = `
8585 on conflict on constraint uniq_deallocation_queue_snapshots do nothing;
8686`
8787
88- // DeallocationQueueSnapshot represents operator allocation decreases that haven't
89- // reached their effective_date yet, and should still be counted for rewards.
9088type DeallocationQueueSnapshot struct {
9189 Operator string `gorm:"column:operator;primaryKey"`
9290 Avs string `gorm:"column:avs;primaryKey"`
@@ -101,13 +99,7 @@ func (DeallocationQueueSnapshot) TableName() string {
10199 return "deallocation_queue_snapshots"
102100}
103101
104- // GenerateAndInsertDeallocationQueueSnapshots calculates and inserts operator allocation
105- // decreases that should still be counted for rewards because their effective_date hasn't
106- // been reached yet.
107- //
108- // This feature is only active after the Sabine fork (when effective_date is populated).
109102func (r * RewardsCalculator ) GenerateAndInsertDeallocationQueueSnapshots (snapshotDate string ) error {
110- // Check if we're past the Sabine fork
111103 forks , err := r .globalConfig .GetRewardsSqlForkDates ()
112104 if err != nil {
113105 r .logger .Sugar ().Errorw ("Failed to get rewards fork dates" , "error" , err )
@@ -120,7 +112,6 @@ func (r *RewardsCalculator) GenerateAndInsertDeallocationQueueSnapshots(snapshot
120112 return nil
121113 }
122114
123- // Get the block number for the snapshot date to compare with fork block
124115 var maxBlock uint64
125116 res := r .grm .Raw (`
126117 SELECT COALESCE(MAX(number), 0) as max_block
@@ -133,7 +124,6 @@ func (r *RewardsCalculator) GenerateAndInsertDeallocationQueueSnapshots(snapshot
133124 return res .Error
134125 }
135126
136- // Only apply deallocation queue logic if we're past the Sabine fork
137127 if maxBlock < sabineFork .BlockNumber {
138128 r .logger .Sugar ().Debugw ("Snapshot date is before Sabine fork, skipping deallocation queue logic" ,
139129 zap .String ("snapshotDate" , snapshotDate ),
@@ -168,38 +158,41 @@ func (r *RewardsCalculator) GenerateAndInsertDeallocationQueueSnapshots(snapshot
168158 return nil
169159}
170160
171- // ListDeallocationQueueSnapshots returns all deallocation queue snapshots for debugging
172- func (r * RewardsCalculator ) ListDeallocationQueueSnapshots () ([]* DeallocationQueueSnapshot , error ) {
173- var snapshots []* DeallocationQueueSnapshot
174- res := r .grm .Model (& DeallocationQueueSnapshot {}).Find (& snapshots )
175- if res .Error != nil {
176- r .logger .Sugar ().Errorw ("Failed to list deallocation queue snapshots" , "error" , res .Error )
177- return nil , res .Error
161+ func (r * RewardsCalculator ) AdjustOperatorShareSnapshotsForDeallocationQueue (snapshotDate string ) error {
162+ adjustQuery := `
163+ insert into operator_share_snapshots(operator, strategy, shares, snapshot)
164+ select
165+ dqs.operator,
166+ dqs.strategy,
167+ sum(dqs.magnitude_decrease::numeric)::text as shares,
168+ dqs.snapshot as snapshot
169+ from deallocation_queue_snapshots dqs
170+ where dqs.snapshot = '{{.snapshotDate}}'
171+ group by dqs.operator, dqs.strategy, dqs.snapshot
172+ on conflict on constraint uniq_operator_share_snapshots
173+ do update set shares = operator_share_snapshots.shares + EXCLUDED.shares;`
174+
175+ query , err := rewardsUtils .RenderQueryTemplate (adjustQuery , map [string ]interface {}{
176+ "snapshotDate" : snapshotDate ,
177+ })
178+ if err != nil {
179+ r .logger .Sugar ().Errorw ("Failed to render deallocation queue adjustment query template" , "error" , err )
180+ return err
178181 }
179- return snapshots , nil
180- }
181182
182- // adjustOperatorAllocationSnapshotsForDeallocationQueueQuery adds deallocation queue
183- // allocations back to operator allocation snapshots.
184- //
185- // This is similar to withdrawal queue adjustment but for operator allocations.
186- // We add back the magnitude_decrease to the current allocation to get the pre-deallocation value.
187- const adjustOperatorAllocationSnapshotsForDeallocationQueueQuery = `
188- -- NOT YET IMPLEMENTED
189- -- This would need to integrate with operator allocation snapshots used in rewards v2.2
190- -- For now, this is a placeholder for future operator set rewards calculation
191- -- TODO: Integrate with operator set rewards calculation
192- `
183+ res := r .grm .Debug ().Exec (query )
184+ if res .Error != nil {
185+ r .logger .Sugar ().Errorw ("Failed to adjust operator_share_snapshots for deallocation queue" ,
186+ zap .String ("snapshotDate" , snapshotDate ),
187+ zap .Error (res .Error ),
188+ )
189+ return res .Error
190+ }
193191
194- // AdjustOperatorAllocationSnapshotsForDeallocationQueue adds deallocation queue
195- // allocations back to operator allocation snapshots.
196- //
197- // NOTE: This is a placeholder for integration with operator set rewards (v2.2).
198- // The actual adjustment logic will depend on how operator allocations are used in rewards.
199- func (r * RewardsCalculator ) AdjustOperatorAllocationSnapshotsForDeallocationQueue (snapshotDate string ) error {
200- // TODO: Implement adjustment logic once operator set rewards calculation structure is finalized
201- r .logger .Sugar ().Debugw ("Deallocation queue adjustment not yet implemented" ,
192+ r .logger .Sugar ().Infow ("Adjusted operator share snapshots for deallocation queue" ,
202193 zap .String ("snapshotDate" , snapshotDate ),
194+ zap .Int64 ("rowsAffected" , res .RowsAffected ),
203195 )
196+
204197 return nil
205198}
0 commit comments