Skip to content

Commit

Permalink
refactor(bot): filter tasks based on wallet mode before processing
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
perrornet committed Aug 19, 2024
1 parent 80b90b3 commit 7748386
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 28 deletions.
1 change: 0 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
21 changes: 20 additions & 1 deletion internal/daemons/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
}
29 changes: 17 additions & 12 deletions utils/configs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
// ProviderType liquidity providersMap type
type ProviderType string
type DbType string
type Mode string

const (
// CEX centralized exchange
Expand All @@ -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"`

Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions utils/provider/bridge/okx/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 1 addition & 13 deletions utils/provider/bridge/okx/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
5 changes: 5 additions & 0 deletions utils/provider/bridge/routernitro/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"omni-balance/utils/error_types"
"omni-balance/utils/provider"
"strconv"
"strings"
"time"

log "omni-balance/utils/logging"
Expand Down Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion utils/wallets/safe/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 7748386

Please sign in to comment.