2020-05-04 06:55:16 -07:00
|
|
|
package testutil
|
2020-04-29 08:52:30 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-04-29 19:36:34 -07:00
|
|
|
|
2020-10-28 12:20:45 -07:00
|
|
|
"github.com/spf13/cobra"
|
2020-08-17 05:47:31 -07:00
|
|
|
"github.com/tendermint/tendermint/libs/cli"
|
|
|
|
|
2020-09-08 09:09:50 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2020-10-28 12:20:45 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/tx"
|
2020-07-14 11:37:14 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/testutil"
|
|
|
|
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
|
2020-10-28 12:20:45 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2021-01-25 08:41:30 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/msgservice"
|
2020-07-02 06:02:28 -07:00
|
|
|
bankcli "github.com/cosmos/cosmos-sdk/x/bank/client/cli"
|
2020-10-28 12:20:45 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/bank/types"
|
2020-04-29 08:52:30 -07:00
|
|
|
)
|
|
|
|
|
2020-07-14 11:37:14 -07:00
|
|
|
func MsgSendExec(clientCtx client.Context, from, to, amount fmt.Stringer, extraArgs ...string) (testutil.BufferWriter, error) {
|
2020-07-02 06:02:28 -07:00
|
|
|
args := []string{from.String(), to.String(), amount.String()}
|
|
|
|
args = append(args, extraArgs...)
|
|
|
|
|
2020-07-14 11:37:14 -07:00
|
|
|
return clitestutil.ExecTestCLICmd(clientCtx, bankcli.NewSendTxCmd(), args)
|
|
|
|
}
|
2020-07-02 06:02:28 -07:00
|
|
|
|
2020-07-14 11:37:14 -07:00
|
|
|
func QueryBalancesExec(clientCtx client.Context, address fmt.Stringer, extraArgs ...string) (testutil.BufferWriter, error) {
|
2020-08-17 05:47:31 -07:00
|
|
|
args := []string{address.String(), fmt.Sprintf("--%s=json", cli.OutputFlag)}
|
2020-07-14 11:37:14 -07:00
|
|
|
args = append(args, extraArgs...)
|
2020-07-02 06:02:28 -07:00
|
|
|
|
2020-07-14 11:37:14 -07:00
|
|
|
return clitestutil.ExecTestCLICmd(clientCtx, bankcli.GetBalancesCmd(), args)
|
2020-07-02 06:02:28 -07:00
|
|
|
}
|
2020-10-28 12:20:45 -07:00
|
|
|
|
|
|
|
// 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])
|
2020-12-14 14:09:51 -08:00
|
|
|
clientCtx, err := client.GetClientTxContext(cmd)
|
2020-10-28 12:20:45 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
toAddr, err := sdk.AccAddressFromBech32(args[1])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-16 03:34:54 -08:00
|
|
|
coins, err := sdk.ParseCoinsNormalized(args[2])
|
2020-10-28 12:20:45 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := types.NewMsgSend(clientCtx.GetFromAddress(), toAddr, coins)
|
2021-01-25 08:41:30 -08:00
|
|
|
svcMsgClientConn := &msgservice.ServiceMsgClientConn{}
|
2020-10-28 12:20:45 -07:00
|
|
|
bankMsgClient := types.NewMsgClient(svcMsgClientConn)
|
2021-02-03 01:47:25 -08:00
|
|
|
_, err = bankMsgClient.Send(cmd.Context(), msg)
|
2020-10-28 12:20:45 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-25 08:41:30 -08:00
|
|
|
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), svcMsgClientConn.GetMsgs()...)
|
2020-10-28 12:20:45 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|