forked from mrsone40/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd.go
More file actions
157 lines (126 loc) · 3.55 KB
/
cmd.go
File metadata and controls
157 lines (126 loc) · 3.55 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package cli
import (
"bytes"
"context"
"github.com/cosmos/gogoproto/proto"
"github.com/spf13/cobra"
sdkmath "cosmossdk.io/math"
"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"
sdk "github.com/cosmos/cosmos-sdk/types"
txtypes "github.com/cosmos/cosmos-sdk/types/tx"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
)
// ExecTestCLICmd builds the client context, mocks the output and executes the command.
func ExecTestCLICmd(clientCtx client.Context, cmd *cobra.Command, extraArgs []string) (testutil.BufferWriter, error) {
cmd.SetArgs(extraArgs)
_, out := testutil.ApplyMockIO(cmd)
clientCtx = clientCtx.WithOutput(out)
ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
if err := cmd.ExecuteContext(ctx); err != nil {
return out, err
}
return out, nil
}
type TestTxConfig struct {
Simulate bool
GenOnly bool
Offline bool
Memo string
Gas uint64
AccNum uint64
Seq uint64
Fee sdk.Coins
IsAsyncBroadcastMode bool
}
func SubmitTestTx(clientCtx client.Context, msg proto.Message, from sdk.AccAddress, config TestTxConfig) (testutil.BufferWriter, error) {
txBuilder := clientCtx.TxConfig.NewTxBuilder()
err := txBuilder.SetMsgs(msg)
if err != nil {
return nil, err
}
if config.Fee != nil {
txBuilder.SetFeeAmount(config.Fee)
} else {
txBuilder.SetFeeAmount(sdk.NewCoins(sdk.NewCoin("stake", sdkmath.NewInt(10)))) // Arbitrary fee
}
if config.Gas != 0 {
txBuilder.SetGasLimit(config.Gas)
} else {
txBuilder.SetGasLimit(flags.DefaultGasLimit) // Need at least 100386
}
if config.Memo != "" {
txBuilder.SetMemo(config.Memo)
}
if config.GenOnly {
txBz, err := clientCtx.TxConfig.TxJSONEncoder()(txBuilder.GetTx())
if err != nil {
return nil, err
}
out := bytes.NewBuffer(txBz)
return out, nil
}
txFactory := tx.Factory{}
txFactory = txFactory.
WithChainID(clientCtx.ChainID).
WithKeybase(clientCtx.Keyring).
WithTxConfig(clientCtx.TxConfig).
WithSignMode(signing.SignMode_SIGN_MODE_DIRECT)
if config.Offline {
txFactory = txFactory.
WithAccountNumber(config.AccNum).
WithSequence(config.Seq)
} else {
accNum, accSeq, err := clientCtx.AccountRetriever.GetAccountNumberSequence(clientCtx, from)
if err != nil {
return nil, err
}
txFactory = txFactory.
WithAccountNumber(accNum).
WithSequence(accSeq)
}
accBytes, err := clientCtx.AddressCodec.StringToBytes(from.String())
if err != nil {
return nil, err
}
keyRecord, err := clientCtx.Keyring.KeyByAddress(accBytes)
if err != nil {
return nil, err
}
err = tx.Sign(context.Background(), txFactory, keyRecord.Name, txBuilder, true)
if err != nil {
return nil, err
}
txBz, err := clientCtx.TxConfig.TxEncoder()(txBuilder.GetTx())
if err != nil {
return nil, err
}
clientCtx.BroadcastMode = flags.BroadcastSync
if config.IsAsyncBroadcastMode {
clientCtx.BroadcastMode = flags.BroadcastAsync
}
var res proto.Message
if config.Simulate {
txSvcClient := txtypes.NewServiceClient(clientCtx)
res, err = txSvcClient.Simulate(context.Background(), &txtypes.SimulateRequest{
TxBytes: txBz,
})
if err != nil {
return nil, err
}
} else {
res, err = clientCtx.BroadcastTxSync(txBz)
if err != nil {
return nil, err
}
}
bz, err := clientCtx.Codec.MarshalJSON(res)
if err != nil {
return nil, err
}
out := bytes.NewBuffer(bz)
return out, err
}