Merge pull request #1288 from c9s/c9s/strategy-deposit2transfer

FIX: [deposit2transfer] call QuerySpotAccount for getting the spot balance
This commit is contained in:
c9s 2023-08-16 12:36:35 +08:00 committed by GitHub
commit 52f9cbd48d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 63 additions and 21 deletions

View File

@ -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

View File

@ -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,21 +144,22 @@ func (s *Strategy) checkDeposits(ctx context.Context) {
return
}
account, err2 := s.session.UpdateAccount(ctx)
// 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
}
bal, ok := account.Balance(d.Asset)
if !ok {
if bal, ok := account.Balance(d.Asset); ok {
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)
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,