From 252f4fbccc28c76a6e92bc82c25aa4e7e3031c92 Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 16 Aug 2023 12:02:18 +0800 Subject: [PATCH 1/2] deposit2transfer: call QuerySpotAccount for getting the spot balance --- pkg/exchange/max/exchange.go | 51 +++++++++++++++++++---- pkg/strategy/deposit2transfer/strategy.go | 33 ++++++++------- 2 files changed, 63 insertions(+), 21 deletions(-) diff --git a/pkg/exchange/max/exchange.go b/pkg/exchange/max/exchange.go index d49eb1992..83b56f1ed 100644 --- a/pkg/exchange/max/exchange.go +++ b/pkg/exchange/max/exchange.go @@ -562,6 +562,35 @@ func (e *Exchange) getLaunchDate() (time.Time, error) { return time.Date(2018, time.June, 21, 0, 0, 0, 0, loc), nil } +func (e *Exchange) QuerySpotAccount(ctx context.Context) (*types.Account, error) { + if err := e.accountQueryLimiter.Wait(ctx); err != nil { + return nil, err + } + + vipLevel, err := e.client.NewGetVipLevelRequest().Do(ctx) + if err != nil { + return nil, err + } + + // MAX returns the fee rate in the following format: + // "maker_fee": 0.0005 -> 0.05% + // "taker_fee": 0.0015 -> 0.15% + a := &types.Account{ + AccountType: types.AccountTypeSpot, + MarginLevel: fixedpoint.Zero, + MakerFeeRate: fixedpoint.NewFromFloat(vipLevel.Current.MakerFee), // 0.15% = 0.0015 + TakerFeeRate: fixedpoint.NewFromFloat(vipLevel.Current.TakerFee), // 0.15% = 0.0015 + } + + balances, err := e.queryBalances(ctx, maxapi.WalletTypeSpot) + if err != nil { + return nil, err + } + a.UpdateBalances(balances) + + return nil, nil +} + func (e *Exchange) QueryAccount(ctx context.Context) (*types.Account, error) { if err := e.accountQueryLimiter.Wait(ctx); err != nil { return nil, err @@ -577,12 +606,17 @@ func (e *Exchange) QueryAccount(ctx context.Context) (*types.Account, error) { // "taker_fee": 0.0015 -> 0.15% a := &types.Account{ - AccountType: types.AccountTypeSpot, MarginLevel: fixedpoint.Zero, MakerFeeRate: fixedpoint.NewFromFloat(vipLevel.Current.MakerFee), // 0.15% = 0.0015 TakerFeeRate: fixedpoint.NewFromFloat(vipLevel.Current.TakerFee), // 0.15% = 0.0015 } + if e.MarginSettings.IsMargin { + a.AccountType = types.AccountTypeMargin + } else { + a.AccountType = types.AccountTypeSpot + } + balances, err := e.QueryAccountBalances(ctx) if err != nil { return nil, err @@ -590,8 +624,6 @@ func (e *Exchange) QueryAccount(ctx context.Context) (*types.Account, error) { a.UpdateBalances(balances) if e.MarginSettings.IsMargin { - a.AccountType = types.AccountTypeMargin - req := e.v3client.NewGetMarginADRatioRequest() adRatio, err := req.Do(ctx) if err != nil { @@ -606,16 +638,21 @@ func (e *Exchange) QueryAccount(ctx context.Context) (*types.Account, error) { } func (e *Exchange) QueryAccountBalances(ctx context.Context) (types.BalanceMap, error) { - if err := e.accountQueryLimiter.Wait(ctx); err != nil { - return nil, err - } - walletType := maxapi.WalletTypeSpot if e.MarginSettings.IsMargin { walletType = maxapi.WalletTypeMargin } + return e.queryBalances(ctx, walletType) +} + +func (e *Exchange) queryBalances(ctx context.Context, walletType maxapi.WalletType) (types.BalanceMap, error) { + if err := e.accountQueryLimiter.Wait(ctx); err != nil { + return nil, err + } + req := e.v3client.NewGetWalletAccountsRequest(walletType) + accounts, err := req.Do(ctx) if err != nil { return nil, err diff --git a/pkg/strategy/deposit2transfer/strategy.go b/pkg/strategy/deposit2transfer/strategy.go index f27529d5d..8be53b701 100644 --- a/pkg/strategy/deposit2transfer/strategy.go +++ b/pkg/strategy/deposit2transfer/strategy.go @@ -20,6 +20,10 @@ type marginTransferService interface { TransferMarginAccountAsset(ctx context.Context, asset string, amount fixedpoint.Value, io types.TransferDirection) error } +type spotAccountQueryService interface { + QuerySpotAccount(ctx context.Context) (*types.Account, error) +} + const ID = "deposit2transfer" var log = logrus.WithField("strategy", ID) @@ -140,22 +144,23 @@ func (s *Strategy) checkDeposits(ctx context.Context) { return } - account, err2 := s.session.UpdateAccount(ctx) - if err2 != nil { - log.WithError(err2).Errorf("unable to update account") - continue + // we can't use the account from margin + amount := d.Amount + if service, ok := s.session.Exchange.(spotAccountQueryService); ok { + account, err2 := service.QuerySpotAccount(ctx) + if err2 != nil { + log.WithError(err2).Errorf("unable to update account") + continue + } + + if bal, ok := account.Balance(d.Asset); ok { + log.Infof("%s balance: %+v", d.Asset, bal) + amount = fixedpoint.Min(bal.Available, amount) + } else { + log.Errorf("unexpected error: %s balance not found", d.Asset) + } } - bal, ok := account.Balance(d.Asset) - if !ok { - log.Errorf("unexpected error: %s balance not found", d.Asset) - return - } - - log.Infof("%s balance: %+v", d.Asset, bal) - - amount := fixedpoint.Min(bal.Available, d.Amount) - bbgo.Notify("Found succeeded deposit %s %s, transferring %s %s into the margin account", d.Amount.String(), d.Asset, amount.String(), d.Asset) From 5cc09dfb9a5dbc090a8c17cf750e22e0750fabae Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 16 Aug 2023 12:26:01 +0800 Subject: [PATCH 2/2] deposit2transfer: improve log format --- pkg/strategy/deposit2transfer/strategy.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/strategy/deposit2transfer/strategy.go b/pkg/strategy/deposit2transfer/strategy.go index 8be53b701..be59562aa 100644 --- a/pkg/strategy/deposit2transfer/strategy.go +++ b/pkg/strategy/deposit2transfer/strategy.go @@ -154,7 +154,7 @@ func (s *Strategy) checkDeposits(ctx context.Context) { } if bal, ok := account.Balance(d.Asset); ok { - log.Infof("%s balance: %+v", d.Asset, bal) + log.Infof("spot account balance %s: %+v", d.Asset, bal) amount = fixedpoint.Min(bal.Available, amount) } else { log.Errorf("unexpected error: %s balance not found", d.Asset)