Skip to content

Commit

Permalink
Merge pull request #68 from cosmwasm/rest-server-fixes
Browse files Browse the repository at this point in the history
Rest server fixes
  • Loading branch information
ethanfrey authored Feb 10, 2020
2 parents a7e8464 + d3abcc6 commit e4be8f1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
13 changes: 13 additions & 0 deletions x/wasm/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) {
r.HandleFunc("/wasm/code", listCodesHandlerFn(cliCtx)).Methods("GET")
r.HandleFunc("/wasm/code/{codeID}", queryCodeHandlerFn(cliCtx)).Methods("GET")
r.HandleFunc("/wasm/code/{codeID}/contracts", listContractsByCodeHandlerFn(cliCtx)).Methods("GET")
r.HandleFunc("/wasm/contract", listAllContractsHandlerFn(cliCtx)).Methods("GET")
r.HandleFunc("/wasm/contract/{contractAddr}", queryContractHandlerFn(cliCtx)).Methods("GET")
r.HandleFunc("/wasm/contract/{contractAddr}/state", queryContractStateAllHandlerFn(cliCtx)).Methods("GET")
Expand Down Expand Up @@ -74,6 +75,18 @@ func queryCodeHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
}
}

func listContractsByCodeHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, keeper.QueryListContractByCode)
res, _, err := cliCtx.Query(route)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
rest.PostProcessResponse(w, cliCtx, string(res))
}
}

func listAllContractsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, keeper.QueryListContracts)
Expand Down
39 changes: 34 additions & 5 deletions x/wasm/internal/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"encoding/json"
"sort"
"strconv"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -13,11 +14,12 @@ import (
)

const (
QueryListContracts = "list-contracts"
QueryGetContract = "contract-info"
QueryGetContractState = "contract-state"
QueryGetCode = "code"
QueryListCode = "list-code"
QueryListContracts = "list-contracts"
QueryListContractByCode = "list-contracts-by-code"
QueryGetContract = "contract-info"
QueryGetContractState = "contract-state"
QueryGetCode = "code"
QueryListCode = "list-code"
)

const (
Expand All @@ -37,6 +39,8 @@ func NewQuerier(keeper Keeper) sdk.Querier {
return queryContractInfo(ctx, path[1], req, keeper)
case QueryListContracts:
return queryContractList(ctx, req, keeper)
case QueryListContractByCode:
return queryContractListByCode(ctx, path[1], req, keeper)
case QueryGetContractState:
if len(path) < 3 {
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "unknown data query endpoint")
Expand Down Expand Up @@ -72,13 +76,34 @@ func queryContractList(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([
addrs = append(addrs, addr.String())
return false
})
sort.Strings(addrs)
bz, err := json.MarshalIndent(addrs, "", " ")
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}

func queryContractListByCode(ctx sdk.Context, codeIDstr string, req abci.RequestQuery, keeper Keeper) ([]byte, error) {
codeID, err := strconv.ParseUint(codeIDstr, 10, 64)
if err != nil {
return nil, err
}

var contracts []types.ContractInfo
keeper.ListContractInfo(ctx, func(addr sdk.AccAddress, info types.ContractInfo) bool {
if info.CodeID == codeID {
contracts = append(contracts, info)
}
return false
})
bz, err := json.MarshalIndent(contracts, "", " ")
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}

func queryContractState(ctx sdk.Context, bech, queryMethod string, req abci.RequestQuery, keeper Keeper) ([]byte, error) {
contractAddr, err := sdk.AccAddressFromBech32(bech)
if err != nil {
Expand Down Expand Up @@ -137,6 +162,8 @@ type ListCodeResponse struct {
ID uint64 `json:"id"`
Creator sdk.AccAddress `json:"creator"`
CodeHash tmbytes.HexBytes `json:"code_hash"`
Source string `json:"source"`
Builder string `json:"builder"`
}

func queryCodeList(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]byte, error) {
Expand All @@ -153,6 +180,8 @@ func queryCodeList(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]byt
ID: i,
Creator: res.Creator,
CodeHash: res.CodeHash,
Source: res.Source,
Builder: res.Builder,
})
}

Expand Down

0 comments on commit e4be8f1

Please sign in to comment.