github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/token/client/cli/query.go (about)

     1  package cli
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/flags"
     6  	"strings"
     7  
     8  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client"
     9  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context"
    10  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec"
    11  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth"
    12  	"github.com/fibonacci-chain/fbc/x/token/types"
    13  	"github.com/spf13/cobra"
    14  )
    15  
    16  // GetQueryCmd returns the cli query commands for this module
    17  func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
    18  	queryCmd := &cobra.Command{
    19  		Use:                        types.ModuleName,
    20  		Short:                      "Querying commands for the token module",
    21  		DisableFlagParsing:         true,
    22  		SuggestionsMinimumDistance: 2,
    23  		RunE:                       client.ValidateCmd,
    24  	}
    25  
    26  	queryCmd.AddCommand(flags.GetCommands(
    27  		getCmdQueryParams(queryRoute, cdc),
    28  		getCmdTokenInfo(queryRoute, cdc),
    29  		//getAccountCmd(queryRoute, cdc),
    30  	)...)
    31  
    32  	return queryCmd
    33  }
    34  
    35  // getCmdTokenInfo queries token info by address
    36  func getCmdTokenInfo(queryRoute string, cdc *codec.Codec) *cobra.Command {
    37  	var owner string
    38  	cmd := &cobra.Command{
    39  		Use:   "info [<symbol>]",
    40  		Short: "query token info by symbol",
    41  		//Args:  cobra.ExactArgs(1),
    42  		RunE: func(cmd *cobra.Command, args []string) error {
    43  			cliCtx := context.NewCLIContext().WithCodec(cdc)
    44  
    45  			switch {
    46  			case len(args) == 1:
    47  				symbol := args[0]
    48  				res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/info/%s", queryRoute, symbol), nil)
    49  				if err != nil {
    50  					fmt.Printf("token does not exist - %s %s\n", symbol, err.Error())
    51  					return nil
    52  				}
    53  
    54  				var token types.TokenResp
    55  				cdc.MustUnmarshalJSON(res, &token)
    56  				return cliCtx.PrintOutput(token)
    57  			case owner != "":
    58  				res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/tokens/%s", queryRoute, owner), nil)
    59  				if err != nil {
    60  					fmt.Printf("Invalid owner address - %s %s\n", owner, err.Error())
    61  					return nil
    62  				}
    63  
    64  				var tokens types.Tokens
    65  				cdc.MustUnmarshalJSON(res, &tokens)
    66  				return cliCtx.PrintOutput(tokens)
    67  			default:
    68  				fmt.Println("At least a [<symbol>] arg or an --owner flag need to be set")
    69  				err := cmd.Help()
    70  				if err != nil {
    71  					return err
    72  				}
    73  				return nil
    74  			}
    75  		},
    76  	}
    77  	cmd.Flags().StringVarP(&owner, "owner", "", "", "Get all the tokens information belong to")
    78  
    79  	return cmd
    80  }
    81  
    82  // getCmdQueryParams implements the query params command.
    83  func getCmdQueryParams(queryRoute string, cdc *codec.Codec) *cobra.Command {
    84  	return &cobra.Command{
    85  		Use:   "params",
    86  		Short: "Query the parameters of the token process",
    87  		Long: strings.TrimSpace(`Query the all the parameters for the governance process:
    88  
    89  $ fbchaincli query token params
    90  `),
    91  		Args: cobra.NoArgs,
    92  		RunE: func(cmd *cobra.Command, args []string) error {
    93  			cliCtx := context.NewCLIContext().WithCodec(cdc)
    94  
    95  			route := fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryParameters)
    96  			bz, _, err := cliCtx.QueryWithData(route, nil)
    97  			if err != nil {
    98  				return err
    99  			}
   100  
   101  			var params types.Params
   102  			cdc.MustUnmarshalJSON(bz, &params)
   103  			return cliCtx.PrintOutput(params)
   104  		},
   105  	}
   106  }
   107  
   108  // just for the object of []string could be inputted into cliCtx.PrintOutput(...)
   109  type Strings []string
   110  
   111  func (strs Strings) String() string {
   112  	return strings.Join(strs, "\n")
   113  }
   114  
   115  func getAccount(ctx *context.CLIContext, address []byte) (auth.Account, error) {
   116  	bz, err := ctx.Codec.MarshalJSON(auth.NewQueryAccountParams(address))
   117  	if err != nil {
   118  		return nil, err
   119  	}
   120  
   121  	route := fmt.Sprintf("custom/%s/%s", auth.StoreKey, auth.QueryAccount)
   122  	res, _, err := ctx.QueryWithData(route, bz)
   123  	if err != nil {
   124  		return nil, err
   125  	}
   126  
   127  	var account auth.Account
   128  	if err := ctx.Codec.UnmarshalJSON(res, &account); err != nil {
   129  		return nil, err
   130  	}
   131  	return account, nil
   132  }