Skip to content

Commit

Permalink
Add log levels, separate whale and bots config, add whitelist for den…
Browse files Browse the repository at this point in the history
…oms, add slack alerting
  • Loading branch information
zale144 committed Apr 29, 2024
1 parent 787e84e commit 7d4e41c
Show file tree
Hide file tree
Showing 6 changed files with 229 additions and 97 deletions.
58 changes: 19 additions & 39 deletions alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,37 @@ type slacker struct {
*slack.Client // TODO: abstract this out
channelID string
enabled bool
alertedLowGas bool // TODO: no good - just a placeholder
logger *zap.Logger
}

func (oc *slacker) begOnSlack(ctx context.Context, orderID, address string, coin sdk.Coin, chainID, node string) (string, error) {
func newSlacker(config slackConfig, logger *zap.Logger) *slacker {
return &slacker{
Client: slack.New(config.AppToken),
channelID: config.ChannelID,
enabled: config.Enabled,
logger: logger.With(zap.String("module", "slack")),
}
}

func (oc *slacker) begOnSlack(
ctx context.Context,
address string,
coin, balance sdk.Coin,
chainID, node string,
) (string, error) {
if !oc.enabled {
oc.logger.Debug("Slack is disabled")
return "", nil
}

oc.logger.With(
zap.String("orderID", orderID),
zap.String("denom", coin.Denom),
zap.String("amount", coin.Amount.String()),
zap.String("amount", coin.String()),
zap.String("balance", balance.String()),
zap.String("address", address),
).Debug("Slack post @poor-bots")

message := fmt.Sprintf("Please sir, send %s to my account %s, so I can fulfill order '%s'. I'm on chain %s, on node %s",
coin.String(), address, orderID, chainID, node)

if orderID == "gas" {
message = fmt.Sprintf("Please sir, send %s to my account %s, so I have enough gas to continue fulfilling orders. I'm on chain %s, on node %s",
coin.String(), address, chainID, node)
}
message := fmt.Sprintf("Please sir, send %s to my account %s. I'm on chain '%s', on node %s and I only have %s.",
coin.String(), address, chainID, node, balance.String())

respChannel, respTimestamp, err := oc.PostMessageContext(
ctx,
Expand All @@ -62,30 +69,3 @@ func (oc *slacker) begOnSlack(ctx context.Context, orderID, address string, coin
)
return respTimestamp, nil
}

func (oc *slacker) alertLowOrderBalance(ctx context.Context, address, chainID, node string, order *demandOrder, coin sdk.Coin) {
// this is lost after a restart
if order.alertedLowFunds {
return
}

oc.logger.Info("Low balance to fulfill order", zap.String("orderID", order.id), zap.String("balance", coin.String()))

if _, err := oc.begOnSlack(ctx, order.id, address, coin, chainID, node); err != nil {
oc.logger.Error("failed to bed on slack", zap.Error(err))
}
order.alertedLowFunds = true
}

func (oc *slacker) alertLowGasBalance(ctx context.Context, address, chainID, node string, coin sdk.Coin) {
if oc.alertedLowGas {
return
}

oc.logger.Warn("Low gas balance", zap.String("balance", coin.String()))

if _, err := oc.begOnSlack(ctx, "gas", address, coin, chainID, node); err != nil {
oc.logger.Error("failed to bed on slack", zap.Error(err))
}
oc.alertedLowGas = true
}
60 changes: 44 additions & 16 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,37 @@ import (
)

type Config struct {
WhaleAccountName string `mapstructure:"whale_account_name"`
BotTopUpFactor uint64 `mapstructure:"bot_top_up_factor"`
KeyringBackend string `mapstructure:"keyring_backend"`
HomeDir string `mapstructure:"home_dir"`
NodeAddress string `mapstructure:"node_address"`
GasPrices string `mapstructure:"gas_prices"`
GasFees string `mapstructure:"gas_fees"`
MinimumGasBalance string `mapstructure:"minimum_gas_balance"`
MaxOrdersPerTx int `mapstructure:"max_orders_per_tx"`
OrderRefreshInterval time.Duration `mapstructure:"order_refresh_interval"`
OrderCleanupInterval time.Duration `mapstructure:"order_cleanup_interval"`
DisputePeriodRefreshInterval time.Duration `mapstructure:"dispute_period_refresh_interval"`
NumberOfBots int `mapstructure:"number_of_bots"`

Whale whaleConfig `mapstructure:"whale"`
Bots botConfig `mapstructure:"bots"`

LogLevel string `mapstructure:"log_level"`
SlackConfig slackConfig `mapstructure:"slack"`
}

type botConfig struct {
NumberOfBots int `mapstructure:"number_of_bots"`
KeyringBackend cosmosaccount.KeyringBackend `mapstructure:"keyring_backend"`
KeyringDir string `mapstructure:"keyring_dir"`
TopUpFactor uint64 `mapstructure:"top_up_factor"`
MaxOrdersPerTx int `mapstructure:"max_orders_per_tx"`
}

type whaleConfig struct {
AccountName string `mapstructure:"account_name"`
KeyringBackend cosmosaccount.KeyringBackend `mapstructure:"keyring_backend"`
KeyringDir string `mapstructure:"keyring_dir"`
AllowedDenoms []string `mapstructure:"allowed_denoms"`
}

type slackConfig struct {
Enabled bool `mapstructure:"enabled"`
BotToken string `mapstructure:"bot_token"`
Expand All @@ -41,6 +55,7 @@ const (
defaultNodeAddress = "http://localhost:36657"
hubAddressPrefix = "dym"
pubKeyPrefix = "pub"
defaultLogLevel = "info"
defaultGasLimit = 300000
defaultGasDenom = "adym"
defaultGasPrices = "2000000000" + defaultGasDenom
Expand All @@ -67,14 +82,19 @@ func initConfig() {
}
defaultHomeDir := home + "/.order-client"

viper.SetDefault("whale_account_name", whaleAccountName)
viper.SetDefault("keyring_backend", testKeyringBackend)
viper.SetDefault("home_dir", defaultHomeDir)
viper.SetDefault("log_level", defaultLogLevel)
viper.SetDefault("whale.account_name", whaleAccountName)
viper.SetDefault("whale.keyring_backend", testKeyringBackend)
viper.SetDefault("whale.allowed_denoms", []string{defaultGasDenom})
viper.SetDefault("whale.keyring_dir", defaultHomeDir)
viper.SetDefault("bots.keyring_backend", testKeyringBackend)
viper.SetDefault("bots.keyring_dir", defaultHomeDir)
viper.SetDefault("bots.number_of_bots", defaultNumberOfBots)
viper.SetDefault("bots.top_up_factor", defaultBotTopUpFactor)
viper.SetDefault("node_address", defaultNodeAddress)
viper.SetDefault("gas_prices", defaultGasPrices)
viper.SetDefault("minimum_gas_balance", defaultMinimumGasBalance)
viper.SetDefault("number_of_bots", defaultNumberOfBots)
viper.SetDefault("bot_top_up_factor", defaultBotTopUpFactor)
viper.SetDefault("max_orders_per_tx", defaultMaxOrdersPerTx)
viper.SetDefault("order_refresh_interval", defaultOrderRefreshInterval)
viper.SetDefault("order_cleanup_interval", defaultOrderCleanupInterval)
Expand All @@ -95,17 +115,25 @@ func initConfig() {
}
}

func getCosmosClientOptions(config Config) []cosmosclient.Option {
type clientConfig struct {
homeDir string
nodeAddress string
gasFees string
gasPrices string
keyringBackend cosmosaccount.KeyringBackend
}

func getCosmosClientOptions(config clientConfig) []cosmosclient.Option {
options := []cosmosclient.Option{
cosmosclient.WithAddressPrefix(hubAddressPrefix),
cosmosclient.WithHome(config.HomeDir),
cosmosclient.WithHome(config.homeDir),
cosmosclient.WithBroadcastMode(flags.BroadcastBlock),
cosmosclient.WithNodeAddress(config.NodeAddress),
cosmosclient.WithFees(config.GasFees),
cosmosclient.WithNodeAddress(config.nodeAddress),
cosmosclient.WithFees(config.gasFees),
cosmosclient.WithGasLimit(defaultGasLimit),
cosmosclient.WithGasPrices(config.GasPrices),
cosmosclient.WithKeyringBackend(cosmosaccount.KeyringBackend(config.KeyringBackend)),
cosmosclient.WithKeyringDir(config.HomeDir),
cosmosclient.WithGasPrices(config.gasPrices),
cosmosclient.WithKeyringBackend(config.keyringBackend),
cosmosclient.WithKeyringDir(config.homeDir),
}
return options
}
Loading

0 comments on commit 7d4e41c

Please sign in to comment.