Skip to content

Commit

Permalink
Fix balance checking for DYM
Browse files Browse the repository at this point in the history
  • Loading branch information
zale144 committed Apr 21, 2024
1 parent 7272e7c commit 8e51c5b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 24 deletions.
1 change: 1 addition & 0 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
35 changes: 18 additions & 17 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand All @@ -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
Expand Down
14 changes: 8 additions & 6 deletions order_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
21 changes: 20 additions & 1 deletion worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
"go.uber.org/zap"
)

Expand All @@ -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))
}
Expand Down

0 comments on commit 8e51c5b

Please sign in to comment.