-
Notifications
You must be signed in to change notification settings - Fork 0
/
configuration_loader.go
98 lines (78 loc) · 3.58 KB
/
configuration_loader.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
package restake
import (
"fmt"
"os"
"time"
file "github.com/tessellated-io/pickaxe/config"
"github.com/tessellated-io/pickaxe/log"
"gopkg.in/yaml.v2"
)
const RestakeConfigFilename = "restake.yml"
// Configuration is configuration for restake
type Configuration struct {
TargetValidator string `yaml:"target_validator" comment:"The name of the validator to Restake for. Ex. 'Tessellated'"`
Ignores []string `yaml:"ignores" comment:"A list of network names to ignore. Ex. 'cosmoshub' or 'osmosis'"`
BotMnemonic string `yaml:"mnemonic" comment:"The mnemonic to use for Restaking"`
Memo string `yaml:"memo" comment:"An optional memo to include in Restake transactions"`
TxPollDelaySeconds uint `yaml:"tx_poll_delay_seconds" comment:"How long to delay between attempts to poll for a tx being included in a block"`
TxPollAttempts uint `yaml:"tx_poll_attempts" comment:"How many attempts to poll for a tx being included before failing."`
NetworkRetryDelaySeconds uint `yaml:"network_retry_delay_seconds" comment:"How long to delay between retries due to RPC failures"`
NetworkRetryAttempts uint `yaml:"network_retry_attempts" comment:"How many attempts to retry due to network errors before failing."`
HealthChecksPingKey string `yaml:"health_checks_ping_key" comment:"A ping API key for healthchecks.io. If empty, no pings will be delivered."`
RunIntervalSeconds uint `yaml:"run_interval_seconds" comment:"How many seconds to wait in between restake runs"`
BatchSize uint `yaml:"batch_size" comment:"What size batches of transactions should be sent in"`
ChainRegistryBaseUrl string `yaml:"chain_registry_base_url" comment:"The base url for the chain registry"`
ValidatorRegistryBaseUrl string `yaml:"validator_registry_base_url" comment:"The base url for the validator registry"`
}
func (c *Configuration) VersionedMemo(version string) string {
return fmt.Sprintf("%s | restake-go %s", c.Memo, version)
}
func (c *Configuration) RunInterval() time.Duration {
return time.Duration(c.RunIntervalSeconds) * time.Second
}
func (c *Configuration) NetworkRetryDelay() time.Duration {
return time.Duration(c.NetworkRetryDelaySeconds) * time.Second
}
func (c *Configuration) TxPollDelay() time.Duration {
return time.Duration(c.TxPollDelaySeconds) * time.Second
}
// configurationLoader loads configuration
type configurationLoader struct {
configurationDirectory string
logger *log.Logger
}
func NewConfigurationLoader(configurationDirectory string, logger *log.Logger) (*configurationLoader, error) {
loader := &configurationLoader{
configurationDirectory: configurationDirectory,
logger: logger,
}
return loader, nil
}
func (cl *configurationLoader) LoadConfiguration() (*Configuration, error) {
configurationFile := cl.getConfigFile()
data, err := os.ReadFile(configurationFile)
if err != nil {
return nil, err
}
loaded := &Configuration{}
err = yaml.Unmarshal(data, loaded)
if err != nil {
return nil, err
}
return loaded, nil
}
func (cl *configurationLoader) Initialize() error {
// Example config
config := &Configuration{}
configFile := cl.getConfigFile()
header := "This is the configuration file for Treasurer"
err := file.WriteYamlWithComments(config, header, configFile, cl.logger)
if err != nil {
cl.logger.Error().Err(err).Msg("error writing file")
return err
}
return nil
}
func (cl *configurationLoader) getConfigFile() string {
return fmt.Sprintf("%s/%s", cl.configurationDirectory, RestakeConfigFilename)
}