package testutil import ( "fmt" "github.com/spf13/cobra" "github.com/tendermint/tendermint/libs/cli" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/client/tx" "github.com/cosmos/cosmos-sdk/testutil" clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/msgservice" bankcli "github.com/cosmos/cosmos-sdk/x/bank/client/cli" "github.com/cosmos/cosmos-sdk/x/bank/types" ) func MsgSendExec(clientCtx client.Context, from, to, amount fmt.Stringer, extraArgs ...string) (testutil.BufferWriter, error) { args := []string{from.String(), to.String(), amount.String()} args = append(args, extraArgs...) return clitestutil.ExecTestCLICmd(clientCtx, bankcli.NewSendTxCmd(), args) } func QueryBalancesExec(clientCtx client.Context, address fmt.Stringer, extraArgs ...string) (testutil.BufferWriter, error) { args := []string{address.String(), fmt.Sprintf("--%s=json", cli.OutputFlag)} args = append(args, extraArgs...) return clitestutil.ExecTestCLICmd(clientCtx, bankcli.GetBalancesCmd(), args) } // newSendTxMsgServiceCmd is just for the purpose of testing ServiceMsg's in an end-to-end case. It is effectively // NewSendTxCmd but using MsgClient. func newSendTxMsgServiceCmd() *cobra.Command { cmd := &cobra.Command{ Use: "send [from_key_or_address] [to_address] [amount]", Short: `Send funds from one account to another. Note, the'--from' flag is ignored as it is implied from [from_key_or_address].`, Args: cobra.ExactArgs(3), RunE: func(cmd *cobra.Command, args []string) error { cmd.Flags().Set(flags.FlagFrom, args[0]) clientCtx, err := client.GetClientTxContext(cmd) if err != nil { return err } toAddr, err := sdk.AccAddressFromBech32(args[1]) if err != nil { return err } coins, err := sdk.ParseCoinsNormalized(args[2]) if err != nil { return err } msg := types.NewMsgSend(clientCtx.GetFromAddress(), toAddr, coins) svcMsgClientConn := &msgservice.ServiceMsgClientConn{} bankMsgClient := types.NewMsgClient(svcMsgClientConn) _, err = bankMsgClient.Send(cmd.Context(), msg) if err != nil { return err } return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), svcMsgClientConn.GetMsgs()...) }, } flags.AddTxFlagsToCmd(cmd) return cmd } // ServiceMsgSendExec is a temporary method to test Msg services in CLI using // x/bank's Msg/Send service. After https://github.com/cosmos/cosmos-sdk/issues/7541 // is merged, this method should be removed, and we should prefer MsgSendExec // instead. func ServiceMsgSendExec(clientCtx client.Context, from, to, amount fmt.Stringer, extraArgs ...string) (testutil.BufferWriter, error) { args := []string{from.String(), to.String(), amount.String()} args = append(args, extraArgs...) return clitestutil.ExecTestCLICmd(clientCtx, newSendTxMsgServiceCmd(), args) }