From b3c03eec20b931dcf72c19e7e333ed31a57ded84 Mon Sep 17 00:00:00 2001 From: Joonas Lehtimaki Date: Wed, 26 Jun 2024 11:34:01 +0300 Subject: [PATCH] feat: add config file support this adds support for config files so that it will dynamically overwrite the ENV vars set by defaults --- README.md | 22 +++++++- cmd/discord-faucet/discord-faucet.go | 19 ++++--- go.mod | 19 +++++++ go.sum | 36 ++++++++++++ pkg/config/config.go | 82 ++++++++++++++++++++++++++++ pkg/discord/discord.go | 27 ++++----- pkg/faucet/faucet.go | 45 ++++++++------- 7 files changed, 204 insertions(+), 46 deletions(-) create mode 100644 pkg/config/config.go diff --git a/README.md b/README.md index 4fad0e8..b634173 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,23 @@ # Discord Faucet -Another Discord Faucet for Cosmos ecosystems, specifically Warden +Another Discord Faucet for Cosmos ecosystems, specifically Warden but can work on +others. + +## Config + +| ENV | Type | Default | +| -------------- | ------ | --------------------------------------------- | +| PORT | string | 8081 | +| ENV_FILE | string | | +| TOKEN | string | | +| PURGE_INTERVAL | string | 10s | +| MNEMONIC | string | | +| NODE | string | https://rpc.buenavista.wardenprotocol.org:443 | +| CHAIN_ID | string | | +| CLI_NAME | string | wardend | +| ACCOUNT_NAME | string | faucet | +| DENOM | string | uward | +| AMOUNT | string | 10000000 | +| FEES | string | 25uward | +| TX_RETRY | int | 10 | +| COOLDOWN | string | 10s | diff --git a/cmd/discord-faucet/discord-faucet.go b/cmd/discord-faucet/discord-faucet.go index 965ab8e..cd98b4c 100644 --- a/cmd/discord-faucet/discord-faucet.go +++ b/cmd/discord-faucet/discord-faucet.go @@ -11,6 +11,7 @@ import ( "github.com/prometheus/client_golang/prometheus/promhttp" log "github.com/rs/zerolog" + "github.com/warden-protocol/discord-faucet/pkg/config" "github.com/warden-protocol/discord-faucet/pkg/discord" ) @@ -23,7 +24,14 @@ func main() { log.ConsoleWriter{Out: os.Stderr, TimeFormat: time.RFC3339}, ).Level(log.InfoLevel).With().Timestamp().Logger() - discordBot, err := discord.InitDiscord() + // load configuration + config, err := config.LoadConfig() + if err != nil { + logger.Fatal().Err(err).Msg("Failed to load configuration") + } + + // init discordbot + discordBot, err := discord.InitDiscord(config) if err != nil { logger.Fatal().Err(err).Msg("Failed to initialize discord bot") } @@ -40,14 +48,9 @@ func main() { http.Handle("/metrics", promhttp.Handler()) - port := os.Getenv("PORT") - if port == "" { - port = "8081" - } - - logger.Info().Msgf("starting metrics server on port %s", port) + logger.Info().Msgf("starting metrics server on port %s", config.Port) server := &http.Server{ - Addr: ":" + port, + Addr: ":" + config.Port, ReadHeaderTimeout: serverTimeout * time.Second, } diff --git a/go.mod b/go.mod index ac1d73e..b25c1e9 100644 --- a/go.mod +++ b/go.mod @@ -4,23 +4,42 @@ go 1.22 require ( github.com/bwmarrin/discordgo v0.28.1 + github.com/caarlos0/env/v10 v10.0.0 github.com/caarlos0/env/v9 v9.0.0 github.com/cosmos/cosmos-sdk v0.50.6 github.com/prometheus/client_golang v1.19.0 github.com/rs/zerolog v1.33.0 + github.com/spf13/viper v1.18.2 ) require ( github.com/beorn7/perks v1.0.1 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/cosmos/btcutil v1.0.5 // indirect + github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/gorilla/websocket v1.5.0 // indirect + github.com/hashicorp/hcl v1.0.0 // indirect + github.com/magiconair/properties v1.8.7 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect + github.com/mitchellh/mapstructure v1.5.0 // indirect + github.com/pelletier/go-toml/v2 v2.1.0 // indirect github.com/prometheus/client_model v0.6.1 // indirect github.com/prometheus/common v0.52.2 // indirect github.com/prometheus/procfs v0.13.0 // indirect + github.com/sagikazarmark/locafero v0.4.0 // indirect + github.com/sagikazarmark/slog-shim v0.1.0 // indirect + github.com/sourcegraph/conc v0.3.0 // indirect + github.com/spf13/afero v1.11.0 // indirect + github.com/spf13/cast v1.6.0 // indirect + github.com/spf13/pflag v1.0.5 // indirect + github.com/subosito/gotenv v1.6.0 // indirect + go.uber.org/multierr v1.10.0 // indirect golang.org/x/crypto v0.22.0 // indirect + golang.org/x/exp v0.0.0-20240404231335-c0f41cb1a7a0 // indirect golang.org/x/sys v0.19.0 // indirect + golang.org/x/text v0.14.0 // indirect google.golang.org/protobuf v1.33.0 // indirect + gopkg.in/ini.v1 v1.67.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 485e108..bf3d520 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/bwmarrin/discordgo v0.28.1 h1:gXsuo2GBO7NbR6uqmrrBDplPUx2T3nzu775q/Rd1aG4= github.com/bwmarrin/discordgo v0.28.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY= +github.com/caarlos0/env/v10 v10.0.0 h1:yIHUBZGsyqCnpTkbjk8asUlx6RFhhEs+h7TOBdgdzXA= +github.com/caarlos0/env/v10 v10.0.0/go.mod h1:ZfulV76NvVPw3tm591U4SwL3Xx9ldzBP9aGxzeN7G18= github.com/caarlos0/env/v9 v9.0.0 h1:SI6JNsOA+y5gj9njpgybykATIylrRMklbs5ch6wO6pc= github.com/caarlos0/env/v9 v9.0.0/go.mod h1:ye5mlCVMYh6tZ+vCgrs/B95sj88cg5Tlnc0XIzgZ020= github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= @@ -13,18 +15,28 @@ github.com/cosmos/cosmos-sdk v0.50.6 h1:efR3MsvMHX5sxS3be+hOobGk87IzlZbSpsI2x/Vw github.com/cosmos/cosmos-sdk v0.50.6/go.mod h1:lVkRY6cdMJ0fG3gp8y4hFrsKZqF4z7y0M2UXFb9Yt40= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= +github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= +github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= +github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= +github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= +github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= +github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4= +github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= @@ -39,11 +51,31 @@ github.com/prometheus/procfs v0.13.0/go.mod h1:cd4PFCR54QLnGKPaKGA6l+cfuNXtht43Z github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8= github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= +github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ= +github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4= +github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE= +github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ= +github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= +github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0= +github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8= +github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY= +github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0= +github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/spf13/viper v1.18.2 h1:LUXCnvUvSM6FXAsj6nnfc8Q2tp1dIgUfY9Kc8GsSOiQ= +github.com/spf13/viper v1.18.2/go.mod h1:EKmWIqdnk5lOcmR72yw6hS+8OPYcwD0jteitLMVB+yk= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= +github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= +go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ= +go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30= golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M= +golang.org/x/exp v0.0.0-20240404231335-c0f41cb1a7a0 h1:985EYyeCOxTpcgOTJpflJUwOeEz0CQOdPt73OzpE9F8= +golang.org/x/exp v0.0.0-20240404231335-c0f41cb1a7a0/go.mod h1:/lliqkxwWAhPjf5oSOIJup2XcqJaw8RGS6k3TGEc7GI= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -53,8 +85,12 @@ golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o= golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI= google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= +gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= +gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/pkg/config/config.go b/pkg/config/config.go new file mode 100644 index 0000000..a048a71 --- /dev/null +++ b/pkg/config/config.go @@ -0,0 +1,82 @@ +package config + +import ( + "errors" + "fmt" + "path/filepath" + "strings" + + "github.com/caarlos0/env/v10" + "github.com/spf13/viper" +) + +var errConfig = errors.New("config error") + +func configError(msg string) error { + return fmt.Errorf("%w: %s", errConfig, msg) +} + +type Config struct { + Port string `env:"PORT" envDefault:"8081" mapstructure:"PORT"` + EnvFile string `env:"ENV_FILE" envDefault:""` + Token string `env:"TOKEN" envDefault:"" mapstructure:"TOKEN"` + PurgeInterval string `env:"PURGE_INTERVAL" envDefault:"10s" mapstructure:"PURGE_INTERVAL"` + Mnemonic string `env:"MNEMONIC" envDefault:"" mapstructure:"MNEMONIC"` + Node string `env:"NODE" envDefault:"https://rpc.buenavista.wardenprotocol.org:443" mapstructure:"NODE"` + ChainID string `env:"CHAIN_ID" envDefault:"buenavista-1" mapstructure:"CHAIN_ID"` + CliName string `env:"CLI_NAME" envDefault:"wardend" mapstructure:"CLI_NAME"` + AccountName string `env:"ACCOUNT_NAME" envDefault:"faucet" mapstructure:"ACCOUNT_NAME"` + Denom string `env:"DENOM" envDefault:"uward" mapstructure:"DENOM"` + Amount string `env:"AMOUNT" envDefault:"10000000" mapstructure:"AMOUNT"` + Fees string `env:"FEES" envDefault:"25uward" mapstructure:"FEES"` + TXRetry int `env:"TX_RETRY" envDefault:"10" mapstructure:"TX_RETRY"` + CoolDown string `env:"COOLDOWN" envDefault:"10s" mapstructure:"COOLDOWN"` +} + +func LoadConfig() (Config, error) { + cfg := Config{} + var err error + + // setDefaults(*cfg) + + if err = env.Parse(&cfg); err != nil { + return Config{}, configError(err.Error()) + } + + if cfg.EnvFile != "" { + if err = loadConfigFile(&cfg); err != nil { + return Config{}, configError(err.Error()) + } + } + return cfg, nil +} + +func loadConfigFile(cfg *Config) error { + var err error + + // parse config file params + // Extract the directory + dir := filepath.Dir(cfg.EnvFile) + "/" + + // Extract the base name (filename without directory) + base := filepath.Base(cfg.EnvFile) + + // Split the base name into name and extension + name := strings.TrimSuffix(base, filepath.Ext(base)) + ext := strings.TrimPrefix(filepath.Ext(base), ".") + + viper.AddConfigPath(dir) + viper.SetConfigName(name) + viper.SetConfigType(ext) + + viper.AutomaticEnv() + err = viper.ReadInConfig() + if err != nil { + return err + } + + if err = viper.Unmarshal(&cfg); err != nil { + return err + } + return nil +} diff --git a/pkg/discord/discord.go b/pkg/discord/discord.go index a01732f..3d70d1e 100644 --- a/pkg/discord/discord.go +++ b/pkg/discord/discord.go @@ -12,16 +12,13 @@ import ( "github.com/caarlos0/env/v9" "github.com/rs/zerolog" + "github.com/warden-protocol/discord-faucet/pkg/config" "github.com/warden-protocol/discord-faucet/pkg/faucet" ) -const ( - defaultPurgeInterval = 10 -) - type Discord struct { Session *discordgo.Session - Token string `env:"TOKEN" envDefault:""` + Token string PurgeInterval time.Duration Requests map[string]time.Time Faucet faucet.Faucet @@ -29,11 +26,12 @@ type Discord struct { *sync.Mutex } -func InitDiscord() (Discord, error) { +func InitDiscord(config config.Config) (Discord, error) { var err error d := Discord{ Mutex: &sync.Mutex{}, + Token: config.Token, } if err = env.Parse(&d); err != nil { return Discord{}, err @@ -56,20 +54,15 @@ func InitDiscord() (Discord, error) { d.Requests = make(map[string]time.Time) d.logger.Info().Msg("initialising faucet") - d.Faucet, err = faucet.InitFaucet() + d.Faucet, err = faucet.InitFaucet(config) if err != nil { return Discord{}, err } d.Faucet.Logger = d.logger - interval := os.Getenv("PURGE_INTERVAL") - if interval == "" { - d.PurgeInterval = defaultPurgeInterval * time.Second - } else { - d.PurgeInterval, err = time.ParseDuration(interval) - if err != nil { - return Discord{}, err - } + d.PurgeInterval, err = time.ParseDuration(config.PurgeInterval) + if err != nil { + return Discord{}, err } return d, nil @@ -126,7 +119,9 @@ func (d *Discord) requestFunds(m *discordgo.MessageCreate) { returnMsg = fmt.Sprintf(":red_circle: %s", err) } else { returnMsg = fmt.Sprintf( - ":white_check_mark: 10 WARD sent to address %s \n %s", + ":white_check_mark: %s %s sent to address %s \n %s", + d.Faucet.Amount, + d.Faucet.Denom, addr, fmt.Sprintf("https://testnet.warden.explorers.guru/transaction/%s", tx), ) diff --git a/pkg/faucet/faucet.go b/pkg/faucet/faucet.go index dfaeeaf..ee600d1 100644 --- a/pkg/faucet/faucet.go +++ b/pkg/faucet/faucet.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "io" - "os" "os/exec" "strings" "time" @@ -12,19 +11,21 @@ import ( "github.com/caarlos0/env/v9" "github.com/cosmos/cosmos-sdk/types/bech32" "github.com/rs/zerolog" + + "github.com/warden-protocol/discord-faucet/pkg/config" ) type Faucet struct { Cooldown time.Duration - Mnemonic string `env:"MNEMONIC" envDefault:""` - Node string `env:"NODE" envDefault:"https://rpc.buenavista.wardenprotocol.org:443"` - ChainID string `env:"CHAIN_ID" envDefault:"buenavista-1"` - CliName string `env:"CLI_NAME" envDefault:"wardend"` - AccountName string `env:"ACCOUNT_NAME" envDefault:"faucet"` - Denom string `env:"DENOM" envDefault:"uward"` - Amount string `env:"AMOUNT" envDefault:"10000000"` - Fees string `env:"FEES" envDefault:"25uward"` - TXRetry int `env:"TX_RETRY" envDefault:"10"` + Mnemonic string + Node string + ChainID string + CliName string + AccountName string + Denom string + Amount string + Fees string + TXRetry int Requests map[string]time.Time Logger zerolog.Logger } @@ -38,14 +39,6 @@ type Out struct { Stderr []byte } -func envOrDefault(key, defaultValue string) string { - v := os.Getenv(key) - if v == "" { - return defaultValue - } - return v -} - func execute(cmdString string) (Out, error) { // Create the command cmd := exec.Command("sh", "-c", cmdString) @@ -115,15 +108,25 @@ func validAddress(addr string) error { return nil } -func InitFaucet() (Faucet, error) { +func InitFaucet(config config.Config) (Faucet, error) { var err error - f := Faucet{} + f := Faucet{ + Mnemonic: config.Mnemonic, + Node: config.Node, + ChainID: config.ChainID, + CliName: config.CliName, + AccountName: config.AccountName, + Denom: config.Denom, + Amount: config.Amount, + Fees: config.Fees, + TXRetry: config.TXRetry, + } if err = env.Parse(&f); err != nil { return Faucet{}, err } - f.Cooldown, err = time.ParseDuration(envOrDefault("COOLDOWN", "12h")) + f.Cooldown, err = time.ParseDuration(config.CoolDown) if err != nil { return Faucet{}, err }