From 5ea82f163addd200431277d6929f2ffe3d629c2c Mon Sep 17 00:00:00 2001 From: Adrian Gallagher Date: Thu, 2 May 2019 12:10:29 +1000 Subject: [PATCH] OKGroup: Fix unmarshalling issues after API update (#288) --- exchanges/okex/okex.go | 62 +++++++++++++++++++++++++++++- exchanges/okgroup/okgroup_types.go | 24 ++++++++---- 2 files changed, 76 insertions(+), 10 deletions(-) diff --git a/exchanges/okex/okex.go b/exchanges/okex/okex.go index 7ad048fd..8e697d74 100644 --- a/exchanges/okex/okex.go +++ b/exchanges/okex/okex.go @@ -3,6 +3,7 @@ package okex import ( "fmt" "net/http" + "strconv" "time" "github.com/thrasher-/gocryptotrader/common" @@ -176,9 +177,66 @@ func (o *OKEX) GetFuturesContractInformation() (resp []okgroup.GetFuturesContrac } // GetFuturesOrderBook List all contracts. This request does not support pagination. The full list will be returned for a request. -func (o *OKEX) GetFuturesOrderBook(request okgroup.GetFuturesOrderBookRequest) (resp okgroup.GetFuturesOrderBookResponse, _ error) { +func (o *OKEX) GetFuturesOrderBook(request okgroup.GetFuturesOrderBookRequest) (resp okgroup.GetFuturesOrderBookResponse, err error) { requestURL := fmt.Sprintf("%v/%v/%v%v", okgroup.OKGroupInstruments, request.InstrumentID, okgroup.OKGroupGetSpotOrderBook, okgroup.FormatParameters(request)) - return resp, o.SendHTTPRequest(http.MethodGet, okGroupFuturesSubsection, requestURL, nil, &resp, true) + + type tempOB struct { + Bids [][]string `json:"bids"` + Asks [][]string `json:"asks"` + Timestamp time.Time `json:"timestamp"` + } + + var tmpOB tempOB + err = o.SendHTTPRequest(http.MethodGet, okGroupFuturesSubsection, requestURL, nil, &tmpOB, true) + if err != nil { + return resp, err + } + + processOB := func(ob [][]string) ([]okgroup.FuturesOrderbookItem, error) { + var processedOB []okgroup.FuturesOrderbookItem + for x := range ob { + price, convErr := strconv.ParseFloat(ob[x][0], 64) + if err != nil { + return nil, convErr + } + + size, convErr := strconv.ParseInt(ob[x][1], 10, 64) + if err != nil { + return nil, convErr + } + + liqOrders, convErr := strconv.ParseInt(ob[x][2], 10, 64) + if err != nil { + return nil, convErr + } + + numOrders, convErr := strconv.ParseInt(ob[x][3], 10, 64) + if err != nil { + return nil, convErr + } + + processedOB = append(processedOB, okgroup.FuturesOrderbookItem{ + Price: price, + Size: size, + ForceLiquidatedOrders: liqOrders, + NumberOrders: numOrders, + }) + } + return processedOB, nil + } + + resp.Bids, err = processOB(tmpOB.Bids) + if err != nil { + return + } + + resp.Asks, err = processOB(tmpOB.Asks) + if err != nil { + return + } + + resp.Timestamp = tmpOB.Timestamp + return resp, nil } // GetAllFuturesTokenInfo Get the last traded price, best bid/ask price, 24 hour trading volume and more info of all contracts. diff --git a/exchanges/okgroup/okgroup_types.go b/exchanges/okgroup/okgroup_types.go index 2811e3fe..4fb08915 100644 --- a/exchanges/okgroup/okgroup_types.go +++ b/exchanges/okgroup/okgroup_types.go @@ -682,11 +682,19 @@ type GetFuturesOrderBookRequest struct { Size int64 `url:"size,omitempty"` // [optional] The size of the price range (max: 200) } +// FuturesOrderbookItem stores an individual futures orderbook item +type FuturesOrderbookItem struct { + Price float64 + Size int64 + ForceLiquidatedOrders int64 // Number of force liquidated orders + NumberOrders int64 // Number of orders on the price +} + // GetFuturesOrderBookResponse response data for GetFuturesOrderBook type GetFuturesOrderBookResponse struct { - Asks [][]float64 `json:"asks"` // [[0: Price, 1: Size price, 2: number of force liquidated orders, 3: number of orders on the price]] - Bids [][]float64 `json:"bids"` // [[0: Price, 1: Size price, 2: number of force liquidated orders, 3: number of orders on the price]] - Timestamp time.Time `json:"timestamp"` + Asks []FuturesOrderbookItem + Bids []FuturesOrderbookItem + Timestamp time.Time } // GetFuturesTokenInfoResponse response data for GetFuturesOrderBook @@ -782,7 +790,7 @@ type GetFuturesCurrentPriceLimitResponse struct { // GetFuturesCurrentMarkPriceResponse response data for GetFuturesCurrentMarkPrice type GetFuturesCurrentMarkPriceResponse struct { - MarkPrice float64 `json:"mark_price"` + MarkPrice float64 `json:"mark_price,string"` InstrumentID string `json:"instrument_id"` Timestamp time.Time `json:"timestamp"` } @@ -798,12 +806,12 @@ type GetFuturesForceLiquidatedOrdersRequest struct { // GetFuturesForceLiquidatedOrdersResponse response data for GetFuturesForceLiquidatedOrders type GetFuturesForceLiquidatedOrdersResponse struct { - Loss float64 `json:"loss"` - Size int64 `json:"size"` - Price float64 `json:"price"` + Loss float64 `json:"loss,string"` + Size int64 `json:"size,string"` + Price float64 `json:"price,string"` CreatedAt string `json:"created_at"` InstrumentID string `json:"instrument_id"` - Type int64 `json:"type"` + Type int64 `json:"type,string"` } // GetFuturesTagPriceResponse response data for GetFuturesTagPrice