Skip to content

Commit

Permalink
Implement rewards APIs (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmcgary authored Nov 25, 2024
2 parents bf33d3d + 4d4b67c commit ee186f9
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 25 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/DataDog/datadog-go/v5 v5.5.0
github.com/Layr-Labs/eigenlayer-contracts v0.4.1-holesky-pepe.0.20240813143901-00fc4b95e9c1
github.com/Layr-Labs/eigenlayer-rewards-proofs v0.2.13
github.com/Layr-Labs/protocol-apis v0.1.0-beta.1
github.com/Layr-Labs/protocol-apis v0.1.0-beta.3.0.20241122223729-1734c60ac737
github.com/ethereum/go-ethereum v1.14.9
github.com/gocarina/gocsv v0.0.0-20240520201108-78e41c74b4b1
github.com/google/uuid v1.6.0
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ github.com/Layr-Labs/eigenlayer-contracts v0.4.1-holesky-pepe.0.20240813143901-0
github.com/Layr-Labs/eigenlayer-contracts v0.4.1-holesky-pepe.0.20240813143901-00fc4b95e9c1/go.mod h1:Ie8YE3EQkTHqG6/tnUS0He7/UPMkXPo/3OFXwSy0iRo=
github.com/Layr-Labs/eigenlayer-rewards-proofs v0.2.13 h1:Blb4AE+jC/vddV71w4/MQAPooM+8EVqv9w2bL4OytgY=
github.com/Layr-Labs/eigenlayer-rewards-proofs v0.2.13/go.mod h1:PD/HoyzZjxDw1tAcZw3yD0yGddo+yhmwQAi+lk298r4=
github.com/Layr-Labs/protocol-apis v0.1.0-beta.1 h1:zwtF2JSwN+vQQcrHFl9WGvWG7A4n2t7GVpzoCPJCpFQ=
github.com/Layr-Labs/protocol-apis v0.1.0-beta.1/go.mod h1:prNA2/mLO5vpMZ2q78Nsn0m97wm28uiRnwO+/yOxigk=
github.com/Layr-Labs/protocol-apis v0.1.0-beta.2 h1:AC79PMIDrhYtZV5HAoaDmvbiCD3ibAMZv+1XFnghYqI=
github.com/Layr-Labs/protocol-apis v0.1.0-beta.2/go.mod h1:prNA2/mLO5vpMZ2q78Nsn0m97wm28uiRnwO+/yOxigk=
github.com/Layr-Labs/protocol-apis v0.1.0-beta.3.0.20241122223729-1734c60ac737 h1:I/0YAw2ue150YuLNavErIQ4t7yoTDuH3nqZkOS7RTjg=
github.com/Layr-Labs/protocol-apis v0.1.0-beta.3.0.20241122223729-1734c60ac737/go.mod h1:prNA2/mLO5vpMZ2q78Nsn0m97wm28uiRnwO+/yOxigk=
github.com/Microsoft/go-winio v0.5.0/go.mod h1:JPGBdM1cNvN/6ISo+n8V5iA4v8pBzdOpzfwIujj1a84=
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
Expand Down
13 changes: 13 additions & 0 deletions pkg/rewards/rewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,19 @@ func (rc *RewardsCalculator) MerkelizeRewardsForSnapshot(snapshotDate string) (*
return accountTree, tokenTree, err
}

func (rc *RewardsCalculator) GetMaxSnapshotDateForCutoffDate(cutoffDate string) (string, error) {
goldStagingTableName := getGoldTableNames(cutoffDate)[Table_7_GoldStaging]

var maxSnapshotStr string
query := fmt.Sprintf(`select to_char(max(snapshot), 'YYYY-MM-DD') as snapshot from %s`, goldStagingTableName)
res := rc.grm.Raw(query).Scan(&maxSnapshotStr)
if res.Error != nil {
rc.logger.Sugar().Errorw("Failed to get max snapshot date", "error", res.Error)
return "", res.Error
}
return maxSnapshotStr, nil
}

type Reward struct {
Earner string
Token string
Expand Down
16 changes: 0 additions & 16 deletions pkg/rpcServer/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,3 @@ func (rpc *RpcServer) GetStateRoot(ctx context.Context, req *sidecarV1.GetStateR
StateRoot: stateRoot.StateRoot,
}, nil
}

func (rpc *RpcServer) GetRewardsRoot(ctx context.Context, req *sidecarV1.GetRewardsRootRequest) (*sidecarV1.GetRewardsRootResponse, error) {
return nil, status.Error(codes.Unimplemented, "method GetRewardsRoot not implemented")
}

func (rpc *RpcServer) GenerateClaimProof(ctx context.Context, req *sidecarV1.GenerateClaimProofRequest) (*sidecarV1.GenerateClaimProofResponse, error) {
return nil, status.Error(codes.Unimplemented, "method GenerateClaimProof not implemented")
}

func (rpc *RpcServer) GetAvailableRewards(ctx context.Context, req *sidecarV1.GetAvailableRewardsRequest) (*sidecarV1.GetAvailableRewardsResponse, error) {
return nil, status.Error(codes.Unimplemented, "method GetAvailableRewards not implemented")
}

func (rpc *RpcServer) GetAvailableRewardsTokens(ctx context.Context, req *sidecarV1.GetAvailableRewardsTokensRequest) (*sidecarV1.GetAvailableRewardsTokensResponse, error) {
return nil, status.Error(codes.Unimplemented, "method GetAvailableRewardsTokens not implemented")
}
98 changes: 93 additions & 5 deletions pkg/rpcServer/rewardsHandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,113 @@ package rpcServer
import (
"context"
sidecarV1 "github.com/Layr-Labs/protocol-apis/gen/protos/eigenlayer/sidecar/v1"
"github.com/Layr-Labs/sidecar/pkg/utils"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

func (rpc *RpcServer) GetRewardsRoot(ctx context.Context, req *sidecarV1.GetRewardsRootRequest) (*sidecarV1.GetRewardsRootResponse, error) {
return nil, status.Error(codes.Unimplemented, "method GetRewardsRoot not implemented")
}

func (rpc *RpcServer) GenerateRewards(ctx context.Context, req *sidecarV1.GenerateRewardsRequest) (*sidecarV1.GenerateRewardsResponse, error) {
snapshotDate := req.GetSnapshot()
cutoffDate := req.GetCutoffDate()

if snapshotDate != "" {
if err := rpc.rewardsCalculator.CalculateRewardsForSnapshotDate(snapshotDate); err != nil {
if cutoffDate != "" {
if err := rpc.rewardsCalculator.CalculateRewardsForSnapshotDate(cutoffDate); err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
} else {
sd, err := rpc.rewardsCalculator.CalculateRewardsForLatestSnapshot()
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
snapshotDate = sd
cutoffDate = sd
}
return &sidecarV1.GenerateRewardsResponse{
Snapshot: snapshotDate,
CutoffDate: cutoffDate,
}, nil
}

func (rpc *RpcServer) GenerateRewardsRoot(ctx context.Context, req *sidecarV1.GenerateRewardsRootRequest) (*sidecarV1.GenerateRewardsRootResponse, error) {
cutoffDate := req.GetCutoffDate()
if cutoffDate == "" {
return nil, status.Error(codes.InvalidArgument, "snapshot date is required")
}

err := rpc.rewardsCalculator.CalculateRewardsForSnapshotDate(cutoffDate)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

rewardsCalcEndDate, err := rpc.rewardsCalculator.GetMaxSnapshotDateForCutoffDate(cutoffDate)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
if rewardsCalcEndDate == "" {
return nil, status.Error(codes.NotFound, "no rewards calculated for the given snapshot date")
}
rpc.Logger.Sugar().Infow("Rewards calculated for snapshot date",
zap.String("cutoffDate", cutoffDate),
zap.String("rewardsCalcEndDate", rewardsCalcEndDate),
)

accountTree, _, err := rpc.rewardsCalculator.MerkelizeRewardsForSnapshot(rewardsCalcEndDate)
if err != nil {
rpc.Logger.Sugar().Errorw("failed to merkelize rewards for snapshot",
zap.Error(err),
zap.String("cutOffDate", cutoffDate),
zap.String("rewardsCalcEndDate", rewardsCalcEndDate),
)
return nil, status.Error(codes.Internal, err.Error())
}

rootString := utils.ConvertBytesToString(accountTree.Root())
rpc.Logger.Sugar().Infow("Rewards root generated",
zap.String("root", rootString),
zap.String("rewardsCalcEndDate", rewardsCalcEndDate),
zap.String("cutoffDate", cutoffDate),
)

return &sidecarV1.GenerateRewardsRootResponse{
RewardsRoot: rootString,
RewardsCalcEndDate: rewardsCalcEndDate,
}, nil
}

func (rpc *RpcServer) GetRewardsForSnapshot(ctx context.Context, req *sidecarV1.GetRewardsForSnapshotRequest) (*sidecarV1.GetRewardsForSnapshotResponse, error) {
return nil, status.Error(codes.Unimplemented, "method GetRewardsForSnapshot not implemented")
}

func (rpc *RpcServer) GetAttributableRewardsForSnapshot(ctx context.Context, req *sidecarV1.GetAttributableRewardsForSnapshotRequest) (*sidecarV1.GetAttributableRewardsForSnapshotResponse, error) {
return nil, status.Error(codes.Unimplemented, "method GetAttributableRewardsForSnapshot not implemented")
}

func (rpc *RpcServer) GetAttributableRewardsForDistributionRoot(ctx context.Context, req *sidecarV1.GetAttributableRewardsForDistributionRootRequest) (*sidecarV1.GetAttributableRewardsForDistributionRootResponse, error) {
return nil, status.Error(codes.Unimplemented, "method GetAttributableRewardsForDistributionRoot not implemented")
}

func (rpc *RpcServer) GenerateClaimProof(ctx context.Context, req *sidecarV1.GenerateClaimProofRequest) (*sidecarV1.GenerateClaimProofResponse, error) {
return nil, status.Error(codes.Unimplemented, "method GenerateClaimProof not implemented")
}

func (rpc *RpcServer) GetAvailableRewards(ctx context.Context, req *sidecarV1.GetAvailableRewardsRequest) (*sidecarV1.GetAvailableRewardsResponse, error) {
return nil, status.Error(codes.Unimplemented, "method GetAvailableRewards not implemented")
}

func (rpc *RpcServer) GetTotalClaimedRewards(ctx context.Context, req *sidecarV1.GetTotalClaimedRewardsRequest) (*sidecarV1.GetTotalClaimedRewardsResponse, error) {
return nil, status.Error(codes.Unimplemented, "method GetTotalClaimedRewards not implemented")
}

func (rpc *RpcServer) GetAvailableRewardsTokens(ctx context.Context, req *sidecarV1.GetAvailableRewardsTokensRequest) (*sidecarV1.GetAvailableRewardsTokensResponse, error) {
return nil, status.Error(codes.Unimplemented, "method GetAvailableRewardsTokens not implemented")
}

func (rpc *RpcServer) GetSummarizedRewardsForEarner(ctx context.Context, req *sidecarV1.GetSummarizedRewardsForEarnerRequest) (*sidecarV1.GetSummarizedRewardsForEarnerResponse, error) {
return nil, status.Error(codes.Unimplemented, "method GetSummarizedRewardsForEarner not implemented")
}

func (rpc *RpcServer) GetClaimedRewardsByBlock(ctx context.Context, req *sidecarV1.GetClaimedRewardsByBlockRequest) (*sidecarV1.GetClaimedRewardsByBlockResponse, error) {
return nil, status.Error(codes.Unimplemented, "method GetClaimedRewardsByBlock not implemented")
}
1 change: 0 additions & 1 deletion pkg/rpcServer/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (

type RpcServer struct {
v1.UnimplementedRpcServer
v1.UnimplementedRewardsServer
Logger *zap.Logger
blockStore storage.BlockStore
stateManager *stateManager.EigenStateManager
Expand Down

0 comments on commit ee186f9

Please sign in to comment.