-
Notifications
You must be signed in to change notification settings - Fork 19
/
chain.go
373 lines (304 loc) · 9.36 KB
/
chain.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package client
import (
"context"
"fmt"
"net/http"
"os"
"github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/go-bip39"
"github.com/golang/protobuf/jsonpb"
lens "github.com/strangelove-ventures/lens/client"
coretypes "github.com/tendermint/tendermint/rpc/core/types"
"go.uber.org/zap"
pb "github.com/cosmology-tech/starship/registry/registry"
)
type ChainClients []*ChainClient
func NewChainClients(logger *zap.Logger, config *Config) (ChainClients, error) {
var clients []*ChainClient
for _, chain := range config.Chains {
client, err := NewChainClient(logger, config, chain.ID)
if err != nil {
logger.Error("unable to create client for chain",
zap.String("chain_id", chain.ID),
zap.Error(err),
)
return nil, err
}
clients = append(clients, client)
}
return clients, nil
}
// GetChainClient returns a chain client pointer for the given chain id
func (cc ChainClients) GetChainClient(chainID string) (*ChainClient, error) {
for _, client := range cc {
if client.GetChainID() == chainID {
return client, nil
}
}
return nil, fmt.Errorf("not found: client chain id %s", chainID)
}
type ChainClient struct {
Logger *zap.Logger
Config *Config
Address string
ChainID string
ChainConfig *lens.ChainClientConfig
Client *lens.ChainClient
}
func NewChainClient(logger *zap.Logger, config *Config, chainID string) (*ChainClient, error) {
cc := config.GetChain(chainID)
chainClient := &ChainClient{
Logger: logger,
Config: config,
ChainID: chainID,
}
// fetch chain registry from the local registry
registry, err := chainClient.GetChainRegistry()
if err != nil {
return nil, err
}
ccc := &lens.ChainClientConfig{
ChainID: chainID,
RPCAddr: cc.GetRPCAddr(),
KeyringBackend: "test",
Debug: true,
Timeout: "20s",
SignModeStr: "direct",
AccountPrefix: *registry.Bech32Prefix,
GasAdjustment: 1.5,
GasPrices: fmt.Sprintf("%f%s", registry.Fees.FeeTokens[0].HighGasPrice, registry.Fees.FeeTokens[0].Denom),
MinGasAmount: 10000,
Slip44: int(registry.Slip44),
Modules: lens.ModuleBasics,
}
client, err := lens.NewChainClient(logger, ccc, os.Getenv("HOME"), os.Stdin, os.Stdout)
if err != nil {
return nil, err
}
chainClient.ChainConfig = ccc
chainClient.Client = client
err = chainClient.Initialize()
if err != nil {
return nil, err
}
return chainClient, nil
}
func (c *ChainClient) GetRPCAddr() string {
return c.Config.GetChain(c.GetChainID()).GetRPCAddr()
}
func (c *ChainClient) GetChainID() string {
return c.ChainID
}
func (c *ChainClient) Initialize() error {
keyName := fmt.Sprintf("genesis-%s", c.GetChainID())
mnemonic, err := c.GetGenesisMnemonic()
if err != nil {
return err
}
wallet, err := c.CreateWallet(keyName, mnemonic)
if err != nil {
return err
}
c.Address = wallet
c.ChainConfig.Key = keyName
return nil
}
// GetChainKeys fetches keys from the chain registry at `/chains/{chain-id}/keys` endpoint
func (c *ChainClient) GetChainKeys() (*pb.Keys, error) {
url := fmt.Sprintf("%s/chains/%s/keys", c.Config.Registry.GetRESTAddr(), c.GetChainID())
resp, err := http.Get(url)
if err != nil {
return nil, err
}
keys := &pb.Keys{}
err = jsonpb.Unmarshal(resp.Body, keys)
if err != nil {
return nil, err
}
return keys, nil
}
// GetGenesisMnemonic fetches the mnemonic from GetChainKeys and returns the first mnemonic in genesis list
func (c *ChainClient) GetGenesisMnemonic() (string, error) {
keys, err := c.GetChainKeys()
if err != nil {
return "", err
}
return keys.Genesis[0].Mnemonic, nil
}
func (c *ChainClient) CreateWallet(keyName, mnemonic string) (string, error) {
// delete key if already exists
_ = c.Client.DeleteKey(keyName)
walletAddr, err := c.Client.RestoreKey(keyName, mnemonic, 118)
if err != nil {
return "", err
}
return walletAddr, nil
}
func (c *ChainClient) CreateRandWallet(keyName string) (string, error) {
entropy, err := bip39.NewEntropy(256)
if err != nil {
return "", err
}
mnemonic, err := bip39.NewMnemonic(entropy)
if err != nil {
return "", err
}
address, err := c.CreateWallet(keyName, mnemonic)
if err != nil {
return "", err
}
return address, nil
}
// GetChainRegistry fetches the chain registry from the registry at `/chains/{chain-id}` endpoint
func (c *ChainClient) GetChainRegistry() (*pb.ChainRegistry, error) {
url := fmt.Sprintf("%s/chains/%s", c.Config.Registry.GetRESTAddr(), c.GetChainID())
resp, err := http.Get(url)
if err != nil {
return nil, err
}
chainRegistry := &pb.ChainRegistry{}
err = jsonpb.Unmarshal(resp.Body, chainRegistry)
if err != nil {
return nil, err
}
// verify chain id from chain registry and config
if chainRegistry.ChainId != c.GetChainID() {
return nil, fmt.Errorf("chain id mismatch: %s != %s", chainRegistry.ChainId, c.GetChainID())
}
return chainRegistry, nil
}
func (c *ChainClient) GetChainDenom() (string, error) {
registry, err := c.GetChainRegistry()
if err != nil {
return "", err
}
return registry.Staking.StakingTokens[0].Denom, nil
}
func (c *ChainClient) MustGetChainDenom() string {
denom, err := c.GetChainDenom()
if err != nil {
panic(err)
}
return denom
}
// GetChainAssets fetches the assets from chain registry at `/chains/{chain-id}/assets` endpoint
func (c *ChainClient) GetChainAssets() ([]*pb.ChainAsset, error) {
url := fmt.Sprintf("%s/chains/%s/assets", c.Config.Registry.GetRESTAddr(), c.GetChainID())
resp, err := http.Get(url)
if err != nil {
return nil, err
}
respAssets := &pb.ResponseChainAssets{}
err = jsonpb.Unmarshal(resp.Body, respAssets)
if err != nil {
return nil, err
}
return respAssets.Assets, nil
}
func (c *ChainClient) GetIBCInfo(chain2 string) (*pb.IBCData, error) {
url := fmt.Sprintf("%s/ibc/%s/%s", c.Config.Registry.GetRESTAddr(), c.GetChainID(), chain2)
resp, err := http.Get(url)
if err != nil {
return nil, err
}
ibcData := &pb.IBCData{}
err = jsonpb.Unmarshal(resp.Body, ibcData)
if err != nil {
return nil, err
}
return ibcData, nil
}
func (c *ChainClient) GetIBCChannel(chain2 string) (*pb.ChannelData, error) {
ibcInfo, err := c.GetIBCInfo(chain2)
if err != nil {
return nil, err
}
return ibcInfo.Channels[0], nil
}
func (c *ChainClient) GetStatus() (*coretypes.ResultStatus, error) {
status, err := c.Client.RPCClient.Status(context.Background())
if err != nil {
return nil, err
}
return status, nil
}
func (c *ChainClient) GetHeight() (int64, error) {
status, err := c.GetStatus()
if err != nil {
return -1, err
}
return status.SyncInfo.LatestBlockHeight, nil
}
func (c *ChainClient) CustomSendMsg(ctx context.Context, keyName string, msg sdk.Msg, memo string) (*sdk.TxResponse, error) {
return c.CustomSendMsgs(ctx, keyName, []sdk.Msg{msg}, memo)
}
// CustomSendMsgs wraps the msgs in a StdTx, signs and sends it. An error is returned if there
// was an issue sending the transaction. A successfully sent, but failed transaction will
// not return an error. If a transaction is successfully sent, the result of the execution
// of that transaction will be logged. A boolean indicating if a transaction was successfully
// sent and executed successfully is returned.
func (c *ChainClient) CustomSendMsgs(ctx context.Context, keyName string, msgs []sdk.Msg, memo string) (*sdk.TxResponse, error) {
cc := c.Client
txf, err := cc.PrepareFactory(cc.TxFactory())
if err != nil {
return nil, err
}
if memo != "" {
txf = txf.WithMemo(memo)
}
// Set the gas amount on the transaction factory
adjusted := uint64(1000000)
txf = txf.WithGas(adjusted)
// Build the transaction builder
txb, err := txf.BuildUnsignedTx(msgs...)
if err != nil {
return nil, err
}
// Attach the signature to the transaction
// c.LogFailedTx(nil, err, msgs)
// Force encoding in the chain specific address
for _, msg := range msgs {
cc.Codec.Marshaler.MustMarshalJSON(msg)
}
err = func() error {
done := cc.SetSDKContext()
// ensure that we allways call done, even in case of an error or panic
defer done()
if err = tx.Sign(txf, keyName, txb, false); err != nil {
return err
}
return nil
}()
if err != nil {
return nil, err
}
// Generate the transaction bytes
txBytes, err := cc.Codec.TxConfig.TxEncoder()(txb.GetTx())
if err != nil {
return nil, err
}
// Broadcast those bytes
res, err := cc.BroadcastTx(ctx, txBytes)
if err != nil {
return nil, err
}
// transaction was executed, log the success or failure using the tx response code
// NOTE: error is nil, logic should use the returned error to determine if the
// transaction was successfully executed.
if res.Code != 0 {
return res, fmt.Errorf("transaction failed with code: %d", res.Code)
}
return res, nil
}
func (c *ChainClient) SendMsg(ctx context.Context, msg sdk.Msg, memo string) (*sdk.TxResponse, error) {
return c.SendMsgs(ctx, []sdk.Msg{msg}, memo)
}
// SendMsgs wraps the msgs in a StdTx, signs and sends it. An error is returned if there
// was an issue sending the transaction. A successfully sent, but failed transaction will
// not return an error. If a transaction is successfully sent, the result of the execution
// of that transaction will be logged. A boolean indicating if a transaction was successfully
// sent and executed successfully is returned.
func (c *ChainClient) SendMsgs(ctx context.Context, msgs []sdk.Msg, memo string) (*sdk.TxResponse, error) {
return c.CustomSendMsgs(ctx, c.ChainConfig.Key, msgs, memo)
}