forked from forbole/callisto
-
Notifications
You must be signed in to change notification settings - Fork 1
/
source.go
173 lines (147 loc) · 4.48 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package remote
import (
"fmt"
"github.com/cosmos/cosmos-sdk/types/query"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/pylons-tech/juno/node/remote"
stakingsource "github.com/pylons-tech/bdjuno/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(
s.Ctx,
&stakingtypes.QueryValidatorRequest{ValidatorAddr: valOper},
remote.GetHeightRequestHeader(height),
)
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) {
header := remote.GetHeightRequestHeader(height)
var validators []stakingtypes.Validator
var nextKey []byte
var stop = false
for !stop {
res, err := s.stakingClient.Validators(
s.Ctx,
&stakingtypes.QueryValidatorsRequest{
Status: status,
Pagination: &query.PageRequest{
Key: nextKey,
Limit: 100, // Query 100 validators at time
},
},
header,
)
if err != nil {
return nil, err
}
nextKey = res.Pagination.NextKey
stop = len(res.Pagination.NextKey) == 0
validators = append(validators, res.Validators...)
}
return validators, nil
}
// GetDelegation implements stakingsource.Source
func (s Source) GetDelegation(height int64, delegator string, valOperAddr string) (stakingtypes.DelegationResponse, error) {
res, err := s.stakingClient.Delegation(
s.Ctx,
&stakingtypes.QueryDelegationRequest{
ValidatorAddr: valOperAddr,
DelegatorAddr: delegator,
},
remote.GetHeightRequestHeader(height),
)
if err != nil {
return stakingtypes.DelegationResponse{}, err
}
return *res.DelegationResponse, nil
}
// GetValidatorDelegations implements stakingsource.Source
func (s Source) GetValidatorDelegations(height int64, validator string) ([]stakingtypes.DelegationResponse, error) {
header := remote.GetHeightRequestHeader(height)
var delegations []stakingtypes.DelegationResponse
var nextKey []byte
var stop = false
for !stop {
res, err := s.stakingClient.ValidatorDelegations(
s.Ctx,
&stakingtypes.QueryValidatorDelegationsRequest{
ValidatorAddr: validator,
Pagination: &query.PageRequest{
Key: nextKey,
Limit: 100, // Query 100 delegations at time
},
},
header,
)
if err != nil {
return nil, err
}
nextKey = res.Pagination.NextKey
stop = len(res.Pagination.NextKey) == 0
delegations = append(delegations, res.DelegationResponses...)
}
return delegations, nil
}
// GetDelegatorDelegations implements stakingsource.Source
func (s Source) GetDelegatorDelegations(height int64, delegator string) ([]stakingtypes.DelegationResponse, error) {
header := remote.GetHeightRequestHeader(height)
var delegations []stakingtypes.DelegationResponse
var nextKey []byte
var stop = false
for !stop {
res, err := s.stakingClient.DelegatorDelegations(
s.Ctx,
&stakingtypes.QueryDelegatorDelegationsRequest{
DelegatorAddr: delegator,
Pagination: &query.PageRequest{
Key: nextKey,
Limit: 100, // Query 100 delegations at time
},
},
header,
)
if err != nil {
return nil, err
}
nextKey = res.Pagination.NextKey
stop = len(res.Pagination.NextKey) == 0
delegations = append(delegations, res.DelegationResponses...)
}
return delegations, nil
}
// GetPool implements stakingsource.Source
func (s Source) GetPool(height int64) (stakingtypes.Pool, error) {
res, err := s.stakingClient.Pool(s.Ctx, &stakingtypes.QueryPoolRequest{}, remote.GetHeightRequestHeader(height))
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(s.Ctx, &stakingtypes.QueryParamsRequest{}, remote.GetHeightRequestHeader(height))
if err != nil {
return stakingtypes.Params{}, err
}
return res.Params, nil
}