/
config.go
158 lines (140 loc) · 4.84 KB
/
config.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
package client
import (
"context"
"fmt"
"time"
registry "github.com/KyleMoser/cosmos-client/client/chain_registry"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/spf13/viper"
"go.uber.org/zap"
)
type ChainClientConfig struct {
ChainName string `json:"-" yaml:"-"`
Key string `json:"key" yaml:"key"`
ChainID string `json:"chain-id" yaml:"chain-id"`
RPCAddr string `json:"rpc-addr" yaml:"rpc-addr"`
GRPCAddr string `json:"grpc-addr" yaml:"grpc-addr"`
AccountPrefix string `json:"account-prefix" yaml:"account-prefix"`
KeyringBackend string `json:"keyring-backend" yaml:"keyring-backend"`
GasAdjustment float64 `json:"gas-adjustment" yaml:"gas-adjustment"`
GasPrices string `json:"gas-prices" yaml:"gas-prices"`
MinGasAmount uint64 `json:"min-gas-amount" yaml:"min-gas-amount"`
KeyDirectory string `json:"key-directory" yaml:"key-directory"`
Debug bool `json:"debug" yaml:"debug"`
Timeout string `json:"timeout" yaml:"timeout"`
BlockTimeout string `json:"block-timeout" yaml:"block-timeout"`
OutputFormat string `json:"output-format" yaml:"output-format"`
SignModeStr string `json:"sign-mode" yaml:"sign-mode"`
ExtraCodecs []string `json:"extra-codecs" yaml:"extra-codecs"`
Modules []module.AppModuleBasic `json:"-" yaml:"-"`
Slip44 int `json:"slip44" yaml:"slip44"`
}
func (ccc *ChainClientConfig) Validate() error {
if _, err := time.ParseDuration(ccc.Timeout); err != nil {
return err
}
if ccc.BlockTimeout != "" {
if _, err := time.ParseDuration(ccc.BlockTimeout); err != nil {
return err
}
}
return nil
}
func GetCosmosHubConfig(keyHome string, debug bool) *ChainClientConfig {
return &ChainClientConfig{
Key: "default",
ChainID: "cosmoshub-4",
RPCAddr: "https://cosmoshub-4.technofractal.com:443",
GRPCAddr: "https://gprc.cosmoshub-4.technofractal.com:443",
AccountPrefix: "cosmos",
KeyringBackend: "test",
GasAdjustment: 1.2,
GasPrices: "0.01uatom",
MinGasAmount: 0,
KeyDirectory: keyHome,
Debug: debug,
Timeout: "20s",
OutputFormat: "json",
SignModeStr: "direct",
}
}
func GetOsmosisConfig(keyHome string, debug bool) *ChainClientConfig {
return &ChainClientConfig{
Key: "default",
ChainID: "osmosis-1",
RPCAddr: "https://osmosis-1.technofractal.com:443",
GRPCAddr: "https://gprc.osmosis-1.technofractal.com:443",
AccountPrefix: "osmo",
KeyringBackend: "test",
GasAdjustment: 1.2,
GasPrices: "0.01uosmo",
MinGasAmount: 0,
KeyDirectory: keyHome,
Debug: debug,
Timeout: "20s",
OutputFormat: "json",
SignModeStr: "direct",
}
}
func GetChainConfigWithOpts(ctx context.Context, c registry.ChainInfo, opts *ChainConfigOptions) (*ChainClientConfig, error) {
debug := viper.GetBool("debug")
home := viper.GetString("home")
assetList, err := c.GetAssetList()
if err != nil {
return nil, err
}
var gasPrices string
if len(assetList.Assets) > 0 {
gasPrices = fmt.Sprintf("%.2f%s", 0.01, assetList.Assets[0].Base)
}
// TODO: better bugfix for scenario where chainclient is noble
if c.ChainName == "noble" {
gasPrices = fmt.Sprintf("%.2f%s", 0.1, "uusdc")
}
var rpc string
if opts != nil && len(opts.PreferredRpcDomains) > 0 {
rpc, err = c.GetRPCEndpointWithDomain(ctx, opts.PreferredRpcDomains)
}
if err != nil && opts != nil && len(opts.PreferredRpcHosts) > 0 {
rpc, err = c.GetPreferredRPCEndpoint(ctx, opts.PreferredRpcHosts)
}
if err != nil || rpc == "" {
rpc, err = c.GetRandomRPCEndpoint(ctx)
if err != nil {
return nil, err
}
}
return &ChainClientConfig{
ChainName: c.ChainName,
Key: "default",
ChainID: c.ChainID,
RPCAddr: rpc,
AccountPrefix: c.Bech32Prefix,
KeyringBackend: "test",
GasAdjustment: 1.2,
GasPrices: gasPrices,
KeyDirectory: home,
Debug: debug,
Timeout: "20s",
OutputFormat: "json",
SignModeStr: "direct",
Slip44: c.Slip44,
}, nil
}
type ChainConfigOptions struct {
PreferredRpcHosts []string
PreferredRpcDomains []string
}
func GetChain(ctx context.Context, chainName string, logger *zap.Logger, options *ChainConfigOptions) (*ChainClientConfig, error) {
registry := registry.DefaultChainRegistry(logger)
chainInfo, err := registry.GetChain(chainName)
if err != nil {
logger.Info(
"Failed to get chain",
zap.String("name", chainName),
zap.Error(err),
)
return nil, err
}
return GetChainConfigWithOpts(ctx, chainInfo, options)
}