Skip to content

Commit

Permalink
CLI commands for submitting query result and getting query result
Browse files Browse the repository at this point in the history
  • Loading branch information
pr0n00gler committed May 23, 2022
1 parent 08b9b29 commit fad4bbd
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
32 changes: 32 additions & 0 deletions x/interchainqueries/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package cli
import (
"context"
"fmt"
"strconv"

// "strings"

"github.com/cosmos/cosmos-sdk/client/flags"
Expand All @@ -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
Expand Down Expand Up @@ -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
}
46 changes: 46 additions & 0 deletions x/interchainqueries/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package cli

import (
"encoding/json"
"fmt"
"io/ioutil"
"strconv"

"github.com/cosmos/cosmos-sdk/client/flags"
Expand All @@ -24,6 +26,7 @@ func GetTxCmd() *cobra.Command {
}

cmd.AddCommand(RegisterInterchainQueryCmd())
cmd.AddCommand(SubmitQueryResultCmd())

return cmd
}
Expand Down Expand Up @@ -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
}

0 comments on commit fad4bbd

Please sign in to comment.