-
Notifications
You must be signed in to change notification settings - Fork 736
/
Copy pathquery.go
419 lines (340 loc) · 13.3 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package e2e
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
ratelimittypes "github.com/cosmos/ibc-apps/modules/rate-limiting/v10/types"
wasmclienttypes "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/v10/types"
icacontrollertypes "github.com/cosmos/ibc-go/v10/modules/apps/27-interchain-accounts/controller/types"
providertypes "github.com/cosmos/interchain-security/v7/x/ccv/provider/types"
evidencetypes "cosmossdk.io/x/evidence/types"
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"
govtypesv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
govtypesv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/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 querySupplyOf(endpoint, denom string) (sdk.Coin, error) {
body, err := httpGet(fmt.Sprintf("%s/cosmos/bank/v1beta1/supply/by_denom?denom=%s", endpoint, denom))
if err != nil {
return sdk.Coin{}, fmt.Errorf("failed to execute HTTP request: %w", err)
}
var supplyOfResp banktypes.QuerySupplyOfResponse
if err := cdc.UnmarshalJSON(body, &supplyOfResp); err != nil {
return sdk.Coin{}, err
}
return supplyOfResp.Amount, nil
}
func queryStakingParams(endpoint string) (stakingtypes.QueryParamsResponse, error) {
body, err := httpGet(fmt.Sprintf("%s/cosmos/staking/v1beta1/params", endpoint))
if err != nil {
return stakingtypes.QueryParamsResponse{}, fmt.Errorf("failed to execute HTTP request: %w", err)
}
var params stakingtypes.QueryParamsResponse
if err := cdc.UnmarshalJSON(body, ¶ms); err != nil {
return stakingtypes.QueryParamsResponse{}, err
}
return params, 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 queryUnbondingDelegation(endpoint string, validatorAddr string, delegatorAddr string) (stakingtypes.QueryUnbondingDelegationResponse, error) {
var res stakingtypes.QueryUnbondingDelegationResponse
body, err := httpGet(fmt.Sprintf("%s/cosmos/staking/v1beta1/validators/%s/delegations/%s/unbonding_delegation", 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) (govtypesv1beta1.QueryProposalResponse, error) {
var govProposalResp govtypesv1beta1.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 queryGovProposalV1(endpoint string, proposalID int) (govtypesv1.QueryProposalResponse, error) {
var govProposalResp govtypesv1.QueryProposalResponse
path := fmt.Sprintf("%s/cosmos/gov/v1/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 sdk.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 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 stakingtypes.Validators{}, fmt.Errorf("failed to execute HTTP request: %w", err)
}
if err := cdc.UnmarshalJSON(body, &res); err != nil {
return stakingtypes.Validators{}, err
}
return stakingtypes.Validators{Validators: 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
}
func queryTokenizeShareRecordByID(endpoint string, recordID int) (stakingtypes.TokenizeShareRecord, error) {
var res stakingtypes.QueryTokenizeShareRecordByIdResponse
body, err := httpGet(fmt.Sprintf("%s/cosmos/staking/v1beta1/tokenize_share_record_by_id/%d", endpoint, recordID))
if err != nil {
return stakingtypes.TokenizeShareRecord{}, fmt.Errorf("failed to execute HTTP request: %w", err)
}
if err := cdc.UnmarshalJSON(body, &res); err != nil {
return stakingtypes.TokenizeShareRecord{}, err
}
return res.Record, nil
}
func queryAllRateLimits(endpoint string) ([]ratelimittypes.RateLimit, error) {
var res ratelimittypes.QueryAllRateLimitsResponse
body, err := httpGet(fmt.Sprintf("%s/Stride-Labs/ibc-rate-limiting/ratelimit/ratelimits", endpoint))
if err != nil {
return []ratelimittypes.RateLimit{}, fmt.Errorf("failed to execute HTTP request: %w", err)
}
if err := cdc.UnmarshalJSON(body, &res); err != nil {
return []ratelimittypes.RateLimit{}, err
}
return res.RateLimits, nil
}
func queryRateLimit(endpoint, channelID, denom string) (ratelimittypes.QueryRateLimitResponse, error) {
var res ratelimittypes.QueryRateLimitResponse
body, err := httpGet(fmt.Sprintf("%s/Stride-Labs/ibc-rate-limiting/ratelimit/ratelimit/%s/by_denom?denom=%s", endpoint, channelID, denom))
if err != nil {
return ratelimittypes.QueryRateLimitResponse{}, fmt.Errorf("failed to execute HTTP request: %w", err)
}
if err := cdc.UnmarshalJSON(body, &res); err != nil {
return ratelimittypes.QueryRateLimitResponse{}, err
}
return res, nil
}
func queryRateLimitsByChainID(endpoint, channelID string) ([]ratelimittypes.RateLimit, error) {
var res ratelimittypes.QueryRateLimitsByChainIdResponse
body, err := httpGet(fmt.Sprintf("%s/Stride-Labs/ibc-rate-limiting/ratelimit/ratelimits/%s", endpoint, channelID))
if err != nil {
return []ratelimittypes.RateLimit{}, fmt.Errorf("failed to execute HTTP request: %w", err)
}
if err := cdc.UnmarshalJSON(body, &res); err != nil {
return []ratelimittypes.RateLimit{}, err
}
return res.RateLimits, nil
}
func queryICAAccountAddress(endpoint, owner, connectionID string) (string, error) {
body, err := httpGet(fmt.Sprintf("%s/ibc/apps/interchain_accounts/controller/v1/owners/%s/connections/%s", endpoint, owner, connectionID))
if err != nil {
return "", fmt.Errorf("failed to execute HTTP request: %w", err)
}
var icaAccountResp icacontrollertypes.QueryInterchainAccountResponse
if err := cdc.UnmarshalJSON(body, &icaAccountResp); err != nil {
return "", err
}
return icaAccountResp.Address, nil
}
func queryBlocksPerEpoch(endpoint string) (int64, error) {
body, err := httpGet(fmt.Sprintf("%s/interchain_security/ccv/provider/params", endpoint))
if err != nil {
return 0, fmt.Errorf("failed to execute HTTP request: %w", err)
}
var response providertypes.QueryParamsResponse
if err = cdc.UnmarshalJSON(body, &response); err != nil {
return 0, err
}
return response.Params.BlocksPerEpoch, nil
}
func queryWasmContractAddress(endpoint, creator string) (string, error) {
body, err := httpGet(fmt.Sprintf("%s/cosmwasm/wasm/v1/contracts/creator/%s", endpoint, creator))
if err != nil {
return "", fmt.Errorf("failed to execute HTTP request: %w", err)
}
var response wasmtypes.QueryContractsByCreatorResponse
if err = cdc.UnmarshalJSON(body, &response); err != nil {
return "", err
}
return response.ContractAddresses[0], nil
}
func queryWasmSmartContractState(endpoint, address, msg string) ([]byte, error) {
body, err := httpGet(fmt.Sprintf("%s/cosmwasm/wasm/v1/contract/%s/smart/%s", endpoint, address, msg))
if err != nil {
return nil, fmt.Errorf("failed to execute HTTP request: %w", err)
}
var response wasmtypes.QuerySmartContractStateResponse
if err = cdc.UnmarshalJSON(body, &response); err != nil {
return nil, err
}
return response.Data, nil
}
func queryIbcWasmChecksums(endpoint string) ([]string, error) {
body, err := httpGet(fmt.Sprintf("%s/ibc/lightclients/wasm/v1/checksums", endpoint))
if err != nil {
return nil, fmt.Errorf("failed to execute HTTP request: %w", err)
}
var response wasmclienttypes.QueryChecksumsResponse
if err = cdc.UnmarshalJSON(body, &response); err != nil {
return nil, err
}
return response.Checksums, nil
}