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

     1  package cli
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     8  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/version"
     9  	"github.com/spf13/cobra"
    10  
    11  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context"
    12  	client "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/flags"
    13  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec"
    14  	"github.com/fibonacci-chain/fbc/x/farm/types"
    15  )
    16  
    17  // GetQueryCmd returns the cli query commands for this module
    18  func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
    19  	// Group farm queries under a subcommand
    20  	farmQueryCmd := &cobra.Command{
    21  		Use:                        types.ModuleName,
    22  		Short:                      fmt.Sprintf("Querying commands for the %s module", types.ModuleName),
    23  		DisableFlagParsing:         true,
    24  		SuggestionsMinimumDistance: 2,
    25  	}
    26  
    27  	farmQueryCmd.AddCommand(
    28  		client.GetCommands(
    29  			GetCmdQueryPool(queryRoute, cdc),
    30  			GetCmdQueryPools(queryRoute, cdc),
    31  			GetCmdQueryPoolNum(queryRoute, cdc),
    32  			GetCmdQueryLockInfo(queryRoute, cdc),
    33  			GetCmdQueryEarnings(queryRoute, cdc),
    34  			GetCmdQueryAccount(queryRoute, cdc),
    35  			GetCmdQueryAccountsLockedTo(queryRoute, cdc),
    36  			GetCmdQueryWhitelist(queryRoute, cdc),
    37  			GetCmdQueryParams(queryRoute, cdc),
    38  		)...,
    39  	)
    40  
    41  	return farmQueryCmd
    42  }
    43  
    44  // GetCmdQueryPool gets the pool query command.
    45  func GetCmdQueryPool(storeName string, cdc *codec.Codec) *cobra.Command {
    46  	return &cobra.Command{
    47  		Use:   "pool [pool-name]",
    48  		Short: "query a pool",
    49  		Long: strings.TrimSpace(
    50  			fmt.Sprintf(`Query details about the kind of coins as reward, the balance and the amount to farm in one block.
    51  
    52  Example:
    53  $ %s query farm pool pool-eth-xxb
    54  `,
    55  				version.ClientName,
    56  			),
    57  		),
    58  		Args: cobra.ExactArgs(1),
    59  		RunE: func(cmd *cobra.Command, args []string) error {
    60  			cliCtx := context.NewCLIContext().WithCodec(cdc)
    61  
    62  			bytes, err := cdc.MarshalJSON(types.NewQueryPoolParams(args[0]))
    63  			if err != nil {
    64  				return err
    65  			}
    66  
    67  			route := fmt.Sprintf("custom/%s/%s", storeName, types.QueryPool)
    68  			resp, _, err := cliCtx.QueryWithData(route, bytes)
    69  			if err != nil {
    70  				return err
    71  			}
    72  
    73  			var pool types.FarmPool
    74  			cdc.MustUnmarshalJSON(resp, &pool)
    75  			return cliCtx.PrintOutput(pool)
    76  		},
    77  	}
    78  }
    79  
    80  // GetCmdQueryPools gets the pools query command.
    81  func GetCmdQueryPools(storeName string, cdc *codec.Codec) *cobra.Command {
    82  	return &cobra.Command{
    83  		Use:   "pools",
    84  		Short: "query for all pools",
    85  		Long: strings.TrimSpace(
    86  			fmt.Sprintf(`Query details about all pools.
    87  
    88  Example:
    89  $ %s query farm pools
    90  `,
    91  				version.ClientName,
    92  			),
    93  		),
    94  		Args: cobra.NoArgs,
    95  		RunE: func(cmd *cobra.Command, _ []string) error {
    96  			cliCtx := context.NewCLIContext().WithCodec(cdc)
    97  
    98  			// fixed to all pools query
    99  			jsonBytes, err := cdc.MarshalJSON(types.NewQueryPoolsParams(1, 0))
   100  			if err != nil {
   101  				return err
   102  			}
   103  
   104  			route := fmt.Sprintf("custom/%s/%s", storeName, types.QueryPools)
   105  			bz, _, err := cliCtx.QueryWithData(route, jsonBytes)
   106  			if err != nil {
   107  				return err
   108  			}
   109  
   110  			var pools types.FarmPools
   111  			cdc.MustUnmarshalJSON(bz, &pools)
   112  			return cliCtx.PrintOutput(pools)
   113  		},
   114  	}
   115  }
   116  
   117  // GetCmdQueryEarnings gets the earnings query command.
   118  func GetCmdQueryEarnings(storeName string, cdc *codec.Codec) *cobra.Command {
   119  	return &cobra.Command{
   120  		Use:   "rewards [pool-name] [address]",
   121  		Short: "query the current rewards of an account",
   122  		Long: strings.TrimSpace(
   123  			fmt.Sprintf(`Query available rewards for an address.
   124  
   125  Example:
   126  $ %s query farm rewards pool-eth-xxb fb1cftp8q8g4aa65nw9s5trwexe77d9t6cr8ndu02
   127  `,
   128  				version.ClientName,
   129  			),
   130  		),
   131  		Args: cobra.ExactArgs(2),
   132  		RunE: func(cmd *cobra.Command, args []string) error {
   133  			cliCtx := context.NewCLIContext().WithCodec(cdc)
   134  			accAddr, err := sdk.AccAddressFromBech32(args[1])
   135  			if err != nil {
   136  				return err
   137  			}
   138  
   139  			jsonBytes, err := cdc.MarshalJSON(types.NewQueryPoolAccountParams(args[0], accAddr))
   140  			if err != nil {
   141  				return err
   142  			}
   143  
   144  			route := fmt.Sprintf("custom/%s/%s", storeName, types.QueryEarnings)
   145  			bz, _, err := cliCtx.QueryWithData(route, jsonBytes)
   146  			if err != nil {
   147  				return err
   148  			}
   149  
   150  			var earnings types.Earnings
   151  			cdc.MustUnmarshalJSON(bz, &earnings)
   152  			return cliCtx.PrintOutput(earnings)
   153  		},
   154  	}
   155  }
   156  
   157  // GetCmdQueryParams gets the pools query command.
   158  func GetCmdQueryParams(storeName string, cdc *codec.Codec) *cobra.Command {
   159  	return &cobra.Command{
   160  		Use:   "params",
   161  		Short: "query the current farm parameters information",
   162  		Long: strings.TrimSpace(
   163  			fmt.Sprintf(`Query values set as farm parameters.
   164  
   165  Example:
   166  $ %s query farm params
   167  `,
   168  				version.ClientName,
   169  			),
   170  		),
   171  		Args: cobra.NoArgs,
   172  		RunE: func(cmd *cobra.Command, _ []string) error {
   173  			cliCtx := context.NewCLIContext().WithCodec(cdc)
   174  			route := fmt.Sprintf("custom/%s/%s", storeName, types.QueryParameters)
   175  			bz, _, err := cliCtx.QueryWithData(route, nil)
   176  			if err != nil {
   177  				return err
   178  			}
   179  
   180  			var params types.Params
   181  			cdc.MustUnmarshalJSON(bz, &params)
   182  			return cliCtx.PrintOutput(params)
   183  		},
   184  	}
   185  }
   186  
   187  // GetCmdQueryWhitelist gets the whitelist query command.
   188  func GetCmdQueryWhitelist(storeName string, cdc *codec.Codec) *cobra.Command {
   189  	return &cobra.Command{
   190  		Use:   "whitelist",
   191  		Short: "query the whitelist of pools to farm fibo",
   192  		Long: strings.TrimSpace(
   193  			fmt.Sprintf(`Query the current whitelist of pools which are approved to farm fibo.
   194  
   195  Example:
   196  $ %s query farm whitelist
   197  `,
   198  				version.ClientName,
   199  			),
   200  		),
   201  		Args: cobra.NoArgs,
   202  		RunE: func(cmd *cobra.Command, _ []string) error {
   203  			cliCtx := context.NewCLIContext().WithCodec(cdc)
   204  			route := fmt.Sprintf("custom/%s/%s", storeName, types.QueryWhitelist)
   205  			bz, _, err := cliCtx.QueryWithData(route, nil)
   206  			if err != nil {
   207  				return err
   208  			}
   209  
   210  			var whitelist types.PoolNameList
   211  			cdc.MustUnmarshalJSON(bz, &whitelist)
   212  			return cliCtx.PrintOutput(whitelist)
   213  		},
   214  	}
   215  }
   216  
   217  // GetCmdQueryAccount gets the account query command.
   218  func GetCmdQueryAccount(storeName string, cdc *codec.Codec) *cobra.Command {
   219  	return &cobra.Command{
   220  		Use:   "account [address]",
   221  		Short: "query the name of pools that an account has locked coins in",
   222  		Long: strings.TrimSpace(
   223  			fmt.Sprintf(`Query the names of all pools that an account has locked coins in.
   224  
   225  Example:
   226  $ %s query farm account fb1cftp8q8g4aa65nw9s5trwexe77d9t6cr8ndu02
   227  `,
   228  				version.ClientName,
   229  			),
   230  		),
   231  		Args: cobra.ExactArgs(1),
   232  		RunE: func(cmd *cobra.Command, args []string) error {
   233  			cliCtx := context.NewCLIContext().WithCodec(cdc)
   234  			accAddr, err := sdk.AccAddressFromBech32(args[0])
   235  			if err != nil {
   236  				return err
   237  			}
   238  
   239  			jsonBytes, err := cdc.MarshalJSON(types.NewQueryAccountParams(accAddr))
   240  			if err != nil {
   241  				return err
   242  			}
   243  
   244  			route := fmt.Sprintf("custom/%s/%s", storeName, types.QueryAccount)
   245  			bz, _, err := cliCtx.QueryWithData(route, jsonBytes)
   246  			if err != nil {
   247  				return err
   248  			}
   249  
   250  			var poolNameList types.PoolNameList
   251  			cdc.MustUnmarshalJSON(bz, &poolNameList)
   252  			return cliCtx.PrintOutput(poolNameList)
   253  		},
   254  	}
   255  }
   256  
   257  // GetCmdQueryPoolNum gets the pool number query command.
   258  func GetCmdQueryPoolNum(storeName string, cdc *codec.Codec) *cobra.Command {
   259  	return &cobra.Command{
   260  		Use:   "pool-num",
   261  		Short: "query the number of pools",
   262  		Long: strings.TrimSpace(
   263  			fmt.Sprintf(`Query the number of pools that already exist.
   264  
   265  Example:
   266  $ %s query farm pool-num
   267  `,
   268  				version.ClientName,
   269  			),
   270  		),
   271  		Args: cobra.NoArgs,
   272  		RunE: func(cmd *cobra.Command, args []string) error {
   273  			cliCtx := context.NewCLIContext().WithCodec(cdc)
   274  
   275  			route := fmt.Sprintf("custom/%s/%s", storeName, types.QueryPoolNum)
   276  			bz, _, err := cliCtx.QueryWithData(route, nil)
   277  			if err != nil {
   278  				return err
   279  			}
   280  
   281  			var poolNum types.PoolNum
   282  			cdc.MustUnmarshalJSON(bz, &poolNum)
   283  			return cliCtx.PrintOutput(poolNum)
   284  		},
   285  	}
   286  }
   287  
   288  // GetCmdQueryAccountsLockedTo gets all addresses of accounts that locked coins in a specific pool
   289  func GetCmdQueryAccountsLockedTo(storeName string, cdc *codec.Codec) *cobra.Command {
   290  	return &cobra.Command{
   291  		Use:   "accounts-locked-to [pool-name]",
   292  		Short: "query the addresses of accounts locked in a pool",
   293  		Long: strings.TrimSpace(
   294  			fmt.Sprintf(`Query all the addresses of accounts that have locked coins in a specific pool.
   295  
   296  Example:
   297  $ %s query farm accounts-locked-to pool-eth-xxb
   298  `,
   299  				version.ClientName,
   300  			),
   301  		),
   302  		Args: cobra.ExactArgs(1),
   303  		RunE: func(cmd *cobra.Command, args []string) error {
   304  			cliCtx := context.NewCLIContext().WithCodec(cdc)
   305  
   306  			jsonBytes, err := cdc.MarshalJSON(types.NewQueryPoolParams(args[0]))
   307  			if err != nil {
   308  				return err
   309  			}
   310  
   311  			route := fmt.Sprintf("custom/%s/%s", storeName, types.QueryAccountsLockedTo)
   312  			bz, _, err := cliCtx.QueryWithData(route, jsonBytes)
   313  			if err != nil {
   314  				return err
   315  			}
   316  
   317  			var accAddrList types.AccAddrList
   318  			cdc.MustUnmarshalJSON(bz, &accAddrList)
   319  			return cliCtx.PrintOutput(accAddrList)
   320  		},
   321  	}
   322  }
   323  
   324  // GetCmdQueryLockInfo gets the lock info of an account's token locking on a specific pool
   325  func GetCmdQueryLockInfo(storeName string, cdc *codec.Codec) *cobra.Command {
   326  	return &cobra.Command{
   327  		Use:   "lock-info [pool-name] [address]",
   328  		Short: "query the lock info of an account on a pool",
   329  		Long: strings.TrimSpace(
   330  			fmt.Sprintf(`Query the lock info of an account's token locking on a specific pool.
   331  
   332  Example:
   333  $ %s query farm lock-info pool-eth-xxb fb1cftp8q8g4aa65nw9s5trwexe77d9t6cr8ndu02 
   334  `,
   335  				version.ClientName,
   336  			),
   337  		),
   338  		Args: cobra.ExactArgs(2),
   339  		RunE: func(cmd *cobra.Command, args []string) error {
   340  			cliCtx := context.NewCLIContext().WithCodec(cdc)
   341  			accAddr, err := sdk.AccAddressFromBech32(args[1])
   342  			if err != nil {
   343  				return err
   344  			}
   345  
   346  			jsonBytes, err := cdc.MarshalJSON(types.NewQueryPoolAccountParams(args[0], accAddr))
   347  			if err != nil {
   348  				return err
   349  			}
   350  
   351  			route := fmt.Sprintf("custom/%s/%s", storeName, types.QueryLockInfo)
   352  			bz, _, err := cliCtx.QueryWithData(route, jsonBytes)
   353  			if err != nil {
   354  				return err
   355  			}
   356  
   357  			var lockInfo types.LockInfo
   358  			cdc.MustUnmarshalJSON(bz, &lockInfo)
   359  			return cliCtx.PrintOutput(lockInfo)
   360  		},
   361  	}
   362  }