Skip to content

Commit

Permalink
add wasm query code by addr rpc method (#3118)
Browse files Browse the repository at this point in the history
Co-authored-by: KamiD <[email protected]>
  • Loading branch information
giskook and KamiD authored Apr 27, 2023
1 parent b8975b3 commit b6cbf43
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions x/wasm/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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"])
Expand Down

0 comments on commit b6cbf43

Please sign in to comment.