diff --git a/cmd/exchaind/rest.go b/cmd/exchaind/rest.go index 2643047542..c1d143cbc8 100644 --- a/cmd/exchaind/rest.go +++ b/cmd/exchaind/rest.go @@ -35,6 +35,7 @@ import ( govrest "github.com/okex/exchain/x/gov/client/rest" orderrest "github.com/okex/exchain/x/order/client/rest" paramsclient "github.com/okex/exchain/x/params/client" + paramsrest "github.com/okex/exchain/x/params/client/rest" slashingrest "github.com/okex/exchain/x/slashing/client/rest" stakingrest "github.com/okex/exchain/x/staking/client/rest" "github.com/okex/exchain/x/token" @@ -102,6 +103,7 @@ func registerRoutesV1(rs *lcd.RestServer, pathPrefix string) { ) mintrest.RegisterRoutes(rs.CliCtx, v1Router) ibctransferrest.RegisterOriginRPCRoutersForGRPC(rs.CliCtx, v1Router) + paramsrest.RegisterRoutes(rs.CliCtx, v1Router) } func registerRoutesV2(rs *lcd.RestServer, pathPrefix string) { diff --git a/x/params/client/cli/query.go b/x/params/client/cli/query.go index af69818a63..5f048a4c55 100644 --- a/x/params/client/cli/query.go +++ b/x/params/client/cli/query.go @@ -3,6 +3,7 @@ package cli import ( "fmt" "github.com/okex/exchain/libs/cosmos-sdk/client/flags" + "strconv" "strings" "github.com/okex/exchain/x/params/types" @@ -90,9 +91,17 @@ func GetCmdQueryBlockConfig(queryRoute string, cdc *codec.Codec) *cobra.Command $ exchaincli query params blockconfig `), - Args: cobra.NoArgs, - RunE: func(_ *cobra.Command, _ []string) error { - cliCtx := context.NewCLIContext().WithCodec(cdc) + Args: cobra.MinimumNArgs(0), + RunE: func(_ *cobra.Command, args []string) error { + height := int64(0) + if len(args) > 0 { + var err error + height, err = strconv.ParseInt(args[0], 10, 64) + if err != nil { + return err + } + } + cliCtx := context.NewCLIContext().WithCodec(cdc).WithHeight(height) route := fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryBlockConfig) bz, _, err := cliCtx.QueryWithData(route, nil) diff --git a/x/params/client/rest/rest.go b/x/params/client/rest/rest.go new file mode 100644 index 0000000000..af54f12697 --- /dev/null +++ b/x/params/client/rest/rest.go @@ -0,0 +1,36 @@ +package rest + +import ( + "fmt" + "github.com/gorilla/mux" + "github.com/okex/exchain/libs/cosmos-sdk/client/context" + "github.com/okex/exchain/libs/cosmos-sdk/types/rest" + "github.com/okex/exchain/x/common" + "github.com/okex/exchain/x/params" + "github.com/okex/exchain/x/params/types" + "net/http" +) + +// RegisterRoutes, a central function to define routes +// which is called by the rest module in main application +func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router) { + r.HandleFunc(fmt.Sprintf("/params/blockconfig"), QueryBlockConfigFn(cliCtx)).Methods("GET") +} + +func QueryBlockConfigFn(cliCtx context.CLIContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) + if !ok { + return + } + + route := fmt.Sprintf("custom/%s/%s", params.RouterKey, types.QueryBlockConfig) + bz, _, err := cliCtx.QueryWithData(route, nil) + if err != nil { + sdkErr := common.ParseSDKError(err.Error()) + common.HandleErrorMsg(w, cliCtx, sdkErr.Code, err.Error()) + return + } + rest.PostProcessResponseBare(w, cliCtx, bz) + } +}