This repository has been archived by the owner on May 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
config.go
148 lines (132 loc) · 3.68 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
package config
import (
"fmt"
"log"
"os"
"path"
"github.com/spf13/viper"
"github.com/strangelove-ventures/lens/client"
"gopkg.in/yaml.v2"
)
//createConfig idempotently creates the config.
func CreateConfig(home string, debug bool) error {
cfgPath := path.Join(home, "config.yaml")
// If the config doesn't exist...
if _, err := os.Stat(cfgPath); os.IsNotExist(err) {
// And the config folder doesn't exist...
// And the home folder doesn't exist
if _, err := os.Stat(home); os.IsNotExist(err) {
// Create the home folder
if err = os.Mkdir(home, os.ModePerm); err != nil {
return err
}
}
}
// Then create the file...
f, err := os.Create(cfgPath)
if err != nil {
return err
}
defer f.Close()
// And write the default config to that location...
if _, err = f.Write(defaultConfig(path.Join(home, "keys"), debug)); err != nil {
return err
}
return nil
}
func OverwriteConfig(cfg *Config) error {
home := viper.GetString("home")
cfgPath := path.Join(home, "config.yaml")
f, err := os.Create(cfgPath)
if err != nil {
return err
}
defer f.Close()
if _, err := f.Write(cfg.MustYAML()); err != nil {
return err
}
log.Printf("updated lens configuration at %s", cfgPath)
return nil
}
// Config represents the config file for the relayer
type Config struct {
DefaultChain string `yaml:"default_chain" json:"default_chain"`
Chains map[string]*client.ChainClientConfig `yaml:"chains" json:"chains"`
Cl map[string]*client.ChainClient `yaml:",omitempty" json:",omitempty"`
}
func (c *Config) GetDefaultClient() *client.ChainClient {
return c.GetClient(c.DefaultChain)
}
func (c *Config) GetClient(chainID string) *client.ChainClient {
if v, ok := c.Cl[chainID]; ok {
return v
}
return nil
}
// Called to initialize the relayer.Chain types on Config
func ValidateConfig(c *Config) error {
for _, chain := range c.Chains {
if err := chain.Validate(); err != nil {
return err
}
}
if c.GetDefaultClient() == nil {
return fmt.Errorf("default chain (%s) configuration not found", c.DefaultChain)
}
return nil
}
// MustYAML returns the yaml string representation of the Paths
func (c Config) MustYAML() []byte {
out, err := yaml.Marshal(c)
if err != nil {
panic(err)
}
return out
}
func defaultConfig(keyHome string, debug bool) []byte {
return Config{
DefaultChain: "quicksilver-1",
Chains: map[string]*client.ChainClientConfig{
"quicksilver-1": GetQuicksilverConfig(keyHome, debug),
"osmosis-1": GetOsmosisConfig(keyHome, debug),
},
}.MustYAML()
}
func GetQuicksilverConfig(keyHome string, debug bool) *client.ChainClientConfig {
return &client.ChainClientConfig{
Key: "default",
ChainID: "quicksilver-1",
RPCAddr: "https://rpc.quicksilver.zone:443",
GRPCAddr: "https://grpc.quicksilver.zone:443",
AccountPrefix: "quick",
KeyringBackend: "test",
GasAdjustment: 1.2,
GasPrices: "0.01uqck",
MinGasAmount: 0,
KeyDirectory: keyHome,
Debug: debug,
Timeout: "20s",
BlockTimeout: "10s",
OutputFormat: "json",
SignModeStr: "direct",
}
}
func GetOsmosisConfig(keyHome string, debug bool) *client.ChainClientConfig {
return &client.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",
BlockTimeout: "10s",
OutputFormat: "json",
SignModeStr: "direct",
}
}