You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Index for finding active (non-completed) withdrawals
31
-
`create index if not exists idx_queued_withdrawals_active on queued_slashing_withdrawals(staker, operator, completed) where completed = false`,
30
+
`create index if not exists idx_queued_withdrawals_active on queued_slashing_withdrawals(staker, operator) where completion_block_number is null`,
32
31
33
32
// Index for completion queries
34
-
`create index if not exists idx_queued_withdrawals_completed on queued_slashing_withdrawals(completion_block_number) where completed = true`,
33
+
`create index if not exists idx_queued_withdrawals_completed on queued_slashing_withdrawals(completion_block_number) where completion_block_number is not null`,
// Add effective_date column for allocation/deallocation rounding
41
-
// This is a computed value based on magnitude changes (round UP for increases, DOWN for decreases)
42
-
// block_timestamp can be derived from block_number FK to blocks table
43
-
`alter table operator_allocations add column if not exists effective_date date`,
39
+
// Create snapshot table following the same pattern as staker_share_snapshots
40
+
// This table will be populated during rewards calculation with rounding logic applied
41
+
`CREATE TABLE IF NOT EXISTS operator_allocation_snapshots (
42
+
operator varchar not null,
43
+
avs varchar not null,
44
+
strategy varchar not null,
45
+
operator_set_id bigint not null,
46
+
magnitude numeric not null,
47
+
snapshot date not null
48
+
)`,
44
49
45
-
// Create index for effective_date queries
46
-
`create index if not exists idx_operator_allocations_effective_date on operator_allocations(operator, avs, strategy, effective_date)`,
50
+
// Create indexes for efficient snapshot queries
51
+
`create index if not exists idx_operator_allocation_snapshots_operator_avs_strategy_snapshot on operator_allocation_snapshots (operator, avs, strategy, snapshot)`,
52
+
`create index if not exists idx_operator_allocation_snapshots_strategy_snapshot on operator_allocation_snapshots (strategy, snapshot)`,
insert into operator_allocation_snapshots(operator, avs, strategy, operator_set_id, magnitude, snapshot)
10
+
WITH ranked_allocation_records as (
11
+
SELECT *,
12
+
ROW_NUMBER() OVER (PARTITION BY operator, avs, strategy, operator_set_id, cast(block_time AS DATE) ORDER BY block_time DESC, log_index DESC) AS rn
13
+
FROM operator_allocations oa
14
+
INNER JOIN blocks b ON oa.block_number = b.number
15
+
WHERE b.block_time < TIMESTAMP '{{.cutoffDate}}'
16
+
),
17
+
-- Get the latest record for each day
18
+
daily_records as (
19
+
SELECT
20
+
operator,
21
+
avs,
22
+
strategy,
23
+
operator_set_id,
24
+
magnitude,
25
+
block_time,
26
+
block_number,
27
+
log_index
28
+
FROM ranked_allocation_records
29
+
WHERE rn = 1
30
+
),
31
+
-- Compare each record with the previous record to determine if it's an increase or decrease
32
+
records_with_comparison as (
33
+
SELECT
34
+
operator,
35
+
avs,
36
+
strategy,
37
+
operator_set_id,
38
+
magnitude,
39
+
block_time,
40
+
LAG(magnitude) OVER (PARTITION BY operator, avs, strategy, operator_set_id ORDER BY block_time, block_number, log_index) as previous_magnitude,
41
+
-- Allocation (increase): Round UP to next day
42
+
-- Deallocation (decrease or no change): Round DOWN to current day
43
+
CASE
44
+
WHEN LAG(magnitude) OVER (PARTITION BY operator, avs, strategy, operator_set_id ORDER BY block_time, block_number, log_index) IS NULL THEN
45
+
-- First allocation: round down to current day (conservative default)
46
+
date_trunc('day', block_time)
47
+
WHEN magnitude > LAG(magnitude) OVER (PARTITION BY operator, avs, strategy, operator_set_id ORDER BY block_time, block_number, log_index) THEN
48
+
-- Increase: round up to next day
49
+
date_trunc('day', block_time) + INTERVAL '1' day
50
+
ELSE
51
+
-- Decrease or no change: round down to current day
52
+
date_trunc('day', block_time)
53
+
END AS snapshot_time
54
+
FROM daily_records
55
+
),
56
+
-- Get the range for each operator, avs, strategy, operator_set_id combination
57
+
allocation_windows as (
58
+
SELECT
59
+
operator,
60
+
avs,
61
+
strategy,
62
+
operator_set_id,
63
+
magnitude,
64
+
snapshot_time as start_time,
65
+
CASE
66
+
-- If the range does not have the end, use the current timestamp truncated to 0 UTC
67
+
WHEN LEAD(snapshot_time) OVER (PARTITION BY operator, avs, strategy, operator_set_id ORDER BY snapshot_time) is null THEN date_trunc('day', TIMESTAMP '{{.cutoffDate}}')
68
+
ELSE LEAD(snapshot_time) OVER (PARTITION BY operator, avs, strategy, operator_set_id ORDER BY snapshot_time)
69
+
END AS end_time
70
+
FROM records_with_comparison
71
+
),
72
+
cleaned_records as (
73
+
SELECT * FROM allocation_windows
74
+
WHERE start_time < end_time
75
+
)
76
+
SELECT
77
+
operator,
78
+
avs,
79
+
strategy,
80
+
operator_set_id,
81
+
magnitude,
82
+
cast(day AS DATE) AS snapshot
83
+
FROM
84
+
cleaned_records
85
+
CROSS JOIN
86
+
generate_series(DATE(start_time), DATE(end_time) - interval '1' day, interval '1' day) AS day
0 commit comments