-
Notifications
You must be signed in to change notification settings - Fork 4
/
cli_query.go
104 lines (88 loc) · 2.74 KB
/
cli_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
package types
import (
"bytes"
"encoding/json"
"fmt"
"github.com/spf13/cobra"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
)
// GetQueryCmd generates the entrypoint for the strongforce module
func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
strongforceQueryCmd := &cobra.Command{
Use: ModuleName,
Short: "Strongforce query subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
strongforceQueryCmd.AddCommand(client.GetCommands(
GetCmdGetState(queryRoute, cdc),
GetCmdAddresses(queryRoute, cdc),
GetCmdAddressesForType(queryRoute, cdc),
)...)
return strongforceQueryCmd
}
func GetCmdGetState(queryRoute string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "state [address]",
Short: "get the state of a contract at the address",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
address := args[0]
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/contract/state/%s", queryRoute, address), nil)
if err != nil {
fmt.Printf("could not state address - %s \n", address)
fmt.Println(err.Error())
return nil
}
var out bytes.Buffer
err = json.Indent(&out, res, "", " ")
if err != nil {
fmt.Println(err.Error())
return nil
}
fmt.Println(string(out.Bytes()))
return nil
},
}
}
func GetCmdAddresses(queryRoute string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "addresses",
Short: "list the addresses of all contracts present",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/contract/addresses", queryRoute), nil)
if err != nil {
fmt.Println("could not list addresses")
fmt.Println(err.Error())
return nil
}
fmt.Println(string(res))
return nil
},
}
}
func GetCmdAddressesForType(queryRoute string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "type",
Short: "list the addresses of contract type",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
typeName := args[0]
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/contract/type/%s", queryRoute, typeName), nil)
if err != nil {
fmt.Printf("could not find address for type: %s \n", typeName)
fmt.Println(err.Error())
return nil
}
fmt.Println(string(res))
return nil
},
}
}