diff --git a/x/interchainqueries/client/cli/query.go b/x/interchainqueries/client/cli/query.go index ec7f848fc..52033914e 100644 --- a/x/interchainqueries/client/cli/query.go +++ b/x/interchainqueries/client/cli/query.go @@ -3,6 +3,8 @@ package cli import ( "context" "fmt" + "strconv" + // "strings" "github.com/cosmos/cosmos-sdk/client/flags" @@ -28,6 +30,7 @@ func GetQueryCmd(queryRoute string) *cobra.Command { cmd.AddCommand(CmdQueryParams()) cmd.AddCommand(CmdQueryRegisteredQueries()) + cmd.AddCommand(CmdQueryRegisteredQueryResult()) // this line is used by starport scaffolding # 1 return cmd @@ -56,3 +59,32 @@ func CmdQueryRegisteredQueries() *cobra.Command { return cmd } + +func CmdQueryRegisteredQueryResult() *cobra.Command { + cmd := &cobra.Command{ + Use: "query-result [query-id]", + Short: "queries result for registered query", + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + clientCtx := client.GetClientContextFromCmd(cmd) + + queryClient := types.NewQueryClient(clientCtx) + + queryID, err := strconv.ParseUint(args[0], 10, 64) + if err != nil { + return fmt.Errorf("failed to parse query id: %w", err) + } + + res, err := queryClient.QueryResult(context.Background(), &types.QueryRegisteredQueryResultRequest{QueryId: queryID}) + if err != nil { + return err + } + + return clientCtx.PrintProto(res) + }, + } + + flags.AddQueryFlagsToCmd(cmd) + + return cmd +} diff --git a/x/interchainqueries/client/cli/tx.go b/x/interchainqueries/client/cli/tx.go index 681da54c3..5c67fd972 100644 --- a/x/interchainqueries/client/cli/tx.go +++ b/x/interchainqueries/client/cli/tx.go @@ -1,7 +1,9 @@ package cli import ( + "encoding/json" "fmt" + "io/ioutil" "strconv" "github.com/cosmos/cosmos-sdk/client/flags" @@ -24,6 +26,7 @@ func GetTxCmd() *cobra.Command { } cmd.AddCommand(RegisterInterchainQueryCmd()) + cmd.AddCommand(SubmitQueryResultCmd()) return cmd } @@ -69,3 +72,46 @@ func RegisterInterchainQueryCmd() *cobra.Command { return cmd } + +func SubmitQueryResultCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "submit-query-result [query-id] [result-file]", + Short: "Submit query result", + Aliases: []string{"submit", "s"}, + Args: cobra.ExactArgs(5), + RunE: func(cmd *cobra.Command, args []string) error { + clientCtx, err := client.GetClientTxContext(cmd) + if err != nil { + return err + } + + sender := clientCtx.GetFromAddress() + queryID, err := strconv.ParseUint(args[0], 10, 64) + if err != nil { + return fmt.Errorf("failed to parse query id: %w", err) + } + + resultFile := args[1] + + result, err := ioutil.ReadFile(resultFile) + if err != nil { + return fmt.Errorf("failed to read query result file: %w", err) + } + + msg := types.MsgSubmitQueryResult{QueryId: queryID, Sender: string(sender)} + if err := json.Unmarshal(result, &msg.Result); err != nil { + return fmt.Errorf("failed to unmarshal query result: %w", err) + } + + if err = msg.ValidateBasic(); err != nil { + return err + } + + return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg) + }, + } + + flags.AddTxFlagsToCmd(cmd) + + return cmd +}