github.com/cosmos/cosmos-sdk@v0.50.10/x/auth/client/cli/broadcast.go (about)

     1  package cli
     2  
     3  import (
     4  	"errors"
     5  	"strings"
     6  
     7  	"github.com/spf13/cobra"
     8  
     9  	"github.com/cosmos/cosmos-sdk/client"
    10  	"github.com/cosmos/cosmos-sdk/client/flags"
    11  	authclient "github.com/cosmos/cosmos-sdk/x/auth/client"
    12  )
    13  
    14  // GetBroadcastCommand returns the tx broadcast command.
    15  func GetBroadcastCommand() *cobra.Command {
    16  	cmd := &cobra.Command{
    17  		Use:   "broadcast [file_path]",
    18  		Short: "Broadcast transactions generated offline",
    19  		Long: strings.TrimSpace(`Broadcast transactions created with the --generate-only
    20  flag and signed with the sign command. Read a transaction from [file_path] and
    21  broadcast it to a node. If you supply a dash (-) argument in place of an input
    22  filename, the command reads from standard input.
    23  
    24  $ <appd> tx broadcast ./mytxn.json
    25  `),
    26  		Args: cobra.ExactArgs(1),
    27  		RunE: func(cmd *cobra.Command, args []string) error {
    28  			clientCtx, err := client.GetClientTxContext(cmd)
    29  			if err != nil {
    30  				return err
    31  			}
    32  
    33  			if offline, _ := cmd.Flags().GetBool(flags.FlagOffline); offline {
    34  				return errors.New("cannot broadcast tx during offline mode")
    35  			}
    36  
    37  			stdTx, err := authclient.ReadTxFromFile(clientCtx, args[0])
    38  			if err != nil {
    39  				return err
    40  			}
    41  
    42  			txBytes, err := clientCtx.TxConfig.TxEncoder()(stdTx)
    43  			if err != nil {
    44  				return err
    45  			}
    46  
    47  			res, err := clientCtx.BroadcastTx(txBytes)
    48  			if err != nil {
    49  				return err
    50  			}
    51  
    52  			return clientCtx.PrintProto(res)
    53  		},
    54  	}
    55  
    56  	flags.AddTxFlagsToCmd(cmd)
    57  
    58  	return cmd
    59  }