Files
gocryptotrader/exchanges/okgroup/okgroup_websocket.go
Ryan O'Hara-Reid 14c72c9c6b Currency System Update (#448)
* initial update of currency system

* WIP progress

* Finish initial currency string error returns

* fix whoopsie testing for non https insecureinos

* Current WIP for getEnabledPairs check and error return

* WIP continued

* When getting enabled pairs throw error when item is not contained in available pairs list

* More updates -WIP

* Wip continued including potential interface

* Current WIP

* pairs manager pass

* drop asset string and just use the map key, plus return some errors and create more work for myself.

* clean and fixed a bug in currency.json, will not populate correctly without coinmarketcap api keys set.

* purge logger references after merge

* go mod tidy after merge

* Pointer change WIP

* fix some issues and added error returns to a few items (WIP)

* WIP

* Clean

* Fix some linter issues

* Fix more linter issues

* even more linters

* xtda nits

* revert pointer change and rm field

* Addr madcozbadd nits

* fix linter issues: shadow declarations

* Fix linter issues: gocritic huge things

* linter issue fix

* Addr nits

* flush go mod files

* after merge woops

* fix shadow dec

* Addr thrasher nits

* addr nits

* fix some issues

* more fixes

* RM println

* Addr glorious nits

* Add helper method for setting assets

* add missing format directive

* Addr nits

* Actually process new futures contracts -_- derp

* WIP for GRPC upgrade for pair management

* update config pairs

* finished disabling and enabling asset

* linked update of tradable pairs to cli

* fix oopsies

* defer writing to file on program termination for currency storage system

* update template

* don't add disabled asset items to initial sync

* Fix enable disabling a list of pairs and added in a slice error type so we can add whats allowable without throwing an error and return a report, also addressed some other nits

* WIP on getting a channel to unsub

* Wip track down unsubscribe bug and start creating streaming interface

* purge websocket orderbook object and centralised updating routine for orderbook

* general clean before interface implementation

* stage one connection interface WIP

* WIP

* repackage wshandler WIP

* find difference of subs and change signature of subscriber functions so we can batch subscriptions and unsubscription in exchanges that support it

* design change on mange subscription routine WIP

* integrated ZB with the new webosocket updates

* WIP - okex conversion

* integrate websocket upgrades for lakebtc, kraken, huobi, hitbtc, gateio, and WIP for coinbene

* integrate another range of exchanges for websocket update

* Added subscriber and unsubscriber methods to websocket functionality

* fixed tests WIP

* amalgamate cache setup with main websocket setup

* reinstate exported fields traffic alert and shutdownC to accommodate gemini and lakebtc implementations

* added in colon

* Updated websocket auth handling as they werent getting passed through. Added a setter method for websocket URL due to the Binance generated auth key/listenKey. Fixed bug which stopped reconnection.

* Fix subscribe candle bug
Fix time conversion in candle
Fix inititial candle history to datahandler
Include funding to orderbook handling
Include funding to trades
Reduce code duplication in sub and unsub functions
Added the ability to include funding currency websocket subscriptions
validated all channels and added more items todo list (Auth items)

* RM line

* bitstamp pass

* btcmarkets pass - still needs to implement unsubscriber functionality and pairs change test.

* Batch outgoing subscriptions and fix unsubscribe bug

* BTSE - bumped time to minute to reduce pinger calling by 75 calls per day. Fix authentication bug and add authentication pass into to-do. Batch outgoing subscription calls

* fix type field and batch outgoing subs and unsubs for coinbasepro

* Batch outgoing subs and unsubs

* Fixes bug when matching return from authentication

* Fix bug where params where being sent out of order due to map ,where depth items werent being subscribed too, where trying to subscribe to too many kline items caused error, where trying to get a nano secocond ID conflicted due to speed of generation.

* Add websocket capability for currency pair change by utilizing full channel subscription list in subscribe function.

* Add error handling

* Fix public: time conversions, subscription list, stopped pushing heartbeat to data handler, aggregated list of connections.

* hitBTC pass

* returned nil instead of error due to period null bids and asks updates coming through.

* Fix auth ping capture and reply. Added in interval handling for kline data. Added correct full trade data handling. Fix subscribe and unsubscribe.

* Fix when websocket auth conn and token generation fail we don't try and auth sub. Fix bug between auth and normal connection id generation and matching. Batch outgoing payloads to increase efficiency. Updated matching functions to utilise channels instead of waitgroups and go routines.

* RM debug output

* rm func to get shutdown channel

* Add unsubscriber functionality, added wsTicker type, removed return as this will impede data flow and cause reconnection when handling and processing data

* okgroup WIP

* *Added missing fields for websocket trades
*Fix bug processing kline interval
*Added fields for websocket ticker struct
*Fix auth bug
	-Updated request and response matching param to interface so we can custom signature match. Stops auth subscribing before a reply is issued.
	-Updated channel inclusion of pair fo auth subs as this was missing.
*Assortment of perfomance improvements

* poloniex pass

* send all trades to data handler, validated enabled and disable pairs

* initial clean

* centralised request matching mechanism

* websocket main improvements WIP

* WIP

* Websocket management via gctcli WIP

* GRPC expansion

* Updated GCTCLI with websocket url and proxy setting functionality which flushes connection

* Fix continuous spawning of routines bug on error with reconnection

* Addr linter issues

* fix subscription bug that I caused when I changed to a switch case

* fix linter issue

* fix woopsie

* End of day WIP

* Fix order submission REST, time conversion, order type conversion, orderID bugs

* fix gateio test and unsubscribe bug

* revert comment out code

* websocketAPI changed to to true in configtest.json

* fix race in gateio test

* End of day WIP for websocket tests.

* BugFix for binance when book isn't seeded. Updated websocket tests. Deprecated subscription manager. RM wrapper funcs.

* Added string title to exchange name as they are saved as lower case in type, reinstated verbose check in websocket.go

* Added verbosity check for setting websocket URL

* fix bug where the asset had a mind of its own

* purge dodgy coding

* Fix tests, drop blocking chan in websocket Dial function

* few more changes

* race condition fix for websocket tests.

* fix intermittent test failure due to underlying hash table storage

* Address madcuzbad nit

* RM superfluous printlines

* Add quick top example with paramater fields

* First pass Glorious nits

* As per madcozbad suggestion return error when enabled pair not found in full return map. Add test.

* addr madcozbadd nits

* as per glorious suggestion rm'd loadedJSON field

* adjusted ticker, added test and RM'd code that can never be executed

* Addr nits and add in crypto rand genration for ID's

* remove global channel declaration and rescoped as this was causing a lock

* as per glorious suggestion restructured return error for websocket

* addr glorious suggestions

* fix linter issues

* purge non-existent pair from testdata

* add side field to struct and parse

* addr glorious nits

* Add verbosity to error returns and logs and fix string parsing in GCTRPC

* fix speeling mistwake

* Adds websocket functionality check before flushing websocket connection

* Addr kraken panic and setting/flush websocket url stage one.

* added websocket url check before setting with tests

* Added in edge case test if by the time we call contains on available pairs it has been changed

* remove error return for func

* Continuation of tests

* continuation of tests

* Stop potential panic within pair creation function

* Implement changes to upstream

* rm sup comment

* fix bug when subscribing and unsubscribing. Also add in boolean to determine there are currencies that need to be flushed via set pairs via gctcli

* fix test

* Fix linter issues

* Fix tests

* turn websocket off in config example

* Fix issue where you cannot enable websocket when config is set to false, also added config websocket enable state saving

* Introduced err var for same error returns

* Add err var exchange base not found

* restructure function

* drop gctscript from generic response name

* drop managesub delay const as its not being used

* correctly implement websocket rate limiting for coinut

* remove quotations

* drop pair management check

* fix spelling

* return error in function to not update currency with unset role

* amalagamted enable/disable into set function and added in pairstore fetch function

* update error description

* rm function

* moved test function to sharedtestvals and move type to types.go

* append delimiter onto currency delimiter strings

* add test coverage

* rm functions as they are set as methods in base

* remove superfluous methods

* Fix issue that would occur when a subscription errored and not appending successful subs

* fix after rebase woopsie

* fix linter issues

* fix bug streamline code

* fix linter issues

* fix linter issues

* fix case where it should not change ID if set but append new

* fix whoopsie

* fix websocket tests

* fix readme, fix wrapper issues reporting template, go mod tidy

* add test coverage

* add test coverage and verified futures pariing

* add in futures bypass as its not currently supported on BTSE until API update and implementation

* removed downside/upside profit contract type as its no longer supported. Added in check in set config pairs to warn user of potential conflict and to manually remove or update.

* If asset enabled add pair and increase code coverage

* remove strings.title, set and fetch with strings.Lower but keep struct field exchangename unchanged. Streamline ticker and orderbook code.

* Add code coverage

* log error if setting default currency fails, add code coverage

* address glorious nits

* Addr xtda nits

* fix linter issues

* addr glorious nits

* xtda nits

* Addr glorious nits

* add subscription protection and removed a superfluous wait call

* fix test

* fix whoopsie

* addr xtda nits

* addr glorious nits

* Added asset types to subscriptions structs, also added in error handling for resubscription errors

* consolidated rpc returned type and added in sucessful strings

* dropped stream timing down to 100ms

* DOC changes

* proxy and url usage string additions

* WIP

* go mod tidy rides again

* Addr nits

* Addr nits, fix tests

* fix wording

* add in test case for currency matching

* Add byte length check on outbound websocket payload subscriptions

* addr thrasher nits

* Addr madcozbadd nits

* addr linter issues

* Addr glorious nits by amalgamating function into one mega amazing function.

* fix futures account subscription bug

* addr glorious nits and reinstated wg.Wait() checks

* changed string to currency delimiter string and setconnected by function
2020-07-24 13:18:09 +10:00

1031 lines
33 KiB
Go

package okgroup
import (
"encoding/json"
"errors"
"fmt"
"hash/crc32"
"net/http"
"strconv"
"strings"
"sync"
"time"
"github.com/gorilla/websocket"
"github.com/thrasher-corp/gocryptotrader/common/crypto"
"github.com/thrasher-corp/gocryptotrader/currency"
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-corp/gocryptotrader/exchanges/stream"
"github.com/thrasher-corp/gocryptotrader/exchanges/stream/buffer"
"github.com/thrasher-corp/gocryptotrader/exchanges/ticker"
"github.com/thrasher-corp/gocryptotrader/log"
)
// List of all websocket channels to subscribe to
const (
// Orderbook events
okGroupWsOrderbookUpdate = "update"
okGroupWsOrderbookPartial = "partial"
// API subsections
okGroupWsSwapSubsection = "swap/"
okGroupWsIndexSubsection = "index/"
okGroupWsFuturesSubsection = "futures/"
okGroupWsSpotSubsection = "spot/"
// Shared API endpoints
okGroupWsCandle = "candle"
okGroupWsCandle60s = okGroupWsCandle + "60s"
okGroupWsCandle180s = okGroupWsCandle + "180s"
okGroupWsCandle300s = okGroupWsCandle + "300s"
okGroupWsCandle900s = okGroupWsCandle + "900s"
okGroupWsCandle1800s = okGroupWsCandle + "1800s"
okGroupWsCandle3600s = okGroupWsCandle + "3600s"
okGroupWsCandle7200s = okGroupWsCandle + "7200s"
okGroupWsCandle14400s = okGroupWsCandle + "14400s"
okGroupWsCandle21600s = okGroupWsCandle + "21600"
okGroupWsCandle43200s = okGroupWsCandle + "43200s"
okGroupWsCandle86400s = okGroupWsCandle + "86400s"
okGroupWsCandle604900s = okGroupWsCandle + "604800s"
okGroupWsTicker = "ticker"
okGroupWsTrade = "trade"
okGroupWsDepth = "depth"
okGroupWsDepth5 = "depth5"
okGroupWsAccount = "account"
okGroupWsMarginAccount = "margin_account"
okGroupWsOrder = "order"
okGroupWsFundingRate = "funding_rate"
okGroupWsPriceRange = "price_range"
okGroupWsMarkPrice = "mark_price"
okGroupWsPosition = "position"
okGroupWsEstimatedPrice = "estimated_price"
// Spot endpoints
okGroupWsSpotTicker = okGroupWsSpotSubsection + okGroupWsTicker
okGroupWsSpotCandle60s = okGroupWsSpotSubsection + okGroupWsCandle60s
okGroupWsSpotCandle180s = okGroupWsSpotSubsection + okGroupWsCandle180s
okGroupWsSpotCandle300s = okGroupWsSpotSubsection + okGroupWsCandle300s
okGroupWsSpotCandle900s = okGroupWsSpotSubsection + okGroupWsCandle900s
okGroupWsSpotCandle1800s = okGroupWsSpotSubsection + okGroupWsCandle1800s
okGroupWsSpotCandle3600s = okGroupWsSpotSubsection + okGroupWsCandle3600s
okGroupWsSpotCandle7200s = okGroupWsSpotSubsection + okGroupWsCandle7200s
okGroupWsSpotCandle14400s = okGroupWsSpotSubsection + okGroupWsCandle14400s
okGroupWsSpotCandle21600s = okGroupWsSpotSubsection + okGroupWsCandle21600s
okGroupWsSpotCandle43200s = okGroupWsSpotSubsection + okGroupWsCandle43200s
okGroupWsSpotCandle86400s = okGroupWsSpotSubsection + okGroupWsCandle86400s
okGroupWsSpotCandle604900s = okGroupWsSpotSubsection + okGroupWsCandle604900s
okGroupWsSpotTrade = okGroupWsSpotSubsection + okGroupWsTrade
okGroupWsSpotDepth = okGroupWsSpotSubsection + okGroupWsDepth
okGroupWsSpotDepth5 = okGroupWsSpotSubsection + okGroupWsDepth5
okGroupWsSpotAccount = okGroupWsSpotSubsection + okGroupWsAccount
okGroupWsSpotMarginAccount = okGroupWsSpotSubsection + okGroupWsMarginAccount
okGroupWsSpotOrder = okGroupWsSpotSubsection + okGroupWsOrder
// Swap endpoints
okGroupWsSwapTicker = okGroupWsSwapSubsection + okGroupWsTicker
okGroupWsSwapCandle60s = okGroupWsSwapSubsection + okGroupWsCandle60s
okGroupWsSwapCandle180s = okGroupWsSwapSubsection + okGroupWsCandle180s
okGroupWsSwapCandle300s = okGroupWsSwapSubsection + okGroupWsCandle300s
okGroupWsSwapCandle900s = okGroupWsSwapSubsection + okGroupWsCandle900s
okGroupWsSwapCandle1800s = okGroupWsSwapSubsection + okGroupWsCandle1800s
okGroupWsSwapCandle3600s = okGroupWsSwapSubsection + okGroupWsCandle3600s
okGroupWsSwapCandle7200s = okGroupWsSwapSubsection + okGroupWsCandle7200s
okGroupWsSwapCandle14400s = okGroupWsSwapSubsection + okGroupWsCandle14400s
okGroupWsSwapCandle21600s = okGroupWsSwapSubsection + okGroupWsCandle21600s
okGroupWsSwapCandle43200s = okGroupWsSwapSubsection + okGroupWsCandle43200s
okGroupWsSwapCandle86400s = okGroupWsSwapSubsection + okGroupWsCandle86400s
okGroupWsSwapCandle604900s = okGroupWsSwapSubsection + okGroupWsCandle604900s
okGroupWsSwapTrade = okGroupWsSwapSubsection + okGroupWsTrade
okGroupWsSwapDepth = okGroupWsSwapSubsection + okGroupWsDepth
okGroupWsSwapDepth5 = okGroupWsSwapSubsection + okGroupWsDepth5
okGroupWsSwapFundingRate = okGroupWsSwapSubsection + okGroupWsFundingRate
okGroupWsSwapPriceRange = okGroupWsSwapSubsection + okGroupWsPriceRange
okGroupWsSwapMarkPrice = okGroupWsSwapSubsection + okGroupWsMarkPrice
okGroupWsSwapPosition = okGroupWsSwapSubsection + okGroupWsPosition
okGroupWsSwapAccount = okGroupWsSwapSubsection + okGroupWsAccount
okGroupWsSwapOrder = okGroupWsSwapSubsection + okGroupWsOrder
// Index endpoints
okGroupWsIndexTicker = okGroupWsIndexSubsection + okGroupWsTicker
okGroupWsIndexCandle60s = okGroupWsIndexSubsection + okGroupWsCandle60s
okGroupWsIndexCandle180s = okGroupWsIndexSubsection + okGroupWsCandle180s
okGroupWsIndexCandle300s = okGroupWsIndexSubsection + okGroupWsCandle300s
okGroupWsIndexCandle900s = okGroupWsIndexSubsection + okGroupWsCandle900s
okGroupWsIndexCandle1800s = okGroupWsIndexSubsection + okGroupWsCandle1800s
okGroupWsIndexCandle3600s = okGroupWsIndexSubsection + okGroupWsCandle3600s
okGroupWsIndexCandle7200s = okGroupWsIndexSubsection + okGroupWsCandle7200s
okGroupWsIndexCandle14400s = okGroupWsIndexSubsection + okGroupWsCandle14400s
okGroupWsIndexCandle21600s = okGroupWsIndexSubsection + okGroupWsCandle21600s
okGroupWsIndexCandle43200s = okGroupWsIndexSubsection + okGroupWsCandle43200s
okGroupWsIndexCandle86400s = okGroupWsIndexSubsection + okGroupWsCandle86400s
okGroupWsIndexCandle604900s = okGroupWsIndexSubsection + okGroupWsCandle604900s
// Futures endpoints
okGroupWsFuturesTicker = okGroupWsFuturesSubsection + okGroupWsTicker
okGroupWsFuturesCandle60s = okGroupWsFuturesSubsection + okGroupWsCandle60s
okGroupWsFuturesCandle180s = okGroupWsFuturesSubsection + okGroupWsCandle180s
okGroupWsFuturesCandle300s = okGroupWsFuturesSubsection + okGroupWsCandle300s
okGroupWsFuturesCandle900s = okGroupWsFuturesSubsection + okGroupWsCandle900s
okGroupWsFuturesCandle1800s = okGroupWsFuturesSubsection + okGroupWsCandle1800s
okGroupWsFuturesCandle3600s = okGroupWsFuturesSubsection + okGroupWsCandle3600s
okGroupWsFuturesCandle7200s = okGroupWsFuturesSubsection + okGroupWsCandle7200s
okGroupWsFuturesCandle14400s = okGroupWsFuturesSubsection + okGroupWsCandle14400s
okGroupWsFuturesCandle21600s = okGroupWsFuturesSubsection + okGroupWsCandle21600s
okGroupWsFuturesCandle43200s = okGroupWsFuturesSubsection + okGroupWsCandle43200s
okGroupWsFuturesCandle86400s = okGroupWsFuturesSubsection + okGroupWsCandle86400s
okGroupWsFuturesCandle604900s = okGroupWsFuturesSubsection + okGroupWsCandle604900s
okGroupWsFuturesTrade = okGroupWsFuturesSubsection + okGroupWsTrade
okGroupWsFuturesEstimatedPrice = okGroupWsFuturesSubsection + okGroupWsTrade
okGroupWsFuturesPriceRange = okGroupWsFuturesSubsection + okGroupWsPriceRange
okGroupWsFuturesDepth = okGroupWsFuturesSubsection + okGroupWsDepth
okGroupWsFuturesDepth5 = okGroupWsFuturesSubsection + okGroupWsDepth5
okGroupWsFuturesMarkPrice = okGroupWsFuturesSubsection + okGroupWsMarkPrice
okGroupWsFuturesAccount = okGroupWsFuturesSubsection + okGroupWsAccount
okGroupWsFuturesPosition = okGroupWsFuturesSubsection + okGroupWsPosition
okGroupWsFuturesOrder = okGroupWsFuturesSubsection + okGroupWsOrder
okGroupWsRateLimit = 30
allowableIterations = 25
delimiterColon = ":"
delimiterDash = "-"
delimiterUnderscore = "_"
maxConnByteLen = 4096
)
// orderbookMutex Ensures if two entries arrive at once, only one can be
// processed at a time
var orderbookMutex sync.Mutex
var defaultSpotSubscribedChannels = []string{okGroupWsSpotDepth,
okGroupWsSpotCandle300s,
okGroupWsSpotTicker,
okGroupWsSpotTrade}
var defaultFuturesSubscribedChannels = []string{okGroupWsFuturesDepth,
okGroupWsFuturesCandle300s,
okGroupWsFuturesTicker,
okGroupWsFuturesTrade}
var defaultIndexSubscribedChannels = []string{okGroupWsIndexCandle300s,
okGroupWsIndexTicker}
var defaultSwapSubscribedChannels = []string{okGroupWsSwapDepth,
okGroupWsSwapCandle300s,
okGroupWsSwapTicker,
okGroupWsSwapTrade,
okGroupWsSwapFundingRate,
okGroupWsSwapMarkPrice}
// WsConnect initiates a websocket connection
func (o *OKGroup) WsConnect() error {
if !o.Websocket.IsEnabled() || !o.IsEnabled() {
return errors.New(stream.WebsocketNotEnabled)
}
var dialer websocket.Dialer
dialer.ReadBufferSize = 8192
dialer.WriteBufferSize = 8192
err := o.Websocket.Conn.Dial(&dialer, http.Header{})
if err != nil {
return err
}
if o.Verbose {
log.Debugf(log.ExchangeSys, "Successful connection to %v\n",
o.Websocket.GetWebsocketURL())
}
go o.WsReadData()
if o.GetAuthenticatedAPISupport(exchange.WebsocketAuthentication) {
err = o.WsLogin()
if err != nil {
log.Errorf(log.ExchangeSys,
"%v - authentication failed: %v\n",
o.Name,
err)
}
}
subs, err := o.GenerateDefaultSubscriptions()
if err != nil {
return err
}
return o.Websocket.SubscribeToChannels(subs)
}
// WsLogin sends a login request to websocket to enable access to authenticated endpoints
func (o *OKGroup) WsLogin() error {
o.Websocket.SetCanUseAuthenticatedEndpoints(true)
unixTime := time.Now().UTC().Unix()
signPath := "/users/self/verify"
hmac := crypto.GetHMAC(crypto.HashSHA256,
[]byte(strconv.FormatInt(unixTime, 10)+http.MethodGet+signPath),
[]byte(o.API.Credentials.Secret),
)
base64 := crypto.Base64Encode(hmac)
request := WebsocketEventRequest{
Operation: "login",
Arguments: []string{
o.API.Credentials.Key,
o.API.Credentials.ClientID,
strconv.FormatInt(unixTime, 10),
base64,
},
}
_, err := o.Websocket.Conn.SendMessageReturnResponse("login", request)
if err != nil {
o.Websocket.SetCanUseAuthenticatedEndpoints(false)
return err
}
return nil
}
// WsReadData receives and passes on websocket messages for processing
func (o *OKGroup) WsReadData() {
o.Websocket.Wg.Add(1)
defer o.Websocket.Wg.Done()
for {
resp := o.Websocket.Conn.ReadMessage()
if resp.Raw == nil {
return
}
err := o.WsHandleData(resp.Raw)
if err != nil {
o.Websocket.DataHandler <- err
}
}
}
// WsHandleData will read websocket raw data and pass to appropriate handler
func (o *OKGroup) WsHandleData(respRaw []byte) error {
var dataResponse WebsocketDataResponse
err := json.Unmarshal(respRaw, &dataResponse)
if err != nil {
return err
}
if len(dataResponse.Data) > 0 {
switch o.GetWsChannelWithoutOrderType(dataResponse.Table) {
case okGroupWsCandle60s, okGroupWsCandle180s, okGroupWsCandle300s,
okGroupWsCandle900s, okGroupWsCandle1800s, okGroupWsCandle3600s,
okGroupWsCandle7200s, okGroupWsCandle14400s, okGroupWsCandle21600s,
okGroupWsCandle43200s, okGroupWsCandle86400s, okGroupWsCandle604900s:
return o.wsProcessCandles(respRaw)
case okGroupWsDepth, okGroupWsDepth5:
return o.WsProcessOrderBook(respRaw)
case okGroupWsTicker:
return o.wsProcessTickers(respRaw)
case okGroupWsTrade:
return o.wsProcessTrades(respRaw)
case okGroupWsOrder:
return o.wsProcessOrder(respRaw)
}
o.Websocket.DataHandler <- stream.UnhandledMessageWarning{
Message: o.Name + stream.UnhandledMessage + string(respRaw),
}
return nil
}
var errorResponse WebsocketErrorResponse
err = json.Unmarshal(respRaw, &errorResponse)
if err == nil && errorResponse.ErrorCode > 0 {
return fmt.Errorf("%v error - %v message: %s ",
o.Name,
errorResponse.ErrorCode,
errorResponse.Message)
}
var eventResponse WebsocketEventResponse
err = json.Unmarshal(respRaw, &eventResponse)
if err == nil && eventResponse.Event != "" {
if eventResponse.Event == "login" {
if o.Websocket.Match.Incoming("login") {
o.Websocket.SetCanUseAuthenticatedEndpoints(eventResponse.Success)
}
}
if o.Verbose {
log.Debug(log.ExchangeSys,
o.Name+" - "+eventResponse.Event+" on channel: "+eventResponse.Channel)
}
}
return nil
}
// StringToOrderStatus converts order status IDs to internal types
func StringToOrderStatus(num int64) (order.Status, error) {
switch num {
case -2:
return order.Rejected, nil
case -1:
return order.Cancelled, nil
case 0:
return order.Active, nil
case 1:
return order.PartiallyFilled, nil
case 2:
return order.Filled, nil
case 3:
return order.New, nil
case 4:
return order.PendingCancel, nil
default:
return order.UnknownStatus, fmt.Errorf("%v not recognised as order status", num)
}
}
func (o *OKGroup) wsProcessOrder(respRaw []byte) error {
var resp WebsocketSpotOrderResponse
err := json.Unmarshal(respRaw, &resp)
if err != nil {
return err
}
for i := range resp.Data {
var oType order.Type
var oSide order.Side
var oStatus order.Status
oType, err = order.StringToOrderType(resp.Data[i].Type)
if err != nil {
o.Websocket.DataHandler <- order.ClassificationError{
Exchange: o.Name,
OrderID: resp.Data[i].OrderID,
Err: err,
}
}
oSide, err = order.StringToOrderSide(resp.Data[i].Side)
if err != nil {
o.Websocket.DataHandler <- order.ClassificationError{
Exchange: o.Name,
OrderID: resp.Data[i].OrderID,
Err: err,
}
}
oStatus, err = StringToOrderStatus(resp.Data[i].State)
if err != nil {
o.Websocket.DataHandler <- order.ClassificationError{
Exchange: o.Name,
OrderID: resp.Data[i].OrderID,
Err: err,
}
}
pair, err := currency.NewPairFromString(resp.Data[i].InstrumentID)
if err != nil {
o.Websocket.DataHandler <- order.ClassificationError{
Exchange: o.Name,
OrderID: resp.Data[i].OrderID,
Err: err,
}
}
o.Websocket.DataHandler <- &order.Detail{
ImmediateOrCancel: resp.Data[i].OrderType == 3,
FillOrKill: resp.Data[i].OrderType == 2,
PostOnly: resp.Data[i].OrderType == 1,
Price: resp.Data[i].Price,
Amount: resp.Data[i].Size,
ExecutedAmount: resp.Data[i].LastFillQty,
RemainingAmount: resp.Data[i].Size - resp.Data[i].LastFillQty,
Exchange: o.Name,
ID: resp.Data[i].OrderID,
Type: oType,
Side: oSide,
Status: oStatus,
AssetType: o.GetAssetTypeFromTableName(resp.Table),
Date: resp.Data[i].CreatedAt,
Pair: pair,
}
}
return nil
}
// wsProcessTickers converts ticker data and sends it to the datahandler
func (o *OKGroup) wsProcessTickers(respRaw []byte) error {
var response WebsocketTickerData
err := json.Unmarshal(respRaw, &response)
if err != nil {
return err
}
a := o.GetAssetTypeFromTableName(response.Table)
for i := range response.Data {
f := strings.Split(response.Data[i].InstrumentID, delimiterDash)
var c currency.Pair
switch a {
case asset.Futures, asset.PerpetualSwap:
c = currency.NewPairWithDelimiter(f[0]+delimiterDash+f[1],
f[2],
delimiterUnderscore)
default:
c = currency.NewPairWithDelimiter(f[0], f[1], delimiterDash)
}
baseVolume := response.Data[i].BaseVolume24h
if response.Data[i].ContractVolume24h != 0 {
baseVolume = response.Data[i].ContractVolume24h
}
quoteVolume := response.Data[i].QuoteVolume24h
if response.Data[i].TokenVolume24h != 0 {
quoteVolume = response.Data[i].TokenVolume24h
}
o.Websocket.DataHandler <- &ticker.Price{
ExchangeName: o.Name,
Open: response.Data[i].Open24h,
Close: response.Data[i].Last,
Volume: baseVolume,
QuoteVolume: quoteVolume,
High: response.Data[i].High24h,
Low: response.Data[i].Low24h,
Bid: response.Data[i].BestBid,
Ask: response.Data[i].BestAsk,
Last: response.Data[i].Last,
AssetType: o.GetAssetTypeFromTableName(response.Table),
Pair: c,
LastUpdated: response.Data[i].Timestamp,
}
}
return nil
}
// wsProcessTrades converts trade data and sends it to the datahandler
func (o *OKGroup) wsProcessTrades(respRaw []byte) error {
var response WebsocketTradeResponse
err := json.Unmarshal(respRaw, &response)
if err != nil {
return err
}
a := o.GetAssetTypeFromTableName(response.Table)
for i := range response.Data {
f := strings.Split(response.Data[i].InstrumentID, delimiterDash)
var c currency.Pair
switch a {
case asset.Futures, asset.PerpetualSwap:
c = currency.NewPairWithDelimiter(f[0]+delimiterDash+f[1],
f[2],
delimiterUnderscore)
default:
c = currency.NewPairWithDelimiter(f[0], f[1], delimiterDash)
}
tSide, err := order.StringToOrderSide(response.Data[i].Side)
if err != nil {
o.Websocket.DataHandler <- order.ClassificationError{
Exchange: o.Name,
Err: err,
}
}
amount := response.Data[i].Size
if response.Data[i].Quantity != 0 {
amount = response.Data[i].Quantity
}
o.Websocket.DataHandler <- stream.TradeData{
Amount: amount,
AssetType: o.GetAssetTypeFromTableName(response.Table),
CurrencyPair: c,
Exchange: o.Name,
Price: response.Data[i].Price,
Side: tSide,
Timestamp: response.Data[i].Timestamp,
}
}
return nil
}
// wsProcessCandles converts candle data and sends it to the data handler
func (o *OKGroup) wsProcessCandles(respRaw []byte) error {
var response WebsocketCandleResponse
err := json.Unmarshal(respRaw, &response)
if err != nil {
return err
}
a := o.GetAssetTypeFromTableName(response.Table)
for i := range response.Data {
f := strings.Split(response.Data[i].InstrumentID, delimiterDash)
var c currency.Pair
switch a {
case asset.Futures, asset.PerpetualSwap:
c = currency.NewPairWithDelimiter(f[0]+delimiterDash+f[1],
f[2],
delimiterUnderscore)
default:
c = currency.NewPairWithDelimiter(f[0], f[1], delimiterDash)
}
timeData, err := time.Parse(time.RFC3339Nano,
response.Data[i].Candle[0])
if err != nil {
return fmt.Errorf("%v Time data could not be parsed: %v",
o.Name,
response.Data[i].Candle[0])
}
candleIndex := strings.LastIndex(response.Table, okGroupWsCandle)
candleInterval := response.Table[candleIndex+len(okGroupWsCandle):]
klineData := stream.KlineData{
AssetType: o.GetAssetTypeFromTableName(response.Table),
Pair: c,
Exchange: o.Name,
Timestamp: timeData,
Interval: candleInterval,
}
klineData.OpenPrice, err = strconv.ParseFloat(response.Data[i].Candle[1], 64)
if err != nil {
return err
}
klineData.HighPrice, err = strconv.ParseFloat(response.Data[i].Candle[2], 64)
if err != nil {
return err
}
klineData.LowPrice, err = strconv.ParseFloat(response.Data[i].Candle[3], 64)
if err != nil {
return err
}
klineData.ClosePrice, err = strconv.ParseFloat(response.Data[i].Candle[4], 64)
if err != nil {
return err
}
klineData.Volume, err = strconv.ParseFloat(response.Data[i].Candle[5], 64)
if err != nil {
return err
}
o.Websocket.DataHandler <- klineData
}
return nil
}
// WsProcessOrderBook Validates the checksum and updates internal orderbook values
func (o *OKGroup) WsProcessOrderBook(respRaw []byte) error {
var response WebsocketOrderBooksData
err := json.Unmarshal(respRaw, &response)
if err != nil {
return err
}
orderbookMutex.Lock()
defer orderbookMutex.Unlock()
a := o.GetAssetTypeFromTableName(response.Table)
for i := range response.Data {
f := strings.Split(response.Data[i].InstrumentID, delimiterDash)
var c currency.Pair
switch a {
case asset.Futures, asset.PerpetualSwap:
c = currency.NewPairWithDelimiter(f[0]+delimiterDash+f[1],
f[2],
delimiterUnderscore)
default:
c = currency.NewPairWithDelimiter(f[0], f[1], delimiterDash)
}
if response.Action == okGroupWsOrderbookPartial {
err := o.WsProcessPartialOrderBook(&response.Data[i], c, a)
if err != nil {
err2 := o.wsResubscribeToOrderbook(&response)
if err2 != nil {
o.Websocket.DataHandler <- err2
}
return err
}
} else if response.Action == okGroupWsOrderbookUpdate {
if len(response.Data[i].Asks) == 0 && len(response.Data[i].Bids) == 0 {
return nil
}
err := o.WsProcessUpdateOrderbook(&response.Data[i], c, a)
if err != nil {
err2 := o.wsResubscribeToOrderbook(&response)
if err2 != nil {
o.Websocket.DataHandler <- err2
}
return err
}
}
}
return nil
}
func (o *OKGroup) wsResubscribeToOrderbook(response *WebsocketOrderBooksData) error {
a := o.GetAssetTypeFromTableName(response.Table)
for i := range response.Data {
f := strings.Split(response.Data[i].InstrumentID, delimiterDash)
var c currency.Pair
switch a {
case asset.Futures, asset.PerpetualSwap:
c = currency.NewPairWithDelimiter(f[0]+delimiterDash+f[1], f[2], delimiterDash)
default:
c = currency.NewPairWithDelimiter(f[0], f[1], delimiterDash)
}
channelToResubscribe := &stream.ChannelSubscription{
Channel: response.Table,
Currency: c,
Asset: a,
}
err := o.Websocket.ResubscribeToChannel(channelToResubscribe)
if err != nil {
return fmt.Errorf("%s resubscribe to orderbook error %s", o.Name, err)
}
}
return nil
}
// AppendWsOrderbookItems adds websocket orderbook data bid/asks into an
// orderbook item array
func (o *OKGroup) AppendWsOrderbookItems(entries [][]interface{}) ([]orderbook.Item, error) {
var items []orderbook.Item
for j := range entries {
amount, err := strconv.ParseFloat(entries[j][1].(string), 64)
if err != nil {
return nil, err
}
price, err := strconv.ParseFloat(entries[j][0].(string), 64)
if err != nil {
return nil, err
}
items = append(items, orderbook.Item{Amount: amount, Price: price})
}
return items, nil
}
// WsProcessPartialOrderBook takes websocket orderbook data and creates an
// orderbook Calculates checksum to ensure it is valid
func (o *OKGroup) WsProcessPartialOrderBook(wsEventData *WebsocketOrderBook, instrument currency.Pair, a asset.Item) error {
signedChecksum := o.CalculatePartialOrderbookChecksum(wsEventData)
if signedChecksum != wsEventData.Checksum {
return fmt.Errorf("%s channel: %s. Orderbook partial for %v checksum invalid",
o.Name,
a,
instrument)
}
if o.Verbose {
log.Debugf(log.ExchangeSys,
"%s passed checksum for instrument %s",
o.Name,
instrument)
}
asks, err := o.AppendWsOrderbookItems(wsEventData.Asks)
if err != nil {
return err
}
bids, err := o.AppendWsOrderbookItems(wsEventData.Bids)
if err != nil {
return err
}
newOrderBook := orderbook.Base{
Asks: asks,
Bids: bids,
AssetType: a,
LastUpdated: wsEventData.Timestamp,
Pair: instrument,
ExchangeName: o.Name,
}
return o.Websocket.Orderbook.LoadSnapshot(&newOrderBook)
}
// WsProcessUpdateOrderbook updates an existing orderbook using websocket data
// After merging WS data, it will sort, validate and finally update the existing
// orderbook
func (o *OKGroup) WsProcessUpdateOrderbook(wsEventData *WebsocketOrderBook, instrument currency.Pair, a asset.Item) error {
update := buffer.Update{
Asset: a,
Pair: instrument,
UpdateTime: wsEventData.Timestamp,
}
var err error
update.Asks, err = o.AppendWsOrderbookItems(wsEventData.Asks)
if err != nil {
return err
}
update.Bids, err = o.AppendWsOrderbookItems(wsEventData.Bids)
if err != nil {
return err
}
err = o.Websocket.Orderbook.Update(&update)
if err != nil {
return err
}
updatedOb := o.Websocket.Orderbook.GetOrderbook(instrument, a)
checksum := o.CalculateUpdateOrderbookChecksum(updatedOb)
if checksum != wsEventData.Checksum {
// re-sub
log.Warnf(log.ExchangeSys, "%s checksum failure for item %s",
o.Name,
wsEventData.InstrumentID)
return errors.New("checksum failed")
}
return nil
}
// CalculatePartialOrderbookChecksum alternates over the first 25 bid and ask
// entries from websocket data. The checksum is made up of the price and the
// quantity with a semicolon (:) deliminating them. This will also work when
// there are less than 25 entries (for whatever reason)
// eg Bid:Ask:Bid:Ask:Ask:Ask
func (o *OKGroup) CalculatePartialOrderbookChecksum(orderbookData *WebsocketOrderBook) int32 {
var checksum strings.Builder
for i := 0; i < allowableIterations; i++ {
if len(orderbookData.Bids)-1 >= i {
checksum.WriteString(orderbookData.Bids[i][0].(string) +
delimiterColon +
orderbookData.Bids[i][1].(string) +
delimiterColon)
}
if len(orderbookData.Asks)-1 >= i {
checksum.WriteString(orderbookData.Asks[i][0].(string) +
delimiterColon +
orderbookData.Asks[i][1].(string) +
delimiterColon)
}
}
checksumStr := strings.TrimSuffix(checksum.String(), delimiterColon)
return int32(crc32.ChecksumIEEE([]byte(checksumStr)))
}
// CalculateUpdateOrderbookChecksum alternates over the first 25 bid and ask
// entries of a merged orderbook. The checksum is made up of the price and the
// quantity with a semicolon (:) deliminating them. This will also work when
// there are less than 25 entries (for whatever reason)
// eg Bid:Ask:Bid:Ask:Ask:Ask
func (o *OKGroup) CalculateUpdateOrderbookChecksum(orderbookData *orderbook.Base) int32 {
var checksum strings.Builder
for i := 0; i < allowableIterations; i++ {
if len(orderbookData.Bids)-1 >= i {
price := strconv.FormatFloat(orderbookData.Bids[i].Price, 'f', -1, 64)
amount := strconv.FormatFloat(orderbookData.Bids[i].Amount, 'f', -1, 64)
checksum.WriteString(price + delimiterColon + amount + delimiterColon)
}
if len(orderbookData.Asks)-1 >= i {
price := strconv.FormatFloat(orderbookData.Asks[i].Price, 'f', -1, 64)
amount := strconv.FormatFloat(orderbookData.Asks[i].Amount, 'f', -1, 64)
checksum.WriteString(price + delimiterColon + amount + delimiterColon)
}
}
checksumStr := strings.TrimSuffix(checksum.String(), delimiterColon)
return int32(crc32.ChecksumIEEE([]byte(checksumStr)))
}
// GenerateDefaultSubscriptions Adds default subscriptions to websocket to be
// handled by ManageSubscriptions()
func (o *OKGroup) GenerateDefaultSubscriptions() ([]stream.ChannelSubscription, error) {
var subscriptions []stream.ChannelSubscription
assets := o.GetAssetTypes()
for x := range assets {
pairs, err := o.GetEnabledPairs(assets[x])
if err != nil {
return nil, err
}
switch assets[x] {
case asset.Spot:
channels := defaultSpotSubscribedChannels
if o.GetAuthenticatedAPISupport(exchange.WebsocketAuthentication) {
channels = append(channels,
okGroupWsSpotMarginAccount,
okGroupWsSpotAccount,
okGroupWsSpotOrder)
}
for i := range pairs {
p, err := o.FormatExchangeCurrency(pairs[i], asset.Spot)
if err != nil {
return nil, err
}
for y := range channels {
subscriptions = append(subscriptions,
stream.ChannelSubscription{
Channel: channels[y],
Currency: p,
Asset: asset.Spot,
})
}
}
case asset.Futures:
channels := defaultFuturesSubscribedChannels
if o.GetAuthenticatedAPISupport(exchange.WebsocketAuthentication) {
channels = append(channels,
okGroupWsFuturesAccount,
okGroupWsFuturesPosition,
okGroupWsFuturesOrder)
}
var futuresAccountPairs currency.Pairs
var futuresAccountCodes currency.Currencies
for i := range pairs {
p, err := o.FormatExchangeCurrency(pairs[i], asset.Futures)
if err != nil {
return nil, err
}
for y := range channels {
if channels[y] == okGroupWsFuturesAccount {
currencyString := strings.Split(pairs[i].String(),
currency.UnderscoreDelimiter)[0]
newP, err := currency.NewPairFromString(currencyString)
if err != nil {
return nil, err
}
if !futuresAccountCodes.Contains(newP.Base) {
// subscribe to coin-margin futures trading mode
subscriptions = append(subscriptions,
stream.ChannelSubscription{
Channel: channels[y],
Currency: currency.NewPair(newP.Base, currency.Code{}),
Asset: asset.Futures,
})
futuresAccountCodes = append(futuresAccountCodes, newP.Base)
}
if newP.Quote != currency.USDT {
// Only allows subscription to USDT margined pair
continue
}
if !futuresAccountPairs.Contains(newP, true) {
subscriptions = append(subscriptions,
stream.ChannelSubscription{
Channel: channels[y],
Currency: newP,
Asset: asset.Futures,
})
futuresAccountPairs = futuresAccountPairs.Add(newP)
}
continue
}
subscriptions = append(subscriptions,
stream.ChannelSubscription{
Channel: channels[y],
Currency: p,
Asset: asset.Futures,
})
}
}
case asset.PerpetualSwap:
channels := defaultSwapSubscribedChannels
if o.GetAuthenticatedAPISupport(exchange.WebsocketAuthentication) {
channels = append(channels,
okGroupWsSwapAccount,
okGroupWsSwapPosition,
okGroupWsSwapOrder)
}
for i := range pairs {
p, err := o.FormatExchangeCurrency(pairs[i], asset.PerpetualSwap)
if err != nil {
return nil, err
}
for y := range channels {
subscriptions = append(subscriptions,
stream.ChannelSubscription{
Channel: channels[y],
Currency: p,
Asset: asset.PerpetualSwap,
})
}
}
case asset.Index:
for i := range pairs {
p, err := o.FormatExchangeCurrency(pairs[i], asset.Index)
if err != nil {
return nil, err
}
for y := range defaultIndexSubscribedChannels {
subscriptions = append(subscriptions,
stream.ChannelSubscription{
Channel: defaultIndexSubscribedChannels[y],
Currency: p,
Asset: asset.Index,
})
}
}
default:
o.Websocket.DataHandler <- errors.New("unhandled asset type")
}
}
return subscriptions, nil
}
// Subscribe sends a websocket message to receive data from the channel
func (o *OKGroup) Subscribe(channelsToSubscribe []stream.ChannelSubscription) error {
return o.handleSubscriptions("subscribe", channelsToSubscribe)
}
// Unsubscribe sends a websocket message to stop receiving data from the channel
func (o *OKGroup) Unsubscribe(channelsToUnsubscribe []stream.ChannelSubscription) error {
return o.handleSubscriptions("unsubscribe", channelsToUnsubscribe)
}
func (o *OKGroup) handleSubscriptions(operation string, subs []stream.ChannelSubscription) error {
request := WebsocketEventRequest{
Operation: operation,
}
var channels []stream.ChannelSubscription
for i := 0; i < len(subs); i++ {
// Temp type to evaluate max byte len after a marshal on batched unsubs
temp := WebsocketEventRequest{
Operation: operation,
}
temp.Arguments = make([]string, len(request.Arguments))
copy(temp.Arguments, request.Arguments)
arg := subs[i].Channel + delimiterColon
if strings.EqualFold(subs[i].Channel, okGroupWsSpotAccount) {
arg += subs[i].Currency.Base.String()
} else {
arg += subs[i].Currency.String()
}
temp.Arguments = append(temp.Arguments, arg)
chunk, err := json.Marshal(request)
if err != nil {
return err
}
if len(chunk) > maxConnByteLen {
// If temp chunk exceeds max byte length determined by the exchange,
// commit last payload.
i-- // reverse position in range to reuse channel unsubscription on
// next iteration
err = o.Websocket.Conn.SendJSONMessage(request)
if err != nil {
return err
}
if operation == "unsubscribe" {
o.Websocket.RemoveSuccessfulUnsubscriptions(channels...)
} else {
o.Websocket.AddSuccessfulSubscriptions(channels...)
}
// Drop prior unsubs and chunked payload args on successful unsubscription
channels = nil
request.Arguments = nil
continue
}
// Add pending chained items
channels = append(channels, subs[i])
request.Arguments = temp.Arguments
}
// Commit left overs to payload
err := o.Websocket.Conn.SendJSONMessage(request)
if err != nil {
return err
}
if operation == "unsubscribe" {
o.Websocket.RemoveSuccessfulUnsubscriptions(channels...)
} else {
o.Websocket.AddSuccessfulSubscriptions(channels...)
}
return nil
}
// GetWsChannelWithoutOrderType takes WebsocketDataResponse.Table and returns
// The base channel name eg receive "spot/depth5:BTC-USDT" return "depth5"
func (o *OKGroup) GetWsChannelWithoutOrderType(table string) string {
index := strings.Index(table, "/")
if index == -1 {
return table
}
channel := table[index+1:]
index = strings.Index(channel, ":")
// Some events do not contain a currency
if index == -1 {
return channel
}
return channel[:index]
}
// GetAssetTypeFromTableName gets the asset type from the table name
// eg "spot/ticker:BTCUSD" results in "SPOT"
func (o *OKGroup) GetAssetTypeFromTableName(table string) asset.Item {
assetIndex := strings.Index(table, "/")
switch table[:assetIndex] {
case asset.Futures.String():
return asset.Futures
case asset.Spot.String():
return asset.Spot
case "swap":
return asset.PerpetualSwap
case asset.Index.String():
return asset.Index
default:
log.Warnf(log.ExchangeSys, "%s unhandled asset type %s",
o.Name,
table[:assetIndex])
return asset.Item(table[:assetIndex])
}
}