Skip to content

Commit

Permalink
Add cli client for contract module
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanfrey committed Jun 16, 2019
1 parent e001674 commit d89f9fc
Showing 1 changed file with 151 additions and 0 deletions.
151 changes: 151 additions & 0 deletions x/contract/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package cli

import (
"strconv"

"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/utils"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
auth "github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/contract"
)

const (
flagTo = "to"
flagAmount = "amount"
)

// GetTxCmd returns the transaction commands for this module
func GetTxCmd(cdc *codec.Codec) *cobra.Command {
txCmd := &cobra.Command{
Use: contract.ModuleName,
Short: "Contract transaction subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: utils.ValidateCmd,
}
txCmd.AddCommand(
StoreCodeCmd(cdc),
)
return txCmd
}

// StoreCodeCmd will upload code to be reused.
func StoreCodeCmd(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "store [from_key_or_address] [wasm file]",
Short: "Upload a wasm binary",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
txBldr := auth.NewTxBuilderFromCLI().WithTxEncoder(utils.GetTxEncoder(cdc))
cliCtx := context.NewCLIContextWithFrom(args[0]).
WithCodec(cdc).
WithAccountDecoder(cdc)

// parse coins trying to be sent
wasm, err := contract.ReadWasmFromFile(args[1])
if err != nil {
return err
}

// build and sign the transaction, then broadcast to Tendermint
msg := contract.MsgStoreCode{
Sender: cliCtx.GetFromAddress(),
WASMByteCode: wasm,
}
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}

cmd = client.PostCommands(cmd)[0]

return cmd
}

// CreateContractCmd will instantiate a contract from previously uploaded code.
func CreateContractCmd(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)

// get the id of the code to instantiate
codeID, err := strconv.Atoi(args[1])
if err != nil {
return err
}

// parse coins trying to be sent
coins, err := sdk.ParseCoins(args[2])
if err != nil {
return err
}

initMsg := args[3]

// build and sign the transaction, then broadcast to Tendermint
msg := contract.MsgCreateContract{
Sender: cliCtx.GetFromAddress(),
Code: contract.CodeID(codeID),
InitFunds: coins,
InitMsg: []byte(initMsg),
}
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}

cmd = client.PostCommands(cmd)[0]

return cmd
}

// SendContractCmd will instantiate a contract from previously uploaded code.
func SendContractCmd(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
}

// parse coins trying to be sent
coins, err := sdk.ParseCoins(args[2])
if err != nil {
return err
}

sendMsg := args[3]

// build and sign the transaction, then broadcast to Tendermint
msg := contract.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
}

0 comments on commit d89f9fc

Please sign in to comment.