xaling: add detectActiveTransfers

This commit is contained in:
c9s 2024-07-31 16:51:00 +08:00
parent 6bc8dffe16
commit 4bf558f9eb
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -114,24 +114,32 @@ func (s *Strategy) aggregateBalances(
return totalBalances, sessionBalances
}
func (s *Strategy) detectActiveTransfers(ctx context.Context, sessions map[string]*bbgo.ExchangeSession) {
func (s *Strategy) detectActiveTransfers(ctx context.Context, sessions map[string]*bbgo.ExchangeSession) (bool, error) {
var err2 error
until := time.Now()
since := until.Add(-time.Hour * 24)
for _, session := range sessions {
if transferService, ok := session.Exchange.(types.ExchangeTransferHistoryService); ok {
withdraws, err := transferService.QueryWithdrawHistory(ctx, "", since, until)
if err != nil {
log.WithError(err).Errorf("unable to query withdraw history")
continue
}
transferService, ok := session.Exchange.(types.ExchangeTransferHistoryService)
if !ok {
continue
}
for _, withdraw := range withdraws {
_ = withdraw
// withdraw.
withdraws, err := transferService.QueryWithdrawHistory(ctx, "", since, until)
if err != nil {
log.WithError(err).Errorf("unable to query withdraw history")
err2 = err
continue
}
for _, withdraw := range withdraws {
switch withdraw.Status {
case types.WithdrawStatusProcessing, types.WithdrawStatusSent, types.WithdrawStatusAwaitingApproval:
return true, nil
}
}
}
return false, err2
}
func (s *Strategy) selectSessionForCurrency(
@ -411,6 +419,14 @@ func (s *Strategy) align(ctx context.Context, sessions map[string]*bbgo.Exchange
}
}
foundActiveTransfer, err := s.detectActiveTransfers(ctx, sessions)
if err != nil {
log.WithError(err).Errorf("unable to check active transfers")
} else if foundActiveTransfer {
log.Warnf("found active transfer, skip balance align check")
return
}
totalBalances, sessionBalances := s.aggregateBalances(ctx, sessions)
_ = sessionBalances