-
Notifications
You must be signed in to change notification settings - Fork 5
/
reward.go
71 lines (58 loc) · 2.33 KB
/
reward.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
package rewards
import (
"context"
"errors"
stakingerrors "github.com/coinbase/staking-client-library-go/client/errors"
innerClient "github.com/coinbase/staking-client-library-go/gen/client/coinbase/staking/rewards/v1"
api "github.com/coinbase/staking-client-library-go/gen/go/coinbase/staking/rewards/v1"
"github.com/googleapis/gax-go/v2"
"google.golang.org/api/iterator"
)
// RewardIterator is an interface for iterating through the response to ListRewards.
type RewardIterator interface {
// PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
PageInfo() *iterator.PageInfo
// Next returns the next result. Its second return value is iterator.Done if there are no more
// results. Once Next returns Done, all subsequent calls will return Done.
Next() (*api.Reward, error)
// Response is the raw response for the current page.
// Calling Next() or InternalFetch() updates this value.
Response() *api.ListRewardsResponse
}
// RewardIteratorImpl is an implementation of RewardIterator that unwraps correctly.
type RewardIteratorImpl struct {
iter *innerClient.RewardIterator
}
// PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
func (n *RewardIteratorImpl) PageInfo() *iterator.PageInfo {
return n.iter.PageInfo()
}
// Next returns the next result. Its second return value is iterator.Done if there are no more
// results. Once Next returns Done, all subsequent calls will return Done.
func (n *RewardIteratorImpl) Next() (*api.Reward, error) {
reward, err := n.iter.Next()
if errors.Is(err, iterator.Done) || err == nil {
return reward, err
}
return reward, stakingerrors.FromError(err)
}
// Response is the raw response for the current page.
// Calling Next() or InternalFetch() updates this value.
func (n *RewardIteratorImpl) Response() *api.ListRewardsResponse {
if n.iter.Response == nil {
return nil
}
response, ok := n.iter.Response.(*api.ListRewardsResponse)
if !ok {
return nil
}
return response
}
// ListRewards helps list onchain rewards of an address for a specific protocol, with optional filters for time range, aggregation period, and more.
func (s *Client) ListRewards(
ctx context.Context,
req *api.ListRewardsRequest,
opts ...gax.CallOption,
) RewardIterator {
return &RewardIteratorImpl{iter: s.client.ListRewards(ctx, req, opts...)}
}