2020-11-05 03:00:51 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-02-23 08:39:48 +00:00
|
|
|
"errors"
|
2020-11-05 03:00:51 +00:00
|
|
|
"time"
|
|
|
|
|
2022-01-23 07:14:29 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/cache"
|
|
|
|
|
2021-12-31 06:12:41 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2020-11-05 03:00:51 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
2022-03-17 12:15:25 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/util"
|
2020-11-05 03:00:51 +00:00
|
|
|
)
|
|
|
|
|
2021-03-13 12:49:51 +00:00
|
|
|
var ErrNotImplemented = errors.New("not implemented")
|
|
|
|
var ErrExchangeRewardServiceNotImplemented = errors.New("exchange does not implement ExchangeRewardService interface")
|
2021-02-23 08:39:48 +00:00
|
|
|
|
2020-11-05 03:00:51 +00:00
|
|
|
type SyncService struct {
|
2021-03-14 03:04:56 +00:00
|
|
|
TradeService *TradeService
|
|
|
|
OrderService *OrderService
|
|
|
|
RewardService *RewardService
|
|
|
|
WithdrawService *WithdrawService
|
|
|
|
DepositService *DepositService
|
2020-11-05 03:00:51 +00:00
|
|
|
}
|
2021-02-19 02:26:13 +00:00
|
|
|
|
2022-03-17 12:06:40 +00:00
|
|
|
func paperTrade() bool {
|
2022-03-17 12:15:25 +00:00
|
|
|
v, ok := util.GetEnvVarBool("PAPER_TRADE")
|
|
|
|
return ok && v
|
2022-03-17 12:06:40 +00:00
|
|
|
}
|
|
|
|
|
2021-02-19 02:26:13 +00:00
|
|
|
// SyncSessionSymbols syncs the trades from the given exchange session
|
2021-02-19 02:42:24 +00:00
|
|
|
func (s *SyncService) SyncSessionSymbols(ctx context.Context, exchange types.Exchange, startTime time.Time, symbols ...string) error {
|
2022-01-23 07:14:29 +00:00
|
|
|
markets, err := cache.LoadExchangeMarketsWithCache(ctx, exchange)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-13 11:32:38 +00:00
|
|
|
|
2022-01-23 07:14:29 +00:00
|
|
|
for _, symbol := range symbols {
|
2022-01-13 11:32:38 +00:00
|
|
|
if _, ok := markets[symbol]; ok {
|
2022-01-23 07:14:29 +00:00
|
|
|
log.Infof("syncing %s %s trades...", exchange.Name(), symbol)
|
2022-01-13 11:32:38 +00:00
|
|
|
if err := s.TradeService.Sync(ctx, exchange, symbol, startTime); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-23 07:14:29 +00:00
|
|
|
|
2022-01-13 11:32:38 +00:00
|
|
|
log.Infof("syncing %s %s orders...", exchange.Name(), symbol)
|
|
|
|
if err := s.OrderService.Sync(ctx, exchange, symbol, startTime); err != nil {
|
|
|
|
return err
|
2022-01-03 19:31:26 +00:00
|
|
|
}
|
2021-02-19 02:26:13 +00:00
|
|
|
}
|
2021-03-14 03:04:56 +00:00
|
|
|
}
|
2021-02-23 08:39:48 +00:00
|
|
|
|
2022-03-17 12:15:25 +00:00
|
|
|
if paperTrade() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("syncing %s deposit records...", exchange.Name())
|
|
|
|
if err := s.DepositService.Sync(ctx, exchange); err != nil {
|
|
|
|
if err != ErrNotImplemented {
|
|
|
|
return err
|
2021-03-14 02:44:02 +00:00
|
|
|
}
|
2022-03-17 12:15:25 +00:00
|
|
|
}
|
2021-03-14 02:44:02 +00:00
|
|
|
|
2022-03-17 12:15:25 +00:00
|
|
|
log.Infof("syncing %s withdraw records...", exchange.Name())
|
|
|
|
if err := s.WithdrawService.Sync(ctx, exchange); err != nil {
|
|
|
|
if err != ErrNotImplemented {
|
|
|
|
return err
|
2021-03-14 02:44:02 +00:00
|
|
|
}
|
2022-03-17 12:15:25 +00:00
|
|
|
}
|
2021-03-14 02:44:02 +00:00
|
|
|
|
2022-03-17 12:15:25 +00:00
|
|
|
log.Infof("syncing %s reward records...", exchange.Name())
|
|
|
|
if err := s.RewardService.Sync(ctx, exchange); err != nil {
|
|
|
|
if err != ErrExchangeRewardServiceNotImplemented {
|
|
|
|
log.Infof("%s reward service is not supported", exchange.Name())
|
|
|
|
return err
|
2021-02-23 08:39:48 +00:00
|
|
|
}
|
2021-02-19 02:26:13 +00:00
|
|
|
}
|
2022-03-17 12:15:25 +00:00
|
|
|
|
2021-02-19 02:26:13 +00:00
|
|
|
return nil
|
|
|
|
}
|