/
query.go
72 lines (63 loc) · 2.15 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
package cli
import (
"fmt"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/dev10/fantom-asset-management/x/assetmanagement/internal/keeper"
"github.com/dev10/fantom-asset-management/x/assetmanagement/internal/types"
"github.com/spf13/cobra"
)
func GetQueryCmd(storeKey string, cdc *codec.Codec) *cobra.Command {
queryCmd := &cobra.Command{
Use: types.ModuleName,
Short: "Querying commands for the asset management module",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
queryCmd.AddCommand(client.GetCommands(
GetCmdFindToken(storeKey, cdc),
GetCmdSymbols(storeKey, cdc),
)...)
return queryCmd
}
// GetCmdFindToken queries information about a token through its unique symbol
func GetCmdFindToken(queryRoute string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "find [symbol]",
Short: "find symbol",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
symbol := args[0]
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s/%s", queryRoute, keeper.QueryToken, symbol), nil)
if err != nil {
fmt.Printf("could not find symbol - '%s'. reason: '%s'\n", symbol, err)
return nil
}
var out types.Token
cdc.MustUnmarshalJSON(res, &out)
return cliCtx.PrintOutput(out)
},
}
}
// GetCmdSymbols queries a list of all symbols
func GetCmdSymbols(queryRoute string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "symbols",
Short: "symbols",
// Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", queryRoute, keeper.QuerySymbols), nil)
if err != nil {
fmt.Printf("could not get query symbols. reason: '%s'\n", err)
return nil
}
var out types.QueryResultSymbol
cdc.MustUnmarshalJSON(res, &out)
return cliCtx.PrintOutput(out)
},
}
}