From 36107fe2858ef79c58a6be19a70dd06d898e5a66 Mon Sep 17 00:00:00 2001 From: Adrian Gallagher Date: Sun, 29 Mar 2015 18:28:00 +1100 Subject: [PATCH] Added LakeBTC Websocket support. --- README.md | 2 +- config_example.json | 2 +- lakebtchttp.go | 11 ++++ lakebtcwebsocket.go | 119 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 lakebtcwebsocket.go diff --git a/README.md b/README.md index 3a822dab..d3ed1e0c 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ A cryptocurrency trading bot supporting multiple exchanges written in Golang. | Huobi | Yes | No |No | ItBit | Yes | NA | NA | | Kraken | Yes | NA | NA -| LakeBTC | Yes | No | NA +| LakeBTC | Yes | Yes | NA |OKCoin (both) | Yes | Yes | No ** NA means not applicable as the Exchange does not support the feature. diff --git a/config_example.json b/config_example.json index cf99e919..bfd780b5 100644 --- a/config_example.json +++ b/config_example.json @@ -131,7 +131,7 @@ "BaseCurrencies": "CNY", "Enabled": true, "Verbose": false, - "Websocket": false, + "Websocket": true, "PollingDelay": 10 }, { diff --git a/lakebtchttp.go b/lakebtchttp.go index 24f32982..8bdfc1e0 100644 --- a/lakebtchttp.go +++ b/lakebtchttp.go @@ -43,6 +43,11 @@ type LakeBTCTicker struct { Volume float64 } +type LakeBTCOrderbook struct { + Bids [][]float64 `json:"asks"` + Asks [][]float64 `json:"bids"` +} + type LakeBTCTickerResponse struct { USD LakeBTCTicker CNY LakeBTCTicker @@ -85,8 +90,14 @@ func (l *LakeBTC) GetFee(maker bool) (float64) { func (l *LakeBTC) Run() { if l.Verbose { + log.Printf("%s Websocket: %s.", l.GetName(), IsEnabled(l.Websocket)) log.Printf("%s polling delay: %ds.\n", l.GetName(), l.PollingDelay) } + + if l.Websocket { + l.WebsocketClient() + } + for l.Enabled { go func() { LakeBTCTickerResponse := l.GetTicker() diff --git a/lakebtcwebsocket.go b/lakebtcwebsocket.go new file mode 100644 index 00000000..03bac44c --- /dev/null +++ b/lakebtcwebsocket.go @@ -0,0 +1,119 @@ +package main + +import ( + "fmt" + "log" + "net/http" + "github.com/gorilla/websocket" +) + +const ( + LAKEBTC_WEBSOCKET_URL = "wss://www.LakeBTC.com/websocket" +) + +func WSRailsSubscribe(channel string, conn *websocket.Conn) { + data := fmt.Sprintf(`["websocket_rails.subscribe", {"data":{"channel": "%s" }}]`, channel) + err := conn.WriteMessage(websocket.TextMessage, []byte(data)) + + if err != nil { + log.Println(err) + return + } +} + +func WSRailsUnsubscribe(channel string, conn *websocket.Conn) { + data := fmt.Sprintf(`["websocket_rails.unsubscribe", {"data":{"channel": "%s" }}]`, channel) + err := conn.WriteMessage(websocket.TextMessage, []byte(data)) + + if err != nil { + log.Println(err) + return + } +} + +func WSRailsPong(id string, conn *websocket.Conn) { + data := fmt.Sprintf(`["websocket_rails.pong", {"data":{"connection_id": %s}}]`, id) + err := conn.WriteMessage(websocket.TextMessage, []byte(data)) + + if err != nil { + log.Println(err) + return + } +} + +func (l *LakeBTC) WebsocketClient() { + var Dialer websocket.Dialer + conn, resp, err := Dialer.Dial(LAKEBTC_WEBSOCKET_URL, http.Header{}) + + if err != nil { + log.Println(err) + return + } + + if l.Verbose { + log.Printf("%s Connected to Websocket.", l.GetName()) + log.Println(resp) + } + + for { + msgType, resp, err := conn.ReadMessage() + if err != nil { + log.Println(err) + break + } + + response := [][]interface{}{} + err = JSONDecode(resp, &response) + + if err != nil { + log.Println(err) + break + } + + if msgType == websocket.TextMessage { + event := response[0][0] + data := response[0][1] + + switch event { + case "client_connected": + WSRailsSubscribe("ticker", conn) + WSRailsSubscribe("orderbook_CNY", conn) + WSRailsSubscribe("orderbook_USD", conn) + case "websocket_rails.subscribe": + case "websocket_rails.ping": + WSRailsPong("null", conn) + case "update": + update := data.(map[string]interface{}) + channel := update["channel"] + data = update["data"] + dataJSON, err := JSONEncode(data) + + if err != nil { + log.Println(err) + continue + } + + switch channel { + case "ticker": + ticker := LakeBTCTickerResponse{} + err = JSONDecode(dataJSON, &ticker) + + if err != nil { + log.Println(err) + continue + } + case "orderbook_USD", "orderbook_CNY": + orderbook := LakeBTCOrderbook{} + err = JSONDecode(dataJSON, &orderbook) + + if err != nil { + log.Println(err) + continue + } + } + } + } + } + conn.Close() + log.Printf("%s Websocket client disconnected.", l.GetName()) +} \ No newline at end of file