Skip to content

Commit

Permalink
feat: GetTotalClaimedRewards rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmcgary committed Jan 22, 2025
1 parent 349415f commit 1652f6c
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 8 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 v1.1.1-0.20250121193118-8112817d1079
github.com/Layr-Labs/protocol-apis v1.1.1-0.20250122185055-8ce2cc7afa86
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
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ github.com/Layr-Labs/protocol-apis v1.1.1-0.20250116155715-919a0a9a27e5 h1:m/I5h
github.com/Layr-Labs/protocol-apis v1.1.1-0.20250116155715-919a0a9a27e5/go.mod h1:prNA2/mLO5vpMZ2q78Nsn0m97wm28uiRnwO+/yOxigk=
github.com/Layr-Labs/protocol-apis v1.1.1-0.20250121193118-8112817d1079 h1:B1b9ghilo70y4MJ7ZkF5w8BuPtslAUP2oE6vfzG6DBA=
github.com/Layr-Labs/protocol-apis v1.1.1-0.20250121193118-8112817d1079/go.mod h1:prNA2/mLO5vpMZ2q78Nsn0m97wm28uiRnwO+/yOxigk=
github.com/Layr-Labs/protocol-apis v1.1.1-0.20250122185055-8ce2cc7afa86 h1:XfVh8deohE3LYXAjYOAjzD4wRPNgbIP3OjV6Syy0FIw=
github.com/Layr-Labs/protocol-apis v1.1.1-0.20250122185055-8ce2cc7afa86/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
23 changes: 22 additions & 1 deletion pkg/rpcServer/rewardsHandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
rewardsV1 "github.com/Layr-Labs/protocol-apis/gen/protos/eigenlayer/sidecar/v1/rewards"
"github.com/Layr-Labs/sidecar/pkg/rewards"
"github.com/Layr-Labs/sidecar/pkg/rewardsCalculatorQueue"
"github.com/Layr-Labs/sidecar/pkg/service/rewardsDataService"
"github.com/Layr-Labs/sidecar/pkg/utils"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -207,8 +208,28 @@ func (rpc *RpcServer) GetAvailableRewards(ctx context.Context, req *rewardsV1.Ge
return nil, status.Error(codes.Unimplemented, "method GetAvailableRewards not implemented")
}

// GetTotalClaimedRewards returns the total claimed rewards for an earner up to, and including, the provided block height.
func (rpc *RpcServer) GetTotalClaimedRewards(ctx context.Context, req *rewardsV1.GetTotalClaimedRewardsRequest) (*rewardsV1.GetTotalClaimedRewardsResponse, error) {
return nil, status.Error(codes.Unimplemented, "method GetTotalClaimedRewards not implemented")
earner := req.GetEarnerAddress()
blockHeight := req.GetBlockHeight()

if earner == "" {
return nil, status.Error(codes.InvalidArgument, "earner address is required")
}

totalClaimed, err := rpc.rewardsDataService.GetTotalClaimedRewards(ctx, earner, blockHeight)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &rewardsV1.GetTotalClaimedRewardsResponse{
Rewards: utils.Map(totalClaimed, func(r *rewardsDataService.TotalClaimedReward, i uint64) *rewardsV1.TotalClaimedReward {
return &rewardsV1.TotalClaimedReward{
Earner: r.Earner,
Token: r.Token,
Amount: r.Amount,
}
}),
}, nil
}

func (rpc *RpcServer) GetAvailableRewardsTokens(ctx context.Context, req *rewardsV1.GetAvailableRewardsTokensRequest) (*rewardsV1.GetAvailableRewardsTokensResponse, error) {
Expand Down
3 changes: 2 additions & 1 deletion pkg/service/baseDataService/base.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package baseDataService

import (
"context"
"github.com/Layr-Labs/sidecar/pkg/storage"
"gorm.io/gorm"
)
Expand All @@ -9,7 +10,7 @@ type BaseDataService struct {
DB *gorm.DB
}

func (b *BaseDataService) GetCurrentBlockHeightIfNotPresent(blockHeight uint64) (uint64, error) {
func (b *BaseDataService) GetCurrentBlockHeightIfNotPresent(ctx context.Context, blockHeight uint64) (uint64, error) {
if blockHeight == 0 {
var currentBlock *storage.Block
res := b.DB.Model(&storage.Block{}).Order("number desc").First(&currentBlock)
Expand Down
10 changes: 5 additions & 5 deletions pkg/service/protocolDataService/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func NewProtocolDataService(
func (pds *ProtocolDataService) ListRegisteredAVSsForOperator(ctx context.Context, operator string, blockHeight uint64) ([]string, error) {
operator = strings.ToLower(operator)

blockHeight, err := pds.BaseDataService.GetCurrentBlockHeightIfNotPresent(blockHeight)
blockHeight, err := pds.BaseDataService.GetCurrentBlockHeightIfNotPresent(ctx, blockHeight)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -75,7 +75,7 @@ func (pds *ProtocolDataService) ListRegisteredAVSsForOperator(ctx context.Contex

func (pds *ProtocolDataService) ListDelegatedStrategiesForOperator(ctx context.Context, operator string, blockHeight uint64) ([]string, error) {
operator = strings.ToLower(operator)
blockHeight, err := pds.BaseDataService.GetCurrentBlockHeightIfNotPresent(blockHeight)
blockHeight, err := pds.BaseDataService.GetCurrentBlockHeightIfNotPresent(ctx, blockHeight)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -217,7 +217,7 @@ type ResultCollector[T any] struct {
}

func (pds *ProtocolDataService) GetOperatorDelegatedStake(ctx context.Context, operator string, strategy string, blockHeight uint64) (*OperatorDelegatedStake, error) {
blockHeight, err := pds.BaseDataService.GetCurrentBlockHeightIfNotPresent(blockHeight)
blockHeight, err := pds.BaseDataService.GetCurrentBlockHeightIfNotPresent(ctx, blockHeight)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -286,7 +286,7 @@ func (pds *ProtocolDataService) GetOperatorDelegatedStake(ctx context.Context, o
}

func (pds *ProtocolDataService) ListDelegatedStakersForOperator(ctx context.Context, operator string, blockHeight uint64, pagination *types.Pagination) ([]string, error) {
bh, err := pds.BaseDataService.GetCurrentBlockHeightIfNotPresent(blockHeight)
bh, err := pds.BaseDataService.GetCurrentBlockHeightIfNotPresent(ctx, blockHeight)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -348,7 +348,7 @@ type StakerShares struct {
func (pds *ProtocolDataService) ListStakerShares(ctx context.Context, staker string, blockHeight uint64) ([]*StakerShares, error) {
shares := make([]*StakerShares, 0)

bh, err := pds.BaseDataService.GetCurrentBlockHeightIfNotPresent(blockHeight)
bh, err := pds.BaseDataService.GetCurrentBlockHeightIfNotPresent(ctx, blockHeight)
if err != nil {
return nil, err
}
Expand Down
37 changes: 37 additions & 0 deletions pkg/service/rewardsDataService/rewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package rewardsDataService

import (
"context"
"database/sql"
"github.com/Layr-Labs/sidecar/internal/config"
"github.com/Layr-Labs/sidecar/pkg/rewards"
"github.com/Layr-Labs/sidecar/pkg/rewards/rewardsTypes"
Expand Down Expand Up @@ -38,3 +39,39 @@ func NewRewardsDataService(
func (rds *RewardsDataService) GetRewardsForSnapshot(ctx context.Context, snapshot string) ([]*rewardsTypes.Reward, error) {
return rds.rewardsCalculator.FetchRewardsForSnapshot(snapshot)
}

type TotalClaimedReward struct {
Earner string
Token string
Amount string
}

func (rds *RewardsDataService) GetTotalClaimedRewards(ctx context.Context, earner string, blockHeight uint64) ([]*TotalClaimedReward, error) {
blockHeight, err := rds.BaseDataService.GetCurrentBlockHeightIfNotPresent(ctx, blockHeight)
if err != nil {
return nil, err
}

query := `
select
earner,
token,
sum(claimed_amount) as amount
from erwards_claimed as rc
where
earner = @earner
and block_number <= @blockHeight
group by 1, 2
`

claimedAmounts := make([]*TotalClaimedReward, 0)
res := rds.db.Raw(query,
sql.Named("earner", earner),
sql.Named("block_height", blockHeight),
).Scan(&claimedAmounts)

if res.Error != nil {
return nil, res.Error
}
return claimedAmounts, nil
}

0 comments on commit 1652f6c

Please sign in to comment.