deposit2transfer: fix mutex lock

This commit is contained in:
c9s 2023-08-08 12:38:23 +08:00
parent 073c4562fd
commit 4a28843a0a
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 13 additions and 8 deletions

View File

@ -9,4 +9,4 @@ exchangeStrategies:
## assets are the assets you want to transfer into the margin account ## assets are the assets you want to transfer into the margin account
assets: assets:
- BTC - USDT

View File

@ -39,7 +39,7 @@ type Strategy struct {
Assets []string `json:"assets"` Assets []string `json:"assets"`
Interval types.Interval `json:"interval"` Interval types.Duration `json:"interval"`
marginTransferService marginTransferService marginTransferService marginTransferService
depositHistoryService types.ExchangeTransferService depositHistoryService types.ExchangeTransferService
@ -56,8 +56,8 @@ func (s *Strategy) ID() string {
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {} func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {}
func (s *Strategy) Defaults() error { func (s *Strategy) Defaults() error {
if s.Interval == "" { if s.Interval == 0 {
s.Interval = types.Interval1m s.Interval = types.Duration(5 * time.Minute)
} }
return nil return nil
@ -87,7 +87,9 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
return errDepositHistoryNotSupport return errDepositHistoryNotSupport
} }
session.UserDataStream.OnStart(func() {
go s.tickWatcher(ctx, s.Interval.Duration()) go s.tickWatcher(ctx, s.Interval.Duration())
})
return nil return nil
} }
@ -97,6 +99,7 @@ func (s *Strategy) tickWatcher(ctx context.Context, interval time.Duration) {
defer ticker.Stop() defer ticker.Stop()
s.checkDeposits(ctx) s.checkDeposits(ctx)
for { for {
select { select {
case <-ctx.Done(): case <-ctx.Done():
@ -121,6 +124,7 @@ func (s *Strategy) checkDeposits(ctx context.Context) {
if len(succeededDeposits) == 0 { if len(succeededDeposits) == 0 {
log.Infof("no %s deposit found", asset) log.Infof("no %s deposit found", asset)
continue
} }
for _, d := range succeededDeposits { for _, d := range succeededDeposits {
@ -163,10 +167,10 @@ func (s *Strategy) scanDepositHistory(ctx context.Context, asset string, duratio
}) })
s.mu.Lock() s.mu.Lock()
defer s.mu.Lock() defer s.mu.Unlock()
for _, deposit := range deposits { for _, deposit := range deposits {
log.Infof("scanning deposit: %+v", deposit) log.Infof("checking deposit: %+v", deposit)
if deposit.Asset != asset { if deposit.Asset != asset {
continue continue
@ -177,9 +181,10 @@ func (s *Strategy) scanDepositHistory(ctx context.Context, asset string, duratio
s.watchingDeposits[deposit.TransactionID] = deposit s.watchingDeposits[deposit.TransactionID] = deposit
} else { } else {
switch deposit.Status { switch deposit.Status {
case types.DepositSuccess: case types.DepositSuccess:
// ignore all deposits that are already success // ignore all deposits that are already success
log.Infof("ignored succeess deposit: %s", deposit.TransactionID) log.Infof("ignored succeess deposit: %s %+v", deposit.TransactionID, deposit)
continue continue
case types.DepositCredited, types.DepositPending: case types.DepositCredited, types.DepositPending: