Skip to content

Commit

Permalink
fix(bot): ensure balance check includes 'balance_on_chain' bot
Browse files Browse the repository at this point in the history
The 'balance_on_chain' bot is now explicitly activated for tokens without a configured bot,
enforcing a default behavior for balance checks. This update addresses an issue where tokens
on specific chains were not being processed due to the absence of a defined bot. Additionally,
the balance check logic has been enhanced to iterate over a list of configured bots,
incorporating support for multiple bot types per token-chain combination.
  • Loading branch information
perrornet committed Aug 16, 2024
1 parent 0b372ad commit 497c448
Show file tree
Hide file tree
Showing 35 changed files with 1,065 additions and 428 deletions.
4 changes: 2 additions & 2 deletions cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ var (
for chainName, chainItems := range result {
for tokenName, TokenItems := range chainItems {
for address, balance := range TokenItems {
if balance.LessThan(decimal.RequireFromString("0")) {
w.AppendRow(table.Row{chainName, tokenName, address, "not found in config"})
if balance.LessThanOrEqual(decimal.RequireFromString("0")) {
// w.AppendRow(table.Row{chainName, tokenName, address, "not found in config"})
continue
}
w.AppendRow(table.Row{chainName, tokenName, address, balance})
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ require (
github.com/ilyakaznacheev/cleanenv v1.5.0
github.com/jedib0t/go-pretty/v6 v6.5.9
github.com/jinzhu/copier v0.4.0
github.com/labstack/gommon v0.3.1
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/pkg/errors v0.9.1
github.com/satori/go.uuid v1.2.0
github.com/schollz/progressbar/v3 v3.14.6
github.com/shopspring/decimal v1.4.0
github.com/spf13/cast v1.6.0
github.com/stretchr/testify v1.9.0
github.com/tidwall/gjson v1.17.3
github.com/uniswapv3-go/uniswapv3-universal-router-decoder-go v0.3.0
github.com/urfave/cli/v2 v2.27.2
github.com/zwgblue/yaml-encoder v0.0.0-20221226083717-a0bdbda0d998
Expand Down Expand Up @@ -126,11 +126,11 @@ require (
github.com/stretchr/objx v0.5.2 // indirect
github.com/supranational/blst v0.3.11 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/tklauser/go-sysconf v0.3.13 // indirect
github.com/tklauser/numcpus v0.7.0 // indirect
github.com/tyler-smith/go-bip39 v1.1.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.1 // indirect
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
go.mongodb.org/mongo-driver v1.14.0 // indirect
Expand Down
19 changes: 7 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -209,16 +209,12 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/labstack/gommon v0.3.1 h1:OomWaJXm7xR6L1HmEtGyQf26TEn7V6X88mktX9kee9o=
github.com/labstack/gommon v0.3.1/go.mod h1:uW6kP17uPlLJsD3ijUYn3/M5bAxtlZhMI6m3MFxTMTM=
github.com/leanovate/gopter v0.2.9 h1:fQjYxZaynp97ozCzfOyOuAGOU4aU/z37zf/tOujFk7c=
github.com/leanovate/gopter v0.2.9/go.mod h1:U2L/78B+KVFIx2VmW6onHJQzXtFb+p5y3y2Sh+Jxxv8=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/mattn/go-colorable v0.1.11/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
Expand Down Expand Up @@ -315,6 +311,13 @@ github.com/supranational/blst v0.3.11 h1:LyU6FolezeWAhvQk0k6O/d49jqgO52MSDDfYgbe
github.com/supranational/blst v0.3.11/go.mod h1:jZJtfjgudtNl4en1tzwPIV3KjUnQUvG3/j+w+fVonLw=
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d h1:vfofYNRScrDdvS342BElfbETmL1Aiz3i2t0zfRj16Hs=
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d/go.mod h1:RRCYJbIwD5jmqPI9XoAFR0OcDxqUctll6zUj/+B4S48=
github.com/tidwall/gjson v1.17.3 h1:bwWLZU7icoKRG+C+0PNwIKC6FCJO/Q3p2pZvuP0jN94=
github.com/tidwall/gjson v1.17.3/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tklauser/go-sysconf v0.3.13 h1:GBUpcahXSpR2xN01jhkNAbTLRk2Yzgggk8IM08lq3r4=
github.com/tklauser/go-sysconf v0.3.13/go.mod h1:zwleP4Q4OehZHGn4CYZDipCgg9usW5IJePewFCGVEa0=
github.com/tklauser/numcpus v0.7.0 h1:yjuerZP127QG9m5Zh/mSO4wqurYil27tHrqwRoRjpr4=
Expand All @@ -325,10 +328,6 @@ github.com/uniswapv3-go/uniswapv3-universal-router-decoder-go v0.3.0 h1:6cA2BelN
github.com/uniswapv3-go/uniswapv3-universal-router-decoder-go v0.3.0/go.mod h1:gIRL4wwG3WyKQbcp3v44N9INbNulPZFq1fGpbLOc4v4=
github.com/urfave/cli/v2 v2.27.2 h1:6e0H+AkS+zDckwPCUrZkKX38mRaau4nL2uipkJpbkcI=
github.com/urfave/cli/v2 v2.27.2/go.mod h1:g0+79LmHHATl7DAcHO99smiR/T7uGLw84w8Y42x+4eM=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.2.1 h1:TVEnxayobAdVkhQfrfes2IzOB6o+z4roRkPF52WA1u4=
github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 h1:+qGGcbkzsfDQNPPe9UDgpxAWQrhbbBXOYJFQDq/dtJw=
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913/go.mod h1:4aEEwZQutDLsQv2Deui4iYQ6DWTxR14g6m8Wv88+Xqk=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down Expand Up @@ -396,9 +395,6 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down Expand Up @@ -457,7 +453,6 @@ gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/datatypes v1.2.1 h1:r+g0bk4LPCW2v4+Ls7aeNgGme7JYdNDQ2VtvlNUfBh0=
Expand Down
3 changes: 1 addition & 2 deletions internal/daemons/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,10 @@ func Run(ctx context.Context, conf configs.Config) error {
continue
}
var botTypes = conf.ListBotNames(wallet.Address, chainName, token.Name)

if len(botTypes) == 0 {
botTypes = append(botTypes, "balance_on_chain")
}

log.Debugf("wallet %s token %s on chain %s has %+v bots to execute", wallet.Address, token.Name, chainName, botTypes)
for _, botType := range botTypes {
w.Add(1)
go func(f func() ([]bot.Task, bot.ProcessType, error), botType string) {
Expand Down
41 changes: 33 additions & 8 deletions internal/daemons/market/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import (
log "omni-balance/utils/logging"

"github.com/ethereum/go-ethereum/common"
"github.com/jinzhu/copier"
"github.com/pkg/errors"
"github.com/shopspring/decimal"
"go.uber.org/zap/zapcore"
)

Expand Down Expand Up @@ -232,6 +234,7 @@ func processOrder(ctx context.Context, order models.Order, conf configs.Config)
if err != nil {
return errors.Wrap(err, "generate order by wallet mode error")
}

providerObj, err := getBestProvider(ctx, order, conf)
if err != nil {
return errors.Wrap(err, "get provider error")
Expand All @@ -243,6 +246,7 @@ func processOrder(ctx context.Context, order models.Order, conf configs.Config)

log.Infof("start rebalance %s on %s use %s provider", order.TokenOutName, order.TargetChainName, providerObj.Name())
args.SourceChainNames = order.TokenInChainNames
args.SourceToken = order.TokenInName
result, providerErr := providerObj.Swap(ctx, args)
if errors.Is(providerErr, context.Canceled) {
return nil
Expand Down Expand Up @@ -277,7 +281,8 @@ func generateOrderByWalletMode(ctx context.Context, order models.Order, conf con
if order.TokenInName != "" {
return order, nil
}
if conf.GetWalletConfig(order.Wallet).Mode != "balance" {
if conf.GetWalletConfig(order.Wallet).Mode != "balance" ||
(order.TokenInName != "" && order.SourceChainName != "") {
return order, nil
}
token := conf.GetWalletTokenInfo(order.Wallet, order.TokenOutName)
Expand All @@ -288,19 +293,39 @@ func generateOrderByWalletMode(ctx context.Context, order models.Order, conf con
if err != nil {
return order, errors.Wrap(err, "new evm client error")
}
tokenInfo := conf.GetTokenInfoOnChain(token.Name, v)
balance, err := chains.GetTokenBalance(ctx, client, tokenInfo.ContractAddress, order.Wallet, tokenInfo.Decimals)
if err != nil {
return order, errors.Wrap(err, "get token balance error")
bots := conf.ListBotNames(order.Wallet, v, token.Name)
if len(bots) == 0 {
bots = append(bots, "balance_on_chain")
}
var total = decimal.Zero
for _, botType := range bots {
balance, err := bot.GetBot(botType).Balance(ctx, bot.Params{
Conf: conf,
Info: bot.Config{
Wallet: conf.GetWallet(order.Wallet),
TokenName: token.Name,
Chain: v,
},
Client: client,
})
if err != nil {
return order, errors.Wrap(err, "get bot balance error")
}
total = total.Add(balance)
}
if balance.Sub(order.Amount).LessThan(threshold) {
if total.Sub(order.Amount).LessThan(threshold) {
continue
}
log.Debugf("wallet %s token %s on chain %s balance is %s, amount is %s, balance - amount >= threshold, can rebalance from this chain", order.Wallet, token.Name, v, total, order.Amount)
sourceChains = append(sourceChains, v)
}
if len(sourceChains) != 0 {
order.TokenInName = token.Name
order.TokenInChainNames = sourceChains
newOrder := new(models.Order)
_ = copier.Copy(newOrder, order)
newOrder.TokenInName = token.Name
newOrder.TokenInChainNames = sourceChains
order = *newOrder
log.Debugf("order mode is 'balance', use %s on %+v as token in, token out is %s on %s", order.TokenInName, order.TokenInChainNames, order.TokenOutName, order.TargetChainName)
}
return order, nil
}
5 changes: 3 additions & 2 deletions internal/daemons/market/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func createUpdateLog(ctx context.Context, order models.Order, result provider.Sw
updateOrder.Status = provider.TxStatus(models.OrderStatusWaitTransferFromOperator)
}
if result.Error != "" {
log.Errorf("#%d wallet %s rebalance %s on %s %s token failed, error info %s", order.ID, wallet.GetAddress(true).Hex(), order.TargetChainName, order.TokenOutName, order.TokenInName, result.Error)
log.Errorf("#%d wallet %s rebalance %s on %s %s token failed, error: %s", order.ID, order.Wallet, order.TokenOutName, order.TargetChainName, order.TokenInName, result.Error)
}

return db.DB().Model(&models.Order{}).Where("id = ?", order.ID).Limit(1).Updates(updateOrder).Error
Expand Down Expand Up @@ -175,9 +175,10 @@ func providerSupportsOrder(ctx context.Context, p provider.Provider, order model
SourceChainNames: order.TokenInChainNames,
})
if err != nil {
log.Debugf("check token %s on %s use %s error: %s", order.TokenOutName, order.TargetChainName, p.Name(), err.Error())
log.Debugf("token %s on %s cannot use %s provider, source chain is '%s', source token is '%s'", order.TokenOutName, order.TargetChainName, p.Name(), order.SourceChainName, order.TokenInName)
return nil, false
}
log.Debugf("check token %s on %s use %s success, the tokenInCosts is %+v", order.TokenOutName, order.TargetChainName, p.Name(), tokenInCosts)
return tokenInCosts, true
}

Expand Down
3 changes: 2 additions & 1 deletion internal/daemons/token_price/token_price.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"omni-balance/internal/models"
"omni-balance/utils"
"omni-balance/utils/configs"
log "omni-balance/utils/logging"
"omni-balance/utils/token_price"
"sync"
"time"

"github.com/labstack/gommon/log"
"github.com/shopspring/decimal"
)

Expand Down Expand Up @@ -39,6 +39,7 @@ func Run(ctx context.Context, conf configs.Config) error {
defer w.Done()
result, err := provider.GetTokenPriceInUSDT(ctx, conf.SourceTokens...)
if err != nil {

log.Warnf("%s get token price error: %s", provider.Name(), err)
return
}
Expand Down
1 change: 0 additions & 1 deletion internal/daemons/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ func FindAllChainBalance(_ context.Context, confPath string, needPrintProgress b
log.Fatalf("get %s balance on %s error: %s", v.tokenName, chainName, err)
return
}
log.Errorf("get %s balance on %s error: %s", v.tokenName, chainName, err)
continue
}
errorCount = 0
Expand Down
8 changes: 6 additions & 2 deletions utils/bot/balance_on_chain/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,18 @@ func (b BalanceOnChain) Name() string {
return "balance_on_chain"
}

func (b BalanceOnChain) Balance(ctx context.Context, args bot.Params) (decimal.Decimal, error) {
token := args.Conf.GetTokenInfoOnChain(args.Info.TokenName, args.Info.Chain)
return args.Info.Wallet.GetExternalBalance(ctx, common.HexToAddress(token.ContractAddress), token.Decimals, args.Client)
}

func (b BalanceOnChain) Check(ctx context.Context, args bot.Params) ([]bot.Task, bot.ProcessType, error) {
var (
config = args.Conf
info = args.Info
tasks []bot.Task
)
token := config.GetTokenInfoOnChain(info.TokenName, args.Info.Chain)
balance, err := info.Wallet.GetExternalBalance(ctx, common.HexToAddress(token.ContractAddress), token.Decimals, args.Client)
balance, err := b.Balance(ctx, args)
if err != nil {
return nil, "", errors.Wrap(err, "get balance error")
}
Expand Down
6 changes: 4 additions & 2 deletions utils/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package bot

import (
"context"
"github.com/ethereum/go-ethereum/ethclient/simulated"
"github.com/shopspring/decimal"
"omni-balance/utils/configs"
"omni-balance/utils/provider"
"omni-balance/utils/wallets"

"github.com/ethereum/go-ethereum/ethclient/simulated"
"github.com/shopspring/decimal"
)

type ProcessType string
Expand All @@ -28,6 +29,7 @@ type Params struct {

type Bot interface {
Check(ctx context.Context, args Params) ([]Task, ProcessType, error)
Balance(ctx context.Context, args Params) (decimal.Decimal, error)
Name() string
}

Expand Down
5 changes: 5 additions & 0 deletions utils/bot/gate_liquidity/gate_liquidity.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"omni-balance/utils/bot/balance_on_chain"
"omni-balance/utils/configs"

"github.com/shopspring/decimal"
"github.com/spf13/cast"
)

Expand All @@ -21,6 +22,10 @@ func (g GateLiquidity) Name() string {
return "gate_liquidity"
}

func (b GateLiquidity) Balance(ctx context.Context, args bot.Params) (decimal.Decimal, error) {
return balance_on_chain.BalanceOnChain{}.Balance(ctx, args)
}

func (b GateLiquidity) Check(ctx context.Context, args bot.Params) ([]bot.Task, bot.ProcessType, error) {
if b.Bot == nil {
b.Bot = balance_on_chain.BalanceOnChain{}
Expand Down
Loading

0 comments on commit 497c448

Please sign in to comment.