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

     1  package cli
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/spf13/cobra"
     8  
     9  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client"
    10  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context"
    11  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/flags"
    12  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec"
    13  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
    14  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/version"
    15  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/supply/internal/types"
    16  )
    17  
    18  // GetQueryCmd returns the cli query commands for this module
    19  func GetQueryCmd(cdc *codec.Codec) *cobra.Command {
    20  	// Group supply queries under a subcommand
    21  	supplyQueryCmd := &cobra.Command{
    22  		Use:                        types.ModuleName,
    23  		Short:                      "Querying commands for the supply module",
    24  		DisableFlagParsing:         true,
    25  		SuggestionsMinimumDistance: 2,
    26  		RunE:                       client.ValidateCmd,
    27  	}
    28  
    29  	supplyQueryCmd.AddCommand(flags.GetCommands(
    30  		GetCmdQueryTotalSupply(cdc),
    31  	)...)
    32  
    33  	return supplyQueryCmd
    34  }
    35  
    36  // GetCmdQueryTotalSupply implements the query total supply command.
    37  func GetCmdQueryTotalSupply(cdc *codec.Codec) *cobra.Command {
    38  	return &cobra.Command{
    39  		Use:   "total [denom]",
    40  		Args:  cobra.MaximumNArgs(1),
    41  		Short: "Query the total supply of coins of the chain",
    42  		Long: strings.TrimSpace(
    43  			fmt.Sprintf(`Query total supply of coins that are held by accounts in the
    44  			chain.
    45  
    46  Example:
    47  $ %s query %s total
    48  
    49  To query for the total supply of a specific coin denomination use:
    50  $ %s query %s total stake
    51  `,
    52  				version.ClientName, types.ModuleName, version.ClientName, types.ModuleName,
    53  			),
    54  		),
    55  		RunE: func(cmd *cobra.Command, args []string) error {
    56  			cliCtx := context.NewCLIContext().WithCodec(cdc)
    57  
    58  			if len(args) == 0 {
    59  				return queryTotalSupply(cliCtx, cdc)
    60  			}
    61  			return querySupplyOf(cliCtx, cdc, args[0])
    62  		},
    63  	}
    64  }
    65  
    66  func queryTotalSupply(cliCtx context.CLIContext, cdc *codec.Codec) error {
    67  	params := types.NewQueryTotalSupplyParams(1, 0) // no pagination
    68  	bz, err := cdc.MarshalJSON(params)
    69  	if err != nil {
    70  		return err
    71  	}
    72  
    73  	res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryTotalSupply), bz)
    74  	if err != nil {
    75  		return err
    76  	}
    77  
    78  	var totalSupply sdk.Coins
    79  	err = cdc.UnmarshalJSON(res, &totalSupply)
    80  	if err != nil {
    81  		return err
    82  	}
    83  
    84  	return cliCtx.PrintOutput(totalSupply)
    85  }
    86  
    87  func querySupplyOf(cliCtx context.CLIContext, cdc *codec.Codec, denom string) error {
    88  	params := types.NewQuerySupplyOfParams(denom)
    89  	bz, err := cdc.MarshalJSON(params)
    90  	if err != nil {
    91  		return err
    92  	}
    93  
    94  	res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QuerySupplyOf), bz)
    95  	if err != nil {
    96  		return err
    97  	}
    98  
    99  	var supply sdk.Dec
   100  	err = cdc.UnmarshalJSON(res, &supply)
   101  	if err != nil {
   102  		return err
   103  	}
   104  
   105  	return cliCtx.PrintOutput(supply)
   106  }