Skip to content

Commit

Permalink
Turn --from into a positional argument in gaiacli tx send (#4146)
Browse files Browse the repository at this point in the history
Closes: #4142
  • Loading branch information
alessio authored Apr 18, 2019
1 parent 1aa3df2 commit 93e8f46
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 23 deletions.
2 changes: 2 additions & 0 deletions .pending/breaking/gaiacli/4142-Turn-gaiacli-tx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#4142 Turn gaiacli tx send's --from into a required argument.
New shorter syntax: `gaiacli tx send FROM TO AMOUNT`
12 changes: 8 additions & 4 deletions client/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,17 @@ type CLIContext struct {
SkipConfirm bool
}

// NewCLIContext returns a new initialized CLIContext with parameters from the
// command line using Viper.
func NewCLIContext() CLIContext {
// NewCLIContextWithFrom returns a new initialized CLIContext with parameters from the
// command line using Viper. It takes a key name or address and populates the FromName and
// FromAddress field accordingly.
func NewCLIContextWithFrom(from string) CLIContext {
var rpc rpcclient.Client

nodeURI := viper.GetString(client.FlagNode)
if nodeURI != "" {
rpc = rpcclient.NewHTTP(nodeURI, "/websocket")
}

from := viper.GetString(client.FlagFrom)
genOnly := viper.GetBool(client.FlagGenerateOnly)
fromAddress, fromName, err := GetFromFields(from, genOnly)
if err != nil {
Expand Down Expand Up @@ -104,6 +104,10 @@ func NewCLIContext() CLIContext {
}
}

// NewCLIContext returns a new initialized CLIContext with parameters from the
// command line using Viper.
func NewCLIContext() CLIContext { return NewCLIContextWithFrom(viper.GetString(client.FlagFrom)) }

func createVerifier() tmlite.Verifier {
trustNodeDefined := viper.IsSet(client.FlagTrustNode)
if !trustNodeDefined {
Expand Down
4 changes: 2 additions & 2 deletions cmd/gaia/cli_test/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,15 +307,15 @@ func (f *Fixtures) CLIConfig(key, value string, flags ...string) {

// TxSend is gaiacli tx send
func (f *Fixtures) TxSend(from string, to sdk.AccAddress, amount sdk.Coin, flags ...string) (bool, string, string) {
cmd := fmt.Sprintf("%s tx send %s %s %v --from=%s", f.GaiacliBinary, to, amount, f.Flags(), from)
cmd := fmt.Sprintf("%s tx send %s %s %s %v", f.GaiacliBinary, from, to, amount, f.Flags())
return executeWriteRetStdStreams(f.T, addFlags(cmd, flags), client.DefaultKeyPass)
}

func (f *Fixtures) txSendWithConfirm(
from string, to sdk.AccAddress, amount sdk.Coin, confirm string, flags ...string,
) (bool, string, string) {

cmd := fmt.Sprintf("%s tx send %s %s %v --from=%s", f.GaiacliBinary, to, amount, f.Flags(), from)
cmd := fmt.Sprintf("%s tx send %s %s %s %v", f.GaiacliBinary, from, to, amount, f.Flags())
return executeWriteRetStdStreams(f.T, addFlags(cmd, flags), confirm, client.DefaultKeyPass)
}

Expand Down
14 changes: 6 additions & 8 deletions docs/cosmos-hub/gaiacli.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,12 @@ When you query an account balance with zero tokens, you will get this error: `No
The following command could be used to send coins from one account to another:

```bash
gaiacli tx send <destination_cosmos> 10faucetToken \
--chain-id=<chain_id> \
--from=<key_name> \
gaiacli tx send <sender_key_name_or_address> <recipient_address> 10faucetToken \
--chain-id=<chain_id>
```

::: warning Note
The `--amount` flag accepts the format `--amount=<value|coin_name>`.
The `amount` argument accepts the format `<value|coin_name>`.
:::

::: tip Note
Expand All @@ -219,19 +218,17 @@ You can simulate a transaction without actually broadcasting it by appending the
`--dry-run` flag to the command line:

```bash
gaiacli tx send <destination_cosmosaccaddr> 10faucetToken \
gaiacli tx send <sender_key_name_or_address> <destination_cosmosaccaddr> 10faucetToken \
--chain-id=<chain_id> \
--from=<key_name> \
--dry-run
```

Furthermore, you can build a transaction and print its JSON format to STDOUT by
appending `--generate-only` to the list of the command line arguments:

```bash
gaiacli tx send <destination_cosmosaccaddr> 10faucetToken \
gaiacli tx send <sender_address> <recipient_address> 10faucetToken \
--chain-id=<chain_id> \
--from=<key_name> \
--generate-only > unsignedSendTx.json
```

Expand All @@ -244,6 +241,7 @@ gaiacli tx sign \

::: tip Note
The `--generate-only` flag prevents `gaiacli` from accessing the local keybase.
Thus when such flag is supplied `<sender_key_name_or_address>` must be an address.
:::

You can validate the transaction's signatures by typing the following:
Expand Down
15 changes: 6 additions & 9 deletions x/bank/client/cli/sendtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,40 +20,37 @@ const (
// SendTxCmd will create a send tx and sign it with the given key.
func SendTxCmd(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "send [to_address] [amount]",
Use: "send [from_key_or_address] [to_address] [amount]",
Short: "Create and sign a send tx",
Args: cobra.ExactArgs(2),
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
txBldr := authtxb.NewTxBuilderFromCLI().WithTxEncoder(utils.GetTxEncoder(cdc))
cliCtx := context.NewCLIContext().
cliCtx := context.NewCLIContextWithFrom(args[0]).
WithCodec(cdc).
WithAccountDecoder(cdc)

if err := cliCtx.EnsureAccountExists(); err != nil {
return err
}

to, err := sdk.AccAddressFromBech32(args[0])
to, err := sdk.AccAddressFromBech32(args[1])
if err != nil {
return err
}

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

from := cliCtx.GetFromAddress()

// build and sign the transaction, then broadcast to Tendermint
msg := bank.NewMsgSend(from, to, coins)
msg := bank.NewMsgSend(cliCtx.GetFromAddress(), to, coins)
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg}, false)
},
}

cmd = client.PostCommands(cmd)[0]
cmd.MarkFlagRequired(client.FlagFrom)

return cmd
}

0 comments on commit 93e8f46

Please sign in to comment.