-
Notifications
You must be signed in to change notification settings - Fork 16
/
tx.go
104 lines (81 loc) · 2.26 KB
/
tx.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"bufio"
"fmt"
"os"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/input"
txns "github.com/cosmos/cosmos-sdk/client/tx"
"github.com/spf13/pflag"
)
func prepareFactory(clientCtx client.Context, txf txns.Factory) (txns.Factory, error) {
from := clientCtx.GetFromAddress()
if err := txf.AccountRetriever().EnsureExists(clientCtx, from); err != nil {
return txf, err
}
initNum, initSeq := txf.AccountNumber(), txf.Sequence()
if initNum == 0 || initSeq == 0 {
num, seq, err := txf.AccountRetriever().GetAccountNumberSequence(clientCtx, from)
if err != nil {
return txf, err
}
if initNum == 0 {
txf = txf.WithAccountNumber(num)
}
if initSeq == 0 {
txf = txf.WithSequence(seq)
}
}
return txf, nil
}
func SendTx(clientCtx client.Context, flagSet *pflag.FlagSet, msgs ...sdk.Msg) (*sdk.TxResponse, error) {
txf := txns.NewFactoryCLI(clientCtx, flagSet)
txf, err := prepareFactory(clientCtx, txf)
if err != nil {
return nil, err
}
if txf.SimulateAndExecute() || clientCtx.Simulate {
_, adjusted, err := txns.CalculateGas(clientCtx, txf, msgs...)
if err != nil {
return nil, err
}
txf = txf.WithGas(adjusted)
_, _ = fmt.Fprintf(os.Stderr, "%s\n", txns.GasEstimateResponse{GasEstimate: txf.Gas()})
}
if clientCtx.Simulate {
return nil, nil
}
tx, err := txns.BuildUnsignedTx(txf, msgs...)
if err != nil {
return nil, err
}
if !clientCtx.SkipConfirm {
out, err := clientCtx.TxConfig.TxJSONEncoder()(tx.GetTx())
if err != nil {
return nil, err
}
_, _ = fmt.Fprintf(os.Stderr, "%s\n\n", out)
buf := bufio.NewReader(os.Stdin)
ok, err := input.GetConfirmation("confirm transaction before signing and broadcasting", buf, os.Stderr)
if err != nil || !ok {
_, _ = fmt.Fprintf(os.Stderr, "%s\n", "cancelled transaction")
return nil, err
}
}
tx.SetFeeGranter(clientCtx.GetFeeGranterAddress())
err = txns.Sign(txf, clientCtx.GetFromName(), tx, true)
if err != nil {
return nil, err
}
txBytes, err := clientCtx.TxConfig.TxEncoder()(tx.GetTx())
if err != nil {
return nil, err
}
// broadcast to a Tendermint node
res, err := clientCtx.BroadcastTx(txBytes)
if err != nil {
return nil, err
}
return res, err
}