-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
73 lines (60 loc) · 2.13 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
* This example code demonstrates rewards client library usage for listing rewards
*/
package main
import (
"context"
"errors"
"fmt"
"log"
"time"
"github.com/coinbase/staking-client-library-go/auth"
"github.com/coinbase/staking-client-library-go/client"
"github.com/coinbase/staking-client-library-go/client/options"
"github.com/coinbase/staking-client-library-go/client/rewards"
filter "github.com/coinbase/staking-client-library-go/client/rewards/rewardsfilter"
api "github.com/coinbase/staking-client-library-go/gen/go/coinbase/staking/rewards/v1"
"google.golang.org/api/iterator"
"google.golang.org/protobuf/encoding/protojson"
)
const (
apiKeyName = "your-api-key-name"
apiPrivateKey = "your-api-private-key"
partialETHAddress = "0x60c7e246344ae3856cf9abe3a2e258d495fc39e0"
)
func main() {
ctx := context.Background()
// Loads the API key.
apiKey, err := auth.NewAPIKey(auth.WithAPIKeyName(apiKeyName, apiPrivateKey))
if err != nil {
log.Fatalf("error loading API key: %s", err.Error())
}
// Creates the Coinbase Staking API client
stakingClient, err := client.New(ctx, options.WithAPIKey(apiKey))
if err != nil {
log.Fatalf("error instantiating staking client: %s", err.Error())
}
// Lists the rewards for the given partial eth address between May 1st, 2024 and May 3rd, 2024 aggregated by day.
partialETHRewardsIter := stakingClient.Rewards.ListRewards(ctx, &api.ListRewardsRequest{
Parent: rewards.Ethereum,
PageSize: 200,
Filter: filter.WithAddress().Eq(partialETHAddress).
And(filter.WithPeriodEndTime().Gte(time.Date(2024, 5, 1, 0, 0, 0, 0, time.Local))).
And(filter.WithPeriodEndTime().Lt(time.Date(2024, 5, 3, 0, 0, 0, 0, time.Local))).String(),
})
// Iterate through the partial eth rewards and pretty print them.
for {
reward, err := partialETHRewardsIter.Next()
if errors.Is(err, iterator.Done) {
break
}
if err != nil {
log.Fatalf("error listing rewards: %s", err.Error())
}
marshaled, err := protojson.MarshalOptions{Indent: " ", Multiline: true}.Marshal(reward)
if err != nil {
log.Fatalf("error marshaling reward: %s", err.Error())
}
fmt.Println(string(marshaled))
}
}