diff --git a/x/wasm/client/rest/query.go b/x/wasm/client/rest/query.go index 7e59a583bb..76f2ce5f5c 100644 --- a/x/wasm/client/rest/query.go +++ b/x/wasm/client/rest/query.go @@ -31,6 +31,7 @@ func registerQueryRoutes(cliCtx clientCtx.CLIContext, r *mux.Router) { r.HandleFunc("/wasm/contract/{contractAddr}/smart/{query}", queryContractStateSmartHandlerFn(cliCtx)).Queries("encoding", "{encoding}").Methods("GET") r.HandleFunc("/wasm/contract/{contractAddr}/raw/{key}", queryContractStateRawHandlerFn(cliCtx)).Queries("encoding", "{encoding}").Methods("GET") r.HandleFunc("/wasm/contract/{contractAddr}/blocked_methods", queryContractBlockedMethodsHandlerFn(cliCtx)).Methods("GET") + r.HandleFunc("/wasm/code/contract/{contractAddr}", queryCodeContractHandlerFn(cliCtx)).Methods("GET") r.HandleFunc("/wasm/params", queryParamsHandlerFn(cliCtx)).Methods("GET") r.HandleFunc("/wasm/extra_params", queryExtraParamsHandlerFn(cliCtx)).Methods("GET") r.HandleFunc("/wasm/whitelist", queryContractWhitelistHandlerFn(cliCtx)).Methods("GET") @@ -268,6 +269,52 @@ func queryContractHandlerFn(cliCtx clientCtx.CLIContext) http.HandlerFunc { } } +func queryCodeContractHandlerFn(cliCtx clientCtx.CLIContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) + if !ok { + return + } + + queryClient := types.NewQueryClient(cliCtx) + res, err := queryClient.ContractInfo( + context.Background(), + &types.QueryContractInfoRequest{ + Address: mux.Vars(r)["contractAddr"], + }, + ) + if err != nil { + rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } + codeId := res.CodeID + + queryCodeClient := types.NewQueryClient(cliCtx) + codeRes, err := queryCodeClient.Code( + context.Background(), + &types.QueryCodeRequest{ + CodeId: codeId, + }, + ) + + if err != nil { + rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } + if codeRes == nil { + rest.WriteErrorResponse(w, http.StatusNotFound, "contract not found") + return + } + + out, err := cliCtx.CodecProy.GetProtocMarshal().MarshalJSON(codeRes) + if err != nil { + rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } + rest.PostProcessResponse(w, cliCtx, json.RawMessage(out)) + } +} + func queryContractBlockedMethodsHandlerFn(cliCtx clientCtx.CLIContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { addr, err := sdk.WasmAddressFromBech32(mux.Vars(r)["contractAddr"])