Skip to content

Commit

Permalink
wip rpc handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmcgary committed Jan 21, 2025
1 parent 13cdf2d commit aaa0060
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 15 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.20250115230325-93c4ebccbeb7
github.com/Layr-Labs/protocol-apis v1.1.1-0.20250116155715-919a0a9a27e5
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
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ github.com/Layr-Labs/protocol-apis v1.1.1-0.20250115220323-135176acb92b h1:eJmPA
github.com/Layr-Labs/protocol-apis v1.1.1-0.20250115220323-135176acb92b/go.mod h1:prNA2/mLO5vpMZ2q78Nsn0m97wm28uiRnwO+/yOxigk=
github.com/Layr-Labs/protocol-apis v1.1.1-0.20250115230325-93c4ebccbeb7 h1:zTOIFjJcCzOZ1PBk9jtoW/bsKSyRzvQyTG2Beutpiuk=
github.com/Layr-Labs/protocol-apis v1.1.1-0.20250115230325-93c4ebccbeb7/go.mod h1:prNA2/mLO5vpMZ2q78Nsn0m97wm28uiRnwO+/yOxigk=
github.com/Layr-Labs/protocol-apis v1.1.1-0.20250116155113-c22028af9e00 h1:7N3ta4X5YRygobnCuAAcKr4T76eUboXVcj+IGW0P2eA=
github.com/Layr-Labs/protocol-apis v1.1.1-0.20250116155113-c22028af9e00/go.mod h1:prNA2/mLO5vpMZ2q78Nsn0m97wm28uiRnwO+/yOxigk=
github.com/Layr-Labs/protocol-apis v1.1.1-0.20250116155715-919a0a9a27e5 h1:m/I5hGK4JpOE7OL7wYlKt9HwlCAgQ0SxeT3dNHfjzFY=
github.com/Layr-Labs/protocol-apis v1.1.1-0.20250116155715-919a0a9a27e5/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
25 changes: 23 additions & 2 deletions pkg/rpcServer/protocolHandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package rpcServer

import (
"context"
"errors"
protocolV1 "github.com/Layr-Labs/protocol-apis/gen/protos/eigenlayer/sidecar/v1/protocol"
"github.com/Layr-Labs/sidecar/pkg/service/types"
)

func (rpc *RpcServer) GetRegisteredAvsForOperator(ctx context.Context, request *protocolV1.GetRegisteredAvsForOperatorRequest) (*protocolV1.GetRegisteredAvsForOperatorResponse, error) {
Expand All @@ -21,8 +23,27 @@ func (rpc *RpcServer) GetOperatorDelegatedStakeForStrategy(ctx context.Context,
}

func (rpc *RpcServer) GetDelegatedStakersForOperator(ctx context.Context, request *protocolV1.GetDelegatedStakersForOperatorRequest) (*protocolV1.GetDelegatedStakersForOperatorResponse, error) {
//TODO implement me
panic("implement me")
operator := request.GetOperatorAddress()
if operator == "" {
return nil, errors.New("operator address is required")
}
var pagination *types.Pagination

if p := request.GetPagination(); p != nil {
pagination = &types.Pagination{
Page: p.GetPageNumber(),
PageSize: p.GetPageSize(),
}
}

delegatedStakers, err := rpc.protocolDataService.ListDelegatedStakersForOperator(operator, request.GetBlockHeight(), pagination)
if err != nil {
return nil, err
}

return &protocolV1.GetDelegatedStakersForOperatorResponse{
StakerAddress: delegatedStakers,
}, nil
}

func (rpc *RpcServer) GetStakerShares(ctx context.Context, request *protocolV1.GetStakerSharesRequest) (*protocolV1.GetStakerSharesResponse, error) {
Expand Down
70 changes: 60 additions & 10 deletions pkg/service/protocolDataService/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ func NewProtocolDataService(
}
}

func (pds *ProtocolDataService) getCurrentBlockHeightIfNotPresent(blockHeight uint64) (uint64, error) {
if blockHeight == 0 {
var currentBlock *storage.Block
res := pds.db.Model(&storage.Block{}).Order("number desc").First(&currentBlock)
if res.Error != nil {
return 0, res.Error
}
blockHeight = currentBlock.Number
}
return blockHeight, nil
}

func (pds *ProtocolDataService) ListRegisteredAVSsForOperator(operator string, blockHeight uint64) (interface{}, error) {
return nil, nil
}
Expand All @@ -39,8 +51,50 @@ func (pds *ProtocolDataService) GetOperatorDelegatedStake(operator string, strat
return nil, nil
}

func (pds *ProtocolDataService) ListDelegatedStakersForOperator(operator string, blockHeight uint64, pagination types.Pagination) (interface{}, error) {
return nil, nil
func (pds *ProtocolDataService) ListDelegatedStakersForOperator(operator string, blockHeight uint64, pagination *types.Pagination) ([]string, error) {
bh, err := pds.getCurrentBlockHeightIfNotPresent(blockHeight)
if err != nil {
return nil, err
}

query := `
with staker_operator_delegations as (
SELECT DISTINCT ON (staker)
staker,
operator,
delegated
FROM sidecar_mainnet_ethereum.staker_delegation_changes
WHERE operator = @operator
AND block_number <= @blockHeight
ORDER BY staker, block_number desc, log_index asc
)
SELECT
sod.staker
from staker_operator_delegations as sod
where sod.delegated = true
`

queryParams := []interface{}{
sql.Named("operator", operator),
sql.Named("blockHeight", bh),
}

if pagination != nil {
query += ` LIMIT @limit`
queryParams = append(queryParams, sql.Named("limit", pagination.PageSize))

if pagination.Page > 0 {
query += ` OFFSET @offset`
queryParams = append(queryParams, sql.Named("offset", pagination.Page*pagination.PageSize))
}
}

var stakers []string
res := pds.db.Raw(query, queryParams...).Scan(&stakers)
if res.Error != nil {
return nil, res.Error
}
return stakers, nil
}

type StakerShares struct {
Expand All @@ -60,13 +114,9 @@ type StakerShares struct {
func (pds *ProtocolDataService) ListStakerShares(staker string, blockHeight uint64) ([]*StakerShares, error) {
shares := make([]*StakerShares, 0)

if blockHeight == 0 {
var currentBlock *storage.Block
res := pds.db.Model(&storage.Block{}).Order("number desc").First(&currentBlock)
if res.Error != nil {
return nil, res.Error
}
blockHeight = currentBlock.Number
bh, err := pds.getCurrentBlockHeightIfNotPresent(blockHeight)
if err != nil {
return nil, err
}

query := `
Expand Down Expand Up @@ -116,7 +166,7 @@ func (pds *ProtocolDataService) ListStakerShares(staker string, blockHeight uint
`
res := pds.db.Raw(query,
sql.Named("staker", staker),
sql.Named("blockHeight", blockHeight),
sql.Named("blockHeight", bh),
).Scan(&shares)
if res.Error != nil {
return nil, res.Error
Expand Down
4 changes: 2 additions & 2 deletions pkg/service/types/pagination.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package types

type Pagination struct {
Page int `json:"page"`
PageSize int `json:"page_size"`
Page uint32 `json:"page"`
PageSize uint32 `json:"page_size"`
}

0 comments on commit aaa0060

Please sign in to comment.