Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: rewards-v2 pay by snapshots #150

Merged
merged 5 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ chart_releases
/snapshots/**/*.sql
/snapshots/**/*.csv

.env
2 changes: 1 addition & 1 deletion .testdataVersion
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1490a0e18dd51ffbb59209ca9cf6513268af21f5
a2dce496e18ec5536eeb8247e442deae289c679d
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ require (
github.com/wealdtech/go-merkletree/v2 v2.6.0
github.com/wk8/go-ordered-map/v2 v2.1.8
go.uber.org/zap v1.27.0
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2
google.golang.org/grpc v1.65.0
gorm.io/driver/postgres v1.5.9
gorm.io/gorm v1.25.10
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,8 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 h1:H2TDz8ibqkAF6YGhCdN3jS9O0/s90v0rJh3X/OLHEUk=
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8=
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M=
google.golang.org/api v0.8.0/go.mod h1:o4eAsZoiT+ibD93RtjEohWalFOjRDx6CVaqeizhEnKg=
Expand Down
2 changes: 1 addition & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func (c *Config) GetForkDates() (ForkMap, error) {
Fork_Amazon: "1970-01-01", // Amazon hard fork was never on preprod as we backfilled
Fork_Nile: "2024-08-14", // Last calculation end timestamp was 8-13: https://holesky.etherscan.io/tx/0xb5a6855e88c79312b7c0e1c9f59ae9890b97f157ea27e69e4f0fadada4712b64#eventlog
Fork_Panama: "2024-10-01",
Fork_Arno: "2024-12-12",
Fork_Arno: "2024-12-04",
}, nil
case Chain_Holesky:
return ForkMap{
Expand Down
2 changes: 1 addition & 1 deletion internal/tests/testdata/combinedRewards/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,5 @@ select
duration,
block_number as block_number
from dbt_preprod_holesky_rewards.rewards_combined
where block_time < '2024-12-10'
where block_time < '2024-12-13'
```
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ FROM transaction_logs t
LEFT JOIN blocks b ON t.block_sequence_id = b.id
WHERE t.address = '0x141d6995556135d4997b2ff72eb443be300353bc'
AND t.event_name = 'OperatorAVSRegistrationStatusUpdated'
AND date_trunc('day', b.block_time) < TIMESTAMP '2024-12-10'
AND date_trunc('day', b.block_time) < TIMESTAMP '2024-12-13'
)
select
operator,
Expand Down
44 changes: 26 additions & 18 deletions internal/tests/testdata/operatorDirectedRewardSubmissions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,36 @@

```sql
WITH strategies AS (
SELECT
tl.*,
output_data->'operatorDirectedRewardsSubmission'->>'token' as token,
SELECT
tl.*,
lower(arguments #>> '{2, Value}') as reward_hash,
output_data->'operatorDirectedRewardsSubmission'->>'token' as token,
output_data->'operatorDirectedRewardsSubmission'->>'duration' as duration,
output_data->'operatorDirectedRewardsSubmission'->>'startTimestamp' as start_timestamp,
strategy_data,
strategy_idx - 1 as strategy_idx -- Subtract 1 for 0-based indexing
FROM transaction_logs as tl,
jsonb_array_elements(output_data->'operatorDirectedRewardsSubmission'->'strategiesAndMultipliers')
WITH ORDINALITY AS t(strategy_data, strategy_idx)
where
FROM transaction_logs as tl,
jsonb_array_elements(output_data->'operatorDirectedRewardsSubmission'->'strategiesAndMultipliers')
WITH ORDINALITY AS t(strategy_data, strategy_idx)
where
address = '0xb22ef643e1e067c994019a4c19e403253c05c2b0'
and event_name = 'OperatorDirectedAVSRewardsSubmissionCreated'
),
operators AS (
SELECT
and event_name = 'OperatorDirectedAVSRewardsSubmissionCreated'
),
operators AS (
SELECT
lower(arguments #>> '{2, Value}') as reward_hash,
operator_data,
operator_data->>'operator' as operator,
output_data->'operatorDirectedRewardsSubmission' as rewards_submission,
operator_idx - 1 as operator_idx -- Subtract 1 to make it 0-based indexing
FROM transaction_logs,
jsonb_array_elements(output_data->'operatorDirectedRewardsSubmission'->'operatorRewards')
WITH ORDINALITY AS t(operator_data, operator_idx)
where
FROM transaction_logs,
jsonb_array_elements(output_data->'operatorDirectedRewardsSubmission'->'operatorRewards')
WITH ORDINALITY AS t(operator_data, operator_idx)
where
address = '0xb22ef643e1e067c994019a4c19e403253c05c2b0'
and event_name = 'OperatorDirectedAVSRewardsSubmissionCreated'
)
and event_name = 'OperatorDirectedAVSRewardsSubmissionCreated'
),
joined_data as (
SELECT
lower(arguments #>> '{1, Value}') as avs,
lower(arguments #>> '{2, Value}') as reward_hash,
Expand All @@ -45,5 +49,9 @@ SELECT
transaction_hash,
log_index
FROM strategies
CROSS JOIN operators;
inner join operators on(
strategies.reward_hash = operators.reward_hash
)
)
select * from joined_data
```
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ select
avs_directory_address
from operator_restaked_strategies
where avs_directory_address = '0x141d6995556135d4997b2ff72eb443be300353bc'
and block_time < '2024-12-10'
and block_time < '2024-12-13'
```

## Expected results
Expand Down
2 changes: 1 addition & 1 deletion internal/tests/testdata/operatorShareSnapshots/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ preprod-rewardsV2
select
*
from dbt_preprod_holesky_rewards.operator_shares
where block_time < '2024-12-10'
where block_time < '2024-12-13'
```

## Expected results
Expand Down
4 changes: 2 additions & 2 deletions internal/tests/testdata/operatorShares/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ SELECT
FROM (
SELECT operator, strategy, shares, transaction_hash, log_index, block_time, block_date, block_number
FROM dbt_testnet_holesky_rewards.operator_share_increases
where block_date < '2024-12-10'
where block_date < '2024-12-13'

UNION ALL

SELECT operator, strategy, shares * -1 AS shares, transaction_hash, log_index, block_time, block_date, block_number
FROM dbt_testnet_holesky_rewards.operator_share_decreases
where block_date < '2024-12-10'
where block_date < '2024-12-13'
) combined_shares
```
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ FROM (
UNION ALL
SELECT *, 'delegations' AS src FROM dbt_preprod_holesky_rewards.staker_delegations
) as delegations_combined
where block_time < '2024-12-10'
where block_time < '2024-12-13'
```


Expand Down
2 changes: 1 addition & 1 deletion internal/tests/testdata/stakerShareSnapshots/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ select
block_date,
block_number
from dbt_preprod_holesky_rewards.staker_shares
where block_time < '2024-12-10'
where block_time < '2024-12-13'
```

## Expected results
Expand Down
36 changes: 18 additions & 18 deletions internal/tests/testdata/stakerShares/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,28 +131,28 @@ SELECT
block_date,
block_number
FROM (
SELECT staker, strategy, shares, 0 as strategy_index, transaction_hash, log_index, block_time, block_date, block_number
FROM dbt_mainnet_ethereum_rewards.staker_deposits
where block_date < '2024-08-20'
SELECT staker, strategy, shares, 0 as strategy_index, transaction_hash, log_index, block_time, block_date, block_number
FROM dbt_preprod_holesky_rewards.staker_deposits
where block_date < '2024-12-13'

UNION ALL
UNION ALL

-- Subtract m1 & m2 withdrawals
SELECT staker, strategy, shares * -1, 0 as strategy_index, transaction_hash, log_index, block_time, block_date, block_number
FROM dbt_mainnet_ethereum_rewards.m1_staker_withdrawals
where block_date < '2024-08-20'
-- Subtract m1 & m2 withdrawals
SELECT staker, strategy, shares * -1, 0 as strategy_index, transaction_hash, log_index, block_time, block_date, block_number
FROM dbt_preprod_holesky_rewards.m1_staker_withdrawals
where block_date < '2024-12-13'

UNION ALL
UNION ALL

SELECT staker, strategy, shares * -1, strategy_index, transaction_hash, log_index, block_time, block_date, block_number
FROM dbt_mainnet_ethereum_rewards.m2_staker_withdrawals
where block_date < '2024-08-20'
SELECT staker, strategy, shares * -1, strategy_index, transaction_hash, log_index, block_time, block_date, block_number
FROM dbt_preprod_holesky_rewards.m2_staker_withdrawals
where block_date < '2024-12-13'

UNION all
UNION all

-- Shares in eigenpod are positive or negative, so no need to multiply by -1
SELECT staker, '0xbeac0eeeeeeeeeeeeeeeeeeeeeeeeeeeeeebeac0' as strategy, shares, 0 as strategy_index, transaction_hash, log_index, block_time, block_date, block_number
FROM dbt_mainnet_ethereum_rewards.eigenpod_shares
where block_date < '2024-08-20'
) combined_staker_shares
-- Shares in eigenpod are positive or negative, so no need to multiply by -1
SELECT staker, '0xbeac0eeeeeeeeeeeeeeeeeeeeeeeeeeeeeebeac0' as strategy, shares, 0 as strategy_index, transaction_hash, log_index, block_time, block_date, block_number
FROM dbt_preprod_holesky_rewards.eigenpod_shares
where block_date < '2024-12-13'
) combined_staker_shares
```
3 changes: 2 additions & 1 deletion pkg/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package postgres
import (
"database/sql"
"fmt"
"regexp"

"github.com/Layr-Labs/sidecar/internal/config"
"github.com/Layr-Labs/sidecar/internal/tests"
"github.com/Layr-Labs/sidecar/pkg/postgres/migrations"
Expand All @@ -11,7 +13,6 @@ import (
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"regexp"
)

type PostgresConfig struct {
Expand Down
12 changes: 4 additions & 8 deletions pkg/rewards/10_goldAvsODRewardAmounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,15 @@ WITH reward_snapshot_operators AS (
ap.reward_hash,
ap.snapshot AS snapshot,
ap.token,
ap.tokens_per_day,
ap.tokens_per_day_decimal,
ap.tokens_per_registered_snapshot_decimal,
ap.avs AS avs,
ap.operator AS operator,
ap.strategy,
ap.multiplier,
ap.reward_submission_date
FROM {{.activeODRewardsTable}} ap
LEFT JOIN operator_avs_registration_snapshots oar
ON ap.avs = oar.avs
AND ap.snapshot = oar.snapshot
AND ap.operator = oar.operator
WHERE oar.avs IS NULL OR oar.operator IS NULL
WHERE
ap.num_registered_snapshots = 0
),

-- Step 2: Dedupe the operator tokens across strategies for each (operator, reward hash, snapshot)
Expand Down Expand Up @@ -56,7 +52,7 @@ operator_token_sums AS (
token,
avs,
operator,
SUM(tokens_per_day_decimal) OVER (PARTITION BY reward_hash, snapshot) AS avs_tokens
SUM(tokens_per_registered_snapshot_decimal) OVER (PARTITION BY reward_hash, snapshot) AS avs_tokens
FROM distinct_operators
)

Expand Down
67 changes: 56 additions & 11 deletions pkg/rewards/7_goldActiveODRewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ WITH
active_rewards_modified AS (
SELECT
*,
amount / (duration / 86400) AS tokens_per_day,
CAST(@cutoffDate AS TIMESTAMP(6)) AS global_end_inclusive -- Inclusive means we DO USE this day as a snapshot
FROM operator_directed_rewards
WHERE end_timestamp >= TIMESTAMP '{{.rewardsStart}}'
Expand All @@ -34,11 +33,12 @@ active_rewards_updated_end_timestamps AS (
*/
start_timestamp AS reward_start_exclusive,
LEAST(global_end_inclusive, end_timestamp) AS reward_end_inclusive,
tokens_per_day,
amount,
token,
multiplier,
strategy,
reward_hash,
duration,
global_end_inclusive,
block_date AS reward_submission_date
FROM active_rewards_modified
Expand All @@ -52,13 +52,12 @@ active_rewards_updated_start_timestamps AS (
COALESCE(MAX(g.snapshot), ap.reward_start_exclusive) AS reward_start_exclusive,
ap.reward_end_inclusive,
ap.token,
-- We use floor to ensure we are always underesimating total tokens per day
FLOOR(ap.tokens_per_day) AS tokens_per_day_decimal,
-- Round down to 15 sigfigs for double precision, ensuring know errouneous round up or down
ap.tokens_per_day * ((POW(10, 15) - 1) / POW(10, 15)) AS tokens_per_day,
-- We use floor to ensure we are always underestimating total tokens per day
FLOOR(ap.amount) AS amount_decimal,
ap.multiplier,
ap.strategy,
ap.reward_hash,
ap.duration,
ap.global_end_inclusive,
ap.reward_submission_date
FROM active_rewards_updated_end_timestamps ap
Expand All @@ -69,10 +68,11 @@ active_rewards_updated_start_timestamps AS (
ap.operator,
ap.reward_end_inclusive,
ap.token,
ap.tokens_per_day,
ap.amount,
ap.multiplier,
ap.strategy,
ap.reward_hash,
ap.duration,
ap.global_end_inclusive,
ap.reward_start_exclusive,
ap.reward_submission_date
Expand Down Expand Up @@ -100,22 +100,67 @@ exploded_active_range_rewards AS (
) AS day
),

-- Step 7: Prepare final active rewards
active_rewards_final AS (
-- Step 7: Prepare cleaned active rewards
active_rewards_cleaned AS (
SELECT
avs,
operator,
CAST(day AS DATE) AS snapshot,
token,
tokens_per_day,
tokens_per_day_decimal,
amount_decimal,
multiplier,
strategy,
duration,
reward_hash,
reward_submission_date
FROM exploded_active_range_rewards
-- Remove snapshots on the start day
WHERE day != reward_start_exclusive
),

-- Step 8: Dedupe the active rewards by (avs, snapshot, operator, reward_hash)
active_rewards_reduced_deduped AS (
SELECT DISTINCT avs, snapshot, operator, reward_hash
FROM active_rewards_cleaned
),

-- Step 9: Divide by the number of snapshots that the operator was registered
op_avs_num_registered_snapshots AS (
SELECT
ar.reward_hash,
ar.operator,
COUNT(*) AS num_registered_snapshots
FROM active_rewards_reduced_deduped ar
JOIN operator_avs_registration_snapshots oar
ON
ar.avs = oar.avs
AND ar.snapshot = oar.snapshot
AND ar.operator = oar.operator
GROUP BY ar.reward_hash, ar.operator
),

-- Step 10: Divide amount to pay by the number of snapshots that the operator was registered
active_rewards_with_registered_snapshots AS (
SELECT
arc.*,
COALESCE(nrs.num_registered_snapshots, 0) as num_registered_snapshots
FROM active_rewards_cleaned arc
LEFT JOIN op_avs_num_registered_snapshots nrs
ON
arc.reward_hash = nrs.reward_hash
AND arc.operator = nrs.operator
),

-- Step 11: Divide amount to pay by the number of snapshots that the operator was registered
active_rewards_final AS (
SELECT
ar.*,
CASE
-- If the operator was not registered for any snapshots, just get regular tokens per day to refund the AVS
WHEN ar.num_registered_snapshots = 0 THEN ar.amount_decimal / (duration / 86400)
ELSE ar.amount_decimal / ar.num_registered_snapshots
END AS tokens_per_registered_snapshot_decimal
FROM active_rewards_with_registered_snapshots ar
)

SELECT * FROM active_rewards_final
Expand Down
Loading
Loading