This repository has been archived by the owner on Nov 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
query.go
196 lines (182 loc) · 6.41 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package cli
import (
"encoding/json"
"fmt"
"net/http"
"github.com/cosmos/cosmos-sdk/client"
"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/spf13/cobra"
clientcmn "github.com/bandprotocol/bandchain/chain/x/oracle/client/common"
"github.com/bandprotocol/bandchain/chain/x/oracle/types"
)
// GetQueryCmd returns the cli query commands for this module.
func GetQueryCmd(storeKey string, cdc *codec.Codec) *cobra.Command {
oracleCmd := &cobra.Command{
Use: types.ModuleName,
Short: "Querying commands for the oracle module",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
oracleCmd.AddCommand(flags.GetCommands(
GetQueryCmdParams(storeKey, cdc),
GetQueryCmdCounts(storeKey, cdc),
GetQueryCmdDataSource(storeKey, cdc),
GetQueryCmdOracleScript(storeKey, cdc),
GetQueryCmdRequest(storeKey, cdc),
GetQueryCmdRequestSearch(storeKey, cdc),
GetQueryCmdValidatorStatus(storeKey, cdc),
GetQueryCmdReporters(storeKey, cdc),
GetQueryActiveValidators(storeKey, cdc),
)...)
return oracleCmd
}
func printOutput(cliCtx context.CLIContext, cdc *codec.Codec, bz []byte, out interface{}) error {
var result types.QueryResult
if err := json.Unmarshal(bz, &result); err != nil {
return err
}
if result.Status != http.StatusOK {
return cliCtx.PrintOutput(result.Result)
}
cdc.MustUnmarshalJSON(result.Result, out)
return cliCtx.PrintOutput(out)
}
// GetQueryCmdParams implements the query parameters command.
func GetQueryCmdParams(route string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "params",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
bz, _, err := cliCtx.Query(fmt.Sprintf("custom/%s/%s", route, types.QueryParams))
if err != nil {
return err
}
return printOutput(cliCtx, cdc, bz, &types.Params{})
},
}
}
// GetQueryCmdCounts implements the query counts command.
func GetQueryCmdCounts(route string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "counts",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
bz, _, err := cliCtx.Query(fmt.Sprintf("custom/%s/%s", route, types.QueryCounts))
if err != nil {
return err
}
return printOutput(cliCtx, cdc, bz, &types.QueryCountsResult{})
},
}
}
// GetQueryCmdDataSource implements the query data source command.
func GetQueryCmdDataSource(route string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "data-source [id]",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
bz, _, err := cliCtx.Query(fmt.Sprintf("custom/%s/%s/%s", route, types.QueryDataSources, args[0]))
if err != nil {
return err
}
return printOutput(cliCtx, cdc, bz, &types.DataSource{})
},
}
}
// GetQueryCmdOracleScript implements the query oracle script command.
func GetQueryCmdOracleScript(route string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "oracle-script [id]",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
bz, _, err := cliCtx.Query(fmt.Sprintf("custom/%s/%s/%s", route, types.QueryOracleScripts, args[0]))
if err != nil {
return err
}
return printOutput(cliCtx, cdc, bz, &types.OracleScript{})
},
}
}
// GetQueryCmdRequest implements the query request command.
func GetQueryCmdRequest(route string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "request [id]",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
bz, _, err := cliCtx.Query(fmt.Sprintf("custom/%s/%s/%s", route, types.QueryRequests, args[0]))
if err != nil {
return err
}
return printOutput(cliCtx, cdc, bz, &types.QueryRequestResult{})
},
}
}
// GetQueryCmdRequestSearch implements the search request command.
func GetQueryCmdRequestSearch(route string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "request-search [oracle-script-id] [calldata] [ask-count] [min-count]",
Args: cobra.ExactArgs(4),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
bz, _, err := clientcmn.QuerySearchLatestRequest(route, cliCtx, args[0], args[1], args[2], args[3])
if err != nil {
return err
}
return printOutput(cliCtx, cdc, bz, &types.QueryRequestResult{})
},
}
}
// GetQueryCmdValidatorStatus implements the query reporter list of validator command.
func GetQueryCmdValidatorStatus(route string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "validator [validator]",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
bz, _, err := cliCtx.Query(fmt.Sprintf("custom/%s/%s/%s", route, types.QueryValidatorStatus, args[0]))
if err != nil {
return err
}
return printOutput(cliCtx, cdc, bz, &types.ValidatorStatus{})
},
}
}
// GetQueryCmdReporters implements the query reporter list of validator command.
func GetQueryCmdReporters(route string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "reporters [validator]",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
bz, _, err := cliCtx.Query(fmt.Sprintf("custom/%s/%s/%s", route, types.QueryReporters, args[0]))
if err != nil {
return err
}
return printOutput(cliCtx, cdc, bz, &[]sdk.AccAddress{})
},
}
}
// GetQueryActiveValidators implements the query active validators command.
func GetQueryActiveValidators(route string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "active-validators",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
bz, _, err := cliCtx.Query(fmt.Sprintf("custom/%s/%s", route, types.QueryActiveValidators))
if err != nil {
return err
}
return printOutput(cliCtx, cdc, bz, &[]types.QueryActiveValidatorResult{})
},
}
}