rewrite reward sync

This commit is contained in:
c9s 2022-06-18 01:42:33 +08:00
parent ac404b20a5
commit ee1ba417cd
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 48 additions and 51 deletions

View File

@ -7,8 +7,8 @@ import (
"strings" "strings"
"time" "time"
sq "github.com/Masterminds/squirrel"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
"github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/exchange/batch" "github.com/c9s/bbgo/pkg/exchange/batch"
"github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/fixedpoint"
@ -23,12 +23,14 @@ type RewardService struct {
DB *sqlx.DB DB *sqlx.DB
} }
func (s *RewardService) QueryLast(ex types.ExchangeName, limit int) ([]types.Reward, error) { func (s *RewardService) QueryLast(ex types.ExchangeName, limit uint64) ([]types.Reward, error) {
sql := "SELECT * FROM `rewards` WHERE `exchange` = :exchange ORDER BY `created_at` DESC LIMIT :limit" sel := SelectLastRewards(ex, limit)
rows, err := s.DB.NamedQuery(sql, map[string]interface{}{ sql, args, err := sel.ToSql()
"exchange": ex, if err != nil {
"limit": limit, return nil, err
}) }
rows, err := s.DB.Queryx(sql, args)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -37,60 +39,45 @@ func (s *RewardService) QueryLast(ex types.ExchangeName, limit int) ([]types.Rew
return s.scanRows(rows) return s.scanRows(rows)
} }
func (s *RewardService) Sync(ctx context.Context, exchange types.Exchange) error { func (s *RewardService) Sync(ctx context.Context, exchange types.Exchange, startTime time.Time) error {
service, ok := exchange.(types.ExchangeRewardService) api, ok := exchange.(types.ExchangeRewardService)
if !ok { if !ok {
return ErrExchangeRewardServiceNotImplemented return ErrExchangeRewardServiceNotImplemented
} }
var rewardKeys = map[string]struct{}{} isMargin, isFutures, _, _ := getExchangeAttributes(exchange)
if isMargin || isFutures {
var startTime time.Time return nil
records, err := s.QueryLast(exchange.Name(), 50)
if err != nil {
return err
} }
if len(records) > 0 { tasks := []SyncTask{
lastRecord := records[0] {
startTime = lastRecord.CreatedAt.Time() Type: types.Trade{},
Select: SelectLastRewards(exchange.Name(), 100),
for _, record := range records { BatchQuery: func(ctx context.Context, startTime, endTime time.Time) (interface{}, chan error) {
rewardKeys[record.UUID] = struct{}{} query := &batch.RewardBatchQuery{
} Service: api,
}
return query.Query(ctx, startTime, endTime)
},
Time: func(obj interface{}) time.Time {
return obj.(types.Reward).CreatedAt.Time()
},
ID: func(obj interface{}) string {
reward := obj.(types.Reward)
return string(reward.Type) + "_" + reward.UUID
},
LogInsert: true,
},
} }
batchQuery := &batch.RewardBatchQuery{Service: service} for _, sel := range tasks {
rewardsC, errC := batchQuery.Query(ctx, startTime, time.Now()) if err := sel.execute(ctx, s.DB, startTime); err != nil {
for reward := range rewardsC {
select {
case <-ctx.Done():
return ctx.Err()
case err := <-errC:
if err != nil {
return err
}
default:
}
if _, ok := rewardKeys[reward.UUID]; ok {
continue
}
logrus.Infof("inserting reward: %s %s %s %f %s", reward.Exchange, reward.Type, reward.Currency, reward.Quantity.Float64(), reward.CreatedAt)
if err := s.Insert(reward); err != nil {
return err return err
} }
} }
return <-errC return nil
} }
type CurrencyPositionMap map[string]fixedpoint.Value type CurrencyPositionMap map[string]fixedpoint.Value
@ -215,3 +202,13 @@ func (s *RewardService) Insert(reward types.Reward) error {
reward) reward)
return err return err
} }
func SelectLastRewards(ex types.ExchangeName, limit uint64) sq.SelectBuilder {
return sq.Select("*").
From("rewards").
Where(sq.And{
sq.Eq{"exchange": ex},
}).
OrderBy("created_at DESC").
Limit(limit)
}

View File

@ -75,13 +75,13 @@ func (s *SyncService) SyncMarginHistory(ctx context.Context, exchange types.Exch
return nil return nil
} }
func (s *SyncService) SyncRewardHistory(ctx context.Context, exchange types.Exchange) error { func (s *SyncService) SyncRewardHistory(ctx context.Context, exchange types.Exchange, startTime time.Time) error {
if _, implemented := exchange.(types.ExchangeRewardService); !implemented { if _, implemented := exchange.(types.ExchangeRewardService); !implemented {
return nil return nil
} }
log.Infof("syncing %s reward records...", exchange.Name()) log.Infof("syncing %s reward records...", exchange.Name())
if err := s.RewardService.Sync(ctx, exchange); err != nil { if err := s.RewardService.Sync(ctx, exchange, startTime); err != nil {
return err return err
} }