-
Notifications
You must be signed in to change notification settings - Fork 237
/
source.go
92 lines (76 loc) · 2.4 KB
/
source.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package remote
import (
"fmt"
"github.com/cosmos/cosmos-sdk/types/query"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/forbole/juno/v5/node/remote"
stakingsource "github.com/forbole/bdjuno/v4/modules/staking/source"
)
var (
_ stakingsource.Source = &Source{}
)
// Source implements stakingsource.Source using a remote node
type Source struct {
*remote.Source
stakingClient stakingtypes.QueryClient
}
// NewSource returns a new Source instance
func NewSource(source *remote.Source, stakingClient stakingtypes.QueryClient) *Source {
return &Source{
Source: source,
stakingClient: stakingClient,
}
}
// GetValidator implements stakingsource.Source
func (s Source) GetValidator(height int64, valOper string) (stakingtypes.Validator, error) {
res, err := s.stakingClient.Validator(
remote.GetHeightRequestContext(s.Ctx, height),
&stakingtypes.QueryValidatorRequest{ValidatorAddr: valOper},
)
if err != nil {
return stakingtypes.Validator{}, fmt.Errorf("error while getting validator: %s", err)
}
return res.Validator, nil
}
// GetValidatorsWithStatus implements stakingsource.Source
func (s Source) GetValidatorsWithStatus(height int64, status string) ([]stakingtypes.Validator, error) {
ctx := remote.GetHeightRequestContext(s.Ctx, height)
var validators []stakingtypes.Validator
var nextKey []byte
var stop = false
for !stop {
res, err := s.stakingClient.Validators(
ctx,
&stakingtypes.QueryValidatorsRequest{
Status: status,
Pagination: &query.PageRequest{
Key: nextKey,
Limit: 100, // Query 100 validators at time
},
},
)
if err != nil {
return nil, err
}
nextKey = res.Pagination.NextKey
stop = len(res.Pagination.NextKey) == 0
validators = append(validators, res.Validators...)
}
return validators, nil
}
// GetPool implements stakingsource.Source
func (s Source) GetPool(height int64) (stakingtypes.Pool, error) {
res, err := s.stakingClient.Pool(remote.GetHeightRequestContext(s.Ctx, height), &stakingtypes.QueryPoolRequest{})
if err != nil {
return stakingtypes.Pool{}, err
}
return res.Pool, nil
}
// GetParams implements stakingsource.Source
func (s Source) GetParams(height int64) (stakingtypes.Params, error) {
res, err := s.stakingClient.Params(remote.GetHeightRequestContext(s.Ctx, height), &stakingtypes.QueryParamsRequest{})
if err != nil {
return stakingtypes.Params{}, err
}
return res.Params, nil
}