forked from lesnikutsa/babylon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsigner_config.go
91 lines (74 loc) · 2.21 KB
/
signer_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
package types
import (
"errors"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/spf13/cast"
"github.com/spf13/viper"
)
func ParseKeyNameFromConfig(opts servertypes.AppOptions) string {
valueInterface := opts.Get("signer-config.key-name")
if valueInterface == nil {
panic("Signer key name should be provided in options")
}
keyName, err := cast.ToStringE(valueInterface)
if err != nil {
panic("Signer key name should be valid string")
}
return keyName
}
func parseGasPriceFromConfig(opts servertypes.AppOptions) (string, error) {
valueInterface := opts.Get("signer-config.gas-price")
if valueInterface == nil {
return "", errors.New("signer gas price should be provided in options")
}
gasPrice, err := cast.ToStringE(valueInterface)
if err != nil {
return "", errors.New("signer gas price should be valid string")
}
coin, err := sdk.ParseDecCoin(gasPrice)
if err != nil {
return "", errors.New("signer gas price is invalid")
}
if !coin.Amount.IsPositive() {
return "", errors.New("gas price should be positive")
}
return gasPrice, nil
}
func parseGasAdjustmentFromConfig(opts servertypes.AppOptions) (float64, error) {
valueInterface := opts.Get("signer-config.gas-adjustment")
if valueInterface == nil {
return 0, errors.New("signer gas adjustment should be provided in options")
}
gasAdjustment, err := cast.ToFloat64E(valueInterface)
if err != nil {
return 0, errors.New("signer gas adjustment should be valid float number")
}
if gasAdjustment <= 1 {
return 0, errors.New("signer gas adjustment should be more than 1")
}
return gasAdjustment, nil
}
// MustGetGasSettings reads GasPrice and GasAdjustment from app.toml file
func MustGetGasSettings(configPath string, v *viper.Viper) (string, float64) {
var (
gasPrice string
gasAdjustment float64
err error
)
v.AddConfigPath(configPath)
v.SetConfigName("app")
v.SetConfigType("toml")
if err := v.ReadInConfig(); err != nil {
panic("failed to read app.toml")
}
gasPrice, err = parseGasPriceFromConfig(v)
if err != nil {
panic(err)
}
gasAdjustment, err = parseGasAdjustmentFromConfig(v)
if err != nil {
panic(err)
}
return gasPrice, gasAdjustment
}