Merge pull request #68 from cosmwasm/rest-server-fixes
Rest server fixes
This commit is contained in:
@@ -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")
|
||||
@@ -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)
|
||||
|
||||
@@ -2,6 +2,7 @@ package keeper
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"sort"
|
||||
"strconv"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
@@ -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 (
|
||||
@@ -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")
|
||||
@@ -72,6 +76,7 @@ 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())
|
||||
@@ -79,6 +84,26 @@ func queryContractList(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([
|
||||
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 {
|
||||
@@ -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) {
|
||||
@@ -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,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user