This repository has been archived by the owner on Jun 16, 2023. It is now read-only.
/
config.go
149 lines (125 loc) · 2.95 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
package types
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"
"text/template"
"github.com/pelletier/go-toml"
)
var (
ct = strings.TrimSpace(`
setup = {{ .Setup }}
version = {{ .Version }}
[chain]
broadcast_mode = "{{ .Chain.BroadcastMode }}"
gas_adjustment = {{ .Chain.GasAdjustment }}
gas = {{ .Chain.Gas }}
gas_prices = "{{ .Chain.GasPrices }}"
id = "{{ .Chain.ID }}"
rpc_address = "{{ .Chain.RPCAddress }}"
simulate_and_execute = {{ .Chain.SimulateAndExecute }}
[cors]
allowed_origins = "{{ .CORS.AllowedOrigins }}"
`)
t = func() *template.Template {
t, err := template.New("config").Parse(ct)
if err != nil {
panic(err)
}
return t
}()
)
type Config struct {
Setup bool `json:"setup"`
Version uint64 `json:"version"`
Chain struct {
BroadcastMode string `json:"broadcast_mode"`
GasAdjustment float64 `json:"gas_adjustment"`
GasPrices string `json:"gas_prices"`
Gas uint64 `json:"gas"`
ID string `json:"id"`
RPCAddress string `json:"rpc_address"`
SimulateAndExecute bool `json:"simulate_and_execute"`
} `json:"chain"`
CORS struct {
AllowedOrigins string `json:"allowed_origins"`
} `json:"cors"`
}
func NewConfig() *Config {
return &Config{}
}
func (c *Config) Copy() *Config {
return &Config{
Setup: c.Setup,
Version: c.Version,
Chain: c.Chain,
CORS: c.CORS,
}
}
func (c *Config) WithDefaultValues() *Config {
c.Setup = true
c.Version = 2
c.Chain.BroadcastMode = "block"
c.Chain.Gas = 5e5
c.Chain.GasAdjustment = 1.05
c.Chain.GasPrices = "0.1udvpn"
c.Chain.ID = "sentinelhub-2"
c.Chain.RPCAddress = "https://rpc.sentinel.co:443"
c.Chain.SimulateAndExecute = false
c.CORS.AllowedOrigins = ""
return c
}
func (c *Config) LoadFromPath(path string) error {
if _, err := os.Stat(path); err != nil {
return err
}
data, err := ioutil.ReadFile(path)
if err != nil {
return err
}
if len(data) == 0 {
*c = Config{}
return nil
}
tree, err := toml.LoadBytes(data)
if err != nil {
return err
}
data, err = json.Marshal(tree.ToMap())
if err != nil {
return err
}
return json.Unmarshal(data, c)
}
func (c *Config) SaveToPath(path string) error {
var buffer bytes.Buffer
if err := t.Execute(&buffer, c); err != nil {
return err
}
return ioutil.WriteFile(path, buffer.Bytes(), 0600)
}
func (c *Config) String() string {
var buffer bytes.Buffer
if err := t.Execute(&buffer, c); err != nil {
panic(err)
}
return buffer.String()
}
func (c *Config) Validate() error {
if c.Chain.BroadcastMode == "" {
return fmt.Errorf("invalid chain->broadcast_mode; expected non-empty value")
}
if c.Chain.GasAdjustment < 0 {
return fmt.Errorf("invalid chain->gas_adjustment; expected non-negative value")
}
if c.Chain.ID == "" {
return fmt.Errorf("invalid chain->id; expected non-empty value")
}
if c.Chain.RPCAddress == "" {
return fmt.Errorf("invalid chain->rpc_address; expected non-empty value")
}
return nil
}