-
Notifications
You must be signed in to change notification settings - Fork 39
/
queries.go
243 lines (198 loc) · 6.74 KB
/
queries.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
package chain
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
"cosmossdk.io/math"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
tmabcitypes "github.com/cometbft/cometbft/abci/types"
"github.com/stretchr/testify/require"
"github.com/classic-terra/core/v3/tests/e2e/initialization"
"github.com/classic-terra/core/v3/tests/e2e/util"
treasurytypes "github.com/classic-terra/core/v3/x/treasury/types"
)
func (n *NodeConfig) QueryGRPCGateway(path string, parameters ...string) ([]byte, error) {
if len(parameters)%2 != 0 {
return nil, fmt.Errorf("invalid number of parameters, must follow the format of key + value")
}
// add the URL for the given validator ID, and prepend to to path.
hostPort, err := n.containerManager.GetHostPort(n.Name, "1317/tcp")
require.NoError(n.t, err)
endpoint := fmt.Sprintf("http://%s", hostPort)
fullQueryPath := fmt.Sprintf("%s/%s", endpoint, path)
var resp *http.Response
require.Eventually(n.t, func() bool {
req, err := http.NewRequest("GET", fullQueryPath, nil)
if err != nil {
return false
}
if len(parameters) > 0 {
q := req.URL.Query()
for i := 0; i < len(parameters); i += 2 {
q.Add(parameters[i], parameters[i+1])
}
req.URL.RawQuery = q.Encode()
}
resp, err = http.DefaultClient.Do(req)
if err != nil {
n.t.Logf("error while executing HTTP request: %s", err.Error())
return false
}
return resp.StatusCode != http.StatusServiceUnavailable
}, initialization.OneMin, time.Millisecond*10, "failed to execute HTTP request")
defer resp.Body.Close()
bz, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d, body: %s", resp.StatusCode, string(bz))
}
return bz, nil
}
// QueryBalancer returns balances at the address.
func (n *NodeConfig) QueryBalances(address string) (sdk.Coins, error) {
path := fmt.Sprintf("cosmos/bank/v1beta1/balances/%s", address)
bz, err := n.QueryGRPCGateway(path)
require.NoError(n.t, err)
var balancesResp banktypes.QueryAllBalancesResponse
if err := util.Cdc.UnmarshalJSON(bz, &balancesResp); err != nil {
return sdk.Coins{}, err
}
return balancesResp.GetBalances(), nil
}
// if coin is zero, return empty coin.
func (n *NodeConfig) QuerySpecificBalance(addr, denom string) (sdk.Coin, error) {
balances, err := n.QueryBalances(addr)
if err != nil {
return sdk.Coin{}, err
}
for _, c := range balances {
if c.Denom == denom {
return c, nil
}
}
return sdk.Coin{}, nil
}
func (n *NodeConfig) QuerySupplyOf(denom string) (math.Int, error) {
path := fmt.Sprintf("cosmos/bank/v1beta1/supply/%s", denom)
bz, err := n.QueryGRPCGateway(path)
require.NoError(n.t, err)
var supplyResp banktypes.QuerySupplyOfResponse
if err := util.Cdc.UnmarshalJSON(bz, &supplyResp); err != nil {
return sdk.NewInt(0), err
}
return supplyResp.Amount.Amount, nil
}
func (n *NodeConfig) QueryTaxRate() (sdk.Dec, error) {
path := "terra/treasury/v1beta1/tax_rate"
bz, err := n.QueryGRPCGateway(path)
require.NoError(n.t, err)
var taxRateResp treasurytypes.QueryTaxRateResponse
if err := util.Cdc.UnmarshalJSON(bz, &taxRateResp); err != nil {
return sdk.ZeroDec(), err
}
return taxRateResp.TaxRate, nil
}
func (n *NodeConfig) QueryBurnTaxExemptionList() ([]string, error) {
path := "terra/treasury/v1beta1/burn_tax_exemption_list"
bz, err := n.QueryGRPCGateway(path)
require.NoError(n.t, err)
var taxRateResp treasurytypes.QueryBurnTaxExemptionListResponse
if err := util.Cdc.UnmarshalJSON(bz, &taxRateResp); err != nil {
return nil, err
}
return taxRateResp.Addresses, nil
}
func (n *NodeConfig) QueryContractsFromID(codeID int) ([]string, error) {
path := fmt.Sprintf("/cosmwasm/wasm/v1/code/%d/contracts", codeID)
bz, err := n.QueryGRPCGateway(path)
require.NoError(n.t, err)
var contractsResponse wasmtypes.QueryContractsByCodeResponse
if err := util.Cdc.UnmarshalJSON(bz, &contractsResponse); err != nil {
return nil, err
}
return contractsResponse.Contracts, nil
}
func (n *NodeConfig) QueryLatestWasmCodeID() uint64 {
path := "/cosmwasm/wasm/v1/code"
bz, err := n.QueryGRPCGateway(path)
require.NoError(n.t, err)
var response wasmtypes.QueryCodesResponse
err = util.Cdc.UnmarshalJSON(bz, &response)
require.NoError(n.t, err)
if len(response.CodeInfos) == 0 {
return 0
}
return response.CodeInfos[len(response.CodeInfos)-1].CodeID
}
func (n *NodeConfig) QueryWasmSmart(contract string, msg string) (interface{}, error) {
// base64-encode the msg
encodedMsg := base64.StdEncoding.EncodeToString([]byte(msg))
path := fmt.Sprintf("/cosmwasm/wasm/v1/contract/%s/smart/%s", contract, encodedMsg)
bz, err := n.QueryGRPCGateway(path)
if err != nil {
return nil, err
}
var response wasmtypes.QuerySmartContractStateResponse
err = util.Cdc.UnmarshalJSON(bz, &response)
if err != nil {
return nil, err
}
var responseJSON interface{}
err = json.Unmarshal(response.Data, &responseJSON)
if err != nil {
return nil, err
}
return responseJSON, nil
}
func (n *NodeConfig) QueryPropStatus(proposalNumber int) (string, error) {
path := fmt.Sprintf("cosmos/gov/v1beta1/proposals/%d", proposalNumber)
bz, err := n.QueryGRPCGateway(path)
require.NoError(n.t, err)
var resp map[string]interface{}
err = json.Unmarshal(bz, &resp)
require.NoError(n.t, err)
status := resp["proposal"].(map[string]interface{})["status"].(string)
return status, nil
}
// QueryHashFromBlock gets block hash at a specific height. Otherwise, error.
func (n *NodeConfig) QueryHashFromBlock(height int64) (string, error) {
block, err := n.rpcClient.Block(context.Background(), &height)
if err != nil {
return "", err
}
return block.BlockID.Hash.String(), nil
}
// QueryCurrentHeight returns the current block height of the node or error.
func (n *NodeConfig) QueryCurrentHeight() (int64, error) {
status, err := n.rpcClient.Status(context.Background())
if err != nil {
return 0, err
}
return status.SyncInfo.LatestBlockHeight, nil
}
// QueryLatestBlockTime returns the latest block time.
func (n *NodeConfig) QueryLatestBlockTime() time.Time {
status, err := n.rpcClient.Status(context.Background())
require.NoError(n.t, err)
return status.SyncInfo.LatestBlockTime
}
// QueryListSnapshots gets all snapshots currently created for a node.
func (n *NodeConfig) QueryListSnapshots() ([]*tmabcitypes.Snapshot, error) {
abciResponse, err := n.rpcClient.ABCIQuery(context.Background(), "/app/snapshots", nil)
if err != nil {
return nil, err
}
var listSnapshots tmabcitypes.ResponseListSnapshots
if err := json.Unmarshal(abciResponse.Response.Value, &listSnapshots); err != nil {
return nil, err
}
return listSnapshots.Snapshots, nil
}