-
Notifications
You must be signed in to change notification settings - Fork 74
/
config.go
267 lines (224 loc) · 7.76 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package main
import (
"bytes"
"encoding/json"
"fmt"
"os"
"path/filepath"
"text/template"
"time"
tmcli "github.com/cometbft/cometbft/libs/cli"
"github.com/cosmos/cosmos-sdk/client"
scconfig "github.com/cosmos/cosmos-sdk/client/config"
"github.com/cosmos/cosmos-sdk/client/flags"
serverconfig "github.com/cosmos/cosmos-sdk/server/config"
oracleconfig "github.com/skip-mev/slinky/oracle/config"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// This code is copied from the Juno implementation: https://github.com/CosmosContracts/juno/pull/601/files
type NeutronCustomClient struct {
scconfig.ClientConfig
Gas string `mapstructure:"gas" json:"gas"`
GasPrices string `mapstructure:"gas-prices" json:"gas-prices"`
GasAdjustment string `mapstructure:"gas-adjustment" json:"gas-adjustment"`
Fees string `mapstructure:"fees" json:"fees"`
// FeeGranter string `mapstructure:"fee-granter" json:"fee-granter"`
// FeePayer string `mapstructure:"fee-payer" json:"fee-payer"`
Note string `mapstructure:"note" json:"note"`
}
// NeutronAppConfig defines the config structure of the neutrond app.toml file. Specifically,
// it wraps the default app.toml config with additional slinky application config params.
type NeutronAppConfig struct {
serverconfig.Config
Oracle oracleconfig.AppConfig `mapstructure:"oracle" json:"oracle"`
}
// initAppConfig initializes a default application configuration for neutrond.
// And a config template string (for how the config will ultimately look on disk).
func initAppConfig() (*NeutronAppConfig, string) {
srvConfig := serverconfig.DefaultConfig()
oracleConfig := oracleconfig.AppConfig{
Enabled: true,
OracleAddress: "localhost:8080",
ClientTimeout: time.Second * 2,
MetricsEnabled: true,
}
return &NeutronAppConfig{
Config: *srvConfig,
Oracle: oracleConfig,
}, serverconfig.DefaultConfigTemplate + oracleconfig.DefaultConfigTemplate
}
// ConfigCmd returns a CLI command to interactively create an application CLI
// config file.
func ConfigCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "config <key> [value]",
Short: "Create or query an application CLI configuration file",
RunE: runConfigCmd,
Args: cobra.RangeArgs(0, 2),
}
return cmd
}
func runConfigCmd(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)
configPath := filepath.Join(clientCtx.HomeDir, "config")
conf, err := getClientConfig(configPath, clientCtx.Viper)
if err != nil {
return fmt.Errorf("couldn't get client config: %w", err)
}
ncc := NeutronCustomClient{
*conf,
os.Getenv("NEUTROND_GAS"),
os.Getenv("NEUTROND_GAS_PRICES"),
os.Getenv("NEUTROND_GAS_ADJUSTMENT"),
os.Getenv("NEUTROND_FEES"),
// os.Getenv("NEUTROND_FEE_GRANTER"),
// os.Getenv("NEUTROND_FEE_PAYER"),
os.Getenv("NEUTROND_NOTE"),
}
switch len(args) {
case 0:
s, err := json.MarshalIndent(ncc, "", "\t")
if err != nil {
return fmt.Errorf("unable to marshal neutron custom client: %w", err)
}
cmd.Println(string(s))
case 1:
// it's a get
key := args[0]
switch key {
case flags.FlagChainID:
cmd.Println(conf.ChainID)
case flags.FlagKeyringBackend:
cmd.Println(conf.KeyringBackend)
case tmcli.OutputFlag:
cmd.Println(conf.Output)
case flags.FlagNode:
cmd.Println(conf.Node)
case flags.FlagBroadcastMode:
cmd.Println(conf.BroadcastMode)
// Custom flags
case flags.FlagGas:
cmd.Println(ncc.Gas)
case flags.FlagGasPrices:
cmd.Println(ncc.GasPrices)
case flags.FlagGasAdjustment:
cmd.Println(ncc.GasAdjustment)
case flags.FlagFees:
cmd.Println(ncc.Fees)
// TODO: add with newer version of cosmos-sdk
// case flags.FlagFeeGranter:
// cmd.Println(ncc.FeeGranter)
// case flags.FlagFeePayer:
// cmd.Println(ncc.FeePayer)
case flags.FlagNote:
cmd.Println(ncc.Note)
default:
err := errUnknownConfigKey(key)
return fmt.Errorf("couldn't get the value for the key: %v, error: %v", key, err)
}
case 2:
// it's set
key, value := args[0], args[1]
switch key {
case flags.FlagChainID:
ncc.ChainID = value
case flags.FlagKeyringBackend:
ncc.KeyringBackend = value
case tmcli.OutputFlag:
ncc.Output = value
case flags.FlagNode:
ncc.Node = value
case flags.FlagBroadcastMode:
ncc.BroadcastMode = value
case flags.FlagGas:
ncc.Gas = value
case flags.FlagGasPrices:
ncc.GasPrices = value
ncc.Fees = "" // resets since we can only use 1 at a time
case flags.FlagGasAdjustment:
ncc.GasAdjustment = value
case flags.FlagFees:
ncc.Fees = value
ncc.GasPrices = "" // resets since we can only use 1 at a time
// TODO: add with newer version of cosmos-sdk
// case flags.FlagFeeGranter:
// ncc.FeeGranter = value
// case flags.FlagFeePayer:
// ncc.FeePayer = value
case flags.FlagNote:
ncc.Note = value
default:
return errUnknownConfigKey(key)
}
confFile := filepath.Join(configPath, "client.toml")
if err := writeConfigToFile(confFile, &ncc); err != nil {
return fmt.Errorf("could not write client config to the file: %v", err)
}
default:
return fmt.Errorf("too many arguments: accepts between 0 and 2 args")
}
return nil
}
const defaultConfigTemplate = `# This is a TOML config file.
# For more information, see https://github.com/toml-lang/toml
###############################################################################
### Client Configuration ###
###############################################################################
# The network chain ID
chain-id = "{{ .ChainID }}"
# The keyring's backend, where the keys are stored (os|file|kwallet|pass|test|memory)
keyring-backend = "{{ .KeyringBackend }}"
# CLI output format (text|json)
output = "{{ .Output }}"
# <host>:<port> to Tendermint RPC interface for this chain
node = "{{ .Node }}"
# Transaction broadcasting mode (sync|async|block)
broadcast-mode = "{{ .BroadcastMode }}"
###############################################################################
### Neutron Tx Configuration ###
###############################################################################
# Amount of gas per transaction
gas = "{{ .Gas }}"
# Price per unit of gas (ex: 0.005untrn)
gas-prices = "{{ .GasPrices }}"
gas-adjustment = "{{ .GasAdjustment }}"
# Fees to use instead of set gas prices
fees = "{{ .Fees }}"
# Memo to include in your Transactions
note = "{{ .Note }}"
`
// TODO: add under fee line when cosmos-sdk version merged
// fee-granter = "{{ .FeeGranter }}"
// fee-payer = "{{ .FeePayer }}"
// writeConfigToFile parses defaultConfigTemplate, renders config using the template and writes it to
// configFilePath.
func writeConfigToFile(configFilePath string, config *NeutronCustomClient) error {
var buffer bytes.Buffer
tmpl := template.New("clientConfigFileTemplate")
configTemplate, err := tmpl.Parse(defaultConfigTemplate)
if err != nil {
return fmt.Errorf("could not parse default config template: %w", err)
}
if err := configTemplate.Execute(&buffer, config); err != nil {
return fmt.Errorf("could not apply data to config template: %w", err)
}
return os.WriteFile(configFilePath, buffer.Bytes(), 0o600)
}
// getClientConfig reads values from client.toml file and unmarshalls them into ClientConfig
func getClientConfig(configPath string, v *viper.Viper) (*scconfig.ClientConfig, error) {
v.AddConfigPath(configPath)
v.SetConfigName("client")
v.SetConfigType("toml")
if err := v.ReadInConfig(); err != nil {
return nil, fmt.Errorf("failed to read client config: %w", err)
}
conf := new(scconfig.ClientConfig)
if err := v.Unmarshal(conf); err != nil {
return nil, fmt.Errorf("failed to unmarshal client config: %w", err)
}
return conf, nil
}
func errUnknownConfigKey(key string) error {
return fmt.Errorf("unknown configuration key: %q", key)
}