service: add time function

This commit is contained in:
c9s 2022-06-01 15:52:45 +08:00
parent 5eaa4706f0
commit 5a4a2db66f
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -2,7 +2,6 @@ package service
import (
"context"
"fmt"
"strconv"
"time"
@ -20,17 +19,17 @@ type MarginService struct {
func (s *MarginService) Sync(ctx context.Context, ex types.Exchange, asset string, startTime time.Time) error {
api, ok := ex.(types.MarginHistory)
if !ok {
return ErrNotImplemented
return nil
}
marginExchange, ok := ex.(types.MarginExchange)
if !ok {
return fmt.Errorf("%T does not implement margin service", ex)
return nil
}
marginSettings := marginExchange.GetMarginSettings()
if !marginSettings.IsMargin {
return fmt.Errorf("exchange instance %s is not using margin", ex.Name())
return nil
}
tasks := []SyncTask{
@ -43,6 +42,9 @@ func (s *MarginService) Sync(ctx context.Context, ex types.Exchange, asset strin
}
return query.Query(ctx, asset, startTime, endTime)
},
Time: func(obj interface{}) time.Time {
return obj.(types.MarginLoan).Time.Time()
},
ID: func(obj interface{}) string {
return strconv.FormatUint(obj.(types.MarginLoan).TransactionID, 10)
},
@ -56,6 +58,9 @@ func (s *MarginService) Sync(ctx context.Context, ex types.Exchange, asset strin
}
return query.Query(ctx, asset, startTime, endTime)
},
Time: func(obj interface{}) time.Time {
return obj.(types.MarginRepay).Time.Time()
},
ID: func(obj interface{}) string {
return strconv.FormatUint(obj.(types.MarginRepay).TransactionID, 10)
},
@ -69,6 +74,9 @@ func (s *MarginService) Sync(ctx context.Context, ex types.Exchange, asset strin
}
return query.Query(ctx, asset, startTime, endTime)
},
Time: func(obj interface{}) time.Time {
return obj.(types.MarginInterest).Time.Time()
},
ID: func(obj interface{}) string {
m := obj.(types.MarginInterest)
return m.Asset + m.IsolatedSymbol + strconv.FormatInt(m.Time.UnixMilli(), 10)
@ -83,6 +91,9 @@ func (s *MarginService) Sync(ctx context.Context, ex types.Exchange, asset strin
}
return query.Query(ctx, startTime, endTime)
},
Time: func(obj interface{}) time.Time {
return obj.(types.MarginLiquidation).UpdatedTime.Time()
},
ID: func(obj interface{}) string {
m := obj.(types.MarginLiquidation)
return strconv.FormatUint(m.OrderID, 10)
@ -130,4 +141,3 @@ func SelectLastMarginLiquidations(ex types.ExchangeName, limit uint64) sq.Select
OrderBy("time").
Limit(limit)
}