diff --git a/cmd.go b/cmd.go index 4353965..b84cfec 100644 --- a/cmd.go +++ b/cmd.go @@ -60,6 +60,7 @@ func initConfig() { viper.SetDefault("account_name", "hub_"+uuid.Must(uuid.NewRandom()).String()[:4]) viper.SetDefault("slack.enabled", true) viper.SetDefault("slack.channel_id", "poor-bots") + viper.SetDefault("minimum_dym_balance", defaultMinimumDymBalance) if cfgFile != "" { // Use config file from the flag. diff --git a/config.go b/config.go index a41a6aa..44a3b88 100644 --- a/config.go +++ b/config.go @@ -10,14 +10,15 @@ import ( ) type Config struct { - KeyringBackend string `mapstructure:"keyring_backend"` - HomeDir string `mapstructure:"home_dir"` - AccountName string `mapstructure:"account_name"` - Mnemonic string `mapstructure:"mnemonic"` - NodeAddress string `mapstructure:"node_address"` - ChainID string `mapstructure:"chain_id"` - GasPrices string `mapstructure:"gas_prices"` - GasFees string `mapstructure:"gas_fees"` + KeyringBackend string `mapstructure:"keyring_backend"` + HomeDir string `mapstructure:"home_dir"` + AccountName string `mapstructure:"account_name"` + Mnemonic string `mapstructure:"mnemonic"` + NodeAddress string `mapstructure:"node_address"` + ChainID string `mapstructure:"chain_id"` + GasPrices string `mapstructure:"gas_prices"` + GasFees string `mapstructure:"gas_fees"` + MinimumDymBalance string `mapstructure:"minimum_dym_balance"` SlackConfig slackConfig `mapstructure:"slack"` } @@ -31,15 +32,15 @@ type slackConfig struct { const ( // nodeAddress = "https://rpc.hwpd.noisnemyd.xyz:443" - nodeAddress = "http://localhost:36657" - chainID = "dymension_100-1" - hubAddressPrefix = "dym" - pubKeyPrefix = "pub" - defaultGasLimit = 300000 - defaultGasPrices = "2000000000adym" - minimumDymBalance = "997863676000000000adym" - testKeyringBackend = "test" - defaultMaxOrdersPerTx = 10 + nodeAddress = "http://localhost:36657" + chainID = "dymension_100-1" + hubAddressPrefix = "dym" + pubKeyPrefix = "pub" + defaultGasLimit = 300000 + defaultGasPrices = "2000000000adym" + defaultMinimumDymBalance = "40000000000adym" + testKeyringBackend = "test" + defaultMaxOrdersPerTx = 10 defaultRefreshInterval = 30 * time.Second defaultFulfillInterval = 2 * time.Second diff --git a/order_client.go b/order_client.go index fdebfc9..2e778ed 100644 --- a/order_client.go +++ b/order_client.go @@ -25,10 +25,11 @@ type orderClient struct { chainID string node string - domu sync.Mutex - demandOrders map[string]*demandOrder // id:demandOrder - maxOrdersPerTx int - disputePeriod uint64 + domu sync.Mutex + demandOrders map[string]*demandOrder // id:demandOrder + maxOrdersPerTx int + minimumDymBalance string + disputePeriod uint64 account client.Account accountName string @@ -76,6 +77,7 @@ func newOrderClient(config Config) (*orderClient, error) { logger: logger, chainID: cosmosClient.Context().ChainID, node: config.NodeAddress, + minimumDymBalance: config.MinimumDymBalance, maxOrdersPerTx: defaultMaxOrdersPerTx, refreshInterval: defaultRefreshInterval, fulfillInterval: defaultFulfillInterval, @@ -304,9 +306,9 @@ func (oc *orderClient) cleanup() error { func (oc *orderClient) checkBalances(ctx context.Context) error { // check dym balance - minDym, err := sdk.ParseCoinNormalized(minimumDymBalance) + minDym, err := sdk.ParseCoinNormalized(oc.minimumDymBalance) if err != nil { - return fmt.Errorf("failed to parse minimum DYM balance for '%s': %w", minimumDymBalance, err) + return fmt.Errorf("failed to parse minimum DYM balance for '%s': %w", oc.minimumDymBalance, err) } _, err = oc.checkBalance(ctx, sdk.NewCoins(minDym)) if err != nil { diff --git a/worker.go b/worker.go index eeec0ba..fcc20ae 100644 --- a/worker.go +++ b/worker.go @@ -4,6 +4,7 @@ import ( "context" "time" + sdk "github.com/cosmos/cosmos-sdk/types" "go.uber.org/zap" ) @@ -18,10 +19,28 @@ func (oc *orderClient) orderRefresher(ctx context.Context) { func (oc *orderClient) orderFulfiller(ctx context.Context) { go oc.worker(ctx, "orderFulfiller", oc.fulfillInterval, func() bool { - if oc.denomSkipped("adym") { // TODO: const + /*if oc.denomSkipped("adym") { // TODO: const + oc.logger.Info("DYM balance low, paused order fulfillments") + return false + }*/ + + balance, err := oc.getAccountBalance(ctx, oc.account.GetAddress().String(), "adym") + if err != nil { + oc.logger.Error("failed to get account balance", zap.Error(err)) + return false + } + + minimumDymBalance, err := sdk.ParseCoinNormalized(oc.minimumDymBalance) + if err != nil { + oc.logger.Error("failed to parse minimum DYM balance", zap.Error(err)) + return false + } + + if balance.IsLT(minimumDymBalance) { oc.logger.Info("DYM balance low, paused order fulfillments") return false } + if err := oc.fulfillOrders(ctx); err != nil { oc.logger.Error("failed to fulfill orders", zap.Error(err)) }