-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement CLI/REST server support for new messages (#131)
* Cleanup ContractInfo type * Add admin to contract instanciation * Add cli commands for new TX * Add rest support for new TX * Update changelog * Make optional admin flag for better UX * Add flag to not accidentally clear admin on update
- Loading branch information
Showing
9 changed files
with
229 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package cli | ||
|
||
import ( | ||
"bufio" | ||
"errors" | ||
"strconv" | ||
|
||
"github.com/CosmWasm/wasmd/x/wasm/internal/types" | ||
"github.com/cosmos/cosmos-sdk/client/context" | ||
"github.com/cosmos/cosmos-sdk/codec" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
"github.com/cosmos/cosmos-sdk/x/auth" | ||
"github.com/cosmos/cosmos-sdk/x/auth/client/utils" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
// MigrateContractCmd will migrate a contract to a new code version | ||
func MigrateContractCmd(cdc *codec.Codec) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "migrate [contract_addr_bech32] [new_code_id_int64] [json_encoded_migration_args]", | ||
Short: "Migrate a wasm contract to a new code version", | ||
Args: cobra.ExactArgs(3), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
inBuf := bufio.NewReader(cmd.InOrStdin()) | ||
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc)) | ||
cliCtx := context.NewCLIContextWithInput(inBuf).WithCodec(cdc) | ||
|
||
contractAddr, err := sdk.AccAddressFromBech32(args[0]) | ||
if err != nil { | ||
return sdkerrors.Wrap(err, "contract") | ||
} | ||
|
||
// get the id of the code to instantiate | ||
codeID, err := strconv.ParseUint(args[1], 10, 64) | ||
if err != nil { | ||
return sdkerrors.Wrap(err, "code id") | ||
} | ||
|
||
migrateMsg := args[2] | ||
|
||
msg := types.MsgMigrateContract{ | ||
Sender: cliCtx.GetFromAddress(), | ||
Contract: contractAddr, | ||
Code: codeID, | ||
MigrateMsg: []byte(migrateMsg), | ||
} | ||
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg}) | ||
}, | ||
} | ||
return cmd | ||
} | ||
|
||
// UpdateContractAdminCmd sets or clears an admin for a contract | ||
func UpdateContractAdminCmd(cdc *codec.Codec) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "set-contract-admin [contract_addr_bech32] [new_admin_addr_bech32]", | ||
Short: "Set new admin for a contract. Can be empty to prevent further migrations", | ||
Args: cobra.RangeArgs(1, 2), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
inBuf := bufio.NewReader(cmd.InOrStdin()) | ||
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc)) | ||
cliCtx := context.NewCLIContextWithInput(inBuf).WithCodec(cdc) | ||
|
||
contractAddr, err := sdk.AccAddressFromBech32(args[0]) | ||
if err != nil { | ||
return sdkerrors.Wrap(err, "contract") | ||
} | ||
var newAdmin sdk.AccAddress | ||
if len(args) > 1 && len(args[1]) != 0 { | ||
newAdmin, err = sdk.AccAddressFromBech32(args[1]) | ||
if err != nil { | ||
return sdkerrors.Wrap(err, "new admin") | ||
} | ||
} else { | ||
// safety net to not accidentally clear an admin | ||
clearAdmin := viper.GetBool(flagNoAdmin) | ||
if !clearAdmin { | ||
return errors.New("new admin address required or no admin flag") | ||
} | ||
} | ||
|
||
msg := types.MsgUpdateAdministrator{ | ||
Sender: cliCtx.GetFromAddress(), | ||
Contract: contractAddr, | ||
NewAdmin: newAdmin, | ||
} | ||
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg}) | ||
}, | ||
} | ||
cmd.Flags().Bool(flagNoAdmin, false, "Remove admin which disables future admin updates and migrations") | ||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package rest | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/CosmWasm/wasmd/x/wasm/internal/types" | ||
"github.com/cosmos/cosmos-sdk/client/context" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/types/rest" | ||
"github.com/cosmos/cosmos-sdk/x/auth/client/utils" | ||
"github.com/gorilla/mux" | ||
) | ||
|
||
func registerNewTxRoutes(cliCtx context.CLIContext, r *mux.Router) { | ||
r.HandleFunc("/wasm/contract/{contractAddr}/admin", setContractAdminHandlerFn(cliCtx)).Methods("PUT") | ||
r.HandleFunc("/wasm/contract/{contractAddr}/code", migrateContractHandlerFn(cliCtx)).Methods("PUT") | ||
} | ||
|
||
type migrateContractReq struct { | ||
BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"` | ||
Admin sdk.AccAddress `json:"admin,omitempty" yaml:"admin"` | ||
codeID uint64 `json:"code_id" yaml:"code_id"` | ||
MigrateMsg []byte `json:"migrate_msg,omitempty" yaml:"migrate_msg"` | ||
} | ||
type updateContractAdministrateReq struct { | ||
BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"` | ||
Admin sdk.AccAddress `json:"admin,omitempty" yaml:"admin"` | ||
} | ||
|
||
func setContractAdminHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
var req updateContractAdministrateReq | ||
if !rest.ReadRESTReq(w, r, cliCtx.Codec, &req) { | ||
return | ||
} | ||
vars := mux.Vars(r) | ||
contractAddr := vars["contractAddr"] | ||
|
||
req.BaseReq = req.BaseReq.Sanitize() | ||
if !req.BaseReq.ValidateBasic(w) { | ||
return | ||
} | ||
|
||
contractAddress, err := sdk.AccAddressFromBech32(contractAddr) | ||
if err != nil { | ||
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) | ||
return | ||
} | ||
|
||
msg := types.MsgUpdateAdministrator{ | ||
Sender: cliCtx.GetFromAddress(), | ||
NewAdmin: req.Admin, | ||
Contract: contractAddress, | ||
} | ||
if err = msg.ValidateBasic(); err != nil { | ||
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) | ||
return | ||
} | ||
|
||
utils.WriteGenerateStdTxResponse(w, cliCtx, req.BaseReq, []sdk.Msg{msg}) | ||
} | ||
} | ||
|
||
func migrateContractHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
var req migrateContractReq | ||
if !rest.ReadRESTReq(w, r, cliCtx.Codec, &req) { | ||
return | ||
} | ||
vars := mux.Vars(r) | ||
contractAddr := vars["contractAddr"] | ||
|
||
req.BaseReq = req.BaseReq.Sanitize() | ||
if !req.BaseReq.ValidateBasic(w) { | ||
return | ||
} | ||
|
||
contractAddress, err := sdk.AccAddressFromBech32(contractAddr) | ||
if err != nil { | ||
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) | ||
return | ||
} | ||
|
||
msg := types.MsgMigrateContract{ | ||
Sender: cliCtx.GetFromAddress(), | ||
Contract: contractAddress, | ||
Code: req.codeID, | ||
MigrateMsg: req.MigrateMsg, | ||
} | ||
if err = msg.ValidateBasic(); err != nil { | ||
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) | ||
return | ||
} | ||
|
||
utils.WriteGenerateStdTxResponse(w, cliCtx, req.BaseReq, []sdk.Msg{msg}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters