-
Notifications
You must be signed in to change notification settings - Fork 50
/
query.go
346 lines (295 loc) · 8.44 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
// Package cli defines the CLI services for the cvm module.
package cli
import (
"encoding/hex"
"errors"
"fmt"
"strings"
"github.com/spf13/cobra"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/shentufoundation/shentu/v2/x/cvm/types"
)
const (
FlagCaller = "caller"
)
// GetQueryCmd returns the cli query commands for this module
func GetQueryCmd() *cobra.Command {
// Group cvm queries under a subcommand
cvmQueryCmd := &cobra.Command{
Use: "cvm",
Short: "Querying commands for the CVM module",
}
cvmQueryCmd.AddCommand(
GetAccountCmd(),
GetCmdCode(),
GetCmdStorage(),
GetCmdAbi(),
GetCmdMeta(),
GetCmdView(),
GetCmdAddressTranslate(),
)
return cvmQueryCmd
}
// GetCmdView returns the CVM contract view transaction command.
func GetCmdView() *cobra.Command {
cmd := &cobra.Command{
Use: "view <address> <function> [<params>...]",
Short: "View CVM contract",
Args: cobra.MinimumNArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
// Caller is an optional flag. If not set it becomes the zero address.
var callerString string
callerString, err = cmd.Flags().GetString(FlagCaller)
if err != nil {
return err
}
if callerString != "" {
_, err := sdk.AccAddressFromBech32(callerString)
if err != nil {
return err
}
}
callee, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return err
}
abiSpec, data, err := parseCallCmd(clientCtx, args[0], callee, args[1], args[2:])
if err != nil {
return err
}
req := &types.QueryViewRequest{
Caller: callerString,
Callee: args[0],
AbiSpec: abiSpec,
FunctionName: args[1],
Data: data,
}
out, err := queryClient.View(cmd.Context(), req)
if err != nil {
return err
}
return clientCtx.PrintProto(out)
},
}
cmd.Flags().String(FlagCaller, "", "optional caller parameter to run the view function with")
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// GetCmdCode returns the CVM code query command.
func GetCmdCode() *cobra.Command {
cmd := &cobra.Command{
Use: "code <address>",
Short: "Get CVM contract code",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
addr := args[0]
req := &types.QueryCodeRequest{
Address: addr,
}
res, err := queryClient.Code(cmd.Context(), req)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// GetCmdStorage returns the CVM storage query command.
func GetCmdStorage() *cobra.Command {
cmd := &cobra.Command{
Use: "storage <address> <key>",
Short: "Get CVM storage data",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
addr := args[0]
key := args[1]
req := &types.QueryStorageRequest{
Address: addr,
Key: key,
}
res, err := queryClient.Storage(cmd.Context(), req)
if err != nil {
fmt.Printf("could not get CVM storage\n" + err.Error() + "\n")
return nil
}
return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// GetCmdAbi returns the CVM code ABI query command.
func GetCmdAbi() *cobra.Command {
cmd := &cobra.Command{
Use: "abi <address>",
Short: "Get CVM contract code ABI",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
addr := args[0]
req := &types.QueryAbiRequest{
Address: addr,
}
addrAbi, err := queryClient.Abi(cmd.Context(), req)
if err != nil {
fmt.Printf("could not get CVM contract code ABI\n" + err.Error() + "\n")
return nil
}
if addrAbi == nil {
fmt.Printf("cannot find CVM contract code ABI\n")
return nil
}
return clientCtx.PrintProto(addrAbi)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// GetCmdMeta returns the CVM metadata query command.
func GetCmdMeta() *cobra.Command {
cmd := &cobra.Command{
Use: "meta <address,hash>",
Short: "Get CVM Metadata hash for an address or Metadata for a hash",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
addrPrefix := sdk.GetConfig().GetBech32AccountAddrPrefix()
queryClient := types.NewQueryClient(clientCtx)
input := args[0]
var meta interface{}
if strings.HasPrefix(input, addrPrefix) {
request := &types.QueryAddressMetaRequest{Address: input}
meta, err = queryClient.AddressMeta(cmd.Context(), request)
} else {
request := &types.QueryMetaRequest{Hash: input}
meta, err = queryClient.Meta(cmd.Context(), request)
}
if err != nil {
fmt.Printf("could not get CVM Metadata\n" + err.Error() + "\n")
return nil
}
fmt.Printf("%v\n", meta)
return nil
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// GetCmdAddressTranslate is a utility query to translate Bech32 addresses to hex and vice versa.
// It is a pure function and does not interact with the handler or keeper.
func GetCmdAddressTranslate() *cobra.Command {
cmd := &cobra.Command{
Use: "address-translate <address>",
Short: "Translate a Bech32 address to hex and vice versa",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
addr := args[0]
errorMsg := `Address is not in a readable format.
Please supply either a Bech32 address ("shentu1...", "shentuvaloper1...")
or a 20-byte hex address ("0x" prefix not necessary).`
config := sdk.GetConfig()
if strings.HasPrefix(addr, config.GetBech32ConsensusAddrPrefix()) { // Bech32 to hex
consAddr, err := sdk.ConsAddressFromBech32(addr)
if err != nil {
fmt.Println(errorMsg)
return err
}
fmt.Println(hex.EncodeToString(consAddr))
return nil
} else if strings.HasPrefix(addr, config.GetBech32ValidatorAddrPrefix()) { // Bech32 to hex
valAddr, err := sdk.ValAddressFromBech32(addr)
if err != nil {
fmt.Println(errorMsg)
return err
}
fmt.Println(hex.EncodeToString(valAddr))
return nil
} else if strings.HasPrefix(addr, config.GetBech32AccountAddrPrefix()) { // Bech32 to hex
accAddr, err := sdk.AccAddressFromBech32(addr)
if err != nil {
fmt.Println(errorMsg)
return err
}
fmt.Println(hex.EncodeToString(accAddr))
return nil
} else { // hex to Bech32
if len(strings.TrimSpace(addr)) == 0 || len(strings.TrimSpace(addr)) > 510 {
fmt.Println(errorMsg)
return errors.New("address needs to be 1-255 bytes")
}
accAddr, err := sdk.AccAddressFromHexUnsafe(addr)
if err != nil {
fmt.Println(errorMsg)
return err
}
fmt.Println(accAddr.String())
valAddr, err := sdk.ValAddressFromHex(addr)
if err != nil {
fmt.Println(errorMsg)
return err
}
fmt.Println(valAddr.String())
consAddr, err := sdk.ConsAddressFromHex(addr)
if err != nil {
fmt.Println(errorMsg)
return err
}
fmt.Println(consAddr.String())
return nil
}
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// GetAccountCmd returns a query account that will display the state of the
// account at a given address.
func GetAccountCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "contract [address]",
Short: "Query contract info",
Long: "Query contract info by address, revert to query normal account info if the address is not a contract",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
queryAccount := &types.QueryAccountRequest{Address: args[0]}
res, err := queryClient.Account(cmd.Context(), queryAccount)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}