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

     1  package cli
     2  
     3  import (
     4  	"context"
     5  	"strconv"
     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/basket/types"
    14  )
    15  
    16  // NewQueryCmd returns a root CLI command handler for all x/basket transaction commands.
    17  func NewQueryCmd() *cobra.Command {
    18  	queryCmd := &cobra.Command{
    19  		Use:   types.RouterKey,
    20  		Short: "query commands for the basket module",
    21  	}
    22  	queryCmd.AddCommand(
    23  		GetCmdQueryTokenBasketById(),
    24  		GetCmdQueryTokenBasketByDenom(),
    25  		GetCmdQueryTokenBaskets(),
    26  		GetCmdQueryBaksetHistoricalMints(),
    27  		GetCmdQueryBaksetHistoricalBurns(),
    28  		GetCmdQueryBaksetHistoricalSwaps(),
    29  	)
    30  
    31  	return queryCmd
    32  }
    33  
    34  func GetCmdQueryTokenBasketById() *cobra.Command {
    35  	cmd := &cobra.Command{
    36  		Use:   "token-basket-by-id [id]",
    37  		Short: "Queries a single basket by id",
    38  		Args:  cobra.MinimumNArgs(1),
    39  		RunE: func(cmd *cobra.Command, args []string) error {
    40  			clientCtx := client.GetClientContextFromCmd(cmd)
    41  
    42  			id, err := strconv.Atoi(args[0])
    43  			if err != nil {
    44  				return err
    45  			}
    46  			queryClient := types.NewQueryClient(clientCtx)
    47  			res, err := queryClient.TokenBasketById(context.Background(), &types.QueryTokenBasketByIdRequest{
    48  				Id: uint64(id),
    49  			})
    50  			if err != nil {
    51  				return err
    52  			}
    53  
    54  			return clientCtx.PrintProto(res)
    55  		},
    56  	}
    57  
    58  	flags.AddQueryFlagsToCmd(cmd)
    59  
    60  	return cmd
    61  }
    62  
    63  func GetCmdQueryTokenBasketByDenom() *cobra.Command {
    64  	cmd := &cobra.Command{
    65  		Use:   "token-basket-by-denom [denom]",
    66  		Short: "Queries a single basket by denom",
    67  		Args:  cobra.MinimumNArgs(1),
    68  		RunE: func(cmd *cobra.Command, args []string) error {
    69  			clientCtx := client.GetClientContextFromCmd(cmd)
    70  
    71  			queryClient := types.NewQueryClient(clientCtx)
    72  			res, err := queryClient.TokenBasketByDenom(context.Background(), &types.QueryTokenBasketByDenomRequest{
    73  				Denom: args[0],
    74  			})
    75  			if err != nil {
    76  				return err
    77  			}
    78  
    79  			return clientCtx.PrintProto(res)
    80  		},
    81  	}
    82  
    83  	flags.AddQueryFlagsToCmd(cmd)
    84  
    85  	return cmd
    86  }
    87  
    88  func GetCmdQueryTokenBaskets() *cobra.Command {
    89  	cmd := &cobra.Command{
    90  		Use:   "token-baskets [tokens] [derivatives_only]",
    91  		Short: "Queries token baskets by filter",
    92  		Args:  cobra.MinimumNArgs(2),
    93  		RunE: func(cmd *cobra.Command, args []string) error {
    94  			clientCtx := client.GetClientContextFromCmd(cmd)
    95  
    96  			tokens := []string{}
    97  			if args[0] != "" {
    98  				tokens = strings.Split(args[0], ",")
    99  			}
   100  			derivativesOnly, err := strconv.ParseBool(args[1])
   101  			if err != nil {
   102  				return err
   103  			}
   104  
   105  			queryClient := types.NewQueryClient(clientCtx)
   106  			res, err := queryClient.TokenBaskets(context.Background(), &types.QueryTokenBasketsRequest{
   107  				Tokens:          tokens,
   108  				DerivativesOnly: derivativesOnly,
   109  			})
   110  			if err != nil {
   111  				return err
   112  			}
   113  
   114  			return clientCtx.PrintProto(res)
   115  		},
   116  	}
   117  
   118  	flags.AddQueryFlagsToCmd(cmd)
   119  
   120  	return cmd
   121  }
   122  
   123  func GetCmdQueryBaksetHistoricalMints() *cobra.Command {
   124  	cmd := &cobra.Command{
   125  		Use:   "historical-mints [basket_id]",
   126  		Short: "Queries basket historical mints on limited period",
   127  		Args:  cobra.MinimumNArgs(1),
   128  		RunE: func(cmd *cobra.Command, args []string) error {
   129  			clientCtx := client.GetClientContextFromCmd(cmd)
   130  
   131  			basketId, err := strconv.Atoi(args[0])
   132  			if err != nil {
   133  				return err
   134  			}
   135  
   136  			queryClient := types.NewQueryClient(clientCtx)
   137  			res, err := queryClient.BaksetHistoricalMints(context.Background(), &types.QueryBasketHistoricalMintsRequest{
   138  				BasketId: uint64(basketId),
   139  			})
   140  			if err != nil {
   141  				return err
   142  			}
   143  
   144  			return clientCtx.PrintProto(res)
   145  		},
   146  	}
   147  
   148  	flags.AddQueryFlagsToCmd(cmd)
   149  
   150  	return cmd
   151  }
   152  
   153  func GetCmdQueryBaksetHistoricalBurns() *cobra.Command {
   154  	cmd := &cobra.Command{
   155  		Use:   "historical-burns [basket_id]",
   156  		Short: "Queries basket historical burns on limited period",
   157  		Args:  cobra.MinimumNArgs(1),
   158  		RunE: func(cmd *cobra.Command, args []string) error {
   159  			clientCtx := client.GetClientContextFromCmd(cmd)
   160  
   161  			basketId, err := strconv.Atoi(args[0])
   162  			if err != nil {
   163  				return err
   164  			}
   165  
   166  			queryClient := types.NewQueryClient(clientCtx)
   167  			res, err := queryClient.BaksetHistoricalBurns(context.Background(), &types.QueryBasketHistoricalBurnsRequest{
   168  				BasketId: uint64(basketId),
   169  			})
   170  			if err != nil {
   171  				return err
   172  			}
   173  
   174  			return clientCtx.PrintProto(res)
   175  		},
   176  	}
   177  
   178  	flags.AddQueryFlagsToCmd(cmd)
   179  
   180  	return cmd
   181  }
   182  
   183  func GetCmdQueryBaksetHistoricalSwaps() *cobra.Command {
   184  	cmd := &cobra.Command{
   185  		Use:   "historical-swaps [basket_id]",
   186  		Short: "Queries basket historical swaps on limited period",
   187  		Args:  cobra.MinimumNArgs(1),
   188  		RunE: func(cmd *cobra.Command, args []string) error {
   189  			clientCtx := client.GetClientContextFromCmd(cmd)
   190  
   191  			basketId, err := strconv.Atoi(args[0])
   192  			if err != nil {
   193  				return err
   194  			}
   195  
   196  			queryClient := types.NewQueryClient(clientCtx)
   197  			res, err := queryClient.BaksetHistoricalSwaps(context.Background(), &types.QueryBasketHistoricalSwapsRequest{
   198  				BasketId: uint64(basketId),
   199  			})
   200  			if err != nil {
   201  				return err
   202  			}
   203  
   204  			return clientCtx.PrintProto(res)
   205  		},
   206  	}
   207  
   208  	flags.AddQueryFlagsToCmd(cmd)
   209  
   210  	return cmd
   211  }