diff --git a/exchanges/huobi/huobi.go b/exchanges/huobi/huobi.go index 18dcd1aa..e57699e2 100644 --- a/exchanges/huobi/huobi.go +++ b/exchanges/huobi/huobi.go @@ -176,7 +176,7 @@ func (h *HUOBI) GetTrades(symbol string) ([]Trade, error) { type response struct { Response - tick struct { + Tick struct { Data []Trade `json:"data"` } `json:"tick"` } @@ -188,7 +188,7 @@ func (h *HUOBI) GetTrades(symbol string) ([]Trade, error) { if result.ErrorMessage != "" { return nil, errors.New(result.ErrorMessage) } - return result.tick.Data, err + return result.Tick.Data, err } // GetTradeHistory returns the trades for the specified symbol @@ -222,7 +222,7 @@ func (h *HUOBI) GetMarketDetail(symbol string) (Detail, error) { type response struct { Response - tick Detail `json:"tick"` + Tick Detail `json:"tick"` } var result response @@ -230,9 +230,9 @@ func (h *HUOBI) GetMarketDetail(symbol string) (Detail, error) { err := common.SendHTTPGetRequest(common.EncodeURLValues(url, vals), true, h.Verbose, &result) if result.ErrorMessage != "" { - return result.tick, errors.New(result.ErrorMessage) + return result.Tick, errors.New(result.ErrorMessage) } - return result.tick, err + return result.Tick, err } // GetSymbols returns an array of symbols supported by Huobi diff --git a/exchanges/huobi/huobi_websocket.go b/exchanges/huobi/huobi_websocket.go index 1120afc8..affe3825 100644 --- a/exchanges/huobi/huobi_websocket.go +++ b/exchanges/huobi/huobi_websocket.go @@ -8,39 +8,41 @@ import ( ) const ( - HUOBI_SOCKETIO_ADDRESS = "https://hq.huobi.com:443" + huobiSocketIOAddress = "https://hq.huobi.com:443" //Service API - HUOBI_SOCKET_REQ_SYMBOL_LIST = "reqSymbolList" - HUOBI_SOCKET_REQ_SYMBOL_DETAIL = "reqSymbolDetail" - HUOBI_SOCKET_REQ_SUBSCRIBE = "reqMsgSubscribe" - HUOBI_SOCKET_REQ_UNSUBSCRIBE = "reqMsgUnsubscribe" + huobiSocketReqSymbolList = "reqSymbolList" + huobiSocketReqSymbolDetail = "reqSymbolDetail" + huobiSocketReqSubscribe = "reqMsgSubscribe" + huobiSocketReqUnsubscribe = "reqMsgUnsubscribe" // Market data API - HUOBI_SOCKET_MARKET_DETAIL = "marketDetail" - HUOBI_SOCKET_TRADE_DETAIL = "tradeDetail" - HUOBI_SOCKET_MARKET_DEPTH_TOP = "marketDepthTop" - HUOBI_SOCKET_MARKET_DEPTH_TOP_SHORT = "marketDepthTopShort" - HUOBI_SOCKET_MARKET_DEPTH = "marketDepth" - HUOBI_SOCKET_MARKET_DEPTH_TOP_DIFF = "marketDepthTopDiff" - HUOBI_SOCKET_MARKET_DEPTH_DIFF = "marketDepthDiff" - HUOBI_SOCKET_MARKET_LAST_KLINE = "lastKLine" - HUOBI_SOCKET_MARKET_LAST_TIMELINE = "lastTimeLine" - HUOBI_SOCKET_MARKET_OVERVIEW = "marketOverview" - HUOBI_SOCKET_MARKET_STATIC = "marketStatic" + huobiSocketMarketDetail = "marketDetail" + huobiSocketTradeDetail = "tradeDetail" + huobiSocketMarketDepthTop = "marketDepthTop" + huobiSocketMarketDepthTopShort = "marketDepthTopShort" + huobiSocketMarketDepth = "marketDepth" + huobiSocketMarketDepthTopDiff = "marketDepthTopDiff" + huobiSocketMarketDepthDiff = "marketDepthDiff" + huobiSocketMarketLastKline = "lastKLine" + huobiSocketMarketLastTimeline = "lastTimeLine" + huobiSocketMarketOverview = "marketOverview" + huobiSocketMarketStatic = "marketStatic" // History data API - HUOBI_SOCKET_REQ_TIMELINE = "reqTimeLine" - HUOBI_SOCKET_REQ_KLINE = "reqKLine" - HUOBI_SOCKET_REQ_DEPTH_TOP = "reqMarketDepthTop" - HUOBI_SOCKET_REQ_DEPTH = "reqMarketDepth" - HUOBI_SOCKET_REQ_TRADE_DETAIL_TOP = "reqTradeDetailTop" - HUOBI_SOCKET_REQ_MARKET_DETAIL = "reqMarketDetail" + huobiSocketReqTimeline = "reqTimeLine" + huobiSocketReqKline = "reqKLine" + huobiSocketReqDepthTop = "reqMarketDepthTop" + huobiSocketReqDepth = "reqMarketDepth" + huobiSocketReqTradeDetailTop = "reqTradeDetailTop" + huobiSocketReqMarketDetail = "reqMarketDetail" ) +// HuobiSocket is a pointer to a IO Socket var HuobiSocket *socketio.SocketIO -type HuobiDepth struct { +// Depth holds depth information +type Depth struct { SymbolID string `json:"symbolId"` Time float64 `json:"time"` Version float64 `json:"version"` @@ -54,24 +56,27 @@ type HuobiDepth struct { AskAmount []float64 `json:"askAmount"` } -type HuobiWebsocketTrade struct { +// WebsocketTrade holds full trade data +type WebsocketTrade struct { Price []float64 `json:"price"` Level []float64 `json:"level"` Amount []float64 `json:"amount"` AccuAmount []float64 `json:"accuAmount"` } -type HuobiWebsocketTradeDetail struct { - SymbolID string `json:"symbolId"` - TradeID []int64 `json:"tradeId"` - Price []float64 `json:"price"` - Time []int64 `json:"time"` - Amount []float64 `json:"amount"` - TopBids []HuobiWebsocketTrade `json:"topBids"` - TopAsks []HuobiWebsocketTrade `json:"topAsks"` +// WebsocketTradeDetail holds specific trade details +type WebsocketTradeDetail struct { + SymbolID string `json:"symbolId"` + TradeID []int64 `json:"tradeId"` + Price []float64 `json:"price"` + Time []int64 `json:"time"` + Amount []float64 `json:"amount"` + TopBids []WebsocketTrade `json:"topBids"` + TopAsks []WebsocketTrade `json:"topAsks"` } -type HuobiWebsocketMarketOverview struct { +// WebsocketMarketOverview holds market overview data +type WebsocketMarketOverview struct { SymbolID string `json:"symbolId"` Last float64 `json:"priceNew"` Open float64 `json:"priceOpen"` @@ -83,7 +88,8 @@ type HuobiWebsocketMarketOverview struct { TotalAmount float64 `json:"totalAmount"` } -type HuobiWebsocketLastTimeline struct { +// WebsocketLastTimeline holds timeline data +type WebsocketLastTimeline struct { ID int64 `json:"_id"` SymbolID string `json:"symbolId"` Time int64 `json:"time"` @@ -93,7 +99,8 @@ type HuobiWebsocketLastTimeline struct { Count int64 `json:"count"` } -type HuobiResponse struct { +// WebsocketResponse is a general response type for websocket +type WebsocketResponse struct { Version int `json:"version"` MsgType string `json:"msgType"` RequestIndex int64 `json:"requestIndex"` @@ -102,6 +109,7 @@ type HuobiResponse struct { Payload map[string]interface{} `json:"payload"` } +// BuildHuobiWebsocketRequest packages a new request func (h *HUOBI) BuildHuobiWebsocketRequest(msgType string, requestIndex int64, symbolRequest []string) map[string]interface{} { request := map[string]interface{}{} request["version"] = 1 @@ -118,7 +126,8 @@ func (h *HUOBI) BuildHuobiWebsocketRequest(msgType string, requestIndex int64, s return request } -func (h *HUOBI) BuildHuobiWebsocketRequestExtra(msgType string, requestIndex int64, symbolIdList interface{}) interface{} { +// BuildHuobiWebsocketRequestExtra packages an extra request +func (h *HUOBI) BuildHuobiWebsocketRequestExtra(msgType string, requestIndex int64, symbolIDList interface{}) interface{} { request := map[string]interface{}{} request["version"] = 1 request["msgType"] = msgType @@ -127,10 +136,11 @@ func (h *HUOBI) BuildHuobiWebsocketRequestExtra(msgType string, requestIndex int request["requestIndex"] = requestIndex } - request["symbolList"] = symbolIdList + request["symbolList"] = symbolIDList return request } +// BuildHuobiWebsocketParamsList packages a parameter list func (h *HUOBI) BuildHuobiWebsocketParamsList(objectName, currency, pushType, period, count, from, to, percentage string) interface{} { list := map[string]interface{}{} list["symbolId"] = currency @@ -160,6 +170,7 @@ func (h *HUOBI) BuildHuobiWebsocketParamsList(objectName, currency, pushType, pe return listCompleted } +// OnConnect handles connection establishment func (h *HUOBI) OnConnect(output chan socketio.Message) { if h.Verbose { log.Printf("%s Connected to Websocket.", h.GetName()) @@ -167,7 +178,7 @@ func (h *HUOBI) OnConnect(output chan socketio.Message) { for _, x := range h.EnabledPairs { currency := common.StringToLower(x) - msg := h.BuildHuobiWebsocketRequestExtra(HUOBI_SOCKET_REQ_SUBSCRIBE, 100, h.BuildHuobiWebsocketParamsList(HUOBI_SOCKET_MARKET_OVERVIEW, currency, "pushLong", "", "", "", "", "")) + msg := h.BuildHuobiWebsocketRequestExtra(huobiSocketReqSubscribe, 100, h.BuildHuobiWebsocketParamsList(huobiSocketMarketOverview, currency, "pushLong", "", "", "", "", "")) result, err := common.JSONEncode(msg) if err != nil { log.Println(err) @@ -176,27 +187,32 @@ func (h *HUOBI) OnConnect(output chan socketio.Message) { } } +// OnDisconnect handles disconnection func (h *HUOBI) OnDisconnect(output chan socketio.Message) { log.Printf("%s Disconnected from websocket server.. Reconnecting.\n", h.GetName()) h.WebsocketClient() } +// OnError handles error issues func (h *HUOBI) OnError() { log.Printf("%s Error with Websocket connection.. Reconnecting.\n", h.GetName()) h.WebsocketClient() } +// OnMessage handles messages from the exchange func (h *HUOBI) OnMessage(message []byte, output chan socketio.Message) { } +// OnRequest handles requests func (h *HUOBI) OnRequest(message []byte, output chan socketio.Message) { - response := HuobiResponse{} + response := WebsocketResponse{} err := common.JSONDecode(message, &response) if err != nil { log.Println(err) } } +// WebsocketClient creates a new websocket client func (h *HUOBI) WebsocketClient() { events := make(map[string]func(message []byte, output chan socketio.Message)) events["request"] = h.OnRequest @@ -211,7 +227,7 @@ func (h *HUOBI) WebsocketClient() { } for h.Enabled && h.Websocket { - err := socketio.ConnectToSocket(HUOBI_SOCKETIO_ADDRESS, HuobiSocket) + err := socketio.ConnectToSocket(huobiSocketIOAddress, HuobiSocket) if err != nil { log.Printf("%s Unable to connect to Websocket. Err: %s\n", h.GetName(), err) continue diff --git a/exchanges/huobi/huobi_wrapper.go b/exchanges/huobi/huobi_wrapper.go index 47239f11..f70623fb 100644 --- a/exchanges/huobi/huobi_wrapper.go +++ b/exchanges/huobi/huobi_wrapper.go @@ -20,7 +20,7 @@ func (h *HUOBI) Start() { // Run implements the HUOBI wrapper func (h *HUOBI) Run() { if h.Verbose { - log.Printf("%s Websocket: %s (url: %s).\n", h.GetName(), common.IsEnabled(h.Websocket), HUOBI_SOCKETIO_ADDRESS) + log.Printf("%s Websocket: %s (url: %s).\n", h.GetName(), common.IsEnabled(h.Websocket), huobiSocketIOAddress) log.Printf("%s polling delay: %ds.\n", h.GetName(), h.RESTPollingDelay) log.Printf("%s %d currencies enabled: %s.\n", h.GetName(), len(h.EnabledPairs), h.EnabledPairs) } @@ -40,17 +40,17 @@ func (h *HUOBI) Run() { if common.StringDataContains(h.BaseCurrencies, "CNY") { cfg := config.GetConfig() - exchCfg, err := cfg.GetExchangeConfig(h.Name) + exchCfg, errCNY := cfg.GetExchangeConfig(h.Name) if err != nil { - log.Printf("%s failed to get exchange config. %s\n", h.Name, err) + log.Printf("%s failed to get exchange config. %s\n", h.Name, errCNY) return } exchCfg.BaseCurrencies = "USD" h.BaseCurrencies = []string{"USD"} - err = cfg.UpdateExchangeConfig(exchCfg) - if err != nil { - log.Printf("%s failed to update config. %s\n", h.Name, err) + errCNY = cfg.UpdateExchangeConfig(exchCfg) + if errCNY != nil { + log.Printf("%s failed to update config. %s\n", h.Name, errCNY) return } }