mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-02 15:10:46 +00:00
* Step one: Sets up connection handler for websockets to always be connected until a shutdown event is received. Sets up a vague subscription handler to ensure subscriptions are subscribed * Adds support for resubscriptions for bitfinex, bitstamp, bitmex and btcc. Adds subscription params for special websocket subscription requirements. Removes subscription monitor from wait group so that it can exist despite a shutdown and continuously check * Adds channel subscription support to bitmex, btse, coibasepro, coinut, gateio, gemini, hitbtc, huobi, hadax, kraken, okgroup, poloniex and zb * Implements unsubscribe for bitfinex, btcc, btse, coinbasepro, gateio, gitbtc, huobi, hadax * ManageSubscriptions now called from WSConnect and made private instead of inside individual exchanges. ManageSubscriptions can now unsubscribe. exchange_websocket_types.go now contains all exchange_websocket.go types to avoid clutter * Adds it to websocket functionality so managesubscriptions will close when not supported * Separates functions into testable functions to ensure logic works. Adds tests. Updates websocket setup to include verbosity (inherited from exchange). Adds no connection tolerance to fatal on failed reconnects * More exchange_websocket tests. Updating to use pointers. Creation of equals func to make comparison easier * Fixes okex, okcoin tests. Fixes race conditions. Removes pointer usage again. * Adds subscribe and unsubscribe to wrappers * Fixes deadlock. Fixes ws verbosity. * Updates all exchanges to properly support subscription/connection feature. Also reintroduces race conditions.... * Moves connection varialbes to struct from package to allow each websocket to have their own reconnection checks. Neatens up logs * Fixes lint/critic issues. Fixes tests. Removes unused function. * Moves websocket ratelimiter to their own const variables. Fixes more race conditions with connecting variable * Removes redundant subscribe functions. Ensuring only the exchange_websocket.go can manage subscriptions. Fixes debug logs to be verbose wrapped * Fixes issue with slice copying. Re-adds okgroup default channels * Adds nolint to append * Adds comments and adds support for gateio auth request subscriptions * Adds new test to ensure slices dont point to the same vars * removes fatals. gofmt goimports * more gofmts * Addresses PR comments, removing empty and redundant lines * Addresses PR comments. Ensures that writing to the websocket is single-threaded by adding a mutex to exchanges. Minimises wrapper code and moves subscription loops to exchange_websocket. Privatises ChannelsToSubscribe, Connecting properties and removeChannelToSubscribe func to prevent unnecessary tampering. * Removes unused mutex. FMTS and IMPORTS * Fixes request lock time change * More specific logs * Renames ws mutex. Fixes bitmex subscriptions. Increased gateio ratelimiter to 120ms. Removes ratelimiter from bitfinex, bitmex, bitstamp, btcc, btse, coibasepro, hitbtc, huobi, hadax, poloniex and zb * changes recieved typo due to not being well received * Fixes parsing issue with Huobi and hadax * Fixes data race with more locks * removes defer locks. fixes huobi/hadax verbose output * Fixes double JSONEncode for coinut. Fixes verbose output for coinut * gofmt,goimport for coinut * Fixes issue where multiple connection monitors can spawn * Removes defer exchange.WebsocketConn.Close() in defer handledata exit as connectionmonitor handles connections instead * gofmt and go import * More fmts
304 lines
7.3 KiB
Go
304 lines
7.3 KiB
Go
package coinbasepro
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/gorilla/websocket"
|
|
"github.com/thrasher-/gocryptotrader/common"
|
|
"github.com/thrasher-/gocryptotrader/currency"
|
|
exchange "github.com/thrasher-/gocryptotrader/exchanges"
|
|
"github.com/thrasher-/gocryptotrader/exchanges/orderbook"
|
|
log "github.com/thrasher-/gocryptotrader/logger"
|
|
)
|
|
|
|
const (
|
|
coinbaseproWebsocketURL = "wss://ws-feed.pro.coinbase.com"
|
|
)
|
|
|
|
// WsConnect initiates a websocket connection
|
|
func (c *CoinbasePro) WsConnect() error {
|
|
if !c.Websocket.IsEnabled() || !c.IsEnabled() {
|
|
return errors.New(exchange.WebsocketNotEnabled)
|
|
}
|
|
|
|
var dialer websocket.Dialer
|
|
|
|
if c.Websocket.GetProxyAddress() != "" {
|
|
proxy, err := url.Parse(c.Websocket.GetProxyAddress())
|
|
if err != nil {
|
|
return fmt.Errorf("coinbasepro_websocket.go error - proxy address %s",
|
|
err)
|
|
}
|
|
|
|
dialer.Proxy = http.ProxyURL(proxy)
|
|
}
|
|
|
|
var err error
|
|
c.WebsocketConn, _, err = dialer.Dial(c.Websocket.GetWebsocketURL(),
|
|
http.Header{})
|
|
if err != nil {
|
|
return fmt.Errorf("coinbasepro_websocket.go error - unable to connect to websocket %s",
|
|
err)
|
|
}
|
|
|
|
c.GenerateDefaultSubscriptions()
|
|
go c.WsHandleData()
|
|
|
|
return nil
|
|
}
|
|
|
|
// WsReadData reads data from the websocket connection
|
|
func (c *CoinbasePro) WsReadData() (exchange.WebsocketResponse, error) {
|
|
_, resp, err := c.WebsocketConn.ReadMessage()
|
|
if err != nil {
|
|
return exchange.WebsocketResponse{}, err
|
|
}
|
|
c.Websocket.TrafficAlert <- struct{}{}
|
|
return exchange.WebsocketResponse{Raw: resp}, nil
|
|
}
|
|
|
|
// WsHandleData handles read data from websocket connection
|
|
func (c *CoinbasePro) WsHandleData() {
|
|
c.Websocket.Wg.Add(1)
|
|
|
|
defer func() {
|
|
c.Websocket.Wg.Done()
|
|
}()
|
|
|
|
for {
|
|
select {
|
|
case <-c.Websocket.ShutdownC:
|
|
return
|
|
default:
|
|
resp, err := c.WsReadData()
|
|
if err != nil {
|
|
c.Websocket.DataHandler <- err
|
|
return
|
|
}
|
|
type MsgType struct {
|
|
Type string `json:"type"`
|
|
Sequence int64 `json:"sequence"`
|
|
ProductID string `json:"product_id"`
|
|
}
|
|
|
|
msgType := MsgType{}
|
|
err = common.JSONDecode(resp.Raw, &msgType)
|
|
if err != nil {
|
|
c.Websocket.DataHandler <- err
|
|
continue
|
|
}
|
|
|
|
if msgType.Type == "subscriptions" || msgType.Type == "heartbeat" {
|
|
continue
|
|
}
|
|
|
|
switch msgType.Type {
|
|
case "error":
|
|
c.Websocket.DataHandler <- errors.New(string(resp.Raw))
|
|
|
|
case "ticker":
|
|
ticker := WebsocketTicker{}
|
|
err := common.JSONDecode(resp.Raw, &ticker)
|
|
if err != nil {
|
|
c.Websocket.DataHandler <- err
|
|
continue
|
|
}
|
|
|
|
c.Websocket.DataHandler <- exchange.TickerData{
|
|
Timestamp: ticker.Time,
|
|
Pair: currency.NewPairFromString(ticker.ProductID),
|
|
AssetType: "SPOT",
|
|
Exchange: c.GetName(),
|
|
OpenPrice: ticker.Open24H,
|
|
HighPrice: ticker.High24H,
|
|
LowPrice: ticker.Low24H,
|
|
ClosePrice: ticker.Price,
|
|
Quantity: ticker.Volume24H,
|
|
}
|
|
|
|
case "snapshot":
|
|
snapshot := WebsocketOrderbookSnapshot{}
|
|
err := common.JSONDecode(resp.Raw, &snapshot)
|
|
if err != nil {
|
|
c.Websocket.DataHandler <- err
|
|
continue
|
|
}
|
|
|
|
err = c.ProcessSnapshot(&snapshot)
|
|
if err != nil {
|
|
c.Websocket.DataHandler <- err
|
|
continue
|
|
}
|
|
|
|
case "l2update":
|
|
update := WebsocketL2Update{}
|
|
err := common.JSONDecode(resp.Raw, &update)
|
|
if err != nil {
|
|
c.Websocket.DataHandler <- err
|
|
continue
|
|
}
|
|
|
|
err = c.ProcessUpdate(update)
|
|
if err != nil {
|
|
c.Websocket.DataHandler <- err
|
|
continue
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// ProcessSnapshot processes the initial orderbook snap shot
|
|
func (c *CoinbasePro) ProcessSnapshot(snapshot *WebsocketOrderbookSnapshot) error {
|
|
var base orderbook.Base
|
|
for _, bid := range snapshot.Bids {
|
|
price, err := strconv.ParseFloat(bid[0].(string), 64)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
amount, err := strconv.ParseFloat(bid[1].(string), 64)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
base.Bids = append(base.Bids,
|
|
orderbook.Item{Price: price, Amount: amount})
|
|
}
|
|
|
|
for _, ask := range snapshot.Asks {
|
|
price, err := strconv.ParseFloat(ask[0].(string), 64)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
amount, err := strconv.ParseFloat(ask[1].(string), 64)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
base.Asks = append(base.Asks,
|
|
orderbook.Item{Price: price, Amount: amount})
|
|
}
|
|
|
|
pair := currency.NewPairFromString(snapshot.ProductID)
|
|
base.AssetType = "SPOT"
|
|
base.Pair = pair
|
|
|
|
err := c.Websocket.Orderbook.LoadSnapshot(&base, c.GetName(), false)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
c.Websocket.DataHandler <- exchange.WebsocketOrderbookUpdate{
|
|
Pair: pair,
|
|
Asset: "SPOT",
|
|
Exchange: c.GetName(),
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// ProcessUpdate updates the orderbook local cache
|
|
func (c *CoinbasePro) ProcessUpdate(update WebsocketL2Update) error {
|
|
var Asks, Bids []orderbook.Item
|
|
|
|
for _, data := range update.Changes {
|
|
price, _ := strconv.ParseFloat(data[1].(string), 64)
|
|
volume, _ := strconv.ParseFloat(data[2].(string), 64)
|
|
|
|
if data[0].(string) == "buy" {
|
|
Bids = append(Bids, orderbook.Item{Price: price, Amount: volume})
|
|
} else {
|
|
Asks = append(Asks, orderbook.Item{Price: price, Amount: volume})
|
|
}
|
|
}
|
|
|
|
if len(Asks) == 0 && len(Bids) == 0 {
|
|
return errors.New("coibasepro_websocket.go error - no data in websocket update")
|
|
}
|
|
|
|
p := currency.NewPairFromString(update.ProductID)
|
|
|
|
err := c.Websocket.Orderbook.Update(Bids, Asks, p, time.Now(), c.GetName(), "SPOT")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
c.Websocket.DataHandler <- exchange.WebsocketOrderbookUpdate{
|
|
Pair: p,
|
|
Asset: "SPOT",
|
|
Exchange: c.GetName(),
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// GenerateDefaultSubscriptions Adds default subscriptions to websocket to be handled by ManageSubscriptions()
|
|
func (c *CoinbasePro) GenerateDefaultSubscriptions() {
|
|
var channels = []string{"heartbeat", "level2", "ticker"}
|
|
enabledCurrencies := c.GetEnabledCurrencies()
|
|
subscriptions := []exchange.WebsocketChannelSubscription{}
|
|
for i := range channels {
|
|
for j := range enabledCurrencies {
|
|
enabledCurrencies[j].Delimiter = "-"
|
|
subscriptions = append(subscriptions, exchange.WebsocketChannelSubscription{
|
|
Channel: channels[i],
|
|
Currency: enabledCurrencies[j],
|
|
})
|
|
}
|
|
}
|
|
c.Websocket.SubscribeToChannels(subscriptions)
|
|
}
|
|
|
|
// Subscribe sends a websocket message to receive data from the channel
|
|
func (c *CoinbasePro) Subscribe(channelToSubscribe exchange.WebsocketChannelSubscription) error {
|
|
subscribe := WebsocketSubscribe{
|
|
Type: "subscribe",
|
|
Channels: []WsChannels{
|
|
{
|
|
Name: channelToSubscribe.Channel,
|
|
ProductIDs: []string{
|
|
channelToSubscribe.Currency.String(),
|
|
},
|
|
},
|
|
},
|
|
}
|
|
return c.wsSend(subscribe)
|
|
}
|
|
|
|
// Unsubscribe sends a websocket message to stop receiving data from the channel
|
|
func (c *CoinbasePro) Unsubscribe(channelToSubscribe exchange.WebsocketChannelSubscription) error {
|
|
subscribe := WebsocketSubscribe{
|
|
Type: "unsubscribe",
|
|
Channels: []WsChannels{
|
|
{
|
|
Name: channelToSubscribe.Channel,
|
|
ProductIDs: []string{
|
|
channelToSubscribe.Currency.String(),
|
|
},
|
|
},
|
|
},
|
|
}
|
|
return c.wsSend(subscribe)
|
|
}
|
|
|
|
// WsSend sends data to the websocket server
|
|
func (c *CoinbasePro) wsSend(data interface{}) error {
|
|
c.wsRequestMtx.Lock()
|
|
defer c.wsRequestMtx.Unlock()
|
|
if c.Verbose {
|
|
log.Debugf("%v sending message to websocket %v", c.Name, data)
|
|
}
|
|
json, err := common.JSONEncode(data)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return c.WebsocketConn.WriteMessage(websocket.TextMessage, json)
|
|
}
|