-
Notifications
You must be signed in to change notification settings - Fork 0
/
flags.go
165 lines (125 loc) · 6.49 KB
/
flags.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
/*
Copyright © 2023 Clovr Labs
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package main
import (
"os"
"strings"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "liquidator",
Short: "A CLI tool to monitor and automate the liquidity of your LND channels",
Run: func(cmd *cobra.Command, args []string) {
//Cobra main
log.Infoln("Starting liquidator")
startLiquidator()
},
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
func init() {
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
//OTEL Expanded vars
os.Setenv("OTEL_EXPORTER_OTLP_ENDPOINT", os.ExpandEnv("$OTEL_EXPORTER_OTLP_ENDPOINT"))
//Remove http:// from the endpoint if present
if strings.HasPrefix(os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT"), "http://") {
os.Setenv("OTEL_EXPORTER_OTLP_ENDPOINT", strings.Replace(os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT"), "http://", "", 1))
}
//Remove https:// from the endpoint if present
if strings.HasPrefix(os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT"), "https://") {
os.Setenv("OTEL_EXPORTER_OTLP_ENDPOINT", strings.Replace(os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT"), "https://", "", 1))
}
os.Setenv("OTLP_RESOURCES_ATTRIBUTES", os.ExpandEnv("$OTLP_RESOURCES_ATTRIBUTES"))
viper.AutomaticEnv() // read in environment variables that match
//Lndconnect uris
rootCmd.Flags().String("lndconnecturis", "", "CSV of lndconnect strings to connect to lnd(s)")
viper.BindPFlag("lndconnecturis", rootCmd.Flags().Lookup("lndconnecturis"))
//Loopdconnect uris
rootCmd.Flags().String("loopdconnecturis", "", "CSV of loopdconnect strings to connect to loopd(s)")
viper.BindPFlag("loopdconnecturis", rootCmd.Flags().Lookup("loopdconnecturis"))
rootCmd.Flags().String("pollingInterval", "15s", "Interval to poll data")
viper.BindPFlag("pollingInterval", rootCmd.Flags().Lookup("pollingInterval"))
rootCmd.Flags().String("logLevel", "info", "Log level from values: {trace, debug, info, warn, error, fatal, panic}")
viper.BindPFlag("logLevel", rootCmd.Flags().Lookup("logLevel"))
rootCmd.Flags().String("logFormat", "text", "Log format from: {text, json}")
viper.BindPFlag("logFormat", rootCmd.Flags().Lookup("logFormat"))
//Flags for nodeguard grpc endpoint
rootCmd.Flags().String("nodeguardHost", "", "Hostname:port to connect to nodeguard")
viper.BindPFlag("nodeguardHost", rootCmd.Flags().Lookup("nodeguardHost"))
//Swap Publication Offset in minutes
rootCmd.Flags().String("swapPublicationOffset", "60m", "Swap publication deadline offset (Maximum time for the swap provider to publish the swap)")
viper.BindPFlag("swapPublicationOffset", rootCmd.Flags().Lookup("swapPublicationOffset"))
// Retries before applying backoff to the swap
rootCmd.Flags().Int("retriesBeforeBackoff", 3, "Number of retries before applying backoff to the swap")
viper.BindPFlag("retriesBeforeBackoff", rootCmd.Flags().Lookup("retriesBeforeBackoff"))
// Coefficient to apply to the backoff
rootCmd.Flags().Float64("backoffCoefficient", 0.95, "Coefficient to apply to the backoff")
viper.BindPFlag("backoffCoefficient", rootCmd.Flags().Lookup("backoffCoefficient"))
// Limit coefficient of the backoff
rootCmd.Flags().Float64("backoffLimit", 0.1, "Limit coefficient of the backoff")
viper.BindPFlag("backoffLimit", rootCmd.Flags().Lookup("backoffLimit"))
// Limit fees for swaps quotes
rootCmd.Flags().Float64("limitQuoteFees", 0.005, "Limit fee ratio for swaps quotes (e.g. onchain+service fee estimation) e.g. 0.01 = 1%")
viper.BindPFlag("limitQuoteFees", rootCmd.Flags().Lookup("limitQuoteFees"))
// Limit fees for swaps L2
rootCmd.Flags().Float64("limitFeesL2", 0.002, "Limit fee ratio for swaps max routing fee e.g. 0.01 = 1%")
viper.BindPFlag("limitFeesL2", rootCmd.Flags().Lookup("limitFeesL2"))
//Sweep conf
rootCmd.Flags().String("sweepConfTarget", "400", "Target number of confirmations for swaps, this uses bitcoin core broken estimator, procced with caution")
viper.BindPFlag("sweepConfTarget", rootCmd.Flags().Lookup("sweepConfTarget"))
//Now we set the global vars
pollingInterval = viper.GetDuration("pollingInterval")
nodeguardHost = viper.GetString("nodeguardHost")
loopdconnectURIs = strings.Split(viper.GetString("loopdconnecturis"), ",")
lndconnectURIs = strings.Split(viper.GetString("lndconnecturis"), ",")
retries = viper.GetInt("retriesBeforeBackoff")
backoffCoefficient = viper.GetFloat64("backoffCoefficient")
backoffLimit = viper.GetFloat64("backoffLimit")
//Set log level and format
logLevel, err := log.ParseLevel(viper.GetString("logLevel"))
if err != nil {
log.Fatal("Invalid log level")
}
log.SetLevel(logLevel)
if viper.GetString("logFormat") == "json" {
log.SetFormatter(&log.JSONFormatter{})
} else if viper.GetString("logFormat") == "text" {
log.SetFormatter(&log.TextFormatter{})
} else {
log.Fatal("Invalid log format")
}
// Log debug of the config
log.Debug("pollingInterval: ", pollingInterval)
log.Debug("logLevel: ", logLevel)
log.Debug("logFormat: ", viper.GetString("logFormat"))
log.Debug("nodeguardHost: ", nodeguardHost)
log.Debug("retriesBeforeBackoff: ", viper.GetInt("retriesBeforeBackoff"))
log.Debug("backoffCoefficient: ", viper.GetFloat64("backoffCoefficient"))
}