Merge PR #4011: Mint/Inflation Querier, LCD and CLI

* A few godoc updates

* More minor tweaks and reformatting

* Implement initial minting querier

* Implement stringer interface for minting params

* Minor cleanup

* Add minting CLI commands

* Implement inflation query command

* Implement annual provisions query and CLI command

* Update x/mint/client/module_client.go

Co-Authored-By: alexanderbez <alexanderbez@users.noreply.github.com>

* Update x/mint/client/module_client.go

Co-Authored-By: alexanderbez <alexanderbez@users.noreply.github.com>

* Update x/mint/client/module_client.go

Co-Authored-By: alexanderbez <alexanderbez@users.noreply.github.com>

* Update x/mint/querier.go

Co-Authored-By: alexanderbez <alexanderbez@users.noreply.github.com>

* Add minting REST client routes/handlers

* Fix build issues

* Implement querier unit tests

* Update gaiacli docs

* Implement LCD tests

* Update Swagger docs

* Add pending log entry

* add examples

Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com>

* revert adding examples

Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com>
This commit is contained in:
Alexander Bezobchuk
2019-04-01 10:28:36 -07:00
committed by Jack Zampolin
parent 120e08f12d
commit b95ade93bd
17 changed files with 635 additions and 30 deletions
+89
View File
@@ -0,0 +1,89 @@
package cli
import (
"fmt"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/mint"
"github.com/spf13/cobra"
)
// GetCmdQueryParams implements a command to return the current minting
// parameters.
func GetCmdQueryParams(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "params",
Short: "Query the current minting parameters",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
route := fmt.Sprintf("custom/%s/%s", mint.QuerierRoute, mint.QueryParameters)
res, err := cliCtx.QueryWithData(route, nil)
if err != nil {
return err
}
var params mint.Params
if err := cdc.UnmarshalJSON(res, &params); err != nil {
return err
}
return cliCtx.PrintOutput(params)
},
}
}
// GetCmdQueryInflation implements a command to return the current minting
// inflation value.
func GetCmdQueryInflation(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "inflation",
Short: "Query the current minting inflation value",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
route := fmt.Sprintf("custom/%s/%s", mint.QuerierRoute, mint.QueryInflation)
res, err := cliCtx.QueryWithData(route, nil)
if err != nil {
return err
}
var inflation sdk.Dec
if err := cdc.UnmarshalJSON(res, &inflation); err != nil {
return err
}
return cliCtx.PrintOutput(inflation)
},
}
}
// GetCmdQueryAnnualProvisions implements a command to return the current minting
// annual provisions value.
func GetCmdQueryAnnualProvisions(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "annual-provisions",
Short: "Query the current minting annual provisions value",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
route := fmt.Sprintf("custom/%s/%s", mint.QuerierRoute, mint.QueryAnnualProvisions)
res, err := cliCtx.QueryWithData(route, nil)
if err != nil {
return err
}
var inflation sdk.Dec
if err := cdc.UnmarshalJSON(res, &inflation); err != nil {
return err
}
return cliCtx.PrintOutput(inflation)
},
}
}
+47
View File
@@ -0,0 +1,47 @@
package clientpackage
import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/x/mint"
"github.com/cosmos/cosmos-sdk/x/mint/client/cli"
"github.com/spf13/cobra"
"github.com/tendermint/go-amino"
)
// ModuleClient exports all CLI client functionality from the minting module.
type ModuleClient struct {
storeKey string
cdc *amino.Codec
}
func NewModuleClient(storeKey string, cdc *amino.Codec) ModuleClient {
return ModuleClient{storeKey, cdc}
}
// GetQueryCmd returns the cli query commands for the minting module.
func (mc ModuleClient) GetQueryCmd() *cobra.Command {
mintingQueryCmd := &cobra.Command{
Use: mint.ModuleName,
Short: "Querying commands for the minting module",
}
mintingQueryCmd.AddCommand(
client.GetCommands(
cli.GetCmdQueryParams(mc.cdc),
cli.GetCmdQueryInflation(mc.cdc),
cli.GetCmdQueryAnnualProvisions(mc.cdc),
)...,
)
return mintingQueryCmd
}
// GetTxCmd returns the transaction commands for the minting module.
func (mc ModuleClient) GetTxCmd() *cobra.Command {
mintTxCmd := &cobra.Command{
Use: mint.ModuleName,
Short: "Minting transaction subcommands",
}
return mintTxCmd
}
+71
View File
@@ -0,0 +1,71 @@
package rest
import (
"fmt"
"net/http"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/cosmos/cosmos-sdk/x/mint"
"github.com/gorilla/mux"
)
func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec) {
r.HandleFunc(
"/minting/parameters",
queryParamsHandlerFn(cdc, cliCtx),
).Methods("GET")
r.HandleFunc(
"/minting/inflation",
queryInflationHandlerFn(cdc, cliCtx),
).Methods("GET")
r.HandleFunc(
"/minting/annual-provisions",
queryAnnualProvisionsHandlerFn(cdc, cliCtx),
).Methods("GET")
}
func queryParamsHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
route := fmt.Sprintf("custom/%s/%s", mint.QuerierRoute, mint.QueryParameters)
res, err := cliCtx.QueryWithData(route, nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
rest.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}
func queryInflationHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
route := fmt.Sprintf("custom/%s/%s", mint.QuerierRoute, mint.QueryInflation)
res, err := cliCtx.QueryWithData(route, nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
rest.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}
func queryAnnualProvisionsHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
route := fmt.Sprintf("custom/%s/%s", mint.QuerierRoute, mint.QueryAnnualProvisions)
res, err := cliCtx.QueryWithData(route, nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
rest.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}
+12
View File
@@ -0,0 +1,12 @@
package rest
import (
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/gorilla/mux"
)
// RegisterRoutes registers minting module REST handlers on the provided router.
func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec) {
registerQueryRoutes(cliCtx, r, cdc)
}