Turn --from into a positional argument in gaiacli tx send (#4146)

Closes: #4142
This commit is contained in:
Alessio Treglia 2019-04-18 19:48:52 +01:00 committed by GitHub
parent 1aa3df2876
commit 93e8f467fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 24 additions and 23 deletions

View File

@ -0,0 +1,2 @@
#4142 Turn gaiacli tx send's --from into a required argument.
New shorter syntax: `gaiacli tx send FROM TO AMOUNT`

View File

@ -58,9 +58,10 @@ 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)
@ -68,7 +69,6 @@ func NewCLIContext() CLIContext {
rpc = rpcclient.NewHTTP(nodeURI, "/websocket")
}
from := viper.GetString(client.FlagFrom)
genOnly := viper.GetBool(client.FlagGenerateOnly)
fromAddress, fromName, err := GetFromFields(from, genOnly)
if err != nil {
@ -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 {

View File

@ -307,7 +307,7 @@ 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)
}
@ -315,7 +315,7 @@ 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)
}

View File

@ -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
@ -219,9 +218,8 @@ 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
```
@ -229,9 +227,8 @@ 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
```
@ -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:

View File

@ -20,12 +20,12 @@ 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)
@ -33,27 +33,24 @@ func SendTxCmd(cdc *codec.Codec) *cobra.Command {
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
}