This repository has been archived by the owner on Nov 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
multi_send.go
52 lines (48 loc) · 1.56 KB
/
multi_send.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main
import (
"bufio"
"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"
"github.com/cosmos/cosmos-sdk/x/auth/client/utils"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/spf13/cobra"
)
// MultiSendTxCmd creates a multi-send tx and signs it with the given key.
func MultiSendTxCmd(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "multi-send [amount] [to_address1] [to_address2] ....",
Short: "Create and sign a multi-send tx",
Args: cobra.MinimumNArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
inBuf := bufio.NewReader(cmd.InOrStdin())
cliCtx := context.NewCLIContext().WithCodec(cdc)
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))
// Parse the coins we are trying to send
coins, err := sdk.ParseCoins(args[0])
if err != nil {
return err
}
accounts := args[1:]
inputCoins := sdk.NewCoins()
outputs := make([]bank.Output, 0, len(accounts))
for _, acc := range accounts {
to, err := sdk.AccAddressFromBech32(acc)
if err != nil {
return err
}
outputs = append(outputs, bank.NewOutput(to, coins))
inputCoins = inputCoins.Add(coins...)
}
msg := bank.NewMsgMultiSend(
[]bank.Input{bank.NewInput(cliCtx.GetFromAddress(), inputCoins)},
outputs,
)
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}
cmd = flags.PostCommands(cmd)[0]
return cmd
}