-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
88 lines (72 loc) · 2.11 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
package config
import (
"io/ioutil"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/pelletier/go-toml"
)
var (
DefaultConfigPath = "config.toml"
DefaultGasLimit = sdk.Gas(500000)
DefaultFees = sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, 100000))
)
// Config defines all necessary configuration parameters.
type Config struct {
RPC RPCConfig `toml:"rpc"`
GRPC GRPCConfig `toml:"grpc"`
WalletConfig WalletConfig `toml:"wallet"`
TxConfig TxConfig `toml:"tx"`
}
// RPCConfig contains configuration of the RPC endpoint.
type RPCConfig struct {
Address string `toml:"address"`
}
// GRPCConfig contains configuration of the gRPC endpoint.
type GRPCConfig struct {
Address string `toml:"address"`
UseTLS bool `toml:"use_tls"`
}
// WalletConfig contains wallet configuration that is used to sign transaction.
type WalletConfig struct {
Mnemonic string `yaml:"mnemonic"`
Password string `yaml:"password"`
}
// TxConfig contains configuration for transaction related parameters.
type TxConfig struct {
GasLimit uint64 `yaml:"gas_limit"`
Fees string `yaml:"fees"`
}
// NewConfig builds a new Config instance.
func NewConfig(rpcCfg RPCConfig, gRPCCfg GRPCConfig) *Config {
return &Config{
RPC: rpcCfg,
GRPC: gRPCCfg,
}
}
// SetupConfig takes the path to a configuration file and returns the properly parsed configuration.
func Read(configPath string) (*Config, error) {
// Use default config path with a file name "config.toml" if it is empty
if configPath == "" {
configPath = DefaultConfigPath
}
data, err := ioutil.ReadFile(configPath)
if err != nil {
return nil, err
}
return ParseString(data)
}
// ParseString attempts to read and parse config from the given string bytes.
// An error reading or parsing the config results in a panic.
func ParseString(configData []byte) (*Config, error) {
var cfg Config
err := toml.Unmarshal(configData, &cfg)
if err != nil {
return nil, err
}
if cfg.TxConfig.GasLimit == 0 {
cfg.TxConfig.GasLimit = DefaultGasLimit
}
if cfg.TxConfig.Fees == "" {
cfg.TxConfig.Fees = DefaultFees.String()
}
return &cfg, nil
}