github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/x/bank/client/cli/tx.go (about)

     1  package cli
     2  
     3  import (
     4  	"bufio"
     5  
     6  	"github.com/spf13/cobra"
     7  
     8  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client"
     9  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context"
    10  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/flags"
    11  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec"
    12  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
    13  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth"
    14  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth/client/utils"
    15  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/bank/internal/types"
    16  )
    17  
    18  // GetTxCmd returns the transaction commands for this module
    19  func GetTxCmd(cdc *codec.Codec) *cobra.Command {
    20  	txCmd := &cobra.Command{
    21  		Use:                        types.ModuleName,
    22  		Short:                      "Bank transaction subcommands",
    23  		DisableFlagParsing:         true,
    24  		SuggestionsMinimumDistance: 2,
    25  		RunE:                       client.ValidateCmd,
    26  	}
    27  	txCmd.AddCommand(
    28  		SendTxCmd(cdc),
    29  	)
    30  	return txCmd
    31  }
    32  
    33  // SendTxCmd will create a send tx and sign it with the given key.
    34  func SendTxCmd(cdc *codec.Codec) *cobra.Command {
    35  	cmd := &cobra.Command{
    36  		Use:   "send [from_key_or_address] [to_address] [amount]",
    37  		Short: "Create and sign a send tx",
    38  		Args:  cobra.ExactArgs(3),
    39  		RunE: func(cmd *cobra.Command, args []string) error {
    40  			inBuf := bufio.NewReader(cmd.InOrStdin())
    41  			txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))
    42  			cliCtx := context.NewCLIContextWithInputAndFrom(inBuf, args[0]).WithCodec(cdc)
    43  
    44  			to, err := sdk.AccAddressFromBech32(args[1])
    45  			if err != nil {
    46  				return err
    47  			}
    48  
    49  			// parse coins trying to be sent
    50  			coins, err := sdk.ParseCoins(args[2])
    51  			if err != nil {
    52  				return err
    53  			}
    54  
    55  			// build and sign the transaction, then broadcast to Tendermint
    56  			msg := types.NewMsgSend(cliCtx.GetFromAddress(), to, coins)
    57  			return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
    58  		},
    59  	}
    60  
    61  	cmd = flags.PostCommands(cmd)[0]
    62  
    63  	return cmd
    64  }