/
context.go
219 lines (197 loc) · 8.18 KB
/
context.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package chain
import (
"os"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/std"
"github.com/cosmos/cosmos-sdk/x/auth/tx"
"github.com/pkg/errors"
keyscodec "github.com/InjectiveLabs/sdk-go/chain/crypto/codec"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
auction "github.com/InjectiveLabs/sdk-go/chain/auction/types"
exchange "github.com/InjectiveLabs/sdk-go/chain/exchange/types"
insurance "github.com/InjectiveLabs/sdk-go/chain/insurance/types"
ocr "github.com/InjectiveLabs/sdk-go/chain/ocr/types"
oracle "github.com/InjectiveLabs/sdk-go/chain/oracle/types"
peggy "github.com/InjectiveLabs/sdk-go/chain/peggy/types"
tokenfactory "github.com/InjectiveLabs/sdk-go/chain/tokenfactory/types"
chaintypes "github.com/InjectiveLabs/sdk-go/chain/types"
wasmx "github.com/InjectiveLabs/sdk-go/chain/wasmx/types"
cosmostypes "github.com/cosmos/cosmos-sdk/types"
signingtypes "github.com/cosmos/cosmos-sdk/types/tx/signing"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
vestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
authztypes "github.com/cosmos/cosmos-sdk/x/authz"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
crisistypes "github.com/cosmos/cosmos-sdk/x/crisis/types"
distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
evidencetypes "github.com/cosmos/cosmos-sdk/x/evidence/types"
feegranttypes "github.com/cosmos/cosmos-sdk/x/feegrant"
govv1types "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
paramproposaltypes "github.com/cosmos/cosmos-sdk/x/params/types/proposal"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
icatypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/types"
ibcfeetypes "github.com/cosmos/ibc-go/v7/modules/apps/29-fee/types"
ibcapplicationtypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
ibccoretypes "github.com/cosmos/ibc-go/v7/modules/core/types"
ibclightclienttypes "github.com/cosmos/ibc-go/v7/modules/light-clients/06-solomachine"
ibctenderminttypes "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint"
)
// NewTxConfig initializes new Cosmos TxConfig with certain signModes enabled.
func NewTxConfig(signModes []signingtypes.SignMode) client.TxConfig {
interfaceRegistry := types.NewInterfaceRegistry()
keyscodec.RegisterInterfaces(interfaceRegistry)
std.RegisterInterfaces(interfaceRegistry)
exchange.RegisterInterfaces(interfaceRegistry)
oracle.RegisterInterfaces(interfaceRegistry)
insurance.RegisterInterfaces(interfaceRegistry)
auction.RegisterInterfaces(interfaceRegistry)
peggy.RegisterInterfaces(interfaceRegistry)
ocr.RegisterInterfaces(interfaceRegistry)
wasmx.RegisterInterfaces(interfaceRegistry)
chaintypes.RegisterInterfaces(interfaceRegistry)
tokenfactory.RegisterInterfaces(interfaceRegistry)
// more cosmos types
authtypes.RegisterInterfaces(interfaceRegistry)
authztypes.RegisterInterfaces(interfaceRegistry)
vestingtypes.RegisterInterfaces(interfaceRegistry)
banktypes.RegisterInterfaces(interfaceRegistry)
crisistypes.RegisterInterfaces(interfaceRegistry)
distributiontypes.RegisterInterfaces(interfaceRegistry)
evidencetypes.RegisterInterfaces(interfaceRegistry)
govtypes.RegisterInterfaces(interfaceRegistry)
govv1types.RegisterInterfaces(interfaceRegistry)
paramproposaltypes.RegisterInterfaces(interfaceRegistry)
ibcapplicationtypes.RegisterInterfaces(interfaceRegistry)
ibccoretypes.RegisterInterfaces(interfaceRegistry)
ibclightclienttypes.RegisterInterfaces(interfaceRegistry)
ibctenderminttypes.RegisterInterfaces(interfaceRegistry)
slashingtypes.RegisterInterfaces(interfaceRegistry)
stakingtypes.RegisterInterfaces(interfaceRegistry)
upgradetypes.RegisterInterfaces(interfaceRegistry)
feegranttypes.RegisterInterfaces(interfaceRegistry)
wasmtypes.RegisterInterfaces(interfaceRegistry)
icatypes.RegisterInterfaces(interfaceRegistry)
marshaler := codec.NewProtoCodec(interfaceRegistry)
return tx.NewTxConfig(marshaler, signModes)
}
// NewClientContext creates a new Cosmos Client context, where chainID
// corresponds to Cosmos chain ID, fromSpec is either name of the key, or bech32-address
// of the Cosmos account. Keyring is required to contain the specified key.
func NewClientContext(
chainId, fromSpec string, kb keyring.Keyring,
) (client.Context, error) {
clientCtx := client.Context{}
interfaceRegistry := types.NewInterfaceRegistry()
keyscodec.RegisterInterfaces(interfaceRegistry)
std.RegisterInterfaces(interfaceRegistry)
exchange.RegisterInterfaces(interfaceRegistry)
insurance.RegisterInterfaces(interfaceRegistry)
auction.RegisterInterfaces(interfaceRegistry)
oracle.RegisterInterfaces(interfaceRegistry)
peggy.RegisterInterfaces(interfaceRegistry)
ocr.RegisterInterfaces(interfaceRegistry)
wasmx.RegisterInterfaces(interfaceRegistry)
chaintypes.RegisterInterfaces(interfaceRegistry)
tokenfactory.RegisterInterfaces(interfaceRegistry)
// more cosmos types
authtypes.RegisterInterfaces(interfaceRegistry)
authztypes.RegisterInterfaces(interfaceRegistry)
vestingtypes.RegisterInterfaces(interfaceRegistry)
banktypes.RegisterInterfaces(interfaceRegistry)
crisistypes.RegisterInterfaces(interfaceRegistry)
distributiontypes.RegisterInterfaces(interfaceRegistry)
evidencetypes.RegisterInterfaces(interfaceRegistry)
govtypes.RegisterInterfaces(interfaceRegistry)
govv1types.RegisterInterfaces(interfaceRegistry)
paramproposaltypes.RegisterInterfaces(interfaceRegistry)
ibcapplicationtypes.RegisterInterfaces(interfaceRegistry)
ibccoretypes.RegisterInterfaces(interfaceRegistry)
ibclightclienttypes.RegisterInterfaces(interfaceRegistry)
ibctenderminttypes.RegisterInterfaces(interfaceRegistry)
slashingtypes.RegisterInterfaces(interfaceRegistry)
stakingtypes.RegisterInterfaces(interfaceRegistry)
upgradetypes.RegisterInterfaces(interfaceRegistry)
feegranttypes.RegisterInterfaces(interfaceRegistry)
wasmtypes.RegisterInterfaces(interfaceRegistry)
icatypes.RegisterInterfaces(interfaceRegistry)
ibcfeetypes.RegisterInterfaces(interfaceRegistry)
marshaler := codec.NewProtoCodec(interfaceRegistry)
encodingConfig := EncodingConfig{
InterfaceRegistry: interfaceRegistry,
Marshaler: marshaler,
TxConfig: NewTxConfig([]signingtypes.SignMode{
signingtypes.SignMode_SIGN_MODE_DIRECT,
}),
}
var keyInfo keyring.Record
if kb != nil {
addr, err := cosmostypes.AccAddressFromBech32(fromSpec)
if err == nil {
record, err := kb.KeyByAddress(addr)
if err != nil {
err = errors.Wrapf(err, "failed to load key info by address %s", addr.String())
return clientCtx, err
}
keyInfo = *record
} else {
// failed to parse Bech32, is it a name?
record, err := kb.Key(fromSpec)
if err != nil {
err = errors.Wrapf(err, "no key in keyring for name: %s", fromSpec)
return clientCtx, err
}
keyInfo = *record
}
}
clientCtx = newContext(
chainId,
encodingConfig,
kb,
keyInfo,
)
return clientCtx, nil
}
type EncodingConfig struct {
InterfaceRegistry types.InterfaceRegistry
Marshaler codec.Codec
TxConfig client.TxConfig
}
func newContext(
chainId string,
encodingConfig EncodingConfig,
kb keyring.Keyring,
keyInfo keyring.Record,
) client.Context {
clientCtx := client.Context{
ChainID: chainId,
Codec: encodingConfig.Marshaler,
InterfaceRegistry: encodingConfig.InterfaceRegistry,
Output: os.Stderr,
OutputFormat: "json",
BroadcastMode: "block",
UseLedger: false,
Simulate: true,
GenerateOnly: false,
Offline: false,
SkipConfirm: true,
TxConfig: encodingConfig.TxConfig,
AccountRetriever: authtypes.AccountRetriever{},
}
if keyInfo.PubKey != nil {
address, err := keyInfo.GetAddress()
if err != nil {
panic(err)
}
clientCtx = clientCtx.WithKeyring(kb)
clientCtx = clientCtx.WithFromAddress(address)
clientCtx = clientCtx.WithFromName(keyInfo.Name)
clientCtx = clientCtx.WithFrom(keyInfo.Name)
}
return clientCtx
}