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

     1  package cli
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context"
     6  	client "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/flags"
     7  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec"
     8  	"github.com/fibonacci-chain/fbc/x/order/keeper"
     9  	"github.com/fibonacci-chain/fbc/x/order/types"
    10  	"github.com/spf13/cobra"
    11  	"github.com/spf13/viper"
    12  	"strings"
    13  )
    14  
    15  // GetQueryCmd returns the cli query commands for this module
    16  func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
    17  	// Group order queries under a subcommand
    18  	queryCmd := &cobra.Command{
    19  		Use:   "order",
    20  		Short: "Querying commands for the order module",
    21  	}
    22  
    23  	queryCmd.AddCommand(client.GetCommands(
    24  		GetCmdQueryOrder(queryRoute, cdc),
    25  		GetCmdDepthBook(queryRoute, cdc),
    26  		GetCmdQueryStore(queryRoute, cdc),
    27  		GetCmdQueryParams(queryRoute, cdc),
    28  	)...)
    29  
    30  	queryCmd.Flags().StringP(client.FlagNode, "n", "tcp://localhost:26657", "Node to connect to")
    31  	return queryCmd
    32  }
    33  
    34  // GetCmdQueryOrder queries order info by orderID
    35  func GetCmdQueryOrder(queryRoute string, cdc *codec.Codec) *cobra.Command {
    36  	return &cobra.Command{
    37  		Use:   "detail [order-id]",
    38  		Short: "Query an order",
    39  		Args:  cobra.ExactArgs(1),
    40  		RunE: func(cmd *cobra.Command, args []string) error {
    41  			cliCtx := context.NewCLIContext().WithCodec(cdc)
    42  			orderID := args[0]
    43  
    44  			res, _, err := cliCtx.QueryWithData(
    45  				fmt.Sprintf("custom/%s/%s/%s", queryRoute, types.QueryOrderDetail, orderID),
    46  				nil)
    47  			if err != nil {
    48  				fmt.Printf("order does not exist - %s \n", orderID)
    49  				return nil
    50  			}
    51  			fmt.Println(string(res))
    52  			return nil
    53  		},
    54  	}
    55  }
    56  
    57  // GetCmdDepthBook queries order book about a product
    58  func GetCmdDepthBook(queryRoute string, cdc *codec.Codec) *cobra.Command {
    59  	cmd := &cobra.Command{
    60  		Use:   "depthbook [product]",
    61  		Short: "Query the depth book of a trading pair",
    62  		Long: strings.TrimSpace(`Query the depth book of a trading pair:
    63  
    64  $ fbchaincli query depthbook mytoken_fibo
    65  
    66  The 'product' is a trading pair in full name of the tokens: ${base-asset-symbol}_${quote-asset-symbol}, for example 'mytoken_fibo'.
    67  `),
    68  		Args: cobra.ExactArgs(1),
    69  		RunE: func(cmd *cobra.Command, args []string) error {
    70  			cliCtx := context.NewCLIContext().WithCodec(cdc)
    71  			product := args[0]
    72  			size := viper.GetUint("size")
    73  			params := keeper.NewQueryDepthBookParams(product, size)
    74  			bz, err := cdc.MarshalJSON(params)
    75  			if err != nil {
    76  				return err
    77  			}
    78  
    79  			res, _, err := cliCtx.QueryWithData(
    80  				fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryDepthBook),
    81  				bz)
    82  			if err != nil {
    83  				fmt.Printf("get depth book of %s failed: %v\n", product, err.Error())
    84  				return nil
    85  			}
    86  
    87  			fmt.Println(string(res))
    88  			return nil
    89  		},
    90  	}
    91  	cmd.Flags().Uint("size", keeper.DefaultBookSize, "depth book single-side size")
    92  	return cmd
    93  }
    94  
    95  // GetCmdQueryStore queries store statistic
    96  func GetCmdQueryStore(queryRoute string, cdc *codec.Codec) *cobra.Command {
    97  	cmd := &cobra.Command{
    98  		Use:   "store",
    99  		Short: "query store of order module",
   100  		RunE: func(cmd *cobra.Command, args []string) error {
   101  			dbFile := viper.GetString("app_dbpath")
   102  			//dump := viper.GetBool("dump")
   103  			if dbFile != "" {
   104  				//// query through db file
   105  				//mapp, db, err := filedb.GetMockApp(dbFile)
   106  				//if err != nil {
   107  				//	return err
   108  				//}
   109  				//defer filedb.CloseApp(db)
   110  				//
   111  				//kp := mapp.OrderKeeper
   112  				//mapp.BeginBlock(abci.RequestBeginBlock{})
   113  				//ctx := mapp.GetState(baseapp.RunTxModeDeliver()).Context()
   114  				//if dump {
   115  				//	keeper.DumpStore(ctx, kp)
   116  				//	return nil
   117  				//}
   118  				//
   119  				//ss := keeper.GetStoreStatistic(ctx, kp)
   120  				//res, errRes := codec.MarshalJSONIndent(cdc, ss)
   121  				//if errRes != nil {
   122  				//	return sdk.ErrInternal(
   123  				//		sdk.AppendMsgToErr("could not marshal result to JSON", errRes.Error()))
   124  				//}
   125  				//fmt.Println(string(res))
   126  			} else {
   127  				cliCtx := context.NewCLIContext().WithCodec(cdc)
   128  				res, _, err := cliCtx.QueryWithData(
   129  					fmt.Sprintf("custom/order/%s", types.QueryStore), nil)
   130  				if err != nil {
   131  					fmt.Printf("query store failed: %v\n", err.Error())
   132  					return nil
   133  				}
   134  
   135  				fmt.Println(string(res))
   136  				return nil
   137  			}
   138  			return nil
   139  		},
   140  	}
   141  	cmd.Flags().String("dbpath", "", "db path (if this path is given, query through local file)")
   142  	cmd.Flags().Bool("dump", false, "dump all key-value constants of specified module")
   143  	return cmd
   144  }
   145  
   146  // GetCmdQueryParams implements the query params command.
   147  func GetCmdQueryParams(queryRoute string, cdc *codec.Codec) *cobra.Command {
   148  	return &cobra.Command{
   149  		Use:   "params",
   150  		Short: "Query the parameters of the order process",
   151  		Long: strings.TrimSpace(`Query the all the parameters for the governance process:
   152  
   153  $ fbchaincli query order params
   154  `),
   155  		Args: cobra.NoArgs,
   156  		RunE: func(cmd *cobra.Command, args []string) error {
   157  			cliCtx := context.NewCLIContext().WithCodec(cdc)
   158  
   159  			route := fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryParameters)
   160  			bz, _, err := cliCtx.QueryWithData(route, nil)
   161  			if err != nil {
   162  				return err
   163  			}
   164  
   165  			var params types.Params
   166  			cdc.MustUnmarshalJSON(bz, &params)
   167  			return cliCtx.PrintOutput(params)
   168  		},
   169  	}
   170  }