Skip to content

Commit

Permalink
Adjust table names
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmcgary committed Dec 2, 2024
1 parent 8cbaf97 commit f719ac0
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 62 deletions.
47 changes: 13 additions & 34 deletions pkg/postgres/migrations/202412021311_stakerOperatorTables/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,20 @@ type Migration struct {

func (m *Migration) Up(db *sql.DB, grm *gorm.DB) error {
queries := []string{
`CREATE TABLE IF NOT EXISTS sot_staker_strategy_payouts (
reward_hash varchar NOT NULL,
snapshot TIMESTAMP NOT NULL,
token varchar NOT NULL,
tokens_per_day double precision NOT NULL,
avs varchar NOT NULL,
strategy varchar NOT NULL,
multiplier numeric NOT NULL,
reward_type varchar NOT NULL,
operator varchar NOT NULL,
staker varchar NOT NULL,
shares numeric NOT NULL,
staker_tokens numeric NOT NULL,
staker_strategy_weight numeric NOT NULL,
staker_total_strategy_weight numeric NOT NULL,
staker_strategy_proportion numeric NOT NULL,
staker_strategy_tokens numeric NOT NULL
`CREATE TABLE IF NOT EXISTS staker_operator (
earner text,
operator text,
reward_type text,
avs text,
token text,
strategy text,
multiplier numeric(78),
shares numeric,
amount numeric,
reward_hash text,
snapshot date
);`,
`CREATE TABLE IF NOT EXISTS sot_operator_strategy_rewards (
reward_hash varchar NOT NULL,
snapshot TIMESTAMP NOT NULL,
token varchar NOT NULL,
tokens_per_day double precision NOT NULL,
avs varchar NOT NULL,
strategy varchar NOT NULL,
multiplier numeric NOT NULL,
reward_type varchar NOT NULL,
operator varchar NOT NULL,
shares numeric NOT NULL,
operator_tokens numeric NOT NULL,
operator_strategy_weight numeric NOT NULL,
operator_total_strategy_weight numeric NOT NULL,
operator_strategy_proportion numeric NOT NULL,
operator_strategy_tokens numeric NOT NULL
)`,
`alter table staker_operator add constraint uniq_staker_operator unique (earner, operator, snapshot, reward_hash, strategy, reward_type);`,
}

for _, query := range queries {
Expand Down
6 changes: 3 additions & 3 deletions pkg/rewards/rewards_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,9 @@ func Test_Rewards(t *testing.T) {

snapshotDates := []string{
"2024-08-02",
//"2024-08-11",
//"2024-08-12",
//"2024-08-19",
"2024-08-11",
"2024-08-12",
"2024-08-19",
}

fmt.Printf("Hydration duration: %v\n", time.Since(testStart))
Expand Down
8 changes: 6 additions & 2 deletions pkg/rewards/stakerOperators/1_stakerStrategyPayouts.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,15 @@ type StakerStrategyPayout struct {
}

func (ssp *StakerStrategyPayout) TableName() string {
return "sot_staker_strategy_payouts"
return "sot_1_staker_strategy_payouts"
}

func (sog *StakerOperatorsGenerator) GenerateAndInsert1StakerStrategyPayouts(cutoffDate string) error {
tableName := "sot_staker_strategy_payouts"
sog.logger.Sugar().Infow("Generating and inserting 1_stakerStrategyPayouts",
"cutoffDate", cutoffDate,
)

tableName := "sot_1_staker_strategy_payouts"
allTableNames := rewardsUtils.GetGoldTableNames(cutoffDate)

query, err := rewardsUtils.RenderQueryTemplate(_1_stakerStrategyPayoutsQuery, map[string]string{
Expand Down
7 changes: 5 additions & 2 deletions pkg/rewards/stakerOperators/2_operatorStrategyRewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,14 @@ type OperatorStrategyRewards struct {
}

func (osr *OperatorStrategyRewards) TableName() string {
return "sot_operator_strategy_rewards"
return "sot_2_operator_strategy_rewards"
}

func (sog *StakerOperatorsGenerator) GenerateAndInsert2OperatorStrategyRewards(cutoffDate string) error {
tableName := "sot_operator_strategy_rewards"
sog.logger.Sugar().Infow("Generating and inserting 2_operatorStrategyRewards",
"cutoffDate", cutoffDate,
)
tableName := "sot_2_operator_strategy_rewards"
allTableNames := rewardsUtils.GetGoldTableNames(cutoffDate)

query, err := rewardsUtils.RenderQueryTemplate(_2_operatorStrategyRewardsQuery, map[string]string{
Expand Down
11 changes: 7 additions & 4 deletions pkg/rewards/stakerOperators/3_rewardsForAllStrategyPayouts.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ rejoined_staker_strategies AS (
rss.*,
rfa.staker_tokens
FROM reward_snapshot_stakers rss
JOIN {{.rewardsForAllTable}} rfa
JOIN {{.rewardsForAllTable}} rfa
ON
rss.snapshot = rfa.snapshot AND
rss.reward_hash = rfa.reward_hash AND
Expand Down Expand Up @@ -92,16 +92,19 @@ type RewardsForAllStrategyPayout struct {
}

func (osr *RewardsForAllStrategyPayout) TableName() string {
return "sot_rewards_for_all_strategy_payout"
return "sot_3_rewards_for_all_strategy_payout"
}

func (sog *StakerOperatorsGenerator) GenerateAndInsert3RewardsForAllStrategyPayout(cutoffDate string) error {
tableName := "sot_rewards_for_all_strategy_payout"
sog.logger.Sugar().Infow("Generating and inserting 3_rewardsForAllStrategyPayoutsQuery",
"cutoffDate", cutoffDate,
)
tableName := "sot_3_rewards_for_all_strategy_payout"
allTableNames := rewardsUtils.GetGoldTableNames(cutoffDate)

query, err := rewardsUtils.RenderQueryTemplate(_3_rewardsForAllStrategyPayoutsQuery, map[string]string{
"activeRewardsTable": allTableNames[rewardsUtils.Table_1_ActiveRewards],
"rewardForAllTable": allTableNames[rewardsUtils.Table_4_RewardsForAll],
"rewardsForAllTable": allTableNames[rewardsUtils.Table_4_RewardsForAll],
})
if err != nil {
sog.logger.Sugar().Errorw("Failed to render 3_rewardsForAllStrategyPayoutsQuery query", "error", err)
Expand Down
7 changes: 5 additions & 2 deletions pkg/rewards/stakerOperators/4_rfaeStakerStrategyPayouts.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,14 @@ type RfaeStakerStrategyPayout struct {
}

func (osr *RfaeStakerStrategyPayout) TableName() string {
return "sot_rfae_staker_strategy_payout"
return "sot_4_rfae_staker_strategy_payout"
}

func (sog *StakerOperatorsGenerator) GenerateAndInsert4RfaeStakerStrategyPayout(cutoffDate string) error {
tableName := "sot_rfae_staker_strategy_payout"
sog.logger.Sugar().Infow("Generating and inserting 4_rfaeStakerStrategyPayout",
"cutoffDate", cutoffDate,
)
tableName := "sot_4_rfae_staker_strategy_payout"
allTableNames := rewardsUtils.GetGoldTableNames(cutoffDate)

query, err := rewardsUtils.RenderQueryTemplate(_4_rfaeStakerStrategyPayoutsQuery, map[string]string{
Expand Down
7 changes: 5 additions & 2 deletions pkg/rewards/stakerOperators/5_rfaeOperatorStrategyPayout.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,14 @@ type RfaeOperatorStrategyPayout struct {
}

func (osr *RfaeOperatorStrategyPayout) TableName() string {
return "sot_rfae_operator_strategy_payout"
return "sot_5_rfae_operator_strategy_payout"
}

func (sog *StakerOperatorsGenerator) GenerateAndInsert5RfaeOperatorStrategyPayout(cutoffDate string) error {
tableName := "sot_rfae_operator_strategy_payout"
sog.logger.Sugar().Infow("Generating and inserting 5_rfaeOperatorStrategyPayout",
"cutoffDate", cutoffDate,
)
tableName := "sot_5_rfae_operator_strategy_payout"
allTableNames := rewardsUtils.GetGoldTableNames(cutoffDate)

query, err := rewardsUtils.RenderQueryTemplate(_5_rfaeOperatorStrategyPayoutsQuery, map[string]string{
Expand Down
17 changes: 10 additions & 7 deletions pkg/rewards/stakerOperators/6_stakerOperatorStaging.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ SELECT
staker_strategy_tokens as amount,
reward_hash,
snapshot
FROM sot_staker_strategy_payouts
FROM sot_1_staker_strategy_payouts
UNION ALL
Expand All @@ -36,7 +36,7 @@ SELECT
operator_strategy_tokens as amount,
reward_hash,
snapshot
FROM sot_operator_strategy_rewards
FROM sot_2_operator_strategy_rewards
UNION all
Expand All @@ -52,7 +52,7 @@ SELECT
staker_strategy_tokens as amount,
reward_hash,
snapshot
FROM sot_rewards_for_all_strategy_payout
FROM sot_3_rewards_for_all_strategy_payout
UNION ALL
Expand All @@ -68,7 +68,7 @@ SELECT
staker_strategy_tokens as amount,
reward_hash,
snapshot
FROM sot_rfae_staker_strategy_payout
FROM sot_4_rfae_staker_strategy_payout
UNION ALL
Expand All @@ -84,7 +84,7 @@ SELECT
operator_strategy_tokens as amount,
reward_hash,
snapshot
FROM sot_rfae_operator_strategy_payout
FROM sot_5_rfae_operator_strategy_payout
`

type StakerOperatorStaging struct {
Expand All @@ -102,8 +102,11 @@ type StakerOperatorStaging struct {
}

func (sog *StakerOperatorsGenerator) GenerateAndInsert6StakerOperatorStaging(cutoffDate string) error {
sog.logger.Sugar().Infow("Generating and inserting 6_stakerOperatorsStaging",
zap.String("cutoffDate", cutoffDate),
)
allTableNames := rewardsUtils.GetGoldTableNames(cutoffDate)
destTableName := allTableNames[rewardsUtils.Sot_7_StakerOperatorTable]
destTableName := allTableNames[rewardsUtils.Sot_6_StakerOperatorStaging]

sog.logger.Sugar().Infow("Generating 6_stakerOperatorsStaging",
zap.String("destTableName", destTableName),
Expand All @@ -122,7 +125,7 @@ func (sog *StakerOperatorsGenerator) GenerateAndInsert6StakerOperatorStaging(cut
if res.Error != nil {
sog.logger.Sugar().Errorw("Failed to generate 6_stakerOperatorsStaging",
zap.String("cutoffDate", cutoffDate),
zap.Error(err),
zap.Error(res.Error),
)
}

Expand Down
93 changes: 93 additions & 0 deletions pkg/rewards/stakerOperators/7_stakerOperator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package stakerOperators

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

const _7_stakerOperator = `
insert into {{.destTableName}} (
earner,
operator,
reward_type,
avs,
token,
strategy,
multiplier,
shares,
amount,
reward_hash,
snapshot
)
select
earner,
operator,
reward_type,
avs,
token,
strategy,
multiplier,
shares,
amount,
reward_hash,
snapshot
from {{.stakerOperatorStaging}}
on conflict on constraint uniq_staker_operator do nothing;
`

type StakerOperator struct {
Earner string
Operator string
RewardType string
Avs string
Token string
Strategy string
Multiplier string
Shares string
Amount string
RewardHash string
Snapshot time.Time
}

func (sog *StakerOperatorsGenerator) GenerateAndInsert7StakerOperator(cutoffDate string) error {
sog.logger.Sugar().Infow("Generating and inserting 7_stakerOperator",
zap.String("cutoffDate", cutoffDate),
)
allTableNames := rewardsUtils.GetGoldTableNames(cutoffDate)
destTableName := allTableNames[rewardsUtils.Sot_7_StakerOperatorTable]

sog.logger.Sugar().Infow("Generating 7_stakerOperator",
zap.String("destTableName", destTableName),
zap.String("cutoffDate", cutoffDate),
)

query, err := rewardsUtils.RenderQueryTemplate(_7_stakerOperator, map[string]string{
"destTableName": destTableName,
"stakerOperatorStaging": allTableNames[rewardsUtils.Sot_6_StakerOperatorStaging],
})
if err != nil {
sog.logger.Sugar().Errorw("Failed to render 7_stakerOperator query", "error", err)
return err
}

res := sog.db.Debug().Exec(query)
if res.Error != nil {
sog.logger.Sugar().Errorw("Failed to generate 7_stakerOperator",
zap.String("cutoffDate", cutoffDate),
zap.Error(res.Error),
)
}

return nil
}

func (sog *StakerOperatorsGenerator) List7StakerOperator() ([]*StakerOperator, error) {
var rewards []*StakerOperator
res := sog.db.Model(&StakerOperator{}).Find(&rewards)
if res.Error != nil {
sog.logger.Sugar().Errorw("Failed to list 7_stakerOperator", "error", res.Error)
return nil, res.Error
}
return rewards, nil
}
20 changes: 14 additions & 6 deletions pkg/rewards/stakerOperators/stakerOperator.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,47 +34,55 @@ func (sog *StakerOperatorsGenerator) GenerateStakerOperatorsTable(cutoffDate str

sog.logger.Sugar().Infow("Generating staker operators table", zap.String("cutoffDate", cutoffDate))
if err := sog.GenerateAndInsert1StakerStrategyPayouts(cutoffDate); err != nil {
sog.logger.Sugar().Errorf("Failed to generate and insert 1 staker strategy rewards",
sog.logger.Sugar().Errorw("Failed to generate and insert 1 staker strategy rewards",
zap.String("cutoffDate", cutoffDate),
zap.Error(err),
)
return err
}

if err := sog.GenerateAndInsert2OperatorStrategyRewards(cutoffDate); err != nil {
sog.logger.Sugar().Errorf("Failed to generate and insert 2 staker strategy rewards",
sog.logger.Sugar().Errorw("Failed to generate and insert 2 staker strategy rewards",
zap.String("cutoffDate", cutoffDate),
zap.Error(err),
)
return err
}

if err := sog.GenerateAndInsert3RewardsForAllStrategyPayout(cutoffDate); err != nil {
sog.logger.Sugar().Errorf("Failed to generate and insert 3 staker strategy rewards",
sog.logger.Sugar().Errorw("Failed to generate and insert 3 staker strategy rewards",
zap.String("cutoffDate", cutoffDate),
zap.Error(err),
)
return err
}

if err := sog.GenerateAndInsert4RfaeStakerStrategyPayout(cutoffDate); err != nil {
sog.logger.Sugar().Errorf("Failed to generate and insert 4 staker strategy rewards",
sog.logger.Sugar().Errorw("Failed to generate and insert 4 staker strategy rewards",
zap.String("cutoffDate", cutoffDate),
zap.Error(err),
)
return err
}

if err := sog.GenerateAndInsert5RfaeOperatorStrategyPayout(cutoffDate); err != nil {
sog.logger.Sugar().Errorf("Failed to generate and insert 5 staker strategy rewards",
sog.logger.Sugar().Errorw("Failed to generate and insert 5 staker strategy rewards",
zap.String("cutoffDate", cutoffDate),
zap.Error(err),
)
return err
}

if err := sog.GenerateAndInsert6StakerOperatorStaging(cutoffDate); err != nil {
sog.logger.Sugar().Errorf("Failed to generate and insert 6 staker strategy rewards",
sog.logger.Sugar().Errorw("Failed to generate and insert 6 staker strategy rewards",
zap.String("cutoffDate", cutoffDate),
zap.Error(err),
)
return err
}

if err := sog.GenerateAndInsert7StakerOperator(cutoffDate); err != nil {
sog.logger.Sugar().Errorw("Failed to generate and insert 7 staker strategy rewards",
zap.String("cutoffDate", cutoffDate),
zap.Error(err),
)
Expand Down

0 comments on commit f719ac0

Please sign in to comment.