Skip to content

Commit

Permalink
feat: impl
Browse files Browse the repository at this point in the history
  • Loading branch information
gpsanant committed Dec 12, 2024
1 parent b4c47a2 commit c38c613
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 14 deletions.
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
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
7 changes: 2 additions & 5 deletions pkg/rewards/10_goldAvsODRewardAmounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@ WITH reward_snapshot_operators AS (
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
num_registered_snapshots = 0
),
-- Step 2: Dedupe the operator tokens across strategies for each (operator, reward hash, snapshot)
Expand Down
52 changes: 43 additions & 9 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,7 +33,7 @@ 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,
Expand All @@ -53,9 +52,9 @@ active_rewards_updated_start_timestamps AS (
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,
FLOOR(ap.amount) AS amount_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,
ap.amount * ((POW(10, 15) - 1) / POW(10, 15)) AS amount,
ap.multiplier,
ap.strategy,
ap.reward_hash,
Expand All @@ -69,7 +68,7 @@ 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,
Expand Down Expand Up @@ -100,15 +99,15 @@ 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,
amount_decimal,
multiplier,
strategy,
reward_hash,
Expand All @@ -118,6 +117,41 @@ active_rewards_final AS (
WHERE day != reward_start_exclusive
)
-- Step 8: Divide by the number of snapshots that the operator was registered
num_registered_snapshots AS (
SELECT
arc.avs,
arc.operator,
COUNT(*) AS num_registered_snapshots,
FROM active_rewards_cleaned arc
JOIN operator_avs_registration_snapshots oar
ON
arc.avs = oar.avs
AND arc.snapshot = oar.snapshot
AND arc.operator = oar.operator
),
-- Step 9: Divide amount to pay by the number of snapshots that the operator was registered
active_rewards_final AS (
SELECT
arc.*,
nrs.num_registered_snapshots,
CASE
-- If the operator was not registered for any snapshots, just get regular tokens per day to refund the AVS
WHEN nrs.num_registered_snapshots = 0 THEN arc.amount_decimal / (duration / 86400)
ELSE arc.amount_decimal / nrs.num_registered_snapshots
END AS tokens_per_day_decimal,
CASE
-- If the operator was not registered for any snapshots, just get regular tokens per day to refund the AVS
WHEN nrs.num_registered_snapshots = 0THEN arc.amount / (duration / 86400)
ELSE arc.amount / nrs.num_registered_snapshots
END AS tokens_per_day
FROM active_rewards_cleaned arc
JOIN num_registered_snapshots nrs
ON
arc.avs = nrs.avs
AND arc.operator = nrs.operator
)
SELECT * FROM active_rewards_final
`

Expand Down

0 comments on commit c38c613

Please sign in to comment.