Skip to content

Commit 8448fae

Browse files
authored
feat: add an optional snapshotDate query param to GetSummarizedRewardsForEarner (#399)
## Description add an optional snapshotDate query param to GetSummarizedRewardsForEarner Fixes #396 ## Type of change - [x] New feature (non-breaking change which adds functionality) - [x] Docs (documentation updates) ## How Has This Been Tested? Added a unit test for `GetBlockHeightForSnapshotDate` ## Checklist: - [x] My code follows the style guidelines of this project - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas - [x] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [x] I have added tests that prove my fix is effective or that my feature works - [x] New and existing unit tests pass locally with my changes - [x] Any dependent changes have been merged and published in downstream modules - [x] I have checked my code and corrected any misspellings
2 parents 6ed106a + 32ca091 commit 8448fae

File tree

11 files changed

+97
-5
lines changed

11 files changed

+97
-5
lines changed

docs/docs/api/rewards-get-summarized-rewards-for-earner.api.mdx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ GetSummarizedRewardsForEarner returns the summarized rewards for the given earne
5151
</Heading>
5252

5353
<ParamsDetails
54-
parameters={[{"name":"earner_address","in":"path","required":true,"schema":{"type":"string"}}]}
54+
parameters={[
55+
{"name":"earner_address","in":"path","required":true,"schema":{"type":"string"}},
56+
{"name":"block_height","in":"query","required":false,"schema":{"type":"integer"},"description":"Block height to use for calculating rewards. If not provided, the latest block is used."},
57+
{"name":"snapshot_date","in":"query","required":false,"schema":{"type":"string"},"description":"Snapshot date in YYYY-MM-DD format. If provided, the minimum block height for that date will be used instead of block_height."}
58+
]}
5559
>
5660

5761
</ParamsDetails>

docs/docs/public-api/rewards-get-summarized-rewards-for-earner.api.mdx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ GetSummarizedRewardsForEarner returns the summarized rewards for the given earne
5151
</Heading>
5252

5353
<ParamsDetails
54-
parameters={[{"name":"earner_address","in":"path","required":true,"schema":{"type":"string"}}]}
54+
parameters={[
55+
{"name":"earner_address","in":"path","required":true,"schema":{"type":"string"}},
56+
{"name":"block_height","in":"query","required":false,"schema":{"type":"integer"},"description":"Block height to use for calculating rewards. If not provided, the latest block is used."},
57+
{"name":"snapshot_date","in":"query","required":false,"schema":{"type":"string"},"description":"Snapshot date in YYYY-MM-DD format. If provided, the minimum block height for that date will be used instead of block_height."}
58+
]}
5559
>
5660

5761
</ParamsDetails>

docs/docs/public-api/schemas/getsummarizedrewardsforearnerrequest.schema.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ sidebar_label: "GetSummarizedRewardsForEarnerRequest"
66
hide_title: true
77
hide_table_of_contents: true
88
schema: true
9-
sample: {"blockHeight":0,"earnerAddress":"string"}
9+
sample: {"blockHeight":0,"earnerAddress":"string","snapshotDate":"2023-01-16"}
1010
custom_edit_url: null
1111
---
1212

@@ -25,7 +25,7 @@ import Heading from "@theme/Heading";
2525

2626

2727
<Schema
28-
schema={{"type":"object","properties":{"blockHeight":{"type":"integer","format":"uint64"},"earnerAddress":{"type":"string"}},"required":["block_height"],"title":"GetSummarizedRewardsForEarnerRequest"}}
28+
schema={{"type":"object","properties":{"blockHeight":{"type":"integer","format":"uint64","description":"Block height to use for calculating rewards. If not provided, the latest block is used."},"earnerAddress":{"type":"string"},"snapshotDate":{"type":"string","description":"Snapshot date in YYYY-MM-DD format. If provided, the minimum block height for that date will be used instead of block_height."}},"required":["block_height"],"title":"GetSummarizedRewardsForEarnerRequest"}}
2929
schemaType={"response"}
3030
>
3131

docs/openapi/api.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,6 +1231,25 @@
12311231
"schema": {
12321232
"type": "string"
12331233
}
1234+
},
1235+
{
1236+
"name": "block_height",
1237+
"in": "query",
1238+
"required": false,
1239+
"schema": {
1240+
"type": "integer",
1241+
"format": "uint64"
1242+
},
1243+
"description": "Block height to use for calculating rewards. If not provided, the latest block is used."
1244+
},
1245+
{
1246+
"name": "snapshot_date",
1247+
"in": "query",
1248+
"required": false,
1249+
"schema": {
1250+
"type": "string"
1251+
},
1252+
"description": "Snapshot date in YYYY-MM-DD format. If provided, the minimum block height for that date will be used instead of block_height."
12341253
}
12351254
],
12361255
"responses": {

docs/openapi/api.public.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,25 @@
108108
"schema": {
109109
"type": "string"
110110
}
111+
},
112+
{
113+
"name": "block_height",
114+
"in": "query",
115+
"required": false,
116+
"schema": {
117+
"type": "integer",
118+
"format": "uint64"
119+
},
120+
"description": "Block height to use for calculating rewards. If not provided, the latest block is used."
121+
},
122+
{
123+
"name": "snapshot_date",
124+
"in": "query",
125+
"required": false,
126+
"schema": {
127+
"type": "string"
128+
},
129+
"description": "Snapshot date in YYYY-MM-DD format. If provided, the minimum block height for that date will be used instead of block_height."
111130
}
112131
],
113132
"responses": {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/DataDog/datadog-go/v5 v5.6.0
77
github.com/Layr-Labs/eigenlayer-contracts v0.4.1-holesky-pepe.0.20240813143901-00fc4b95e9c1
88
github.com/Layr-Labs/eigenlayer-rewards-proofs v0.2.13
9-
github.com/Layr-Labs/protocol-apis v1.13.2
9+
github.com/Layr-Labs/protocol-apis v1.14.1-0.20250523180146-9ebe4230a014
1010
github.com/ProtonMail/go-crypto v1.1.6
1111
github.com/agiledragon/gomonkey/v2 v2.13.0
1212
github.com/akuity/grpc-gateway-client v0.0.0-20240912082144-55a48e8b4b89

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ github.com/Layr-Labs/protocol-apis v1.13.1 h1:fnknM3bITUZJovTVGdNgjS9NJeZyFoxu39
1616
github.com/Layr-Labs/protocol-apis v1.13.1/go.mod h1:tyzQDWHu4/dmBSRKNRXi65wLic3j5B+7YQ8lMQB08aM=
1717
github.com/Layr-Labs/protocol-apis v1.13.2 h1:WKzw8B0wObbZ9TBb+SuzXsgz+IXIRO2LvNUEkTvtZ0c=
1818
github.com/Layr-Labs/protocol-apis v1.13.2/go.mod h1:tyzQDWHu4/dmBSRKNRXi65wLic3j5B+7YQ8lMQB08aM=
19+
github.com/Layr-Labs/protocol-apis v1.14.1-0.20250523180146-9ebe4230a014 h1:Fobe61GmMwq5LsGv4XGlNle28J1wM+kR6JSu8tEiAAk=
20+
github.com/Layr-Labs/protocol-apis v1.14.1-0.20250523180146-9ebe4230a014/go.mod h1:tyzQDWHu4/dmBSRKNRXi65wLic3j5B+7YQ8lMQB08aM=
1921
github.com/Microsoft/go-winio v0.5.0/go.mod h1:JPGBdM1cNvN/6ISo+n8V5iA4v8pBzdOpzfwIujj1a84=
2022
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
2123
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=

pkg/rpcServer/rewardsHandlers.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,11 +360,20 @@ func withDefaultValue(value string, defaultValue string) string {
360360
func (rpc *RpcServer) GetSummarizedRewardsForEarner(ctx context.Context, req *rewardsV1.GetSummarizedRewardsForEarnerRequest) (*rewardsV1.GetSummarizedRewardsForEarnerResponse, error) {
361361
earner := req.GetEarnerAddress()
362362
blockHeight := req.GetBlockHeight()
363+
snapshotDate := req.GetSnapshotDate()
363364

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

369+
if snapshotDate != "" {
370+
var err error
371+
blockHeight, err = rpc.rewardsDataService.GetBlockHeightForSnapshotDate(ctx, snapshotDate)
372+
if err != nil {
373+
return nil, status.Error(codes.Internal, err.Error())
374+
}
375+
}
376+
368377
summarizedRewards, err := rpc.rewardsDataService.GetSummarizedRewards(ctx, earner, nil, blockHeight)
369378
if err != nil {
370379
return nil, status.Error(codes.Internal, err.Error())

pkg/service/baseDataService/base.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
78
"github.com/Layr-Labs/sidecar/pkg/eigenState/stateManager"
89
"github.com/Layr-Labs/sidecar/pkg/storage"
910
"gorm.io/gorm"
@@ -49,3 +50,16 @@ func (b *BaseDataService) GetLatestConfirmedBlock(ctx context.Context) (*storage
4950

5051
return b.GetBlock(ctx, stateRoot.EthBlockNumber)
5152
}
53+
54+
func (b *BaseDataService) GetBlockHeightForSnapshotDate(ctx context.Context, snapshotDate string) (uint64, error) {
55+
var block storage.Block
56+
res := b.DB.Model(&storage.Block{}).
57+
Where("to_char(block_time, 'YYYY-MM-DD') = ?", snapshotDate).
58+
Order("number ASC").
59+
First(&block)
60+
61+
if res.Error != nil {
62+
return 0, res.Error
63+
}
64+
return block.Number, nil
65+
}

pkg/service/rewardsDataService/rewards.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,14 @@ func setTokenValueInMap(tokenMap map[string]*SummarizedReward, values []*RewardA
425425
}
426426
}
427427

428+
func (rds *RewardsDataService) GetBlockHeightForSnapshotDate(ctx context.Context, snapshotDate string) (uint64, error) {
429+
blockHeight, err := rds.BaseDataService.GetBlockHeightForSnapshotDate(ctx, snapshotDate)
430+
if err != nil {
431+
return 0, err
432+
}
433+
return blockHeight, nil
434+
}
435+
428436
// GetSummarizedRewards returns the summarized rewards for a given earner at a given block height.
429437
// The blockHeight will be used to find the root that is <= the provided blockHeight
430438
func (rds *RewardsDataService) GetSummarizedRewards(ctx context.Context, earner string, tokens []string, blockHeight uint64) ([]*SummarizedReward, error) {

0 commit comments

Comments
 (0)