-
Notifications
You must be signed in to change notification settings - Fork 109
/
query.go
418 lines (353 loc) · 12 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
package cli
import (
"fmt"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/ethereum/go-ethereum/common"
"github.com/spf13/cobra"
"github.com/axelarnetwork/axelar-core/x/evm/keeper"
evmclient "github.com/axelarnetwork/axelar-core/x/evm/client"
"github.com/axelarnetwork/axelar-core/x/evm/types"
)
// GetQueryCmd returns the cli query commands for this module
func GetQueryCmd(queryRoute string) *cobra.Command {
evmQueryCmd := &cobra.Command{
Use: "evm",
Short: fmt.Sprintf("Querying commands for the %s module", types.ModuleName),
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
evmQueryCmd.AddCommand(
GetCmdAddress(queryRoute),
GetCmdAxelarGatewayAddress(queryRoute),
GetCmdTokenAddress(queryRoute),
GetCmdDepositState(queryRoute),
GetCmdBytecode(queryRoute),
GetCmdQueryBatchedCommands(queryRoute),
GetCmdLatestBatchedCommands(queryRoute),
GetCmdPendingCommands(queryRoute),
GetCmdCommand(queryRoute),
GetCmdBurnerInfo(queryRoute),
GetCmdChains(queryRoute),
GetCmdConfirmationHeight(queryRoute),
)
return evmQueryCmd
}
// GetCmdAddress returns the query for an EVM chain address
func GetCmdAddress(queryRoute string) *cobra.Command {
cmd := &cobra.Command{
Use: "address [chain]",
Short: "Returns the EVM address",
Args: cobra.ExactArgs(1),
}
keyRole := cmd.Flags().String("key-role", "", "the role of the key to get the address for")
keyID := cmd.Flags().String("key-id", "", "the ID of the key to get the address for")
cmd.RunE = func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
var query string
var param string
switch {
case *keyRole != "" && *keyID == "":
query = keeper.QAddressByKeyRole
param = *keyRole
case *keyRole == "" && *keyID != "":
query = keeper.QAddressByKeyID
param = *keyID
default:
return fmt.Errorf("one and only one of the two flags key-role and key-id has to be set")
}
bz, _, err := clientCtx.Query(fmt.Sprintf("custom/%s/%s/%s/%s", queryRoute, query, args[0], param))
if err != nil {
return sdkerrors.Wrap(err, types.ErrAddress)
}
var res types.QueryAddressResponse
if err := res.Unmarshal(bz); err != nil {
return sdkerrors.Wrap(types.ErrEVM, err.Error())
}
return clientCtx.PrintProto(&res)
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// GetCmdTokenAddress returns the query for an EVM chain master address that owns the AxelarGateway contract
func GetCmdTokenAddress(queryRoute string) *cobra.Command {
cmd := &cobra.Command{
Use: "token-address [chain]",
Short: fmt.Sprintf("Query a token address by by either %s or %s", keeper.BySymbol, keeper.ByAsset),
Args: cobra.ExactArgs(1),
}
symbol := cmd.Flags().String(keeper.BySymbol, "", "lookup token by symbol")
asset := cmd.Flags().String(keeper.ByAsset, "", "lookup token by asset name")
cmd.RunE = func(cmd *cobra.Command, args []string) error {
cliCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
var bz []byte
switch {
case *symbol != "" && *asset == "":
bz, _, err = cliCtx.Query(fmt.Sprintf("custom/%s/%s/%s/%s", queryRoute, keeper.QTokenAddressBySymbol, args[0], *symbol))
case *symbol == "" && *asset != "":
bz, _, err = cliCtx.Query(fmt.Sprintf("custom/%s/%s/%s/%s", queryRoute, keeper.QTokenAddressByAsset, args[0], *asset))
default:
return fmt.Errorf("lookup must be either by asset name or symbol")
}
if err != nil {
return err
}
var res types.QueryTokenAddressResponse
types.ModuleCdc.UnmarshalLengthPrefixed(bz, &res)
return cliCtx.PrintProto(&res)
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// GetCmdDepositState returns the query for an ERC20 deposit transaction state
func GetCmdDepositState(queryRoute string) *cobra.Command {
cmd := &cobra.Command{
Use: "deposit-state [chain] [txID] [burner address] [amount]",
Short: "Query the state of a deposit transaction",
Args: cobra.ExactArgs(4),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
chain := args[0]
txID := common.HexToHash(args[1])
burnerAddress := common.HexToAddress(args[2])
amount := sdk.NewUintFromString(args[3])
queryClient := types.NewQueryServiceClient(cliCtx)
res, err := queryClient.DepositState(cmd.Context(), &types.DepositStateRequest{
Chain: chain,
Params: &types.QueryDepositStateParams{
TxID: types.Hash(txID),
BurnerAddress: types.Address(burnerAddress),
Amount: amount.String(),
},
})
if err != nil {
return err
}
return cliCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// GetCmdAxelarGatewayAddress returns the query for the AxelarGateway contract address
func GetCmdAxelarGatewayAddress(queryRoute string) *cobra.Command {
cmd := &cobra.Command{
Use: "gateway-address [chain]",
Short: "Query the Axelar Gateway contract address",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s/%s", queryRoute, keeper.QAxelarGatewayAddress, args[0]), nil)
if err != nil {
fmt.Printf(types.ErrFGatewayAddress, err.Error())
return nil
}
out := common.BytesToAddress(res)
return cliCtx.PrintObjectLegacy(out.Hex())
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// GetCmdBytecode fetches the bytecodes of an EVM contract
func GetCmdBytecode(queryRoute string) *cobra.Command {
cmd := &cobra.Command{
Use: "bytecode [chain] [contract]",
Short: "Fetch the bytecodes of an EVM contract [contract] for chain [chain]",
Long: fmt.Sprintf("Fetch the bytecodes of an EVM contract [contract] for chain [chain]. "+
"The value for [contract] can be either '%s', '%s', '%s', or '%s'.",
keeper.BCGateway, keeper.BCGatewayDeployment, keeper.BCToken, keeper.BCBurner),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s/%s/%s", queryRoute, keeper.QBytecode, args[0], args[1]), nil)
if err != nil {
return sdkerrors.Wrapf(err, types.ErrFBytecode, args[1])
}
fmt.Println("0x" + common.Bytes2Hex(res))
return nil
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// GetCmdQueryBatchedCommands returns the query to get the batched commands
func GetCmdQueryBatchedCommands(queryRoute string) *cobra.Command {
cmd := &cobra.Command{
Use: "batched-commands [chain] [batchedCommandsID]",
Short: "Get the signed batched commands that can be wrapped in an EVM transaction to be executed in Axelar Gateway",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
chain := args[0]
idHex := args[1]
bz, _, err := clientCtx.Query(fmt.Sprintf("custom/%s/%s/%s/%s", queryRoute, keeper.QBatchedCommands, chain, idHex))
if err != nil {
return sdkerrors.Wrapf(err, "could not get batched commands %s", idHex)
}
var res types.QueryBatchedCommandsResponse
types.ModuleCdc.MustUnmarshalLengthPrefixed(bz, &res)
return clientCtx.PrintProto(&res)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// GetCmdLatestBatchedCommands returns the query to get the latest batched commands
func GetCmdLatestBatchedCommands(queryRoute string) *cobra.Command {
cmd := &cobra.Command{
Use: "latest-batched-commands [chain]",
Short: "Get the latest batched commands that can be wrapped in an EVM transaction to be executed in Axelar Gateway",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
chain := args[0]
bz, _, err := clientCtx.Query(fmt.Sprintf("custom/%s/%s/%s", queryRoute, keeper.QLatestBatchedCommands, chain))
if err != nil {
return sdkerrors.Wrapf(err, "could not get the latest batched commands for chain %s", chain)
}
var res types.QueryBatchedCommandsResponse
types.ModuleCdc.MustUnmarshalLengthPrefixed(bz, &res)
return clientCtx.PrintProto(&res)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// GetCmdPendingCommands returns the query to get the list of commands not yet added to a batch
func GetCmdPendingCommands(queryRoute string) *cobra.Command {
cmd := &cobra.Command{
Use: "pending-commands [chain]",
Short: "Get the list of commands not yet added to a batch",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
res, err := evmclient.QueryPendingCommands(clientCtx, args[0])
if err != nil {
return err
}
return clientCtx.PrintProto(&res)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// GetCmdCommand returns the query to get the command with the given ID on the specified chain
func GetCmdCommand(queryRoute string) *cobra.Command {
cmd := &cobra.Command{
Use: "command [chain] [id]",
Short: "Get information about an EVM gateway command given a chain and the command ID",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
res, err := evmclient.QueryCommand(clientCtx, args[0], args[1])
if err != nil {
return err
}
return clientCtx.PrintProto(&res)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// GetCmdBurnerInfo returns the query to get the burner info for the specified address
func GetCmdBurnerInfo(queryRoute string) *cobra.Command {
cmd := &cobra.Command{
Use: "burner-info [deposit address]",
Short: "Get information about a burner address",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryServiceClient(clientCtx)
res, err := queryClient.BurnerInfo(cmd.Context(),
&types.BurnerInfoRequest{
Address: types.Address(common.HexToAddress(args[0])),
})
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// GetCmdChains returns the query to get all EVM chains
func GetCmdChains(queryRoute string) *cobra.Command {
cmd := &cobra.Command{
Use: "chains",
Short: "Get EVM chains",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
res, err := evmclient.QueryChains(clientCtx)
if err != nil {
return err
}
return clientCtx.PrintProto(&res)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// GetCmdConfirmationHeight returns the query to get the minimum confirmation height for the given chain
func GetCmdConfirmationHeight(queryRoute string) *cobra.Command {
cmd := &cobra.Command{
Use: "confirmation-height [chain]",
Short: "Returns the minimum confirmation height for the given chain",
Args: cobra.ExactArgs(1),
}
cmd.RunE = func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryServiceClient(clientCtx)
res, err := queryClient.ConfirmationHeight(cmd.Context(),
&types.ConfirmationHeightRequest{
Chain: args[0],
})
if err != nil {
return err
}
return clientCtx.PrintProto(res)
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}