From 8e85274876b10b1ade23614efcabfc6d704e2fba Mon Sep 17 00:00:00 2001 From: c9s Date: Thu, 11 Mar 2021 16:44:43 +0800 Subject: [PATCH] fix used time field for withdraw --- pkg/exchange/max/exchange.go | 4 ++-- pkg/service/withdraw.go | 18 +++++++++++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/pkg/exchange/max/exchange.go b/pkg/exchange/max/exchange.go index 748b454e2..bb03b7deb 100644 --- a/pkg/exchange/max/exchange.go +++ b/pkg/exchange/max/exchange.go @@ -504,7 +504,7 @@ func (e *Exchange) QueryWithdrawHistory(ctx context.Context, asset string, since startTime = endTime } else { // its in descending order, so we get the first record - startTime = time.Unix(withdraws[0].UpdatedAt, 0) + startTime = time.Unix(withdraws[0].CreatedAt, 0) } } @@ -560,7 +560,7 @@ func (e *Exchange) QueryDepositHistory(ctx context.Context, asset string, since, if len(deposits) < limit { startTime = endTime } else { - startTime = time.Unix(deposits[0].UpdatedAt, 0) + startTime = time.Unix(deposits[0].CreatedAt, 0) } } diff --git a/pkg/service/withdraw.go b/pkg/service/withdraw.go index 7c2fe8f14..4a84a80dc 100644 --- a/pkg/service/withdraw.go +++ b/pkg/service/withdraw.go @@ -13,10 +13,12 @@ type WithdrawService struct { DB *sqlx.DB } +// Sync syncs the withdraw records into db func (s *WithdrawService) Sync(ctx context.Context, ex types.Exchange) error { txnIDs := map[string]struct{}{} - records, err := s.QueryLast(ex.Name(), 10) + // query descending + records, err := s.QueryLast(ex.Name(), 100) if err != nil { return err } @@ -30,15 +32,25 @@ func (s *WithdrawService) Sync(ctx context.Context, ex types.Exchange) error { return ErrNotImplemented } - withdraws, err := transferApi.QueryWithdrawHistory(ctx, "", records[0].ApplyTime.Time(), time.Now()) + since := time.Time{} + if len(records) > 0 { + since = records[len(records)-1].ApplyTime.Time() + } + + // asset "" means all assets + withdraws, err := transferApi.QueryWithdrawHistory(ctx, "", since, time.Now()) if err != nil { return err } for _, withdraw := range withdraws { - if _, exists := txnIDs[withdraw.TransactionID] ; exists { + if _, exists := txnIDs[withdraw.TransactionID]; exists { continue } + + if err := s.Insert(withdraw); err != nil { + return err + } } return nil