From 8c62316e8255bea39bd034c37aea116720477448 Mon Sep 17 00:00:00 2001 From: Adrian Gallagher Date: Tue, 4 Jun 2019 14:34:00 +1000 Subject: [PATCH] websocket QA --- exchanges/bitmex/bitmex_websocket.go | 2 +- exchanges/bitmex/bitmex_wrapper.go | 4 +- exchanges/btse/btse_websocket.go | 13 ++- exchanges/coinut/coinut_websocket.go | 4 +- exchanges/huobihadax/huobihadax_types.go | 4 +- exchanges/okex/okex_wrapper.go | 6 +- exchanges/okgroup/okgroup_wrapper.go | 2 +- exchanges/poloniex/poloniex_types.go | 1 + exchanges/poloniex/poloniex_websocket.go | 141 +++-------------------- 9 files changed, 38 insertions(+), 139 deletions(-) diff --git a/exchanges/bitmex/bitmex_websocket.go b/exchanges/bitmex/bitmex_websocket.go index 6527b38b..33ad3182 100644 --- a/exchanges/bitmex/bitmex_websocket.go +++ b/exchanges/bitmex/bitmex_websocket.go @@ -388,7 +388,7 @@ func (b *Bitmex) processOrderbook(data []OrderBookL2, action string, currencyPai // GenerateDefaultSubscriptions Adds default subscriptions to websocket to be handled by ManageSubscriptions() func (b *Bitmex) GenerateDefaultSubscriptions() { - contracts := b.GetEnabledPairs(assets.AssetTypeSpot) + contracts := b.GetEnabledPairs(assets.AssetTypePerpetualContract) channels := []string{bitmexWSOrderbookL2, bitmexWSTrade} subscriptions := []exchange.WebsocketChannelSubscription{ { diff --git a/exchanges/bitmex/bitmex_wrapper.go b/exchanges/bitmex/bitmex_wrapper.go index 440cce33..a066d301 100644 --- a/exchanges/bitmex/bitmex_wrapper.go +++ b/exchanges/bitmex/bitmex_wrapper.go @@ -75,11 +75,11 @@ func (b *Bitmex) SetDefaults() { // Upside and Downside profit contracts use the same format fmt2 := currency.PairStore{ RequestFormat: ¤cy.PairFormat{ - Delimiter: "-", + Delimiter: "_", Uppercase: true, }, ConfigFormat: ¤cy.PairFormat{ - Delimiter: "-", + Delimiter: "_", Uppercase: true, }, } diff --git a/exchanges/btse/btse_websocket.go b/exchanges/btse/btse_websocket.go index 0308ee07..245cb2f2 100644 --- a/exchanges/btse/btse_websocket.go +++ b/exchanges/btse/btse_websocket.go @@ -91,7 +91,7 @@ func (b *BTSE) WsHandleData() { ProductID string `json:"product_id"` } - if strings.Contains(string(resp.Raw), "connect success") { + if strings.Contains(string(resp.Raw), "Welcome to BTSE") { if b.Verbose { log.Debugf("%s websocket client successfully connected to %s", b.Name, b.Websocket.GetWebsocketURL()) @@ -121,11 +121,12 @@ func (b *BTSE) WsHandleData() { } b.Websocket.DataHandler <- exchange.TickerData{ - Timestamp: time.Now(), - Pair: currency.NewPairDelimiter(t.ProductID, "-"), - AssetType: assets.AssetTypeSpot, - Exchange: b.GetName(), - OpenPrice: price, + Timestamp: time.Now(), + Pair: currency.NewPairDelimiter(t.ProductID, "-"), + AssetType: assets.AssetTypeSpot, + Exchange: b.GetName(), + ClosePrice: price, + Quantity: t.LastSize, } case "snapshot": snapshot := websocketOrderbookSnapshot{} diff --git a/exchanges/coinut/coinut_websocket.go b/exchanges/coinut/coinut_websocket.go index 42079b88..a70481eb 100644 --- a/exchanges/coinut/coinut_websocket.go +++ b/exchanges/coinut/coinut_websocket.go @@ -78,8 +78,10 @@ func (c *COINUT) WsHandleData() { continue } + currencyPair := instrumentListByCode[ticker.InstID] c.Websocket.DataHandler <- exchange.TickerData{ Timestamp: time.Unix(0, ticker.Timestamp), + Pair: currency.NewPairFromString(currencyPair), Exchange: c.GetName(), AssetType: assets.AssetTypeSpot, HighPrice: ticker.HighestBuy, @@ -225,7 +227,7 @@ func (c *COINUT) GetNonce() int64 { func (c *COINUT) WsSetInstrumentList() error { err := c.wsSend(wsRequest{ Request: "inst_list", - SecType: "spot", + SecType: "SPOT", Nonce: c.GetNonce(), }) if err != nil { diff --git a/exchanges/huobihadax/huobihadax_types.go b/exchanges/huobihadax/huobihadax_types.go index c533c317..34efc9e6 100644 --- a/exchanges/huobihadax/huobihadax_types.go +++ b/exchanges/huobihadax/huobihadax_types.go @@ -313,9 +313,9 @@ type WsTrade struct { ID int64 `json:"id"` Timestamp int64 `json:"ts"` Data []struct { - Amount float64 `json:"amount"` + ID float64 `json:"id"` Timestamp int64 `json:"ts"` - ID float64 `json:"id,string"` + Amount float64 `json:"amount"` Price float64 `json:"price"` Direction string `json:"direction"` } `json:"data"` diff --git a/exchanges/okex/okex_wrapper.go b/exchanges/okex/okex_wrapper.go index 475e5ed7..24105113 100644 --- a/exchanges/okex/okex_wrapper.go +++ b/exchanges/okex/okex_wrapper.go @@ -113,7 +113,7 @@ func (o *OKEX) Start(wg *sync.WaitGroup) { // Run implements the OKEX wrapper func (o *OKEX) Run() { if o.Verbose { - log.Debugf("%s Websocket: %s. (url: %s).\n", o.GetName(), common.IsEnabled(o.Websocket.IsEnabled()), o.WebsocketURL) + log.Debugf("%s Websocket: %s. (url: %s).\n", o.GetName(), common.IsEnabled(o.Websocket.IsEnabled()), o.API.Endpoints.WebsocketURL) } if !o.GetEnabledFeatures().AutoPairUpdates { @@ -153,14 +153,14 @@ func (o *OKEX) FetchTradablePairs(asset assets.AssetType) ([]string, error) { return pairs, nil case assets.AssetTypePerpetualSwap: - prods, err := o.GetAllSwapTokensInformation() + prods, err := o.GetSwapContractInformation() if err != nil { return nil, err } var pairs []string for x := range prods { - pairs = append(pairs, prods[x].InstrumentID) + pairs = append(pairs, prods[x].UnderlyingIndex+"_"+prods[x].QuoteCurrency+"_SWAP") } return pairs, nil case assets.AssetTypeIndex: diff --git a/exchanges/okgroup/okgroup_wrapper.go b/exchanges/okgroup/okgroup_wrapper.go index 5dfbce0a..f9f1e54a 100644 --- a/exchanges/okgroup/okgroup_wrapper.go +++ b/exchanges/okgroup/okgroup_wrapper.go @@ -37,7 +37,7 @@ func (o *OKGroup) Setup(exch *config.ExchangeConfig) error { exch.Name, exch.Features.Enabled.Websocket, exch.Verbose, - o.WebsocketURL, + o.API.Endpoints.WebsocketURL, exch.API.Endpoints.WebsocketURL) } diff --git a/exchanges/poloniex/poloniex_types.go b/exchanges/poloniex/poloniex_types.go index c4faae61..9ecb7616 100644 --- a/exchanges/poloniex/poloniex_types.go +++ b/exchanges/poloniex/poloniex_types.go @@ -4,6 +4,7 @@ import "github.com/thrasher-/gocryptotrader/currency" // Ticker holds ticker data type Ticker struct { + ID int `json:"id"` Last float64 `json:"last,string"` LowestAsk float64 `json:"lowestAsk,string"` HighestBid float64 `json:"highestBid,string"` diff --git a/exchanges/poloniex/poloniex_websocket.go b/exchanges/poloniex/poloniex_websocket.go index 4060f78c..ed43d954 100644 --- a/exchanges/poloniex/poloniex_websocket.go +++ b/exchanges/poloniex/poloniex_websocket.go @@ -27,8 +27,8 @@ const ( ) var ( - // CurrencyIDMap stores a map of currencies associated with their ID - CurrencyIDMap map[string]int + // currencyIDMap stores a map of currencies associated with their ID + currencyIDMap map[int]string ) // WsConnect initiates a websocket connection @@ -54,15 +54,15 @@ func (p *Poloniex) WsConnect() error { return err } - if CurrencyIDMap == nil { - CurrencyIDMap = make(map[string]int) - resp, err := p.GetCurrencies() + if currencyIDMap == nil { + currencyIDMap = make(map[int]string) + resp, err := p.GetTicker() if err != nil { return err } for k, v := range resp { - CurrencyIDMap[k] = v.ID + currencyIDMap[v.ID] = k } } @@ -143,6 +143,8 @@ func (p *Poloniex) WsHandleData() { case wsTickerDataID: tickerData := data[2].([]interface{}) var t WsTicker + + currencyPair := currencyIDMap[int(tickerData[0].(float64))] t.LastPrice, _ = strconv.ParseFloat(tickerData[1].(string), 64) t.LowestAsk, _ = strconv.ParseFloat(tickerData[2].(string), 64) t.HighestBid, _ = strconv.ParseFloat(tickerData[3].(string), 64) @@ -158,11 +160,13 @@ func (p *Poloniex) WsHandleData() { t.LowestTradePrice24H, _ = strconv.ParseFloat(tickerData[9].(string), 64) p.Websocket.DataHandler <- exchange.TickerData{ - Timestamp: time.Now(), - Exchange: p.GetName(), - AssetType: assets.AssetTypeSpot, - LowPrice: t.LowestAsk, - HighPrice: t.HighestBid, + Timestamp: time.Now(), + Pair: currency.NewPairDelimiter(currencyPair, "_"), + Exchange: p.GetName(), + AssetType: assets.AssetTypeSpot, + ClosePrice: t.LastPrice, + LowPrice: t.LowestAsk, + HighPrice: t.HighestBid, } case ws24HourExchangeVolumeID: case wsHeartbeat: @@ -201,7 +205,7 @@ func (p *Poloniex) WsHandleData() { Pair: currency.NewPairFromString(currencyPair), } case "o": - currencyPair := CurrencyPairID[chanID] + currencyPair := currencyIDMap[chanID] err := p.WsProcessOrderbookUpdate(dataL3, currencyPair) if err != nil { p.Websocket.DataHandler <- err @@ -214,9 +218,9 @@ func (p *Poloniex) WsHandleData() { Pair: currency.NewPairFromString(currencyPair), } case "t": - currencyPair := CurrencyPairID[chanID] + currencyPair := currencyIDMap[chanID] var trade WsTrade - trade.Symbol = CurrencyPairID[chanID] + trade.Symbol = currencyIDMap[chanID] trade.TradeID, _ = strconv.ParseInt(dataL3[1].(string), 10, 64) // 1 for buy 0 for sell side := "buy" @@ -328,115 +332,6 @@ func (p *Poloniex) WsProcessOrderbookUpdate(target []interface{}, symbol string) assets.AssetTypeSpot) } -// CurrencyPairID contains a list of IDS for currency pairs. -var CurrencyPairID = map[int]string{ - 7: "BTC_BCN", - 14: "BTC_BTS", - 15: "BTC_BURST", - 20: "BTC_CLAM", - 25: "BTC_DGB", - 27: "BTC_DOGE", - 24: "BTC_DASH", - 38: "BTC_GAME", - 43: "BTC_HUC", - 50: "BTC_LTC", - 51: "BTC_MAID", - 58: "BTC_OMNI", - 61: "BTC_NAV", - 64: "BTC_NMC", - 69: "BTC_NXT", - 75: "BTC_PPC", - 89: "BTC_STR", - 92: "BTC_SYS", - 97: "BTC_VIA", - 100: "BTC_VTC", - 108: "BTC_XCP", - 114: "BTC_XMR", - 116: "BTC_XPM", - 117: "BTC_XRP", - 112: "BTC_XEM", - 148: "BTC_ETH", - 150: "BTC_SC", - 153: "BTC_EXP", - 155: "BTC_FCT", - 160: "BTC_AMP", - 162: "BTC_DCR", - 163: "BTC_LSK", - 167: "BTC_LBC", - 168: "BTC_STEEM", - 170: "BTC_SBD", - 171: "BTC_ETC", - 174: "BTC_REP", - 177: "BTC_ARDR", - 178: "BTC_ZEC", - 182: "BTC_STRAT", // nolint: misspell - 184: "BTC_PASC", - 185: "BTC_GNT", - 187: "BTC_GNO", - 189: "BTC_BCH", - 192: "BTC_ZRX", - 194: "BTC_CVC", - 196: "BTC_OMG", - 198: "BTC_GAS", - 200: "BTC_STORJ", - 201: "BTC_EOS", - 204: "BTC_SNT", - 207: "BTC_KNC", - 210: "BTC_BAT", - 213: "BTC_LOOM", - 221: "BTC_QTUM", - 121: "USDT_BTC", - 216: "USDT_DOGE", - 122: "USDT_DASH", - 123: "USDT_LTC", - 124: "USDT_NXT", - 125: "USDT_STR", - 126: "USDT_XMR", - 127: "USDT_XRP", - 149: "USDT_ETH", - 219: "USDT_SC", - 218: "USDT_LSK", - 173: "USDT_ETC", - 175: "USDT_REP", - 180: "USDT_ZEC", - 217: "USDT_GNT", - 191: "USDT_BCH", - 220: "USDT_ZRX", - 203: "USDT_EOS", - 206: "USDT_SNT", - 209: "USDT_KNC", - 212: "USDT_BAT", - 215: "USDT_LOOM", - 223: "USDT_QTUM", - 129: "XMR_BCN", - 132: "XMR_DASH", - 137: "XMR_LTC", - 138: "XMR_MAID", - 140: "XMR_NXT", - 181: "XMR_ZEC", - 166: "ETH_LSK", - 169: "ETH_STEEM", - 172: "ETH_ETC", - 176: "ETH_REP", - 179: "ETH_ZEC", - 186: "ETH_GNT", - 188: "ETH_GNO", - 190: "ETH_BCH", - 193: "ETH_ZRX", - 195: "ETH_CVC", - 197: "ETH_OMG", - 199: "ETH_GAS", - 202: "ETH_EOS", - 205: "ETH_SNT", - 208: "ETH_KNC", - 211: "ETH_BAT", - 214: "ETH_LOOM", - 222: "ETH_QTUM", - 224: "USDC_BTC", - 226: "USDC_USDT", - 225: "USDC_ETH", -} - // GenerateDefaultSubscriptions Adds default subscriptions to websocket to be handled by ManageSubscriptions() func (p *Poloniex) GenerateDefaultSubscriptions() { subscriptions := []exchange.WebsocketChannelSubscription{}