/
query.go
182 lines (163 loc) · 5.05 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
package cli
import (
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/certikfoundation/shentu/x/oracle/types"
)
const (
FlagOperator = "operator"
)
// GetQueryCmd returns the cli query commands for this module.
func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
oracleQueryCmds := &cobra.Command{
Use: types.ModuleName,
Short: "Oracle staking subcommands",
}
oracleQueryCmds.AddCommand(flags.GetCommands(
GetCmdOperator(queryRoute, cdc),
GetCmdOperators(queryRoute, cdc),
GetCmdWithdraws(queryRoute, cdc),
GetCmdTask(queryRoute, cdc),
GetCmdResponse(queryRoute, cdc),
)...)
return oracleQueryCmds
}
// GetCmdOperator returns the operator query command.
func GetCmdOperator(queryRoute string, cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "operator <address>",
Short: "Get operator information",
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/operator/%s", queryRoute, args[0]), nil)
if err != nil {
return err
}
var out types.Operator
cdc.MustUnmarshalJSON(res, &out)
return cliCtx.PrintOutput(out)
},
}
return cmd
}
// GetCmdOperators returns the operators query command.
func GetCmdOperators(queryRoute string, cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "operators",
Short: "Get operators information",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/operators", queryRoute), nil)
if err != nil {
return err
}
var out types.Operators
cdc.MustUnmarshalJSON(res, &out)
return cliCtx.PrintOutput(out)
},
}
return cmd
}
// GetCmdWithdraws returns the withdrawals query command.
func GetCmdWithdraws(queryRoute string, cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "withdraws",
Short: "Get all withdrawals",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/withdraws", queryRoute), nil)
if err != nil {
return err
}
var out types.Withdraws
cdc.MustUnmarshalJSON(res, &out)
return cliCtx.PrintOutput(out)
},
}
return cmd
}
// GetCmdTask returns the task query command.
func GetCmdTask(queryRoute string, cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "task <flags>",
Short: "Get task information",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
contract := viper.GetString(FlagContract)
if contract == "" {
return fmt.Errorf("contract address is required")
}
function := viper.GetString(FlagFunction)
if function == "" {
return fmt.Errorf("function is required")
}
params := types.NewQueryTaskParams(contract, function)
bz, err := cdc.MarshalJSON(params)
if err != nil {
return err
}
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/task", queryRoute), bz)
if err != nil {
return err
}
var out types.Task
cdc.MustUnmarshalJSON(res, &out)
return cliCtx.PrintOutput(out)
},
}
cmd.Flags().String(FlagContract, "", "Provide the contract address")
cmd.Flags().String(FlagFunction, "", "Provide the function")
return cmd
}
// GetCmdResponse returns the response query command.
func GetCmdResponse(queryRoute string, cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "response <flags>",
Short: "Get response information",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
contract := viper.GetString(FlagContract)
if contract == "" {
return fmt.Errorf("contract address is required")
}
function := viper.GetString(FlagFunction)
if function == "" {
return fmt.Errorf("function is required")
}
operatorStr := viper.GetString(FlagOperator)
if operatorStr == "" {
return fmt.Errorf("opeartor Address is required")
}
operatorAddress, err := sdk.AccAddressFromBech32(operatorStr)
if err != nil {
return err
}
params := types.NewQueryResponseParams(contract, function, operatorAddress)
bz, err := cdc.MarshalJSON(params)
if err != nil {
return err
}
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/response", queryRoute), bz)
if err != nil {
return err
}
var out types.Response
cdc.MustUnmarshalJSON(res, &out)
return cliCtx.PrintOutput(out)
},
}
cmd.Flags().String(FlagContract, "", "Provide the contract address")
cmd.Flags().String(FlagFunction, "", "Provide the function")
cmd.Flags().String(FlagOperator, "", "Provide the operator")
return cmd
}