forked from InjectiveLabs/peggo
/
context.go
74 lines (64 loc) · 1.97 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
package client
import (
"os"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
cosmostypes "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/pkg/errors"
umeeapp "github.com/umee-network/umee/v3/app"
)
// 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) {
var keyInfo *keyring.Record
if kb != nil {
addr, err := cosmostypes.AccAddressFromBech32(fromSpec)
if err == nil {
keyInfo, err = kb.KeyByAddress(addr)
if err != nil {
err = errors.Wrapf(err, "failed to load key info by address %s", addr.String())
return client.Context{}, err
}
} else {
// failed to parse Bech32, is it a name?
keyInfo, err = kb.Key(fromSpec)
if err != nil {
err = errors.Wrapf(err, "no key in keyring for name: %s", fromSpec)
return client.Context{}, err
}
}
}
encodingConfig := umeeapp.MakeEncodingConfig()
clientCtx := client.Context{
ChainID: chainID,
Codec: encodingConfig.Codec,
InterfaceRegistry: encodingConfig.InterfaceRegistry,
Output: os.Stderr,
OutputFormat: "json",
BroadcastMode: "block",
UseLedger: false,
Simulate: false,
GenerateOnly: false,
Offline: false,
SkipConfirm: true,
TxConfig: encodingConfig.TxConfig,
AccountRetriever: authtypes.AccountRetriever{},
}
if keyInfo != nil {
addr, err := keyInfo.GetAddress()
if err != nil {
return clientCtx, err
}
clientCtx = clientCtx.WithKeyring(kb)
clientCtx = clientCtx.WithFromAddress(addr)
clientCtx = clientCtx.WithFromName(keyInfo.Name)
clientCtx = clientCtx.WithFrom(keyInfo.Name)
}
return clientCtx, nil
}