From 58aa732fe810e744ec8a49d957976ae667156efd Mon Sep 17 00:00:00 2001 From: ylsGit Date: Mon, 14 Nov 2022 10:38:53 +0800 Subject: [PATCH] Merge PR: query Feesplit params by rest api (#2732) --- x/feesplit/client/rest/rest.go | 52 ++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/x/feesplit/client/rest/rest.go b/x/feesplit/client/rest/rest.go index 9ac8d970b2..fd3b4e45cb 100644 --- a/x/feesplit/client/rest/rest.go +++ b/x/feesplit/client/rest/rest.go @@ -21,11 +21,13 @@ func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router) { r.HandleFunc("/feesplit/contract/{contract}", contractHandlerFn(cliCtx)).Methods("GET") r.HandleFunc("/feesplit/deployer/{deployer}", deployerHandlerFn(cliCtx)).Methods("GET") r.HandleFunc("/feesplit/withdrawer/{withdrawer}", withdrawerHandlerFn(cliCtx)).Methods("GET") + r.HandleFunc("/feesplit/parameters", queryParamsHandlerFn(cliCtx)).Methods("GET") } func RegisterRoutesV2(cliCtx context.CLIContext, r *mux.Router) { r.HandleFunc("/feesplit/contract/{contract}", contractHandlerFnV2(cliCtx)).Methods("GET") r.HandleFunc("/feesplit/deployer/{deployer}", deployerHandlerFnV2(cliCtx)).Methods("GET") + r.HandleFunc("/feesplit/parameters", queryParamsHandlerFnV2(cliCtx)).Methods("GET") } // FeeSplitSharesProposalRESTHandler defines feesplit proposal handler @@ -33,6 +35,25 @@ func FeeSplitSharesProposalRESTHandler(context.CLIContext) govRest.ProposalRESTH return govRest.ProposalRESTHandler{} } +func queryParamsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) + if !ok { + return + } + + res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", + types.RouterKey, types.QueryParameters), nil) + if err != nil { + rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } + + cliCtx = cliCtx.WithHeight(height) + rest.PostProcessResponse(w, cliCtx, res) + } +} + func contractHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { contract := mux.Vars(r)["contract"] @@ -255,3 +276,34 @@ func deployerHandlerFnV2(cliCtx context.CLIContext) http.HandlerFunc { rest.PostProcessResponse(w, cliCtx, resultJson) } } + +func queryParamsHandlerFnV2(cliCtx context.CLIContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) + if !ok { + return + } + + res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", + types.RouterKey, types.QueryParameters), nil) + if err != nil { + rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } + + var result types.QueryParamsResponse + if err := cliCtx.Codec.UnmarshalJSON(res, &result); err != nil { + comm.HandleErrorMsg(w, cliCtx, comm.CodeUnMarshalJSONFailed, err.Error()) + return + } + + resultJson, err := json.Marshal(comm.GetBaseResponse(result)) + if err != nil { + comm.HandleErrorMsg(w, cliCtx, comm.CodeMarshalJSONFailed, err.Error()) + return + } + + cliCtx = cliCtx.WithHeight(height) + rest.PostProcessResponse(w, cliCtx, resultJson) + } +}