Skip to content

Commit

Permalink
Bitstamp: Websocket API upgrade to v2 (thrasher-corp#307)
Browse files Browse the repository at this point in the history
* Subscribe/Unsubscribe methods added

* migration to v3

* removed orderbook from rest

* WsUpdateOrderbook updated to reflect changes to v2

* Added comment for exported func

* removed logging

* unexported structs that are not used globally moved seed to own function

* unexported functions not used outside package

* Support reconnection message from bitstamp

* moved from range key/val

* using ticket.Spot instead of string

* Seperated out WsReadData & WsHandleData to allow for better testing of websocket messages

* ah should continue to next iteration and not break execution on json decode

* code formatting clean up

* reworded connection message

* return out of method instead of just breaking loop

* formatting changes and replaced SPOT with ticket.Spot type
  • Loading branch information
xtda authored and thrasher- committed May 22, 2019
1 parent 7a4ffef commit 506d601
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 168 deletions.
6 changes: 4 additions & 2 deletions exchanges/bitstamp/bitstamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"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 @@ -63,7 +64,7 @@ const (
type Bitstamp struct {
exchange.Base
Balance Balances
WebsocketConn WebsocketConn
WebsocketConn *websocket.Conn
wsRequestMtx sync.Mutex
}

Expand Down Expand Up @@ -116,6 +117,7 @@ func (b *Bitstamp) Setup(exch *config.ExchangeConfig) {
b.APISecret = exch.APISecret
b.SetAPIKeys(exch.APIKey, exch.APISecret, b.ClientID, false)
b.AuthenticatedAPISupport = true
b.WebsocketURL = bitstampWSURL
err := b.SetCurrencyPairFormat()
if err != nil {
log.Fatal(err)
Expand All @@ -142,7 +144,7 @@ func (b *Bitstamp) Setup(exch *config.ExchangeConfig) {
exch.Name,
exch.Websocket,
exch.Verbose,
BitstampPusherKey,
bitstampWSURL,
exch.WebsocketURL)
if err != nil {
log.Fatal(err)
Expand Down
62 changes: 36 additions & 26 deletions exchanges/bitstamp/bitstamp_types.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package bitstamp

import pusher "github.com/toorop/go-pusher"

// Ticker holds ticker information
type Ticker struct {
Last float64 `json:"last,string"`
Expand Down Expand Up @@ -160,34 +158,46 @@ const (
errStr string = "error"
)

// WebsocketConn defines a pusher websocket connection
type WebsocketConn struct {
Client *pusher.Client
Data chan *pusher.Event
Trade chan *pusher.Event
type websocketEventRequest struct {
Event string `json:"event"`
Data websocketData `json:"data"`
}

type websocketData struct {
Channel string `json:"channel"`
}

type websocketResponse struct {
Event string `json:"event"`
Channel string `json:"channel"`
}

type websocketTradeResponse struct {
websocketResponse
Data websocketTradeData `json:"data"`
}

// PusherOrderbook holds order book information to be pushed
type PusherOrderbook struct {
Asks [][]string `json:"asks"`
Bids [][]string `json:"bids"`
Timestamp int64 `json:"timestamp,string"`
type websocketTradeData struct {
Microtimestamp string `json:"microtimestamp"`
Amount float64 `json:"amount"`
BuyOrderID int64 `json:"buy_order_id"`
SellOrderID int64 `json:"sell_order_id"`
AmountStr string `json:"amount_str"`
PriceStr string `json:"price_str"`
Timestamp string `json:"timestamp"`
Price float64 `json:"price"`
Type int `json:"type"`
ID int `json:"id"`
}

// PusherTrade holds trade information to be pushed
type PusherTrade struct {
Price float64 `json:"price"`
Amount float64 `json:"amount"`
ID int64 `json:"id"`
Type int64 `json:"type"`
Timestamp int64 `json:"timestamp,string"`
BuyOrderID int64 `json:"buy_order_id"`
SellOrderID int64 `json:"sell_order_id"`
type websocketOrderBookResponse struct {
websocketResponse
Data websocketOrderBook `json:"data"`
}

// PusherOrders defines order information
type PusherOrders struct {
ID int64 `json:"id"`
Amount float64 `json:"amount"`
Price float64 `json:""`
type websocketOrderBook struct {
Asks [][]string `json:"asks"`
Bids [][]string `json:"bids"`
Timestamp int64 `json:"timestamp,string"`
Microtimestamp string `json:"microtimestamp"`
}
Loading

0 comments on commit 506d601

Please sign in to comment.