github.com/lino-network/lino@v0.6.11/x/account/client/cli/query.go (about)

     1  package cli
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/cosmos/cosmos-sdk/client"
     7  	"github.com/cosmos/cosmos-sdk/client/context"
     8  	"github.com/cosmos/cosmos-sdk/codec"
     9  	"github.com/spf13/cobra"
    10  
    11  	linotypes "github.com/lino-network/lino/types"
    12  	"github.com/lino-network/lino/utils"
    13  	"github.com/lino-network/lino/x/account/model"
    14  	"github.com/lino-network/lino/x/account/types"
    15  )
    16  
    17  func GetQueryCmd(cdc *codec.Codec) *cobra.Command {
    18  	cmd := &cobra.Command{
    19  		Use:                        types.ModuleName,
    20  		Short:                      "Querying commands for the account module",
    21  		DisableFlagParsing:         true,
    22  		SuggestionsMinimumDistance: 2,
    23  		RunE:                       client.ValidateCmd,
    24  	}
    25  	cmd.AddCommand(client.GetCommands(
    26  		utils.SimpleQueryCmd(
    27  			"info <username>",
    28  			"info <username>",
    29  			types.QuerierRoute, types.QueryAccountInfo,
    30  			1, &model.AccountInfo{})(cdc),
    31  		utils.SimpleQueryCmd(
    32  			"bank <username>",
    33  			"bank <username>",
    34  			types.QuerierRoute, types.QueryAccountBank,
    35  			1, &model.AccountBank{})(cdc),
    36  		utils.SimpleQueryCmd(
    37  			"bank-addr <address>",
    38  			"bank-addr <address>",
    39  			types.QuerierRoute, types.QueryAccountBankByAddress,
    40  			1, &model.AccountBank{})(cdc),
    41  		utils.SimpleQueryCmd(
    42  			"meta <username>",
    43  			"meta <username>",
    44  			types.QuerierRoute, types.QueryAccountMeta,
    45  			1, &model.AccountMeta{})(cdc),
    46  		utils.SimpleQueryCmd(
    47  			"supply",
    48  			"supply",
    49  			types.QuerierRoute, types.QuerySupply,
    50  			0, &model.Supply{})(cdc),
    51  		getQueryPoolCmds(cdc),
    52  	)...)
    53  	return cmd
    54  }
    55  
    56  // getQueryPoolCmds - return a commands that queries the pool.
    57  func getQueryPoolCmds(cdc *codec.Codec) *cobra.Command {
    58  	return &cobra.Command{
    59  		Use:   "pool [poolname]",
    60  		Short: "pool prints balance of all pools, if not specified, unit: LINO",
    61  		Args:  cobra.MaximumNArgs(1),
    62  		RunE: func(cmd *cobra.Command, args []string) error {
    63  			cliCtx := context.NewCLIContext().WithCodec(cdc)
    64  
    65  			poolnames := linotypes.ListPools()
    66  			if len(args) == 1 {
    67  				poolnames = []linotypes.PoolName{linotypes.PoolName(args[0])}
    68  			}
    69  
    70  			fmt.Printf("|%-25s|%-10s|\n", "Pool Name", "LINO")
    71  			for _, poolname := range poolnames {
    72  				uri := fmt.Sprintf("custom/%s/%s/%s",
    73  					types.QuerierRoute, types.QueryPool, poolname)
    74  				res, _, err := cliCtx.QueryWithData(uri, nil)
    75  				if err != nil {
    76  					fmt.Printf("Failed to Query and Print: %s, because %s", uri, err)
    77  					return nil
    78  				}
    79  				rst := &linotypes.Coin{}
    80  				cdc.MustUnmarshalJSON(res, rst)
    81  				fmt.Printf("|%-25s|%-10s|\n", poolname, rst.ToLino())
    82  			}
    83  			return nil
    84  		},
    85  	}
    86  }