2020-05-18 21:33:08 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
2020-08-23 21:41:54 +00:00
|
|
|
authclient "github.com/cosmos/cosmos-sdk/x/auth/client/utils"
|
2020-05-18 21:33:08 +00:00
|
|
|
|
|
|
|
"github.com/cosmos/ethermint/x/faucet/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetTxCmd return faucet sub-command for tx
|
|
|
|
func GetTxCmd(cdc *codec.Codec) *cobra.Command {
|
|
|
|
faucetTxCmd := &cobra.Command{
|
|
|
|
Use: types.ModuleName,
|
|
|
|
Short: "faucet transaction subcommands",
|
|
|
|
DisableFlagParsing: true,
|
|
|
|
SuggestionsMinimumDistance: 2,
|
|
|
|
RunE: client.ValidateCmd,
|
|
|
|
}
|
|
|
|
|
|
|
|
faucetTxCmd.AddCommand(flags.PostCommands(
|
2020-05-20 21:29:26 +00:00
|
|
|
GetCmdRequest(cdc),
|
2020-05-18 21:33:08 +00:00
|
|
|
)...)
|
|
|
|
|
|
|
|
return faucetTxCmd
|
|
|
|
}
|
|
|
|
|
2020-05-20 21:29:26 +00:00
|
|
|
// GetCmdRequest is the CLI command to fund an address with the requested coins
|
|
|
|
func GetCmdRequest(cdc *codec.Codec) *cobra.Command {
|
2020-05-18 21:33:08 +00:00
|
|
|
return &cobra.Command{
|
2020-05-20 21:29:26 +00:00
|
|
|
Use: "request [amount] [other-recipient (optional)]",
|
|
|
|
Short: "request an address with the requested coins",
|
2020-05-18 21:33:08 +00:00
|
|
|
Args: cobra.RangeArgs(1, 2),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
inBuf := bufio.NewReader(cmd.InOrStdin())
|
2020-10-22 20:39:51 +00:00
|
|
|
clientCtx := context.NewCLIContext().WithCodec(cdc)
|
2020-05-18 21:33:08 +00:00
|
|
|
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(authclient.GetTxEncoder(cdc))
|
|
|
|
|
|
|
|
amount, err := sdk.ParseCoins(args[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var recipient sdk.AccAddress
|
|
|
|
if len(args) == 1 {
|
2020-10-22 20:39:51 +00:00
|
|
|
recipient = clientCtx.GetFromAddress()
|
2020-05-18 21:33:08 +00:00
|
|
|
} else {
|
|
|
|
recipient, err = sdk.AccAddressFromBech32(args[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-22 20:39:51 +00:00
|
|
|
msg := types.NewMsgFund(amount, clientCtx.GetFromAddress(), recipient)
|
2020-05-18 21:33:08 +00:00
|
|
|
if err := msg.ValidateBasic(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-22 20:39:51 +00:00
|
|
|
return authclient.GenerateOrBroadcastMsgs(clientCtx, txBldr, []sdk.Msg{msg})
|
2020-05-18 21:33:08 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|