Merge PR #2099: Query staking Pool and Params
This commit is contained in:
committed by
Christopher Goes
parent
68f5ee786c
commit
cfb5acca6e
@@ -417,3 +417,81 @@ func GetCmdQueryRedelegations(storeName string, cdc *wire.Codec) *cobra.Command
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
// GetCmdQueryPool implements the pool query command.
|
||||
func GetCmdQueryPool(storeName string, cdc *wire.Codec) *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "pool",
|
||||
Short: "Query the current staking pool values",
|
||||
Args: cobra.ExactArgs(0),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
key := stake.PoolKey
|
||||
cliCtx := context.NewCLIContext().WithCodec(cdc)
|
||||
|
||||
res, err := cliCtx.QueryStore(key, storeName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
pool := types.MustUnmarshalPool(cdc, res)
|
||||
|
||||
switch viper.Get(cli.OutputFlag) {
|
||||
case "text":
|
||||
human := pool.HumanReadableString()
|
||||
|
||||
fmt.Println(human)
|
||||
|
||||
case "json":
|
||||
// parse out the pool
|
||||
output, err := wire.MarshalJSONIndent(cdc, pool)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Println(string(output))
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
// GetCmdQueryPool implements the params query command.
|
||||
func GetCmdQueryParams(storeName string, cdc *wire.Codec) *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "parameters",
|
||||
Short: "Query the current staking parameters information",
|
||||
Args: cobra.ExactArgs(0),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
key := stake.ParamKey
|
||||
cliCtx := context.NewCLIContext().WithCodec(cdc)
|
||||
|
||||
res, err := cliCtx.QueryStore(key, storeName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
params := types.MustUnmarshalParams(cdc, res)
|
||||
|
||||
switch viper.Get(cli.OutputFlag) {
|
||||
case "text":
|
||||
human := params.HumanReadableString()
|
||||
|
||||
fmt.Println(human)
|
||||
|
||||
case "json":
|
||||
// parse out the params
|
||||
output, err := wire.MarshalJSONIndent(cdc, params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Println(string(output))
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
@@ -68,6 +68,18 @@ func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *wire.Cod
|
||||
validatorHandlerFn(cliCtx, cdc),
|
||||
).Methods("GET")
|
||||
|
||||
// Get the current state of the staking pool
|
||||
r.HandleFunc(
|
||||
"/stake/pool",
|
||||
poolHandlerFn(cliCtx, cdc),
|
||||
).Methods("GET")
|
||||
|
||||
// Get the current staking parameter values
|
||||
r.HandleFunc(
|
||||
"/stake/parameters",
|
||||
paramsHandlerFn(cliCtx, cdc),
|
||||
).Methods("GET")
|
||||
|
||||
}
|
||||
|
||||
// already resolve the rational shares to not handle this in the client
|
||||
@@ -554,3 +566,63 @@ func validatorHandlerFn(cliCtx context.CLIContext, cdc *wire.Codec) http.Handler
|
||||
w.Write(output)
|
||||
}
|
||||
}
|
||||
|
||||
// HTTP request handler to query the pool information
|
||||
func poolHandlerFn(cliCtx context.CLIContext, cdc *wire.Codec) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
key := stake.PoolKey
|
||||
|
||||
res, err := cliCtx.QueryStore(key, storeName)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(fmt.Sprintf("couldn't query pool. Error: %s", err.Error())))
|
||||
return
|
||||
}
|
||||
|
||||
pool, err := types.UnmarshalPool(cdc, res)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
output, err := cdc.MarshalJSON(pool)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
w.Write(output)
|
||||
}
|
||||
}
|
||||
|
||||
// HTTP request handler to query the staking params values
|
||||
func paramsHandlerFn(cliCtx context.CLIContext, cdc *wire.Codec) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
key := stake.ParamKey
|
||||
|
||||
res, err := cliCtx.QueryStore(key, storeName)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(fmt.Sprintf("couldn't query parameters. Error: %s", err.Error())))
|
||||
return
|
||||
}
|
||||
|
||||
params, err := types.UnmarshalParams(cdc, res)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
output, err := cdc.MarshalJSON(params)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
w.Write(output)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,9 +2,11 @@ package types
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/wire"
|
||||
)
|
||||
|
||||
// defaultUnbondingTime reflects three weeks in seconds as the default
|
||||
@@ -43,3 +45,36 @@ func DefaultParams() Params {
|
||||
BondDenom: "steak",
|
||||
}
|
||||
}
|
||||
|
||||
// HumanReadableString returns a human readable string representation of the
|
||||
// parameters.
|
||||
func (p Params) HumanReadableString() string {
|
||||
|
||||
resp := "Pool \n"
|
||||
resp += fmt.Sprintf("Maximum Annual Inflation Rate Change: %s\n", p.InflationRateChange)
|
||||
resp += fmt.Sprintf("Max Inflation Rate: %s\n", p.InflationMax)
|
||||
resp += fmt.Sprintf("Min Inflation Tate: %s\n", p.InflationMin)
|
||||
resp += fmt.Sprintf("Bonded Token Goal (%s): %s\n", "s", p.GoalBonded)
|
||||
resp += fmt.Sprintf("Unbonding Time: %s\n", p.UnbondingTime)
|
||||
resp += fmt.Sprintf("Max Validators: %d: \n", p.MaxValidators)
|
||||
resp += fmt.Sprintf("Bonded Coin Denomination: %s\n", p.BondDenom)
|
||||
return resp
|
||||
}
|
||||
|
||||
// unmarshal the current staking params value from store key or panic
|
||||
func MustUnmarshalParams(cdc *wire.Codec, value []byte) Params {
|
||||
params, err := UnmarshalParams(cdc, value)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return params
|
||||
}
|
||||
|
||||
// unmarshal the current staking params value from store key
|
||||
func UnmarshalParams(cdc *wire.Codec, value []byte) (params Params, err error) {
|
||||
err = cdc.UnmarshalBinary(value, ¶ms)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
+36
-1
@@ -6,13 +6,14 @@ import (
|
||||
"time"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/wire"
|
||||
)
|
||||
|
||||
// Pool - dynamic parameters of the current state
|
||||
type Pool struct {
|
||||
LooseTokens sdk.Dec `json:"loose_tokens"` // tokens which are not bonded in a validator
|
||||
BondedTokens sdk.Dec `json:"bonded_tokens"` // reserve of bonded tokens
|
||||
InflationLastTime time.Time `json:"inflation_last_time"` // block which the last inflation was processed // TODO make time
|
||||
InflationLastTime time.Time `json:"inflation_last_time"` // block which the last inflation was processed
|
||||
Inflation sdk.Dec `json:"inflation"` // current annual inflation rate
|
||||
|
||||
DateLastCommissionReset int64 `json:"date_last_commission_reset"` // unix timestamp for last commission accounting reset (daily)
|
||||
@@ -123,3 +124,37 @@ func (p Pool) NextInflation(params Params) (inflation sdk.Dec) {
|
||||
|
||||
return inflation
|
||||
}
|
||||
|
||||
// HumanReadableString returns a human readable string representation of a
|
||||
// pool.
|
||||
func (p Pool) HumanReadableString() string {
|
||||
|
||||
resp := "Pool \n"
|
||||
resp += fmt.Sprintf("Loose Tokens: %s\n", p.LooseTokens)
|
||||
resp += fmt.Sprintf("Bonded Tokens: %s\n", p.BondedTokens)
|
||||
resp += fmt.Sprintf("Token Supply: %s\n", p.TokenSupply())
|
||||
resp += fmt.Sprintf("Bonded Ratio: %v\n", p.BondedRatio())
|
||||
resp += fmt.Sprintf("Previous Inflation Block: %s\n", p.InflationLastTime)
|
||||
resp += fmt.Sprintf("Inflation: %v\n", p.Inflation)
|
||||
resp += fmt.Sprintf("Date of Last Commission Reset: %d\n", p.DateLastCommissionReset)
|
||||
resp += fmt.Sprintf("Previous Bonded Shares: %v\n", p.PrevBondedShares)
|
||||
return resp
|
||||
}
|
||||
|
||||
// unmarshal the current pool value from store key or panics
|
||||
func MustUnmarshalPool(cdc *wire.Codec, value []byte) Pool {
|
||||
pool, err := UnmarshalPool(cdc, value)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return pool
|
||||
}
|
||||
|
||||
// unmarshal the current pool value from store key
|
||||
func UnmarshalPool(cdc *wire.Codec, value []byte) (pool Pool, err error) {
|
||||
err = cdc.UnmarshalBinary(value, &pool)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user