-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
170 lines (153 loc) · 5.01 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
package cli
import (
"fmt"
"github.com/evoblockchain/evochain/libs/cosmos-sdk/client/context"
client "github.com/evoblockchain/evochain/libs/cosmos-sdk/client/flags"
"github.com/evoblockchain/evochain/libs/cosmos-sdk/codec"
"github.com/evoblockchain/evochain/x/order/keeper"
"github.com/evoblockchain/evochain/x/order/types"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"strings"
)
// GetQueryCmd returns the cli query commands for this module
func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
// Group order queries under a subcommand
queryCmd := &cobra.Command{
Use: "order",
Short: "Querying commands for the order module",
}
queryCmd.AddCommand(client.GetCommands(
GetCmdQueryOrder(queryRoute, cdc),
GetCmdDepthBook(queryRoute, cdc),
GetCmdQueryStore(queryRoute, cdc),
GetCmdQueryParams(queryRoute, cdc),
)...)
queryCmd.Flags().StringP(client.FlagNode, "n", "tcp://localhost:26657", "Node to connect to")
return queryCmd
}
// GetCmdQueryOrder queries order info by orderID
func GetCmdQueryOrder(queryRoute string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "detail [order-id]",
Short: "Query an order",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
orderID := args[0]
res, _, err := cliCtx.QueryWithData(
fmt.Sprintf("custom/%s/%s/%s", queryRoute, types.QueryOrderDetail, orderID),
nil)
if err != nil {
fmt.Printf("order does not exist - %s \n", orderID)
return nil
}
fmt.Println(string(res))
return nil
},
}
}
// GetCmdDepthBook queries order book about a product
func GetCmdDepthBook(queryRoute string, cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "depthbook [product]",
Short: "Query the depth book of a trading pair",
Long: strings.TrimSpace(`Query the depth book of a trading pair:
$ evochaincli query depthbook mytoken_evo
The 'product' is a trading pair in full name of the tokens: ${base-asset-symbol}_${quote-asset-symbol}, for example 'mytoken_evo'.
`),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
product := args[0]
size := viper.GetUint("size")
params := keeper.NewQueryDepthBookParams(product, size)
bz, err := cdc.MarshalJSON(params)
if err != nil {
return err
}
res, _, err := cliCtx.QueryWithData(
fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryDepthBook),
bz)
if err != nil {
fmt.Printf("get depth book of %s failed: %v\n", product, err.Error())
return nil
}
fmt.Println(string(res))
return nil
},
}
cmd.Flags().Uint("size", keeper.DefaultBookSize, "depth book single-side size")
return cmd
}
// GetCmdQueryStore queries store statistic
func GetCmdQueryStore(queryRoute string, cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "store",
Short: "query store of order module",
RunE: func(cmd *cobra.Command, args []string) error {
dbFile := viper.GetString("app_dbpath")
//dump := viper.GetBool("dump")
if dbFile != "" {
//// query through db file
//mapp, db, err := filedb.GetMockApp(dbFile)
//if err != nil {
// return err
//}
//defer filedb.CloseApp(db)
//
//kp := mapp.OrderKeeper
//mapp.BeginBlock(abci.RequestBeginBlock{})
//ctx := mapp.GetState(baseapp.RunTxModeDeliver()).Context()
//if dump {
// keeper.DumpStore(ctx, kp)
// return nil
//}
//
//ss := keeper.GetStoreStatistic(ctx, kp)
//res, errRes := codec.MarshalJSONIndent(cdc, ss)
//if errRes != nil {
// return sdk.ErrInternal(
// sdk.AppendMsgToErr("could not marshal result to JSON", errRes.Error()))
//}
//fmt.Println(string(res))
} else {
cliCtx := context.NewCLIContext().WithCodec(cdc)
res, _, err := cliCtx.QueryWithData(
fmt.Sprintf("custom/order/%s", types.QueryStore), nil)
if err != nil {
fmt.Printf("query store failed: %v\n", err.Error())
return nil
}
fmt.Println(string(res))
return nil
}
return nil
},
}
cmd.Flags().String("dbpath", "", "db path (if this path is given, query through local file)")
cmd.Flags().Bool("dump", false, "dump all key-value constants of specified module")
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 order process",
Long: strings.TrimSpace(`Query the all the parameters for the governance process:
$ evochaincli query order 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)
},
}
}