Skip to content

Commit

Permalink
Refactor and cleanup rewards helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmcgary committed Dec 2, 2024
1 parent af9bf25 commit c5f33b7
Show file tree
Hide file tree
Showing 20 changed files with 81 additions and 84 deletions.
7 changes: 4 additions & 3 deletions pkg/rewards/1_goldActiveRewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package rewards

import (
"database/sql"
"github.com/Layr-Labs/sidecar/pkg/rewardsUtils"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -100,8 +101,8 @@ select * from active_rewards_final
// @param startDate: The lower bound of when to calculate rewards from. If we're running rewards for the first time,
// this will be "1970-01-01". If this is a subsequent run, this will be the last snapshot date.
func (r *RewardsCalculator) Generate1ActiveRewards(snapshotDate string) error {
allTableNames := getGoldTableNames(snapshotDate)
destTableName := allTableNames[Table_1_ActiveRewards]
allTableNames := rewardsUtils.GetGoldTableNames(snapshotDate)
destTableName := allTableNames[rewardsUtils.Table_1_ActiveRewards]

rewardsStart := "1970-01-01 00:00:00" // This will always start as this date and get's updated later in the query

Expand All @@ -111,7 +112,7 @@ func (r *RewardsCalculator) Generate1ActiveRewards(snapshotDate string) error {
zap.String("destTableName", destTableName),
)

query, err := renderQueryTemplate(_1_goldActiveRewardsQuery, map[string]string{
query, err := rewardsUtils.RenderQueryTemplate(_1_goldActiveRewardsQuery, map[string]string{
"destTableName": destTableName,
"rewardsStart": rewardsStart,
"cutoffDate": snapshotDate,
Expand Down
9 changes: 5 additions & 4 deletions pkg/rewards/2_goldStakerRewardAmounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rewards
import (
"database/sql"
"github.com/Layr-Labs/sidecar/internal/config"
"github.com/Layr-Labs/sidecar/pkg/rewardsUtils"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -133,8 +134,8 @@ ORDER BY reward_hash, snapshot, staker, operator
`

func (rc *RewardsCalculator) GenerateGold2StakerRewardAmountsTable(snapshotDate string, forks config.ForkMap) error {
allTableNames := getGoldTableNames(snapshotDate)
destTableName := allTableNames[Table_2_StakerRewardAmounts]
allTableNames := rewardsUtils.GetGoldTableNames(snapshotDate)
destTableName := allTableNames[rewardsUtils.Table_2_StakerRewardAmounts]

rc.logger.Sugar().Infow("Generating staker reward amounts",
zap.String("cutoffDate", snapshotDate),
Expand All @@ -143,9 +144,9 @@ func (rc *RewardsCalculator) GenerateGold2StakerRewardAmountsTable(snapshotDate
zap.String("nileHardforkDate", forks[config.Fork_Nile]),
)

query, err := renderQueryTemplate(_2_goldStakerRewardAmountsQuery, map[string]string{
query, err := rewardsUtils.RenderQueryTemplate(_2_goldStakerRewardAmountsQuery, map[string]string{
"destTableName": destTableName,
"activeRewardsTable": allTableNames[Table_1_ActiveRewards],
"activeRewardsTable": allTableNames[rewardsUtils.Table_1_ActiveRewards],
})
if err != nil {
rc.logger.Sugar().Errorw("Failed to render query template", "error", err)
Expand Down
9 changes: 5 additions & 4 deletions pkg/rewards/3_goldOperatorRewardAmounts.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rewards

import (
"github.com/Layr-Labs/sidecar/pkg/rewardsUtils"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -36,17 +37,17 @@ SELECT * FROM distinct_operators
`

func (rc *RewardsCalculator) GenerateGold3OperatorRewardAmountsTable(snapshotDate string) error {
allTableNames := getGoldTableNames(snapshotDate)
destTableName := allTableNames[Table_3_OperatorRewardAmounts]
allTableNames := rewardsUtils.GetGoldTableNames(snapshotDate)
destTableName := allTableNames[rewardsUtils.Table_3_OperatorRewardAmounts]

rc.logger.Sugar().Infow("Generating staker reward amounts",
zap.String("cutoffDate", snapshotDate),
zap.String("destTableName", destTableName),
)

query, err := renderQueryTemplate(_3_goldOperatorRewardAmountsQuery, map[string]string{
query, err := rewardsUtils.RenderQueryTemplate(_3_goldOperatorRewardAmountsQuery, map[string]string{
"destTableName": destTableName,
"stakerRewardAmountsTable": allTableNames[Table_2_StakerRewardAmounts],
"stakerRewardAmountsTable": allTableNames[rewardsUtils.Table_2_StakerRewardAmounts],
})
if err != nil {
rc.logger.Sugar().Errorw("Failed to render query template", "error", err)
Expand Down
9 changes: 5 additions & 4 deletions pkg/rewards/4_goldRewardsForAll.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rewards

import (
"github.com/Layr-Labs/sidecar/pkg/rewardsUtils"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -67,17 +68,17 @@ SELECT * from staker_tokens
`

func (rc *RewardsCalculator) GenerateGold4RewardsForAllTable(snapshotDate string) error {
allTableNames := getGoldTableNames(snapshotDate)
destTableName := allTableNames[Table_4_RewardsForAll]
allTableNames := rewardsUtils.GetGoldTableNames(snapshotDate)
destTableName := allTableNames[rewardsUtils.Table_4_RewardsForAll]

rc.logger.Sugar().Infow("Generating rewards for all table",
zap.String("cutoffDate", snapshotDate),
zap.String("destTableName", destTableName),
)

query, err := renderQueryTemplate(_4_goldRewardsForAllQuery, map[string]string{
query, err := rewardsUtils.RenderQueryTemplate(_4_goldRewardsForAllQuery, map[string]string{
"destTableName": destTableName,
"activeRewardsTable": allTableNames[Table_1_ActiveRewards],
"activeRewardsTable": allTableNames[rewardsUtils.Table_1_ActiveRewards],
})
if err != nil {
rc.logger.Sugar().Errorw("Failed to render query template", "error", err)
Expand Down
9 changes: 5 additions & 4 deletions pkg/rewards/5_goldRfaeStakers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rewards
import (
"database/sql"
"github.com/Layr-Labs/sidecar/internal/config"
"github.com/Layr-Labs/sidecar/pkg/rewardsUtils"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -118,17 +119,17 @@ ORDER BY reward_hash, snapshot, staker, operator
`

func (rc *RewardsCalculator) GenerateGold5RfaeStakersTable(snapshotDate string, forks config.ForkMap) error {
allTableNames := getGoldTableNames(snapshotDate)
destTableName := allTableNames[Table_5_RfaeStakers]
allTableNames := rewardsUtils.GetGoldTableNames(snapshotDate)
destTableName := allTableNames[rewardsUtils.Table_5_RfaeStakers]

rc.logger.Sugar().Infow("Generating rfae stakers table",
zap.String("cutoffDate", snapshotDate),
zap.String("destTableName", destTableName),
)

query, err := renderQueryTemplate(_5_goldRfaeStakersQuery, map[string]string{
query, err := rewardsUtils.RenderQueryTemplate(_5_goldRfaeStakersQuery, map[string]string{
"destTableName": destTableName,
"activeRewardsTable": allTableNames[Table_1_ActiveRewards],
"activeRewardsTable": allTableNames[rewardsUtils.Table_1_ActiveRewards],
})
if err != nil {
rc.logger.Sugar().Errorw("Failed to render query template", "error", err)
Expand Down
9 changes: 5 additions & 4 deletions pkg/rewards/6_goldRfaeOperators.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rewards

import (
"github.com/Layr-Labs/sidecar/pkg/rewardsUtils"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -36,17 +37,17 @@ SELECT * FROM distinct_operators
`

func (rc *RewardsCalculator) GenerateGold6RfaeOperatorsTable(snapshotDate string) error {
allTableNames := getGoldTableNames(snapshotDate)
destTableName := allTableNames[Table_6_RfaeOperators]
allTableNames := rewardsUtils.GetGoldTableNames(snapshotDate)
destTableName := allTableNames[rewardsUtils.Table_6_RfaeOperators]

rc.logger.Sugar().Infow("Generating rfae operators table",
zap.String("cutoffDate", snapshotDate),
zap.String("destTableName", destTableName),
)

query, err := renderQueryTemplate(_6_goldRfaeOperatorsQuery, map[string]string{
query, err := rewardsUtils.RenderQueryTemplate(_6_goldRfaeOperatorsQuery, map[string]string{
"destTableName": destTableName,
"rfaeStakersTable": allTableNames[Table_5_RfaeStakers],
"rfaeStakersTable": allTableNames[rewardsUtils.Table_5_RfaeStakers],
})
if err != nil {
rc.logger.Sugar().Errorw("Failed to render query template", "error", err)
Expand Down
23 changes: 12 additions & 11 deletions pkg/rewards/7_goldStaging.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package rewards

import (
"database/sql"
"github.com/Layr-Labs/sidecar/pkg/rewardsUtils"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -85,21 +86,21 @@ FROM deduped_earners
`

func (rc *RewardsCalculator) GenerateGold7StagingTable(snapshotDate string) error {
allTableNames := getGoldTableNames(snapshotDate)
destTableName := allTableNames[Table_7_GoldStaging]
allTableNames := rewardsUtils.GetGoldTableNames(snapshotDate)
destTableName := allTableNames[rewardsUtils.Table_7_GoldStaging]

rc.logger.Sugar().Infow("Generating gold staging",
zap.String("cutoffDate", snapshotDate),
zap.String("destTableName", destTableName),
)

query, err := renderQueryTemplate(_7_goldStagingQuery, map[string]string{
query, err := rewardsUtils.RenderQueryTemplate(_7_goldStagingQuery, map[string]string{
"destTableName": destTableName,
"stakerRewardAmountsTable": allTableNames[Table_2_StakerRewardAmounts],
"operatorRewardAmountsTable": allTableNames[Table_3_OperatorRewardAmounts],
"rewardsForAllTable": allTableNames[Table_4_RewardsForAll],
"rfaeStakerTable": allTableNames[Table_5_RfaeStakers],
"rfaeOperatorTable": allTableNames[Table_6_RfaeOperators],
"stakerRewardAmountsTable": allTableNames[rewardsUtils.Table_2_StakerRewardAmounts],
"operatorRewardAmountsTable": allTableNames[rewardsUtils.Table_3_OperatorRewardAmounts],
"rewardsForAllTable": allTableNames[rewardsUtils.Table_4_RewardsForAll],
"rfaeStakerTable": allTableNames[rewardsUtils.Table_5_RfaeStakers],
"rfaeOperatorTable": allTableNames[rewardsUtils.Table_6_RfaeOperators],
})
if err != nil {
rc.logger.Sugar().Errorw("Failed to render query template", "error", err)
Expand All @@ -123,18 +124,18 @@ type GoldStagingRow struct {
}

func (rc *RewardsCalculator) ListGoldStagingRowsForSnapshot(snapshotDate string) ([]*GoldStagingRow, error) {
allTableNames := getGoldTableNames(snapshotDate)
allTableNames := rewardsUtils.GetGoldTableNames(snapshotDate)

results := make([]*GoldStagingRow, 0)
query, err := renderQueryTemplate(`
query, err := rewardsUtils.RenderQueryTemplate(`
SELECT
earner,
snapshot::text as snapshot,
reward_hash,
token,
amount
FROM {{.goldStagingTable}} WHERE DATE(snapshot) < @cutoffDate`, map[string]string{
"goldStagingTable": allTableNames[Table_7_GoldStaging],
"goldStagingTable": allTableNames[rewardsUtils.Table_7_GoldStaging],
})
if err != nil {
rc.logger.Sugar().Errorw("Failed to render query template", "error", err)
Expand Down
7 changes: 4 additions & 3 deletions pkg/rewards/8_goldFinal.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rewards

import (
"github.com/Layr-Labs/sidecar/pkg/rewardsUtils"
"go.uber.org/zap"
"time"
)
Expand All @@ -25,14 +26,14 @@ type GoldRow struct {
}

func (rc *RewardsCalculator) GenerateGold8FinalTable(snapshotDate string) error {
allTableNames := getGoldTableNames(snapshotDate)
allTableNames := rewardsUtils.GetGoldTableNames(snapshotDate)

rc.logger.Sugar().Infow("Generating gold final table",
zap.String("cutoffDate", snapshotDate),
)

query, err := renderQueryTemplate(_8_goldFinalQuery, map[string]string{
"goldStagingTable": allTableNames[Table_7_GoldStaging],
query, err := rewardsUtils.RenderQueryTemplate(_8_goldFinalQuery, map[string]string{
"goldStagingTable": allTableNames[rewardsUtils.Table_7_GoldStaging],
})
if err != nil {
rc.logger.Sugar().Errorw("Failed to render query template", "error", err)
Expand Down
4 changes: 3 additions & 1 deletion pkg/rewards/combinedRewards.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package rewards

import "github.com/Layr-Labs/sidecar/pkg/rewardsUtils"

const rewardsCombinedQuery = `
with combined_rewards as (
select
Expand Down Expand Up @@ -43,7 +45,7 @@ const rewardsCombinedQuery = `
func (r *RewardsCalculator) GenerateAndInsertCombinedRewards(snapshotDate string) error {
tableName := "combined_rewards"

query, err := renderQueryTemplate(rewardsCombinedQuery, map[string]string{
query, err := rewardsUtils.RenderQueryTemplate(rewardsCombinedQuery, map[string]string{
"cutoffDate": snapshotDate,
})
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion pkg/rewards/operatorAvsRegistrationSnaphots.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package rewards

import "github.com/Layr-Labs/sidecar/pkg/rewardsUtils"

// Operator AVS Registration Windows: Ranges at which an operator has registered for an AVS
// 0. Ranked: Rank the operator state changes by block_time and log_index since sqlite lacks LEAD/LAG functions
// 1. Marked_statuses: Denote which registration status comes after one another
Expand Down Expand Up @@ -103,7 +105,7 @@ CROSS JOIN generate_series(DATE(start_time), DATE(end_time) - interval '1' day,
func (r *RewardsCalculator) GenerateAndInsertOperatorAvsRegistrationSnapshots(snapshotDate string) error {
tableName := "operator_avs_registration_snapshots"

query, err := renderQueryTemplate(operatorAvsRegistrationSnapshotsQuery, map[string]string{
query, err := rewardsUtils.RenderQueryTemplate(operatorAvsRegistrationSnapshotsQuery, map[string]string{
"cutoffDate": snapshotDate,
})
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion pkg/rewards/operatorAvsStrategySnapshots.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package rewards

import "github.com/Layr-Labs/sidecar/pkg/rewardsUtils"

// Operator AVS Strategy Windows: Ranges for which an Operator, Strategy is restaked on an AVS
// 1. Ranked_records: Order all records. Round up block_time to 0 UTC
// 2. Latest_records: Get latest records for each (operator, avs, strategy, day) combination
Expand Down Expand Up @@ -142,7 +144,7 @@ func (r *RewardsCalculator) GenerateAndInsertOperatorAvsStrategySnapshots(snapsh
tableName := "operator_avs_strategy_snapshots"
contractAddresses := r.globalConfig.GetContractsMapForChain()

query, err := renderQueryTemplate(operatorAvsStrategyWindowsQuery, map[string]string{
query, err := rewardsUtils.RenderQueryTemplate(operatorAvsStrategyWindowsQuery, map[string]string{
"cutoffDate": snapshotDate,
})
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion pkg/rewards/operatorShareSnapshots.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package rewards

import "github.com/Layr-Labs/sidecar/pkg/rewardsUtils"

const operatorShareSnapshotsQuery = `
WITH ranked_operator_records as (
SELECT *,
Expand Down Expand Up @@ -48,7 +50,7 @@ FROM
func (r *RewardsCalculator) GenerateAndInsertOperatorShareSnapshots(snapshotDate string) error {
tableName := "operator_share_snapshots"

query, err := renderQueryTemplate(operatorShareSnapshotsQuery, map[string]string{
query, err := rewardsUtils.RenderQueryTemplate(operatorShareSnapshotsQuery, map[string]string{
"cutoffDate": snapshotDate,
})
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion pkg/rewards/operatorShares.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package rewards

import "github.com/Layr-Labs/sidecar/pkg/rewardsUtils"

const operatorSharesQuery = `
select
operator,
Expand All @@ -17,7 +19,7 @@ const operatorSharesQuery = `
func (r *RewardsCalculator) GenerateAndInsertOperatorShares(snapshotDate string) error {
tableName := "operator_shares"

query, err := renderQueryTemplate(operatorSharesQuery, map[string]string{
query, err := rewardsUtils.RenderQueryTemplate(operatorSharesQuery, map[string]string{
"cutoffDate": snapshotDate,
})
if err != nil {
Expand Down
Loading

0 comments on commit c5f33b7

Please sign in to comment.