-
Notifications
You must be signed in to change notification settings - Fork 532
/
node_tx_bank_send.go
77 lines (62 loc) · 1.64 KB
/
node_tx_bank_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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package ignitecmd
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/spf13/cobra"
"github.com/ignite/cli/v29/ignite/pkg/cliui"
)
func NewNodeTxBankSend() *cobra.Command {
c := &cobra.Command{
Use: "send [from_account_or_address] [to_account_or_address] [amount]",
Short: "Send funds from one account to another.",
RunE: nodeTxBankSendHandler,
Args: cobra.ExactArgs(3),
}
return c
}
func nodeTxBankSendHandler(cmd *cobra.Command, args []string) error {
session := cliui.New()
defer session.End()
var (
fromAccountInput = args[0]
toAccountInput = args[1]
amount = args[2]
generateOnly = getGenerateOnly(cmd)
)
client, err := newNodeCosmosClient(cmd)
if err != nil {
return err
}
// fromAccountInput must be an account of the keyring
fromAccount, err := client.Account(fromAccountInput)
if err != nil {
return err
}
// toAccountInput can be an account of the keyring or a raw address
toAddress, err := client.Address(toAccountInput)
if err != nil {
toAddress = toAccountInput
}
coins, err := sdk.ParseCoinsNormalized(amount)
if err != nil {
return err
}
tx, err := client.BankSendTx(cmd.Context(), fromAccount, toAddress, coins)
if err != nil {
return err
}
if generateOnly {
json, err := tx.EncodeJSON()
if err != nil {
return err
}
return session.Println(string(json))
}
session.StartSpinner("Sending transaction...")
resp, err := tx.Broadcast(cmd.Context())
if err != nil {
return err
}
session.Printf("Transaction broadcast successful! (hash = %s)\n", resp.TxHash)
session.Printf("%s sent from %s to %s\n", amount, fromAccountInput, toAccountInput)
return nil
}