-
Notifications
You must be signed in to change notification settings - Fork 0
/
e2e_cli_util.go
169 lines (132 loc) · 4.37 KB
/
e2e_cli_util.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
package e2e
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
staketypes "github.com/cosmos/cosmos-sdk/x/staking/types"
globalfee "github.com/psangwoo/parkd/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) {
resp, err := http.Get(fmt.Sprintf("%s/cosmos/bank/v1beta1/balances/%s", endpoint, addr))
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
}
var balancesResp banktypes.QueryAllBalancesResponse
if err := cdc.UnmarshalJSON(bz, &balancesResp); err != nil {
return nil, err
}
return balancesResp.Balances, nil
}
func queryGaiaDenomBalance(endpoint, addr, denom string) (sdk.Coin, error) {
var zeroCoin sdk.Coin
path := fmt.Sprintf(
"%s/cosmos/bank/v1beta1/balances/%s/by_denom?denom=%s",
endpoint, addr, denom,
)
resp, err := http.Get(path) //nolint:gosec // this is used as a part of the e2e suite.
if err != nil {
return zeroCoin, fmt.Errorf("failed to execute HTTP request: %w", err)
}
defer resp.Body.Close()
bz, err := io.ReadAll(resp.Body)
if err != nil {
return zeroCoin, err
}
var balanceResp banktypes.QueryBalanceResponse
if err := cdc.UnmarshalJSON(bz, &balanceResp); err != nil {
return zeroCoin, err
}
return *balanceResp.Balance, nil
}
func queryGovProposal(endpoint string, proposalID int) (govv1beta1.QueryProposalResponse, error) {
var govProposalResp govv1beta1.QueryProposalResponse
path := fmt.Sprintf("%s/cosmos/gov/v1beta1/proposals/%d", endpoint, proposalID)
resp, err := http.Get(path) //nolint:gosec // this is only used during tests
if err != nil {
return govProposalResp, fmt.Errorf("failed to execute HTTP request: %w", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return govProposalResp, err
}
if err := cdc.UnmarshalJSON(body, &govProposalResp); err != nil {
return govProposalResp, err
}
return govProposalResp, nil
}
func queryGlobalFees(endpoint string) (amt sdk.DecCoins, err error) {
resp, err := http.Get(fmt.Sprintf("%s/gaia/globalfee/v1beta1/minimum_gas_prices", endpoint))
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
}
var fees globalfee.QueryMinimumGasPricesResponse
if err := cdc.UnmarshalJSON(bz, &fees); err != nil {
return sdk.DecCoins{}, err
}
return fees.MinimumGasPrices, nil
}
func queryDelegation(endpoint string, validatorAddr string, delegatorAddr string) (staketypes.QueryDelegationResponse, error) {
var delegationRes staketypes.QueryDelegationResponse
resp, err := http.Get(fmt.Sprintf("%s/cosmos/staking/v1beta1/validators/%s/delegations/%s", endpoint, validatorAddr, delegatorAddr)) //nolint:gosec // this is only used during tests
if err != nil {
return delegationRes, fmt.Errorf("failed to execute HTTP request: %w", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return delegationRes, err
}
if err := cdc.UnmarshalJSON(body, &delegationRes); err != nil {
return delegationRes, err
}
return delegationRes, nil
}