github.com/Finschia/finschia-sdk@v0.49.1/x/fbridge/client/cli/query.go (about)

     1  package cli
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  
     7  	"github.com/spf13/cobra"
     8  
     9  	"github.com/Finschia/finschia-sdk/client"
    10  	"github.com/Finschia/finschia-sdk/client/flags"
    11  	"github.com/Finschia/finschia-sdk/version"
    12  	"github.com/Finschia/finschia-sdk/x/fbridge/types"
    13  )
    14  
    15  const (
    16  	flagSequences = "sequences"
    17  )
    18  
    19  // NewQueryCmd returns the query commands for fbridge module
    20  func NewQueryCmd() *cobra.Command {
    21  	cmd := &cobra.Command{
    22  		Use:   types.ModuleName,
    23  		Short: "Querying commands for the fbridge module",
    24  	}
    25  
    26  	cmd.AddCommand(
    27  		NewQueryParamsCmd(),
    28  		NewQueryNextSeqSendCmd(),
    29  		NewQuerySeqToBlocknumsCmd(),
    30  		NewQueryMembersCmd(),
    31  		NewQueryMemberCmd(),
    32  		NewQueryProposalsCmd(),
    33  		NewQueryProposalCmd(),
    34  		NewQueryVotesCmd(),
    35  		NewQueryVoteCmd(),
    36  		NewQueryBridgeStatusCmd(),
    37  	)
    38  
    39  	return cmd
    40  }
    41  
    42  func NewQueryParamsCmd() *cobra.Command {
    43  	cmd := &cobra.Command{
    44  		Use:   "params",
    45  		Short: "Query the current fbridge module parameters",
    46  		Args:  cobra.NoArgs,
    47  		RunE: func(cmd *cobra.Command, args []string) error {
    48  			clientCtx, err := client.GetClientQueryContext(cmd)
    49  			if err != nil {
    50  				return err
    51  			}
    52  			qc := types.NewQueryClient(clientCtx)
    53  			res, err := qc.Params(cmd.Context(), &types.QueryParamsRequest{})
    54  			if err != nil {
    55  				return err
    56  			}
    57  
    58  			return clientCtx.PrintProto(res)
    59  		},
    60  	}
    61  
    62  	flags.AddQueryFlagsToCmd(cmd)
    63  	return cmd
    64  }
    65  
    66  func NewQueryNextSeqSendCmd() *cobra.Command {
    67  	cmd := &cobra.Command{
    68  		Use:   "sending-next-seq",
    69  		Short: "Query the next sequence number for sending",
    70  		Args:  cobra.NoArgs,
    71  		RunE: func(cmd *cobra.Command, args []string) error {
    72  			clientCtx, err := client.GetClientQueryContext(cmd)
    73  			if err != nil {
    74  				return err
    75  			}
    76  			qc := types.NewQueryClient(clientCtx)
    77  			res, err := qc.NextSeqSend(cmd.Context(), &types.QueryNextSeqSendRequest{})
    78  			if err != nil {
    79  				return err
    80  			}
    81  
    82  			return clientCtx.PrintProto(res)
    83  		},
    84  	}
    85  
    86  	flags.AddQueryFlagsToCmd(cmd)
    87  	return cmd
    88  }
    89  
    90  func NewQuerySeqToBlocknumsCmd() *cobra.Command {
    91  	cmd := &cobra.Command{
    92  		Use:     "seq-to-blocknums",
    93  		Short:   "Query the block number for given sequence numbers",
    94  		Args:    cobra.NoArgs,
    95  		Example: fmt.Sprintf("%s query %s seq-to-blocknums --sequences=1,2,3", version.AppName, types.ModuleName),
    96  		RunE: func(cmd *cobra.Command, args []string) error {
    97  			clientCtx, err := client.GetClientQueryContext(cmd)
    98  			if err != nil {
    99  				return err
   100  			}
   101  			qc := types.NewQueryClient(clientCtx)
   102  
   103  			seqSlice, err := cmd.Flags().GetInt64Slice(flagSequences)
   104  			if err != nil {
   105  				return err
   106  			}
   107  
   108  			seqs := make([]uint64, len(seqSlice))
   109  			for i, seq := range seqSlice {
   110  				seqs[i] = uint64(seq)
   111  			}
   112  
   113  			res, err := qc.SeqToBlocknums(cmd.Context(), &types.QuerySeqToBlocknumsRequest{Seqs: seqs})
   114  			if err != nil {
   115  				return err
   116  			}
   117  
   118  			return clientCtx.PrintProto(res)
   119  		},
   120  	}
   121  
   122  	cmd.Flags().Int64Slice(flagSequences, []int64{}, "comma separated list of bridge sequnece numbers")
   123  	flags.AddQueryFlagsToCmd(cmd)
   124  	return cmd
   125  }
   126  
   127  func NewQueryMembersCmd() *cobra.Command {
   128  	cmd := &cobra.Command{
   129  		Use:     "members [role]",
   130  		Short:   "Query the members of spcific group registered on the bridge (guardian|operator|judge)",
   131  		Args:    cobra.ExactArgs(1),
   132  		Example: fmt.Sprintf("%s query %s members guardian", version.AppName, types.ModuleName),
   133  		RunE: func(cmd *cobra.Command, args []string) error {
   134  			clientCtx, err := client.GetClientQueryContext(cmd)
   135  			if err != nil {
   136  				return err
   137  			}
   138  			qc := types.NewQueryClient(clientCtx)
   139  
   140  			res, err := qc.Members(cmd.Context(), &types.QueryMembersRequest{Role: args[0]})
   141  			if err != nil {
   142  				return err
   143  			}
   144  
   145  			return clientCtx.PrintProto(res)
   146  		},
   147  	}
   148  
   149  	flags.AddQueryFlagsToCmd(cmd)
   150  	return cmd
   151  }
   152  
   153  func NewQueryMemberCmd() *cobra.Command {
   154  	cmd := &cobra.Command{
   155  		Use:     "member [address]",
   156  		Short:   "Query the roles of a specific member registered on the bridge",
   157  		Args:    cobra.ExactArgs(1),
   158  		Example: fmt.Sprintf("%s query %s member link1...", version.AppName, types.ModuleName),
   159  		RunE: func(cmd *cobra.Command, args []string) error {
   160  			clientCtx, err := client.GetClientQueryContext(cmd)
   161  			if err != nil {
   162  				return err
   163  			}
   164  			qc := types.NewQueryClient(clientCtx)
   165  
   166  			res, err := qc.Member(cmd.Context(), &types.QueryMemberRequest{Address: args[0]})
   167  			if err != nil {
   168  				return err
   169  			}
   170  
   171  			return clientCtx.PrintProto(res)
   172  		},
   173  	}
   174  
   175  	flags.AddQueryFlagsToCmd(cmd)
   176  	return cmd
   177  }
   178  
   179  func NewQueryProposalsCmd() *cobra.Command {
   180  	cmd := &cobra.Command{
   181  		Use:     "proposals",
   182  		Short:   "Query all role proposals",
   183  		Args:    cobra.NoArgs,
   184  		Example: fmt.Sprintf("%s query %s proposals", version.AppName, types.ModuleName),
   185  		RunE: func(cmd *cobra.Command, args []string) error {
   186  			clientCtx, err := client.GetClientQueryContext(cmd)
   187  			if err != nil {
   188  				return err
   189  			}
   190  			qc := types.NewQueryClient(clientCtx)
   191  			pageReq, err := client.ReadPageRequest(cmd.Flags())
   192  			if err != nil {
   193  				return err
   194  			}
   195  
   196  			res, err := qc.Proposals(cmd.Context(), &types.QueryProposalsRequest{Pagination: pageReq})
   197  			if err != nil {
   198  				return err
   199  			}
   200  
   201  			return clientCtx.PrintProto(res)
   202  		},
   203  	}
   204  
   205  	flags.AddQueryFlagsToCmd(cmd)
   206  	flags.AddPaginationFlagsToCmd(cmd, "all proposals")
   207  	return cmd
   208  }
   209  
   210  func NewQueryProposalCmd() *cobra.Command {
   211  	cmd := &cobra.Command{
   212  		Use:     "proposal [proposal_id]",
   213  		Short:   "Query a specific role proposal",
   214  		Args:    cobra.ExactArgs(1),
   215  		Example: fmt.Sprintf("%s query %s proposal 1", version.AppName, types.ModuleName),
   216  		RunE: func(cmd *cobra.Command, args []string) error {
   217  			clientCtx, err := client.GetClientQueryContext(cmd)
   218  			if err != nil {
   219  				return err
   220  			}
   221  			qc := types.NewQueryClient(clientCtx)
   222  
   223  			id, err := strconv.ParseUint(args[0], 10, 64)
   224  			if err != nil {
   225  				return err
   226  			}
   227  
   228  			res, err := qc.Proposal(cmd.Context(), &types.QueryProposalRequest{ProposalId: id})
   229  			if err != nil {
   230  				return err
   231  			}
   232  
   233  			return clientCtx.PrintProto(res)
   234  		},
   235  	}
   236  
   237  	flags.AddQueryFlagsToCmd(cmd)
   238  	return cmd
   239  }
   240  
   241  func NewQueryVotesCmd() *cobra.Command {
   242  	cmd := &cobra.Command{
   243  		Use:     "votes [proposal_id]",
   244  		Short:   "Query all votes for a specific role proposal",
   245  		Args:    cobra.ExactArgs(1),
   246  		Example: fmt.Sprintf("%s query %s votes 1", version.AppName, types.ModuleName),
   247  		RunE: func(cmd *cobra.Command, args []string) error {
   248  			clientCtx, err := client.GetClientQueryContext(cmd)
   249  			if err != nil {
   250  				return err
   251  			}
   252  			qc := types.NewQueryClient(clientCtx)
   253  
   254  			id, err := strconv.ParseUint(args[0], 10, 64)
   255  			if err != nil {
   256  				return err
   257  			}
   258  
   259  			res, err := qc.Votes(cmd.Context(), &types.QueryVotesRequest{ProposalId: id})
   260  			if err != nil {
   261  				return err
   262  			}
   263  
   264  			return clientCtx.PrintProto(res)
   265  		},
   266  	}
   267  
   268  	flags.AddQueryFlagsToCmd(cmd)
   269  	return cmd
   270  }
   271  
   272  func NewQueryVoteCmd() *cobra.Command {
   273  	cmd := &cobra.Command{
   274  		Use:     "vote [proposal_id] [voter]",
   275  		Short:   "Query a specific vote for a role proposal",
   276  		Args:    cobra.ExactArgs(2),
   277  		Example: fmt.Sprintf("%s query %s vote 1 link1...", version.AppName, types.ModuleName),
   278  		RunE: func(cmd *cobra.Command, args []string) error {
   279  			clientCtx, err := client.GetClientQueryContext(cmd)
   280  			if err != nil {
   281  				return err
   282  			}
   283  			qc := types.NewQueryClient(clientCtx)
   284  
   285  			id, err := strconv.ParseUint(args[0], 10, 64)
   286  			if err != nil {
   287  				return err
   288  			}
   289  
   290  			res, err := qc.Vote(cmd.Context(), &types.QueryVoteRequest{ProposalId: id, Voter: args[1]})
   291  			if err != nil {
   292  				return err
   293  			}
   294  
   295  			return clientCtx.PrintProto(res)
   296  		},
   297  	}
   298  
   299  	flags.AddQueryFlagsToCmd(cmd)
   300  	return cmd
   301  }
   302  
   303  func NewQueryBridgeStatusCmd() *cobra.Command {
   304  	cmd := &cobra.Command{
   305  		Use:   "status",
   306  		Short: "Query the current status of the bridge",
   307  		Args:  cobra.NoArgs,
   308  		RunE: func(cmd *cobra.Command, args []string) error {
   309  			clientCtx, err := client.GetClientQueryContext(cmd)
   310  			if err != nil {
   311  				return err
   312  			}
   313  			qc := types.NewQueryClient(clientCtx)
   314  			res, err := qc.BridgeStatus(cmd.Context(), &types.QueryBridgeStatusRequest{})
   315  			if err != nil {
   316  				return err
   317  			}
   318  			return clientCtx.PrintProto(res)
   319  		},
   320  	}
   321  
   322  	flags.AddQueryFlagsToCmd(cmd)
   323  	return cmd
   324  }