ftx: subscribe order update

This commit is contained in:
ycdesu 2021-03-27 19:02:19 +08:00
parent 4ac67332a4
commit 2e2ae46bae
3 changed files with 18 additions and 6 deletions

View File

@ -32,9 +32,7 @@ func (s *Stream) Connect(ctx context.Context) error {
// If it's not public only, let's do the authentication.
if atomic.LoadInt32(&s.publicOnly) == 0 {
logger.Infof("subscribe private events")
s.wsService.Subscribe(
newLoginRequest(s.wsService.key, s.wsService.secret, time.Now()),
)
s.subscribePrivateEvents()
}
if err := s.wsService.Connect(ctx); err != nil {
@ -44,6 +42,16 @@ func (s *Stream) Connect(ctx context.Context) error {
return nil
}
func (s *Stream) subscribePrivateEvents() {
s.wsService.Subscribe(
newLoginRequest(s.wsService.key, s.wsService.secret, time.Now()),
)
s.wsService.Subscribe(websocketRequest{
Operation: subscribe,
Channel: privateOrdersChannel,
})
}
func (s *Stream) SetPublicOnly() {
atomic.StoreInt32(&s.publicOnly, 1)
}

View File

@ -43,7 +43,6 @@ var errSubscriptionFailed = fmt.Errorf("failed to subscribe")
func (w *WebsocketService) sendSubscriptions() error {
conn := w.Conn()
for _, s := range w.subscriptions {
logger.Infof("s: %+v", s)
if err := conn.WriteJSON(s); err != nil {
return fmt.Errorf("can't send subscription request %+v: %w", s, errSubscriptionFailed)
}

View File

@ -22,9 +22,14 @@ const unsubscribe operation = "unsubscribe"
type channel string
const orderbook channel = "orderbook"
const trades channel = "trades"
const ticker channel = "ticker"
const privateOrdersChannel channel = "orders"
/*
Private:
order update: `{'op': 'subscribe', 'channel': 'orders'}`
login: `{"args": { "key": "<api_key>", "sign": "<signature>", "time": <ts> }, "op": "login" }`
*/
type websocketRequest struct {
Operation operation `json:"op"`