Skip to content

Commit

Permalink
Kraken websocket support (thrasher-corp#264)
Browse files Browse the repository at this point in the history
* Initial commit. Adds ticker, candle and trade, subscription support

* Adds support for spread and orderbooks

* Adds new currency pair delimiter ("/"), Adds dedicated websocket Connected channel handler, Updates Kraken websocket capability definition, Refines websocket tests to connect and disconnect without freezing, separates WebsocketUnsubscribeEventRequest ChannelID into its own struct WebsocketUnsubscribeByChannelIDEventRequest to prevent bad json WS requests, Adds asset type to orderbook, Kraken WS handles connection better

* Removes duplicate type, reverts config value

* Addresses error returns and changes writeToWebsocket to use byte array. Removes deferred funcs in tests. Increases test listening limit for rare cases

* Fixes verbose log. Rearranges WS Connect async ordering. Fixes DATA RACE. Fixes random okex tests. Ensures Kraken WS tests only connect once
  • Loading branch information
gloriousCode authored and thrasher- committed Apr 3, 2019
1 parent a0e2910 commit 107cf76
Show file tree
Hide file tree
Showing 10 changed files with 960 additions and 19 deletions.
2 changes: 1 addition & 1 deletion currency/pair.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func NewPairFromIndex(currencyPair, index string) (Pair, error) {
// NewPairFromString converts currency string into a new CurrencyPair
// with or without delimeter
func NewPairFromString(currencyPair string) Pair {
delimiters := []string{"_", "-"}
delimiters := []string{"_", "-", "/"}
var delimiter string
for _, x := range delimiters {
if strings.Contains(currencyPair, x) {
Expand Down
18 changes: 9 additions & 9 deletions exchanges/exchange_websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ func (w *Websocket) trafficMonitor(wg *sync.WaitGroup) {
select {
case <-w.ShutdownC: // Returns on shutdown channel close
return

case <-w.TrafficAlert: // Resets timer on traffic
if !w.connected {
w.Connected <- struct{}{}
Expand Down Expand Up @@ -194,20 +193,21 @@ func (w *Websocket) Connect() error {

w.ShutdownC = make(chan struct{}, 1)

var anotherWG sync.WaitGroup
anotherWG.Add(1)
go w.trafficMonitor(&anotherWG)
anotherWG.Wait()

err := w.connector()
if err != nil {
return fmt.Errorf("exchange_websocket.go connection error %s",
err)
}

// Divert for incoming websocket traffic
w.Connected <- struct{}{}
w.connected = true
if !w.connected {
w.Connected <- struct{}{}
w.connected = true
}

var anotherWG sync.WaitGroup
anotherWG.Add(1)
go w.trafficMonitor(&anotherWG)
anotherWG.Wait()

return nil
}
Expand Down
19 changes: 19 additions & 0 deletions exchanges/kraken/kraken.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
"net/url"
"strconv"
"strings"
"sync"
"time"

"github.com/gorilla/websocket"
"github.com/thrasher-/gocryptotrader/common"
"github.com/thrasher-/gocryptotrader/config"
"github.com/thrasher-/gocryptotrader/currency"
Expand Down Expand Up @@ -56,7 +58,9 @@ const (
// Kraken is the overarching type across the alphapoint package
type Kraken struct {
exchange.Base
WebsocketConn *websocket.Conn
CryptoFee, FiatFee float64
mu sync.Mutex
}

// SetDefaults sets current default settings
Expand Down Expand Up @@ -86,6 +90,12 @@ func (k *Kraken) SetDefaults() {
k.APIUrlDefault = krakenAPIURL
k.APIUrl = k.APIUrlDefault
k.WebsocketInit()
k.WebsocketURL = krakenWSURL
k.Websocket.Functionality = exchange.WebsocketTickerSupported |
exchange.WebsocketTradeDataSupported |
exchange.WebsocketKlineSupported |
exchange.WebsocketOrderbookSupported

}

// Setup sets current exchange configuration
Expand All @@ -100,6 +110,7 @@ func (k *Kraken) Setup(exch config.ExchangeConfig) {
k.SetHTTPClientUserAgent(exch.HTTPUserAgent)
k.RESTPollingDelay = exch.RESTPollingDelay
k.Verbose = exch.Verbose
k.Websocket.SetWsStatusAndConnection(exch.Websocket)
k.BaseCurrencies = exch.BaseCurrencies
k.AvailablePairs = exch.AvailablePairs
k.EnabledPairs = exch.EnabledPairs
Expand All @@ -123,6 +134,14 @@ func (k *Kraken) Setup(exch config.ExchangeConfig) {
if err != nil {
log.Fatal(err)
}
err = k.WebsocketSetup(k.WsConnect,
exch.Name,
exch.Websocket,
krakenWSURL,
exch.WebsocketURL)
if err != nil {
log.Fatal(err)
}
}
}

Expand Down
Loading

0 comments on commit 107cf76

Please sign in to comment.