forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
151 additions
and
0 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
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 | ||
} |