x/staking: cli migration to use gRPC query client (#6728)
* migrated to use gRPC query client * removed codecs usage * review change * added read command flags * added pagination flags * fixed limit issue * added helper function for default pagination flags * review changes * review change * review changes Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
co-authored by
Federico Kunze
mergify[bot]
parent
cdcb51a922
commit
c37f683cc1
@@ -61,6 +61,9 @@ const (
|
||||
FlagPage = "page"
|
||||
FlagLimit = "limit"
|
||||
FlagSignMode = "sign-mode"
|
||||
FlagPageKey = "page-key"
|
||||
FlagOffset = "offset"
|
||||
FlagCountTotal = "count-total"
|
||||
)
|
||||
|
||||
// LineBreak can be included in a command list to provide a blank line
|
||||
@@ -123,6 +126,14 @@ func AddTxFlagsToCmd(cmd *cobra.Command) {
|
||||
viper.BindPFlag(FlagKeyringBackend, cmd.Flags().Lookup(FlagKeyringBackend))
|
||||
}
|
||||
|
||||
// AddPaginationFlagsToCmd adds common paginations flags to command
|
||||
func AddPaginationFlagsToCmd(cmd *cobra.Command, query string) {
|
||||
cmd.Flags().String(FlagPageKey, "", fmt.Sprintf("pagination page-key of %s to query for", query))
|
||||
cmd.Flags().Uint64(FlagOffset, 0, fmt.Sprintf("pagination offset of %s to query for", query))
|
||||
cmd.Flags().Uint64(FlagLimit, 100, fmt.Sprintf("pagination limit of %s to query for", query))
|
||||
cmd.Flags().Bool(FlagCountTotal, false, fmt.Sprintf("count total number of records in %s to query for", query))
|
||||
}
|
||||
|
||||
// GasSetting encapsulates the possible values passed through the --gas flag.
|
||||
type GasSetting struct {
|
||||
Simulate bool
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
"github.com/cosmos/cosmos-sdk/types/query"
|
||||
"github.com/spf13/pflag"
|
||||
)
|
||||
|
||||
// Paginate returns the correct starting and ending index for a paginated query,
|
||||
// given that client provides a desired page and limit of objects and the handler
|
||||
// provides the total number of objects. The start page is assumed to be 1-indexed.
|
||||
@@ -35,3 +41,18 @@ func Paginate(numObjs, page, limit, defLimit int) (start, end int) {
|
||||
|
||||
return start, end
|
||||
}
|
||||
|
||||
// ReadPageRequest reads and builds the necessary page request flags for pagination.
|
||||
func ReadPageRequest(flagSet *pflag.FlagSet) *query.PageRequest {
|
||||
pageKey, _ := flagSet.GetString(flags.FlagPageKey)
|
||||
offset, _ := flagSet.GetUint64(flags.FlagOffset)
|
||||
limit, _ := flagSet.GetUint64(flags.FlagLimit)
|
||||
countTotal, _ := flagSet.GetBool(flags.FlagCountTotal)
|
||||
|
||||
return &query.PageRequest{
|
||||
Key: []byte(pageKey),
|
||||
Offset: offset,
|
||||
Limit: limit,
|
||||
CountTotal: countTotal,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user