Add CLI + REST support for contract history

This commit is contained in:
Alex Peters
2020-07-20 14:42:05 +02:00
parent 430da1dbf5
commit e62f2cac44
2 changed files with 51 additions and 2 deletions

View File

@@ -36,6 +36,7 @@ func GetQueryCmd(cdc *codec.Codec) *cobra.Command {
GetCmdListContractByCode(cdc),
GetCmdQueryCode(cdc),
GetCmdGetContractInfo(cdc),
GetCmdGetContractHistory(cdc),
GetCmdGetContractState(cdc),
)...)
return queryCmd
@@ -267,6 +268,32 @@ func GetCmdGetContractStateSmart(cdc *codec.Codec) *cobra.Command {
return cmd
}
// GetCmdGetContractHistory prints the code history for a given contract
func GetCmdGetContractHistory(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "contract-history [bech32_address]",
Short: "Prints out the code history for a contract given its address",
Long: "Prints out the code history for a contract given its address",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
addr, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return err
}
route := fmt.Sprintf("custom/%s/%s/%s", types.QuerierRoute, keeper.QueryContractHistory, addr.String())
res, _, err := cliCtx.Query(route)
if err != nil {
return err
}
fmt.Println(string(res))
return nil
},
}
}
type argumentDecoder struct {
// dec is the default decoder
dec func(string) ([]byte, error)

View File

@@ -23,6 +23,7 @@ func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) {
r.HandleFunc("/wasm/code/{codeID}/contracts", listContractsByCodeHandlerFn(cliCtx)).Methods("GET")
r.HandleFunc("/wasm/contract/{contractAddr}", queryContractHandlerFn(cliCtx)).Methods("GET")
r.HandleFunc("/wasm/contract/{contractAddr}/state", queryContractStateAllHandlerFn(cliCtx)).Methods("GET")
r.HandleFunc("/wasm/contract/{contractAddr}/history", queryContractHistoryFn(cliCtx)).Methods("GET")
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")
}
@@ -153,7 +154,6 @@ func queryContractStateAllHandlerFn(cliCtx context.CLIContext) http.HandlerFunc
rest.PostProcessResponse(w, cliCtx, resultData)
}
}
func queryContractStateRawHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
decoder := newArgDecoder(hex.DecodeString)
@@ -230,6 +230,29 @@ func queryContractStateSmartHandlerFn(cliCtx context.CLIContext) http.HandlerFun
}
}
func queryContractHistoryFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
addr, err := sdk.AccAddressFromBech32(mux.Vars(r)["contractAddr"])
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
if !ok {
return
}
route := fmt.Sprintf("custom/%s/%s/%s", types.QuerierRoute, keeper.QueryContractHistory, addr.String())
res, height, err := cliCtx.Query(route)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
cliCtx = cliCtx.WithHeight(height)
rest.PostProcessResponse(w, cliCtx, json.RawMessage(res))
}
}
type argumentDecoder struct {
// dec is the default decoder
dec func(string) ([]byte, error)
@@ -241,7 +264,6 @@ func newArgDecoder(def func(string) ([]byte, error)) *argumentDecoder {
}
func (a *argumentDecoder) DecodeString(s string) ([]byte, error) {
switch a.encoding {
case "hex":
return hex.DecodeString(s)