Complete tx command and wire to module

This commit is contained in:
Ethan Frey 2019-11-22 17:30:33 +01:00
parent 6ddd637733
commit 0b74b12b0e
2 changed files with 79 additions and 83 deletions

View File

@ -1,11 +1,12 @@
package client
package cli
import (
"bufio"
// "strconv"
"io/ioutil"
"strconv"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
@ -31,11 +32,11 @@ func GetTxCmd(cdc *codec.Codec) *cobra.Command {
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
txCmd.AddCommand(
txCmd.AddCommand(client.PostCommands(
StoreCodeCmd(cdc),
// InstantiateContractCmd(cdc),
// ExecuteContractCmd(cdc),
)
InstantiateContractCmd(cdc),
ExecuteContractCmd(cdc),
)...)
return txCmd
}
@ -64,92 +65,85 @@ func StoreCodeCmd(cdc *codec.Codec) *cobra.Command {
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}
cmd = client.PostCommands(cmd)[0]
return cmd
}
// // InstantiateContractCmd will instantiate a contract from previously uploaded code.
// func InstantiateContractCmd(cdc *codec.Codec) *cobra.Command {
// cmd := &cobra.Command{
// Use: "create [from_key_or_address] [code_id_int64] [coins] [json_encoded_init_args]",
// Short: "Instantiate a wasm contract",
// Args: cobra.ExactArgs(4),
// RunE: func(cmd *cobra.Command, args []string) error {
// txBldr := auth.NewTxBuilderFromCLI().WithTxEncoder(utils.GetTxEncoder(cdc))
// cliCtx := context.NewCLIContextWithFrom(args[0]).
// WithCodec(cdc).
// WithAccountDecoder(cdc)
// InstantiateContractCmd will instantiate a contract from previously uploaded code.
func InstantiateContractCmd(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "create [from_key_or_address] [code_id_int64] [json_encoded_init_args]",
Short: "Instantiate a wasm contract",
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.NewCLIContextWithInputAndFrom(inBuf, args[0]).WithCodec(cdc)
// // get the id of the code to instantiate
// codeID, err := strconv.Atoi(args[1])
// if err != nil {
// return err
// }
// get the id of the code to instantiate
codeID, err := strconv.ParseUint(args[1], 10, 64)
if err != nil {
return err
}
// // parse coins trying to be sent
// coins, err := sdk.ParseCoins(args[2])
// if err != nil {
// return err
// }
amounstStr := viper.GetString(flagAmount)
amount, err := sdk.ParseCoins(amounstStr)
if err != nil {
return err
}
// initMsg := args[3]
initMsg := args[2]
// // build and sign the transaction, then broadcast to Tendermint
// msg := MsgCreateContract{
// Sender: cliCtx.GetFromAddress(),
// Code: CodeID(codeID),
// InitFunds: coins,
// InitMsg: []byte(initMsg),
// }
// return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
// },
// }
// build and sign the transaction, then broadcast to Tendermint
msg := types.MsgInstantiateContract{
Sender: cliCtx.GetFromAddress(),
Code: codeID,
InitFunds: amount,
InitMsg: []byte(initMsg),
}
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}
// cmd = client.PostCommands(cmd)[0]
cmd.Flags().String(flagAmount, "", "Coins to send to the contract during instantiation")
return cmd
}
// return cmd
// }
// ExecuteContractCmd will instantiate a contract from previously uploaded code.
func ExecuteContractCmd(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "send [from_key_or_address] [contract_addr_bech32] [json_encoded_send_args]",
Short: "Execute a command on a wasm contract",
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.NewCLIContextWithInputAndFrom(inBuf, args[0]).WithCodec(cdc)
// // ExecuteContractCmd will instantiate a contract from previously uploaded code.
// func ExecuteContractCmd(cdc *codec.Codec) *cobra.Command {
// cmd := &cobra.Command{
// Use: "send [from_key_or_address] [contract_addr_bech32] [coins] [json_encoded_send_args]",
// Short: "Instantiate a wasm contract",
// Args: cobra.ExactArgs(4),
// RunE: func(cmd *cobra.Command, args []string) error {
// txBldr := auth.NewTxBuilderFromCLI().WithTxEncoder(utils.GetTxEncoder(cdc))
// cliCtx := context.NewCLIContextWithFrom(args[0]).
// WithCodec(cdc).
// WithAccountDecoder(cdc)
// get the id of the code to instantiate
contractAddr, err := sdk.AccAddressFromBech32(args[1])
if err != nil {
return err
}
// // get the id of the code to instantiate
// contractAddr, err := sdk.AccAddressFromBech32(args[1])
// if err != nil {
// return err
// }
amounstStr := viper.GetString(flagAmount)
amount, err := sdk.ParseCoins(amounstStr)
if err != nil {
return err
}
// // parse coins trying to be sent
// coins, err := sdk.ParseCoins(args[2])
// if err != nil {
// return err
// }
execMsg := args[3]
// sendMsg := args[3]
// build and sign the transaction, then broadcast to Tendermint
msg := types.MsgExecuteContract{
Sender: cliCtx.GetFromAddress(),
Contract: contractAddr,
SentFunds: amount,
Msg: []byte(execMsg),
}
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}
// // build and sign the transaction, then broadcast to Tendermint
// msg := MsgSendContract{
// Sender: cliCtx.GetFromAddress(),
// Contract: contractAddr,
// Payment: coins,
// Msg: []byte(sendMsg),
// }
// return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
// },
// }
// cmd = client.PostCommands(cmd)[0]
// return cmd
// }
cmd.Flags().String(flagAmount, "", "Coins to send to the contract along with command")
return cmd
}

View File

@ -12,7 +12,7 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
// "github.com/cosmwasm/wasmd/x/wasm/client/cli"
"github.com/cosmwasm/wasmd/x/wasm/client/cli"
// "github.com/cosmwasm/wasmd/x/wasm/client/rest"
)
@ -57,7 +57,9 @@ func (AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router
}
// GetTxCmd returns the root tx command for the wasm module.
func (AppModuleBasic) GetTxCmd(_ *codec.Codec) *cobra.Command { return nil }
func (AppModuleBasic) GetTxCmd(cdc *codec.Codec) *cobra.Command {
return cli.GetTxCmd(cdc)
}
// GetQueryCmd returns no root query command for the wasm module.
func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command {