2022-04-08 11:21:57 +00:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
2022-04-13 11:43:08 +00:00
|
|
|
"context"
|
2022-04-12 07:53:40 +00:00
|
|
|
"fmt"
|
2022-04-08 11:21:57 +00:00
|
|
|
"net"
|
2022-04-12 14:25:48 +00:00
|
|
|
"time"
|
2022-04-08 11:21:57 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2022-04-12 08:55:42 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2022-04-08 11:21:57 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/reflection"
|
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/bbgo"
|
|
|
|
"github.com/c9s/bbgo/pkg/pb"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
2022-04-13 07:29:23 +00:00
|
|
|
type UserDataService struct {
|
2022-04-08 11:21:57 +00:00
|
|
|
Config *bbgo.Config
|
|
|
|
Environ *bbgo.Environment
|
|
|
|
Trader *bbgo.Trader
|
|
|
|
|
2022-04-13 06:14:25 +00:00
|
|
|
pb.UnimplementedUserDataServiceServer
|
|
|
|
}
|
|
|
|
|
2022-04-13 07:29:23 +00:00
|
|
|
func (s *UserDataService) Subscribe(request *pb.UserDataRequest, server pb.UserDataService_SubscribeServer) error {
|
2022-04-13 11:43:08 +00:00
|
|
|
streamPool := map[string]types.Stream{}
|
2022-04-13 07:29:23 +00:00
|
|
|
for sessionName, session := range s.Environ.Sessions() {
|
|
|
|
if request.Session == sessionName {
|
|
|
|
userDataStream := session.Exchange.NewStream()
|
|
|
|
userDataStream.OnOrderUpdate(func(order types.Order) {
|
|
|
|
err := server.Send(&pb.UserData{
|
2022-04-13 11:43:08 +00:00
|
|
|
Channel: pb.Channel_ORDER,
|
|
|
|
Event: pb.Event_UPDATE,
|
|
|
|
Orders: []*pb.Order{transOrder(session, order)},
|
2022-04-13 07:29:23 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Errorf("grpc: can not send user data")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
userDataStream.OnTradeUpdate(func(trade types.Trade) {
|
|
|
|
err := server.Send(&pb.UserData{
|
2022-04-13 11:43:08 +00:00
|
|
|
Channel: pb.Channel_TRADE,
|
|
|
|
Event: pb.Event_UPDATE,
|
|
|
|
Trades: []*pb.Trade{transTrade(session, trade)},
|
2022-04-13 07:29:23 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Errorf("grpc: can not send user data")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
userDataStream.OnBalanceUpdate(func(balances types.BalanceMap) {
|
2022-04-13 11:43:08 +00:00
|
|
|
err := server.Send(&pb.UserData{
|
|
|
|
Channel: pb.Channel_BALANCE,
|
|
|
|
Event: pb.Event_UPDATE,
|
|
|
|
Balances: transBalances(session, balances),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Errorf("grpc: can not send user data")
|
|
|
|
}
|
2022-04-13 07:29:23 +00:00
|
|
|
})
|
2022-04-13 11:43:08 +00:00
|
|
|
streamPool[ sessionName ] = userDataStream
|
2022-04-13 07:29:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
<-server.Context().Done()
|
2022-04-13 11:43:08 +00:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
for _, stream := range streamPool {
|
|
|
|
if err := stream.Close(); err != nil {
|
|
|
|
log.WithError(err).Errorf("user data stream close error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-04-13 06:14:25 +00:00
|
|
|
return nil
|
2022-04-08 11:21:57 +00:00
|
|
|
}
|
|
|
|
|
2022-04-13 07:29:23 +00:00
|
|
|
type MarketDataService struct {
|
|
|
|
Config *bbgo.Config
|
|
|
|
Environ *bbgo.Environment
|
|
|
|
Trader *bbgo.Trader
|
|
|
|
|
|
|
|
pb.UnimplementedMarketDataServiceServer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MarketDataService) Subscribe(request *pb.SubscribeRequest, server pb.MarketDataService_SubscribeServer) error {
|
2022-04-12 07:53:40 +00:00
|
|
|
exchangeSubscriptions := map[string][]types.Subscription{}
|
|
|
|
for _, sub := range request.Subscriptions {
|
|
|
|
session, ok := s.Environ.Session(sub.Exchange)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("exchange %s not found", sub.Exchange)
|
|
|
|
}
|
|
|
|
|
2022-04-13 05:06:26 +00:00
|
|
|
ss, err := toSubscriptions(sub)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-04-12 07:53:40 +00:00
|
|
|
}
|
2022-04-13 05:06:26 +00:00
|
|
|
|
|
|
|
exchangeSubscriptions[session.Name] = append(exchangeSubscriptions[session.Name], ss)
|
2022-04-12 07:53:40 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 09:48:30 +00:00
|
|
|
streamPool := map[string]types.Stream{}
|
2022-04-12 07:53:40 +00:00
|
|
|
for sessionName, subs := range exchangeSubscriptions {
|
2022-04-12 15:25:41 +00:00
|
|
|
session, ok := s.Environ.Session(sessionName)
|
|
|
|
if !ok {
|
|
|
|
log.Errorf("session %s not found", sessionName)
|
|
|
|
continue
|
|
|
|
}
|
2022-04-12 08:55:42 +00:00
|
|
|
|
2022-04-12 15:25:41 +00:00
|
|
|
stream := session.Exchange.NewStream()
|
|
|
|
stream.SetPublicOnly()
|
|
|
|
for _, sub := range subs {
|
|
|
|
log.Infof("%s subscribe %s %s %+v", sessionName, sub.Channel, sub.Symbol, sub.Options)
|
|
|
|
stream.Subscribe(sub.Channel, sub.Symbol, sub.Options)
|
2022-04-12 07:53:40 +00:00
|
|
|
}
|
2022-04-12 15:25:41 +00:00
|
|
|
|
2022-04-13 03:52:51 +00:00
|
|
|
stream.OnMarketTrade(func(trade types.Trade) {
|
|
|
|
if err := server.Send(transMarketTrade(session, trade)); err != nil {
|
|
|
|
log.WithError(err).Error("grpc stream send error")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-04-12 15:25:41 +00:00
|
|
|
stream.OnBookSnapshot(func(book types.SliceOrderBook) {
|
|
|
|
if err := server.Send(transBook(session, book, pb.Event_SNAPSHOT)); err != nil {
|
|
|
|
log.WithError(err).Error("grpc stream send error")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
stream.OnBookUpdate(func(book types.SliceOrderBook) {
|
|
|
|
if err := server.Send(transBook(session, book, pb.Event_UPDATE)); err != nil {
|
|
|
|
log.WithError(err).Error("grpc stream send error")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
stream.OnKLineClosed(func(kline types.KLine) {
|
2022-04-13 06:14:25 +00:00
|
|
|
err := server.Send(transKLineResponse(session, kline))
|
2022-04-12 15:25:41 +00:00
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("grpc stream send error")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
streamPool[sessionName] = stream
|
2022-04-12 09:48:30 +00:00
|
|
|
}
|
|
|
|
|
2022-04-13 03:52:51 +00:00
|
|
|
for _, stream := range streamPool {
|
2022-04-12 09:48:30 +00:00
|
|
|
go stream.Connect(server.Context())
|
2022-04-12 07:53:40 +00:00
|
|
|
}
|
|
|
|
|
2022-04-13 03:52:51 +00:00
|
|
|
defer func() {
|
|
|
|
for _, stream := range streamPool {
|
|
|
|
if err := stream.Close(); err != nil {
|
2022-04-13 11:43:08 +00:00
|
|
|
log.WithError(err).Errorf("market data stream close error")
|
2022-04-13 03:52:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
ctx := server.Context()
|
|
|
|
<-ctx.Done()
|
|
|
|
return ctx.Err()
|
2022-04-08 11:21:57 +00:00
|
|
|
}
|
|
|
|
|
2022-04-13 07:29:23 +00:00
|
|
|
func (s *MarketDataService) QueryKLines(ctx context.Context, request *pb.QueryKLinesRequest) (*pb.QueryKLinesResponse, error) {
|
2022-04-08 11:21:57 +00:00
|
|
|
exchangeName, err := types.ValidExchangeName(request.Exchange)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, session := range s.Environ.Sessions() {
|
|
|
|
if session.ExchangeName == exchangeName {
|
|
|
|
response := &pb.QueryKLinesResponse{
|
|
|
|
Klines: nil,
|
|
|
|
Error: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
options := types.KLineQueryOptions{
|
|
|
|
Limit: int(request.Limit),
|
|
|
|
}
|
|
|
|
|
2022-04-12 14:25:48 +00:00
|
|
|
endTime := time.Now()
|
|
|
|
if request.EndTime != 0 {
|
|
|
|
endTime = time.Unix(request.EndTime, 0)
|
|
|
|
}
|
|
|
|
options.EndTime = &endTime
|
|
|
|
|
|
|
|
if request.StartTime != 0 {
|
|
|
|
startTime := time.Unix(request.StartTime, 0)
|
|
|
|
options.StartTime = &startTime
|
|
|
|
}
|
|
|
|
|
2022-04-08 11:21:57 +00:00
|
|
|
klines, err := session.Exchange.QueryKLines(ctx, request.Symbol, types.Interval(request.Interval), options)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, kline := range klines {
|
2022-04-13 06:14:25 +00:00
|
|
|
response.Klines = append(response.Klines, transKLine(session, kline))
|
2022-04-08 11:21:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2022-04-13 07:29:23 +00:00
|
|
|
type Server struct {
|
|
|
|
Config *bbgo.Config
|
|
|
|
Environ *bbgo.Environment
|
|
|
|
Trader *bbgo.Trader
|
|
|
|
}
|
|
|
|
|
2022-04-08 11:21:57 +00:00
|
|
|
func (s *Server) ListenAndServe(bind string) error {
|
|
|
|
conn, err := net.Listen("tcp", bind)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to bind network at %s", bind)
|
|
|
|
}
|
|
|
|
|
|
|
|
var grpcServer = grpc.NewServer()
|
2022-04-13 07:29:23 +00:00
|
|
|
pb.RegisterMarketDataServiceServer(grpcServer, &MarketDataService{
|
|
|
|
Config: s.Config,
|
|
|
|
Environ: s.Environ,
|
|
|
|
Trader: s.Trader,
|
|
|
|
})
|
|
|
|
|
|
|
|
pb.RegisterUserDataServiceServer(grpcServer, &UserDataService{
|
|
|
|
Config: s.Config,
|
|
|
|
Environ: s.Environ,
|
|
|
|
Trader: s.Trader,
|
|
|
|
})
|
2022-04-08 11:21:57 +00:00
|
|
|
|
|
|
|
reflection.Register(grpcServer)
|
|
|
|
|
|
|
|
if err := grpcServer.Serve(conn); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to serve grpc connections")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|