-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
132 lines (113 loc) · 3.7 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
package cli
import (
"fmt"
"github.com/furyaxyz/fuxchain/libs/cosmos-sdk/client/flags"
"strings"
"github.com/furyaxyz/fuxchain/libs/cosmos-sdk/client"
"github.com/furyaxyz/fuxchain/libs/cosmos-sdk/client/context"
"github.com/furyaxyz/fuxchain/libs/cosmos-sdk/codec"
"github.com/furyaxyz/fuxchain/libs/cosmos-sdk/x/auth"
"github.com/furyaxyz/fuxchain/x/token/types"
"github.com/spf13/cobra"
)
// GetQueryCmd returns the cli query commands for this module
func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
queryCmd := &cobra.Command{
Use: types.ModuleName,
Short: "Querying commands for the token module",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
queryCmd.AddCommand(flags.GetCommands(
getCmdQueryParams(queryRoute, cdc),
getCmdTokenInfo(queryRoute, cdc),
//getAccountCmd(queryRoute, cdc),
)...)
return queryCmd
}
// getCmdTokenInfo queries token info by address
func getCmdTokenInfo(queryRoute string, cdc *codec.Codec) *cobra.Command {
var owner string
cmd := &cobra.Command{
Use: "info [<symbol>]",
Short: "query token info by symbol",
//Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
switch {
case len(args) == 1:
symbol := args[0]
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/info/%s", queryRoute, symbol), nil)
if err != nil {
fmt.Printf("token does not exist - %s %s\n", symbol, err.Error())
return nil
}
var token types.TokenResp
cdc.MustUnmarshalJSON(res, &token)
return cliCtx.PrintOutput(token)
case owner != "":
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/tokens/%s", queryRoute, owner), nil)
if err != nil {
fmt.Printf("Invalid owner address - %s %s\n", owner, err.Error())
return nil
}
var tokens types.Tokens
cdc.MustUnmarshalJSON(res, &tokens)
return cliCtx.PrintOutput(tokens)
default:
fmt.Println("At least a [<symbol>] arg or an --owner flag need to be set")
err := cmd.Help()
if err != nil {
return err
}
return nil
}
},
}
cmd.Flags().StringVarP(&owner, "owner", "", "", "Get all the tokens information belong to")
return cmd
}
// getCmdQueryParams implements the query params command.
func getCmdQueryParams(queryRoute string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "params",
Short: "Query the parameters of the token process",
Long: strings.TrimSpace(`Query the all the parameters for the governance process:
$ fuxchaincli query token params
`),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
route := fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryParameters)
bz, _, err := cliCtx.QueryWithData(route, nil)
if err != nil {
return err
}
var params types.Params
cdc.MustUnmarshalJSON(bz, ¶ms)
return cliCtx.PrintOutput(params)
},
}
}
// just for the object of []string could be inputted into cliCtx.PrintOutput(...)
type Strings []string
func (strs Strings) String() string {
return strings.Join(strs, "\n")
}
func getAccount(ctx *context.CLIContext, address []byte) (auth.Account, error) {
bz, err := ctx.Codec.MarshalJSON(auth.NewQueryAccountParams(address))
if err != nil {
return nil, err
}
route := fmt.Sprintf("custom/%s/%s", auth.StoreKey, auth.QueryAccount)
res, _, err := ctx.QueryWithData(route, bz)
if err != nil {
return nil, err
}
var account auth.Account
if err := ctx.Codec.UnmarshalJSON(res, &account); err != nil {
return nil, err
}
return account, nil
}