Merge pull request #1803 from c9s/c9s/d2t/check-transfer-amount
Some checks are pending
Go / build (1.21, 6.2) (push) Waiting to run
golang-lint / lint (push) Waiting to run

FIX: [deposit2transfer] check transfer amount before transferring
This commit is contained in:
c9s 2024-11-04 17:57:14 +08:00 committed by GitHub
commit 822c2bd5e0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -45,7 +45,8 @@ type Strategy struct {
Assets []string `json:"assets"` Assets []string `json:"assets"`
Interval types.Duration `json:"interval"` Interval types.Duration `json:"interval"`
TransferDelay types.Duration `json:"transferDelay"`
marginTransferService marginTransferService marginTransferService marginTransferService
depositHistoryService types.ExchangeTransferService depositHistoryService types.ExchangeTransferService
@ -70,6 +71,14 @@ func (s *Strategy) Defaults() error {
s.Interval = types.Duration(5 * time.Minute) s.Interval = types.Duration(5 * time.Minute)
} }
if s.TransferDelay == 0 {
s.TransferDelay = types.Duration(3 * time.Second)
}
return nil
}
func (s *Strategy) Initialize() error {
if s.logger == nil { if s.logger == nil {
s.logger = log.Dup() s.logger = log.Dup()
} }
@ -144,6 +153,13 @@ func (s *Strategy) checkDeposits(ctx context.Context) {
if len(succeededDeposits) == 0 { if len(succeededDeposits) == 0 {
logger.Debugf("no %s deposit found", asset) logger.Debugf("no %s deposit found", asset)
continue continue
} else {
logger.Infof("found %d %s deposits", len(succeededDeposits), asset)
}
if s.TransferDelay > 0 {
logger.Infof("delaying transfer for %s...", s.TransferDelay.Duration())
time.Sleep(s.TransferDelay.Duration())
} }
for _, d := range succeededDeposits { for _, d := range succeededDeposits {
@ -168,12 +184,20 @@ func (s *Strategy) checkDeposits(ctx context.Context) {
continue continue
} }
if bal, ok := account.Balance(d.Asset); ok { bal, ok := account.Balance(d.Asset)
if ok {
logger.Infof("spot account balance %s: %+v", d.Asset, bal) logger.Infof("spot account balance %s: %+v", d.Asset, bal)
amount = fixedpoint.Min(bal.Available, amount) amount = fixedpoint.Min(bal.Available, amount)
} else { } else {
logger.Errorf("unexpected error: %s balance not found", d.Asset) logger.Errorf("unexpected error: %s balance not found", d.Asset)
} }
if amount.IsZero() || amount.Sign() < 0 {
bbgo.Notify("Found succeeded deposit %s %s, but the balance %s %s is insufficient, skip transferring",
d.Amount.String(), d.Asset,
bal.Available.String(), bal.Currency)
continue
}
} }
bbgo.Notify("Found succeeded deposit %s %s, transferring %s %s into the margin account", bbgo.Notify("Found succeeded deposit %s %s, transferring %s %s into the margin account",