Skip to content

Commit

Permalink
x/staking: cli migration to use gRPC query client (cosmos#6728)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Jul 17, 2020
1 parent cdcb51a commit c37f683
Show file tree
Hide file tree
Showing 4 changed files with 233 additions and 165 deletions.
11 changes: 11 additions & 0 deletions client/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions client/utils.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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,
}
}
Loading

0 comments on commit c37f683

Please sign in to comment.