Skip to content

Commit 9faf22a

Browse files
committed
implement VIEW-Based Withdrawal Queue Adjustment
1 parent 5c5e3f9 commit 9faf22a

File tree

15 files changed

+82
-326
lines changed

15 files changed

+82
-326
lines changed

pkg/postgres/migrations/202511171438_withdrawalAndDeallocationQueues/up.go

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,10 @@ type Migration struct {
1212
}
1313

1414
func (m *Migration) Up(db *sql.DB, grm *gorm.DB, cfg *config.Config) error {
15-
queries := []string{
16-
17-
// =============================================================================
18-
// PART 1: Create withdrawal_queue_share_snapshots table for rewards calculation
19-
// =============================================================================
15+
// NOTE: Deallocation queue removed - delays handled by operator_allocation_snapshots (PR #474) rounding logic
2016

21-
// Table to track shares in withdrawal queue that should still earn rewards
22-
// Stakers continue earning while in 14-day queue because they're still taking slashing risk
17+
queries := []string{
18+
// Tracks shares in withdrawal queue earning rewards during 14-day period
2319
`create table if not exists withdrawal_queue_share_snapshots (
2420
staker varchar not null,
2521
strategy varchar not null,
@@ -30,41 +26,8 @@ func (m *Migration) Up(db *sql.DB, grm *gorm.DB, cfg *config.Config) error {
3026
primary key (staker, strategy, snapshot),
3127
constraint uniq_withdrawal_queue_share_snapshots unique (staker, strategy, snapshot)
3228
)`,
33-
34-
// Index for querying withdrawal queue share snapshots by snapshot date
3529
`create index if not exists idx_withdrawal_queue_share_snapshots_snapshot on withdrawal_queue_share_snapshots(snapshot)`,
36-
37-
// Index for staker lookups
3830
`create index if not exists idx_withdrawal_queue_share_snapshots_staker on withdrawal_queue_share_snapshots(staker, snapshot)`,
39-
40-
// =============================================================================
41-
// PART 2: Create deallocation_queue_snapshots table for operator allocation tracking
42-
// =============================================================================
43-
44-
// Table to track operator allocation decreases that haven't reached effective_date yet
45-
// Operators continue earning on old (higher) allocation until effective_date
46-
// Similar concept to withdrawal queue but for operator deallocations
47-
`create table if not exists deallocation_queue_snapshots (
48-
operator varchar not null,
49-
avs varchar not null,
50-
operator_set_id bigint not null,
51-
strategy varchar not null,
52-
magnitude_decrease numeric not null,
53-
block_date date not null,
54-
effective_date date not null,
55-
snapshot date not null,
56-
primary key (operator, avs, operator_set_id, strategy, snapshot),
57-
constraint uniq_deallocation_queue_snapshots unique (operator, avs, operator_set_id, strategy, snapshot)
58-
)`,
59-
60-
// Index for querying deallocation queue snapshots by snapshot date
61-
`create index if not exists idx_deallocation_queue_snapshots_snapshot on deallocation_queue_snapshots(snapshot)`,
62-
63-
// Index for operator lookups
64-
`create index if not exists idx_deallocation_queue_snapshots_operator on deallocation_queue_snapshots(operator, snapshot)`,
65-
66-
// Index for AVS lookups
67-
`create index if not exists idx_deallocation_queue_snapshots_avs on deallocation_queue_snapshots(avs, snapshot)`,
6831
}
6932

7033
for _, query := range queries {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package _202512021500_stakerSharesView
2+
3+
import (
4+
"database/sql"
5+
"fmt"
6+
7+
"github.com/Layr-Labs/sidecar/internal/config"
8+
"gorm.io/gorm"
9+
)
10+
11+
func (m *Migration) Down(db *sql.DB, grm *gorm.DB, cfg *config.Config) error {
12+
queries := []string{
13+
`drop view if exists staker_share_snapshots_final`,
14+
}
15+
16+
for _, query := range queries {
17+
res := grm.Exec(query)
18+
if res.Error != nil {
19+
fmt.Printf("Error executing query: %s\n", query)
20+
return res.Error
21+
}
22+
}
23+
return nil
24+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package _202512021500_stakerSharesView
2+
3+
import (
4+
"database/sql"
5+
"fmt"
6+
7+
"github.com/Layr-Labs/sidecar/internal/config"
8+
"gorm.io/gorm"
9+
)
10+
11+
type Migration struct {
12+
}
13+
14+
func (m *Migration) Up(db *sql.DB, grm *gorm.DB, cfg *config.Config) error {
15+
queries := []string{
16+
// VIEW combines base shares + withdrawal queue adjustments
17+
`create view if not exists staker_share_snapshots_final as
18+
select
19+
coalesce(base.staker, wq.staker) as staker,
20+
coalesce(base.strategy, wq.strategy) as strategy,
21+
(coalesce(base.shares::numeric, 0) + coalesce(wq.shares::numeric, 0))::text as shares,
22+
coalesce(base.snapshot, wq.snapshot) as snapshot
23+
from staker_share_snapshots base
24+
full outer join withdrawal_queue_share_snapshots wq
25+
on base.staker = wq.staker
26+
and base.strategy = wq.strategy
27+
and base.snapshot = wq.snapshot`,
28+
}
29+
30+
for _, query := range queries {
31+
res := grm.Exec(query)
32+
if res.Error != nil {
33+
fmt.Printf("Error executing query: %s\n", query)
34+
return res.Error
35+
}
36+
}
37+
return nil
38+
}
39+
40+
func (m *Migration) GetName() string {
41+
return "202512021500_stakerSharesView"
42+
}

pkg/postgres/migrations/migrator.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ import (
8484
_202511051502_keyRotationScheduled "github.com/Layr-Labs/sidecar/pkg/postgres/migrations/202511051502_keyRotationScheduled"
8585
_202511141700_withdrawalQueueAndAllocationRounding "github.com/Layr-Labs/sidecar/pkg/postgres/migrations/202511141700_withdrawalQueueAndAllocationRounding"
8686
_202511171438_withdrawalAndDeallocationQueues "github.com/Layr-Labs/sidecar/pkg/postgres/migrations/202511171438_withdrawalAndDeallocationQueues"
87+
_202512021500_stakerSharesView "github.com/Layr-Labs/sidecar/pkg/postgres/migrations/202512021500_stakerSharesView"
8788
)
8889

8990
// Migration interface defines the contract for database migrations.
@@ -230,6 +231,7 @@ func (m *Migrator) MigrateAll() error {
230231
&_202511051502_keyRotationScheduled.Migration{},
231232
&_202511141700_withdrawalQueueAndAllocationRounding.Migration{},
232233
&_202511171438_withdrawalAndDeallocationQueues.Migration{},
234+
&_202512021500_stakerSharesView.Migration{},
233235
}
234236

235237
for _, migration := range migrations {

pkg/rewards/13_goldStakerODOperatorSetRewardAmounts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ staker_strategy_shares AS (
7373
sdo.*,
7474
sss.shares
7575
FROM staker_delegated_operators sdo
76-
JOIN staker_share_snapshots sss
76+
JOIN staker_share_snapshots_final sss
7777
ON sdo.staker = sss.staker
7878
AND sdo.snapshot = sss.snapshot
7979
AND sdo.strategy = sss.strategy

pkg/rewards/2_goldStakerRewardAmounts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ staker_avs_strategy_shares AS (
5656
sdo.*,
5757
sss.shares
5858
FROM staker_delegated_operators sdo
59-
JOIN staker_share_snapshots sss
59+
JOIN staker_share_snapshots_final sss
6060
ON
6161
sdo.staker = sss.staker AND
6262
sdo.snapshot = sss.snapshot AND

pkg/rewards/4_goldRewardsForAll.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ WITH reward_snapshot_stakers AS (
2020
sss.staker,
2121
sss.shares
2222
FROM {{.activeRewardsTable}} ap
23-
JOIN staker_share_snapshots as sss
23+
JOIN staker_share_snapshots_final as sss
2424
ON ap.strategy = sss.strategy and ap.snapshot = sss.snapshot
2525
WHERE ap.reward_type = 'all_stakers'
2626
-- Parse out negative shares and zero multiplier so there is no division by zero case

pkg/rewards/5_goldRfaeStakers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ staker_strategy_shares AS (
6666
sdo.*,
6767
sss.shares
6868
FROM staker_delegated_operators sdo
69-
JOIN staker_share_snapshots sss
69+
JOIN staker_share_snapshots_final sss
7070
ON
7171
sdo.staker = sss.staker AND
7272
sdo.snapshot = sss.snapshot AND

pkg/rewards/9_goldStakerODRewardAmounts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ staker_avs_strategy_shares AS (
6565
sdo.*,
6666
sss.shares
6767
FROM staker_delegated_operators sdo
68-
JOIN staker_share_snapshots sss
68+
JOIN staker_share_snapshots_final sss
6969
ON sdo.staker = sss.staker
7070
AND sdo.snapshot = sss.snapshot
7171
AND sdo.strategy = sss.strategy

pkg/rewards/deallocationQueueShareSnapshots.go

Lines changed: 0 additions & 203 deletions
This file was deleted.

0 commit comments

Comments
 (0)