/
query.go
256 lines (214 loc) · 7.68 KB
/
query.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package e2e
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
authvesting "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
disttypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
evidencetypes "github.com/cosmos/cosmos-sdk/x/evidence/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
globalfee "github.com/cosmos/gaia/v9/x/globalfee/types"
)
func queryGaiaTx(endpoint, txHash string) error {
resp, err := http.Get(fmt.Sprintf("%s/cosmos/tx/v1beta1/txs/%s", endpoint, txHash))
if err != nil {
return fmt.Errorf("failed to execute HTTP request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("tx query returned non-200 status: %d", resp.StatusCode)
}
var result map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return fmt.Errorf("failed to read response body: %w", err)
}
txResp := result["tx_response"].(map[string]interface{})
if v := txResp["code"]; v.(float64) != 0 {
return fmt.Errorf("tx %s failed with status code %v", txHash, v)
}
return nil
}
// if coin is zero, return empty coin.
func getSpecificBalance(endpoint, addr, denom string) (amt sdk.Coin, err error) {
balances, err := queryGaiaAllBalances(endpoint, addr)
if err != nil {
return amt, err
}
for _, c := range balances {
if strings.Contains(c.Denom, denom) {
amt = c
break
}
}
return amt, nil
}
func queryGaiaAllBalances(endpoint, addr string) (sdk.Coins, error) {
body, err := httpGet(fmt.Sprintf("%s/cosmos/bank/v1beta1/balances/%s", endpoint, addr))
if err != nil {
return nil, fmt.Errorf("failed to execute HTTP request: %w", err)
}
var balancesResp banktypes.QueryAllBalancesResponse
if err := cdc.UnmarshalJSON(body, &balancesResp); err != nil {
return nil, err
}
return balancesResp.Balances, nil
}
func queryGlobalFees(endpoint string) (amt sdk.DecCoins, err error) {
body, err := httpGet(fmt.Sprintf("%s/gaia/globalfee/v1beta1/minimum_gas_prices", endpoint))
if err != nil {
return nil, fmt.Errorf("failed to execute HTTP request: %w", err)
}
var fees globalfee.QueryMinimumGasPricesResponse
if err := cdc.UnmarshalJSON(body, &fees); err != nil {
return sdk.DecCoins{}, err
}
return fees.MinimumGasPrices, nil
}
func queryDelegation(endpoint string, validatorAddr string, delegatorAddr string) (stakingtypes.QueryDelegationResponse, error) {
var res stakingtypes.QueryDelegationResponse
body, err := httpGet(fmt.Sprintf("%s/cosmos/staking/v1beta1/validators/%s/delegations/%s", endpoint, validatorAddr, delegatorAddr))
if err != nil {
return res, err
}
if err = cdc.UnmarshalJSON(body, &res); err != nil {
return res, err
}
return res, nil
}
func queryDelegatorWithdrawalAddress(endpoint string, delegatorAddr string) (disttypes.QueryDelegatorWithdrawAddressResponse, error) {
var res disttypes.QueryDelegatorWithdrawAddressResponse
body, err := httpGet(fmt.Sprintf("%s/cosmos/distribution/v1beta1/delegators/%s/withdraw_address", endpoint, delegatorAddr))
if err != nil {
return res, err
}
if err = cdc.UnmarshalJSON(body, &res); err != nil {
return res, err
}
return res, nil
}
func queryGovProposal(endpoint string, proposalID int) (govtypes.QueryProposalResponse, error) {
var govProposalResp govtypes.QueryProposalResponse
path := fmt.Sprintf("%s/cosmos/gov/v1beta1/proposals/%d", endpoint, proposalID)
body, err := httpGet(path)
if err != nil {
return govProposalResp, fmt.Errorf("failed to execute HTTP request: %w", err)
}
if err := cdc.UnmarshalJSON(body, &govProposalResp); err != nil {
return govProposalResp, err
}
return govProposalResp, nil
}
func queryAccount(endpoint, address string) (acc authtypes.AccountI, err error) {
var res authtypes.QueryAccountResponse
resp, err := http.Get(fmt.Sprintf("%s/cosmos/auth/v1beta1/accounts/%s", endpoint, address))
if err != nil {
return nil, fmt.Errorf("failed to execute HTTP request: %w", err)
}
defer resp.Body.Close()
bz, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if err := cdc.UnmarshalJSON(bz, &res); err != nil {
return nil, err
}
return acc, cdc.UnpackAny(res.Account, &acc)
}
func queryDelayedVestingAccount(endpoint, address string) (authvesting.DelayedVestingAccount, error) {
baseAcc, err := queryAccount(endpoint, address)
if err != nil {
return authvesting.DelayedVestingAccount{}, err
}
acc, ok := baseAcc.(*authvesting.DelayedVestingAccount)
if !ok {
return authvesting.DelayedVestingAccount{},
fmt.Errorf("cannot cast %v to DelayedVestingAccount", baseAcc)
}
return *acc, nil
}
func queryContinuousVestingAccount(endpoint, address string) (authvesting.ContinuousVestingAccount, error) {
baseAcc, err := queryAccount(endpoint, address)
if err != nil {
return authvesting.ContinuousVestingAccount{}, err
}
acc, ok := baseAcc.(*authvesting.ContinuousVestingAccount)
if !ok {
return authvesting.ContinuousVestingAccount{},
fmt.Errorf("cannot cast %v to ContinuousVestingAccount", baseAcc)
}
return *acc, nil
}
func queryPermanentLockedAccount(endpoint, address string) (authvesting.PermanentLockedAccount, error) { //nolint:unused // this is called during e2e tests
baseAcc, err := queryAccount(endpoint, address)
if err != nil {
return authvesting.PermanentLockedAccount{}, err
}
acc, ok := baseAcc.(*authvesting.PermanentLockedAccount)
if !ok {
return authvesting.PermanentLockedAccount{},
fmt.Errorf("cannot cast %v to PermanentLockedAccount", baseAcc)
}
return *acc, nil
}
func queryPeriodicVestingAccount(endpoint, address string) (authvesting.PeriodicVestingAccount, error) { //nolint:unused // this is called during e2e tests
baseAcc, err := queryAccount(endpoint, address)
if err != nil {
return authvesting.PeriodicVestingAccount{}, err
}
acc, ok := baseAcc.(*authvesting.PeriodicVestingAccount)
if !ok {
return authvesting.PeriodicVestingAccount{},
fmt.Errorf("cannot cast %v to PeriodicVestingAccount", baseAcc)
}
return *acc, nil
}
func queryValidator(endpoint, address string) (stakingtypes.Validator, error) {
var res stakingtypes.QueryValidatorResponse
body, err := httpGet(fmt.Sprintf("%s/cosmos/staking/v1beta1/validators/%s", endpoint, address))
if err != nil {
return stakingtypes.Validator{}, fmt.Errorf("failed to execute HTTP request: %w", err)
}
if err := cdc.UnmarshalJSON(body, &res); err != nil {
return stakingtypes.Validator{}, err
}
return res.Validator, nil
}
func queryValidators(endpoint string) (stakingtypes.Validators, error) {
var res stakingtypes.QueryValidatorsResponse
body, err := httpGet(fmt.Sprintf("%s/cosmos/staking/v1beta1/validators", endpoint))
if err != nil {
return nil, fmt.Errorf("failed to execute HTTP request: %w", err)
}
if err := cdc.UnmarshalJSON(body, &res); err != nil {
return nil, err
}
return res.Validators, nil
}
func queryEvidence(endpoint, hash string) (evidencetypes.QueryEvidenceResponse, error) { //nolint:unused // this is called during e2e tests
var res evidencetypes.QueryEvidenceResponse
body, err := httpGet(fmt.Sprintf("%s/cosmos/evidence/v1beta1/evidence/%s", endpoint, hash))
if err != nil {
return res, err
}
if err = cdc.UnmarshalJSON(body, &res); err != nil {
return res, err
}
return res, nil
}
func queryAllEvidence(endpoint string) (evidencetypes.QueryAllEvidenceResponse, error) {
var res evidencetypes.QueryAllEvidenceResponse
body, err := httpGet(fmt.Sprintf("%s/cosmos/evidence/v1beta1/evidence", endpoint))
if err != nil {
return res, err
}
if err = cdc.UnmarshalJSON(body, &res); err != nil {
return res, err
}
return res, nil
}