github.com/KiraCore/sekai@v0.3.43/x/tokens/client/cli/query.go (about)

     1  package cli
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/spf13/cobra"
     9  
    10  	"github.com/cosmos/cosmos-sdk/client"
    11  	"github.com/cosmos/cosmos-sdk/client/flags"
    12  
    13  	"github.com/KiraCore/sekai/x/tokens/types"
    14  )
    15  
    16  // NewQueryCmd returns a root CLI command handler for all x/tokens transaction commands.
    17  func NewQueryCmd() *cobra.Command {
    18  	queryCmd := &cobra.Command{
    19  		Use:   types.RouterKey,
    20  		Short: "query commands for the tokens module",
    21  	}
    22  	queryCmd.AddCommand(
    23  		GetCmdQueryTokenAlias(),
    24  		GetCmdQueryAllTokenAliases(),
    25  		GetCmdQueryTokenAliasesByDenom(),
    26  		GetCmdQueryTokenRate(),
    27  		GetCmdQueryAllTokenRates(),
    28  		GetCmdQueryTokenRatesByDenom(),
    29  		GetCmdQueryTokenBlackWhites(),
    30  	)
    31  
    32  	return queryCmd
    33  }
    34  
    35  // GetCmdQueryTokenAlias the query token alias command.
    36  func GetCmdQueryTokenAlias() *cobra.Command {
    37  	cmd := &cobra.Command{
    38  		Use:   "alias <symbol>",
    39  		Short: "Get the token alias by symbol",
    40  		Args:  cobra.MinimumNArgs(1),
    41  		RunE: func(cmd *cobra.Command, args []string) error {
    42  			clientCtx := client.GetClientContextFromCmd(cmd)
    43  
    44  			params := &types.TokenAliasRequest{Symbol: args[0]}
    45  
    46  			queryClient := types.NewQueryClient(clientCtx)
    47  			res, err := queryClient.GetTokenAlias(context.Background(), params)
    48  			if err != nil {
    49  				return err
    50  			}
    51  
    52  			if res.Data == nil {
    53  				return fmt.Errorf("%s symbol does not exist", params.Symbol)
    54  			}
    55  
    56  			return clientCtx.PrintProto(res.Data)
    57  		},
    58  	}
    59  
    60  	flags.AddQueryFlagsToCmd(cmd)
    61  
    62  	return cmd
    63  }
    64  
    65  // GetCmdQueryAllTokenAliases the query all token aliases command.
    66  func GetCmdQueryAllTokenAliases() *cobra.Command {
    67  	cmd := &cobra.Command{
    68  		Use:   "all-aliases",
    69  		Short: "Get all token aliases",
    70  		RunE: func(cmd *cobra.Command, args []string) error {
    71  			clientCtx := client.GetClientContextFromCmd(cmd)
    72  
    73  			params := &types.AllTokenAliasesRequest{}
    74  
    75  			queryClient := types.NewQueryClient(clientCtx)
    76  			res, err := queryClient.GetAllTokenAliases(context.Background(), params)
    77  			if err != nil {
    78  				return err
    79  			}
    80  
    81  			return clientCtx.PrintProto(res)
    82  		},
    83  	}
    84  
    85  	flags.AddQueryFlagsToCmd(cmd)
    86  
    87  	return cmd
    88  }
    89  
    90  // GetCmdQueryTokenAliasesByDenom the query token aliases by denom command.
    91  func GetCmdQueryTokenAliasesByDenom() *cobra.Command {
    92  	cmd := &cobra.Command{
    93  		Use:   "aliases-by-denom [aliases]",
    94  		Short: "Get token aliases by denom",
    95  		Args:  cobra.MinimumNArgs(1),
    96  		RunE: func(cmd *cobra.Command, args []string) error {
    97  			clientCtx := client.GetClientContextFromCmd(cmd)
    98  
    99  			denoms := strings.Split(args[0], ",")
   100  			params := &types.TokenAliasesByDenomRequest{
   101  				Denoms: denoms,
   102  			}
   103  
   104  			queryClient := types.NewQueryClient(clientCtx)
   105  			res, err := queryClient.GetTokenAliasesByDenom(context.Background(), params)
   106  			if err != nil {
   107  				return err
   108  			}
   109  
   110  			return clientCtx.PrintProto(res)
   111  		},
   112  	}
   113  
   114  	flags.AddQueryFlagsToCmd(cmd)
   115  
   116  	return cmd
   117  }
   118  
   119  // GetCmdQueryTokenRate the query token rate command.
   120  func GetCmdQueryTokenRate() *cobra.Command {
   121  	cmd := &cobra.Command{
   122  		Use:   "rate <denom>",
   123  		Short: "Get the token rate by denom",
   124  		Args:  cobra.MinimumNArgs(1),
   125  		RunE: func(cmd *cobra.Command, args []string) error {
   126  			clientCtx := client.GetClientContextFromCmd(cmd)
   127  
   128  			params := &types.TokenRateRequest{Denom: args[0]}
   129  
   130  			queryClient := types.NewQueryClient(clientCtx)
   131  			res, err := queryClient.GetTokenRate(context.Background(), params)
   132  			if err != nil {
   133  				return err
   134  			}
   135  
   136  			if res.Data == nil {
   137  				return fmt.Errorf("%s denom does not exist", params.Denom)
   138  			}
   139  
   140  			return clientCtx.PrintProto(res.Data)
   141  		},
   142  	}
   143  
   144  	flags.AddQueryFlagsToCmd(cmd)
   145  
   146  	return cmd
   147  }
   148  
   149  // GetCmdQueryAllTokenRates the query all token rates command.
   150  func GetCmdQueryAllTokenRates() *cobra.Command {
   151  	cmd := &cobra.Command{
   152  		Use:   "all-rates",
   153  		Short: "Get all token rates",
   154  		RunE: func(cmd *cobra.Command, args []string) error {
   155  			clientCtx := client.GetClientContextFromCmd(cmd)
   156  
   157  			params := &types.AllTokenRatesRequest{}
   158  
   159  			queryClient := types.NewQueryClient(clientCtx)
   160  			res, err := queryClient.GetAllTokenRates(context.Background(), params)
   161  			if err != nil {
   162  				return err
   163  			}
   164  
   165  			return clientCtx.PrintProto(res)
   166  		},
   167  	}
   168  
   169  	flags.AddQueryFlagsToCmd(cmd)
   170  
   171  	return cmd
   172  }
   173  
   174  // GetCmdQueryTokenRatesByDenom the query token aliases by denom command.
   175  func GetCmdQueryTokenRatesByDenom() *cobra.Command {
   176  	cmd := &cobra.Command{
   177  		Use:   "rates-by-denom",
   178  		Short: "Get token rates by denom",
   179  		Args:  cobra.MinimumNArgs(1),
   180  		RunE: func(cmd *cobra.Command, args []string) error {
   181  			clientCtx := client.GetClientContextFromCmd(cmd)
   182  
   183  			denoms := strings.Split(args[0], ",")
   184  			params := &types.TokenRatesByDenomRequest{
   185  				Denoms: denoms,
   186  			}
   187  
   188  			queryClient := types.NewQueryClient(clientCtx)
   189  			res, err := queryClient.GetTokenRatesByDenom(context.Background(), params)
   190  			if err != nil {
   191  				return err
   192  			}
   193  
   194  			return clientCtx.PrintProto(res)
   195  		},
   196  	}
   197  
   198  	flags.AddQueryFlagsToCmd(cmd)
   199  
   200  	return cmd
   201  }
   202  
   203  // GetCmdQueryTokenBlackWhites the query token blacklists / whitelists
   204  func GetCmdQueryTokenBlackWhites() *cobra.Command {
   205  	cmd := &cobra.Command{
   206  		Use:   "token-black-whites",
   207  		Short: "Get token black whites",
   208  		Args:  cobra.MinimumNArgs(0),
   209  		RunE: func(cmd *cobra.Command, args []string) error {
   210  			clientCtx := client.GetClientContextFromCmd(cmd)
   211  			params := &types.TokenBlackWhitesRequest{}
   212  
   213  			queryClient := types.NewQueryClient(clientCtx)
   214  			res, err := queryClient.GetTokenBlackWhites(context.Background(), params)
   215  			if err != nil {
   216  				return err
   217  			}
   218  
   219  			return clientCtx.PrintProto(res)
   220  		},
   221  	}
   222  
   223  	flags.AddQueryFlagsToCmd(cmd)
   224  
   225  	return cmd
   226  }