From 77483862fa4e951c9db6de8739b6dc5089074e96 Mon Sep 17 00:00:00 2001 From: perror <23651751+perrornet@users.noreply.github.com> Date: Mon, 19 Aug 2024 20:52:52 +0800 Subject: [PATCH] refactor(bot): filter tasks based on wallet mode before processing Filter out tasks that do not match the wallet's mode (balance/swap) before they are processed, ensuring that only relevant tasks are considered. This optimizes the processing logic and prevents unnecessary operations. --- cmd/main.go | 1 - internal/daemons/bot/bot.go | 21 +++++++++++++++- utils/configs/config.go | 29 +++++++++++++--------- utils/provider/bridge/okx/utils.go | 4 +++ utils/provider/bridge/okx/vars.go | 14 +---------- utils/provider/bridge/routernitro/utils.go | 5 ++++ utils/wallets/safe/utils.go | 8 +++++- 7 files changed, 54 insertions(+), 28 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 84f3c5f..df73a8b 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -64,7 +64,6 @@ func Usage(_ *cli.Context) error { } func Action(cli *cli.Context) error { - fmt.Println(cli.String("conf")) if err := initConfig(ctx, cli.Bool("placeholder"), cli.String("conf"), cli.String("port")); err != nil { return errors.Wrap(err, "init config") } diff --git a/internal/daemons/bot/bot.go b/internal/daemons/bot/bot.go index 45f64c7..4001027 100644 --- a/internal/daemons/bot/bot.go +++ b/internal/daemons/bot/bot.go @@ -12,6 +12,7 @@ import ( log "omni-balance/utils/logging" "github.com/ethereum/go-ethereum/ethclient/simulated" + "github.com/jinzhu/copier" "github.com/pkg/errors" ) @@ -106,6 +107,24 @@ func process(ctx context.Context, conf configs.Config, walletAddress, tokenName, if err != nil { return nil, bot.Parallel, err } - return tasks, processType, nil + var result []bot.Task + for index, task := range tasks { + if task.TokenInName != "" { + result = append(result, tasks[index]) + continue + } + walletConf := conf.GetWalletConfig(task.Wallet) + if !walletConf.Mode.IsBalance() { + continue + } + var t = new(bot.Task) + if err := copier.Copy(t, &task); err != nil { + return nil, bot.Parallel, err + } + t.TokenInName = task.TokenOutName + log.Debugf("%s mode is balance, change tokenInName to %s", task.Wallet, t.TokenInName) + result = append(result, *t) + } + return result, processType, nil } } diff --git a/utils/configs/config.go b/utils/configs/config.go index 81f6f00..83a6e8b 100644 --- a/utils/configs/config.go +++ b/utils/configs/config.go @@ -20,6 +20,7 @@ import ( // ProviderType liquidity providersMap type type ProviderType string type DbType string +type Mode string const ( // CEX centralized exchange @@ -39,6 +40,21 @@ const ( SQLite DbType = "SQLite" ) +const ( + Balance Mode = "balance" + Swap Mode = "swap" +) + +func (m Mode) String() string { + return string(m) +} +func (m Mode) IsBalance() bool { + return m == Balance +} +func (m Mode) IsSwap() bool { + return m == Swap +} + type Config struct { ApiKey string `json:"api_key" yaml:"apiKey" comment:"API key"` @@ -83,7 +99,7 @@ type Wallet struct { // BotTypes The type of monitoring, support: balance_on_chain, helix_liquidity, gate_liquidity, the default is balance_on_chain. BotTypes []BotConfig `json:"bot_types" yaml:"botTypes" comment:"BotTypes The type of monitoring, support: balance_on_chain, helix_liquidity, gate_liquidity, the default is balance_on_chain."` Address string `json:"address" yaml:"address" comment:"Monitoring address"` - Mode string `json:"mode" yaml:"mode" comment:"rebalance mode, support: balance, swap. balance: no swap, only transfer token on chain; swap: swap token on chain"` + Mode Mode `json:"mode" yaml:"mode" comment:"rebalance mode, support: balance, swap. balance: no swap, only transfer token on chain; swap: swap token on chain"` Operator Operator `json:"operator" yaml:"operator" comment:"Used to isolate the monitoring address and the operation address, preventing the leakage of the monitoring address private key. If Operator is empty, it is not enabled. If 'multi_sign_type' is not empty, 'address' is multi sign address, 'operator' is multi sign operator address."` MultiSignType string `json:"multi_sign_type" yaml:"multiSignType" comment:"multi sign address type, support: safe. If not empty, 'address' is multi sign address, 'operator' is multi sign operator address"` Tokens []WalletToken `json:"tokens" yaml:"tokens" comment:"Tokens to be monitored"` @@ -459,17 +475,6 @@ func (c *Config) GetWalletConfig(wallet string) Wallet { return c.wallets[wallet] } -func (c *Config) GetWalletMode(wallet string) string { - switch strings.ToLower(c.wallets[wallet].Mode) { - case "balance": - return "balance" - case "swap": - return "swap" - default: - return "balance" - } -} - func (c *Config) GetWalletTokenInfo(wallet, tokenName string) WalletToken { for _, token := range c.GetWalletConfig(wallet).Tokens { if !strings.EqualFold(token.Name, tokenName) { diff --git a/utils/provider/bridge/okx/utils.go b/utils/provider/bridge/okx/utils.go index 56d9753..d2aad64 100644 --- a/utils/provider/bridge/okx/utils.go +++ b/utils/provider/bridge/okx/utils.go @@ -164,10 +164,14 @@ func (o *OKX) GetBestTokenInChain(ctx context.Context, args provider.SwapParams) return } + wallet := o.conf.GetWalletConfig(args.Sender.GetAddress().Hex()) for _, sourceToken := range o.conf.SourceTokens { if args.SourceToken != "" && sourceToken.Name != args.SourceToken { continue } + if wallet.Mode.IsBalance() && !strings.EqualFold(sourceToken.Name, args.TargetToken) { + continue + } for _, v := range sourceToken.Chains { if args.SourceChain != "" && v != args.SourceChain { continue diff --git a/utils/provider/bridge/okx/vars.go b/utils/provider/bridge/okx/vars.go index ec210a4..d0e1078 100644 --- a/utils/provider/bridge/okx/vars.go +++ b/utils/provider/bridge/okx/vars.go @@ -139,20 +139,8 @@ type BuildTxData struct { } type QuoteData struct { - FromChainId int `json:"fromChainId"` - FromToken struct { - Decimals int `json:"decimals"` - TokenContractAddress string `json:"tokenContractAddress"` - TokenSymbol string `json:"tokenSymbol"` - } `json:"fromToken"` FromTokenAmount string `json:"fromTokenAmount"` - ToChainId int `json:"toChainId"` - ToToken struct { - Decimals int `json:"decimals"` - TokenContractAddress string `json:"tokenContractAddress"` - TokenSymbol string `json:"tokenSymbol"` - } `json:"toToken"` - RouterList []struct { + RouterList []struct { EstimateTime string `json:"estimateTime"` FromDexRouterList []struct { Router string `json:"router"` diff --git a/utils/provider/bridge/routernitro/utils.go b/utils/provider/bridge/routernitro/utils.go index 6612dbb..94477c4 100644 --- a/utils/provider/bridge/routernitro/utils.go +++ b/utils/provider/bridge/routernitro/utils.go @@ -12,6 +12,7 @@ import ( "omni-balance/utils/error_types" "omni-balance/utils/provider" "strconv" + "strings" "time" log "omni-balance/utils/logging" @@ -174,10 +175,14 @@ func (r Routernitro) GetBestQuote(ctx context.Context, args provider.SwapParams) return } + wallet := r.conf.GetWalletConfig(args.Sender.GetAddress().Hex()) for _, sourceToken := range r.conf.SourceTokens { if args.SourceToken != "" && sourceToken.Name != args.SourceToken { continue } + if wallet.Mode.IsBalance() && !strings.EqualFold(sourceToken.Name, args.TargetToken) { + continue + } for _, v := range sourceToken.Chains { if args.SourceChain != "" && v != args.SourceChain { continue diff --git a/utils/wallets/safe/utils.go b/utils/wallets/safe/utils.go index 2e38da1..6472ef7 100644 --- a/utils/wallets/safe/utils.go +++ b/utils/wallets/safe/utils.go @@ -110,10 +110,16 @@ type Transaction struct { func (s *Safe) GetDomainByCtx(ctx context.Context) string { chainName := cast.ToString(ctx.Value(constant.ChainNameKeyInCtx)) + if chainName == "" { + debug.PrintStack() + log.Fatalf("chain name not found in context") + } if _, ok := safeDomain[chainName]; ok { return safeDomain[chainName] } - panic("chain name not found in context") + debug.PrintStack() + log.Fatalf("chain name %s not found in safe domain", chainName) + return "" } func (s *Safe) GetChainIdByCtx(ctx context.Context) int {