mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 14:55:16 +00:00
Merge pull request #1281 from c9s/c9s/strategy-deposit2transfer
FIX: [deposit2transfer] add lastAssetDepositTimes for immediate success deposits
This commit is contained in:
commit
ecc0928ef5
|
@ -47,6 +47,8 @@ type Strategy struct {
|
||||||
session *bbgo.ExchangeSession
|
session *bbgo.ExchangeSession
|
||||||
watchingDeposits map[string]types.Deposit
|
watchingDeposits map[string]types.Deposit
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
|
|
||||||
|
lastAssetDepositTimes map[string]time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Strategy) ID() string {
|
func (s *Strategy) ID() string {
|
||||||
|
@ -74,6 +76,7 @@ func (s *Strategy) InstanceID() string {
|
||||||
func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
|
func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
|
||||||
s.session = session
|
s.session = session
|
||||||
s.watchingDeposits = make(map[string]types.Deposit)
|
s.watchingDeposits = make(map[string]types.Deposit)
|
||||||
|
s.lastAssetDepositTimes = make(map[string]time.Time)
|
||||||
|
|
||||||
var ok bool
|
var ok bool
|
||||||
|
|
||||||
|
@ -161,7 +164,7 @@ func (s *Strategy) scanDepositHistory(ctx context.Context, asset string, duratio
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// sort the recent deposit records in descending order
|
// sort the recent deposit records in ascending order
|
||||||
sort.Slice(deposits, func(i, j int) bool {
|
sort.Slice(deposits, func(i, j int) bool {
|
||||||
return deposits[i].Time.Time().Before(deposits[j].Time.Time())
|
return deposits[i].Time.Time().Before(deposits[j].Time.Time())
|
||||||
})
|
})
|
||||||
|
@ -183,9 +186,16 @@ func (s *Strategy) scanDepositHistory(ctx context.Context, asset string, duratio
|
||||||
switch deposit.Status {
|
switch deposit.Status {
|
||||||
|
|
||||||
case types.DepositSuccess:
|
case types.DepositSuccess:
|
||||||
// ignore all deposits that are already success
|
if depositTime, ok := s.lastAssetDepositTimes[asset]; ok {
|
||||||
log.Infof("ignored succeess deposit: %s %+v", deposit.TransactionID, deposit)
|
// if it's newer than the latest deposit time, then we just add it the monitoring list
|
||||||
continue
|
if deposit.Time.After(depositTime) {
|
||||||
|
log.Infof("adding new success deposit: %s", deposit.TransactionID)
|
||||||
|
s.watchingDeposits[deposit.TransactionID] = deposit
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// ignore all initial deposit history that are already success
|
||||||
|
log.Infof("ignored succeess deposit: %s %+v", deposit.TransactionID, deposit)
|
||||||
|
}
|
||||||
|
|
||||||
case types.DepositCredited, types.DepositPending:
|
case types.DepositCredited, types.DepositPending:
|
||||||
log.Infof("adding pending deposit: %s", deposit.TransactionID)
|
log.Infof("adding pending deposit: %s", deposit.TransactionID)
|
||||||
|
@ -194,6 +204,15 @@ func (s *Strategy) scanDepositHistory(ctx context.Context, asset string, duratio
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(deposits) > 0 {
|
||||||
|
lastDeposit := deposits[len(deposits)-1]
|
||||||
|
if lastTime, ok := s.lastAssetDepositTimes[asset]; ok {
|
||||||
|
s.lastAssetDepositTimes[asset] = later(lastDeposit.Time.Time(), lastTime)
|
||||||
|
} else {
|
||||||
|
s.lastAssetDepositTimes[asset] = lastDeposit.Time.Time()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var succeededDeposits []types.Deposit
|
var succeededDeposits []types.Deposit
|
||||||
for _, deposit := range s.watchingDeposits {
|
for _, deposit := range s.watchingDeposits {
|
||||||
if deposit.Status == types.DepositSuccess {
|
if deposit.Status == types.DepositSuccess {
|
||||||
|
@ -212,3 +231,11 @@ func (s *Strategy) scanDepositHistory(ctx context.Context, asset string, duratio
|
||||||
|
|
||||||
return succeededDeposits, nil
|
return succeededDeposits, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func later(a, b time.Time) time.Time {
|
||||||
|
if a.After(b) {
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user