Fixed go vet and linter issues for Huobi exchange.

This commit is contained in:
Ryan O'Hara-Reid
2018-02-26 13:26:40 +11:00
parent 9a04d06890
commit bfdb6ba307
3 changed files with 67 additions and 51 deletions

View File

@@ -176,7 +176,7 @@ func (h *HUOBI) GetTrades(symbol string) ([]Trade, error) {
type response struct { type response struct {
Response Response
tick struct { Tick struct {
Data []Trade `json:"data"` Data []Trade `json:"data"`
} `json:"tick"` } `json:"tick"`
} }
@@ -188,7 +188,7 @@ func (h *HUOBI) GetTrades(symbol string) ([]Trade, error) {
if result.ErrorMessage != "" { if result.ErrorMessage != "" {
return nil, errors.New(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 // GetTradeHistory returns the trades for the specified symbol
@@ -222,7 +222,7 @@ func (h *HUOBI) GetMarketDetail(symbol string) (Detail, error) {
type response struct { type response struct {
Response Response
tick Detail `json:"tick"` Tick Detail `json:"tick"`
} }
var result response 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) err := common.SendHTTPGetRequest(common.EncodeURLValues(url, vals), true, h.Verbose, &result)
if result.ErrorMessage != "" { 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 // GetSymbols returns an array of symbols supported by Huobi

View File

@@ -8,39 +8,41 @@ import (
) )
const ( const (
HUOBI_SOCKETIO_ADDRESS = "https://hq.huobi.com:443" huobiSocketIOAddress = "https://hq.huobi.com:443"
//Service API //Service API
HUOBI_SOCKET_REQ_SYMBOL_LIST = "reqSymbolList" huobiSocketReqSymbolList = "reqSymbolList"
HUOBI_SOCKET_REQ_SYMBOL_DETAIL = "reqSymbolDetail" huobiSocketReqSymbolDetail = "reqSymbolDetail"
HUOBI_SOCKET_REQ_SUBSCRIBE = "reqMsgSubscribe" huobiSocketReqSubscribe = "reqMsgSubscribe"
HUOBI_SOCKET_REQ_UNSUBSCRIBE = "reqMsgUnsubscribe" huobiSocketReqUnsubscribe = "reqMsgUnsubscribe"
// Market data API // Market data API
HUOBI_SOCKET_MARKET_DETAIL = "marketDetail" huobiSocketMarketDetail = "marketDetail"
HUOBI_SOCKET_TRADE_DETAIL = "tradeDetail" huobiSocketTradeDetail = "tradeDetail"
HUOBI_SOCKET_MARKET_DEPTH_TOP = "marketDepthTop" huobiSocketMarketDepthTop = "marketDepthTop"
HUOBI_SOCKET_MARKET_DEPTH_TOP_SHORT = "marketDepthTopShort" huobiSocketMarketDepthTopShort = "marketDepthTopShort"
HUOBI_SOCKET_MARKET_DEPTH = "marketDepth" huobiSocketMarketDepth = "marketDepth"
HUOBI_SOCKET_MARKET_DEPTH_TOP_DIFF = "marketDepthTopDiff" huobiSocketMarketDepthTopDiff = "marketDepthTopDiff"
HUOBI_SOCKET_MARKET_DEPTH_DIFF = "marketDepthDiff" huobiSocketMarketDepthDiff = "marketDepthDiff"
HUOBI_SOCKET_MARKET_LAST_KLINE = "lastKLine" huobiSocketMarketLastKline = "lastKLine"
HUOBI_SOCKET_MARKET_LAST_TIMELINE = "lastTimeLine" huobiSocketMarketLastTimeline = "lastTimeLine"
HUOBI_SOCKET_MARKET_OVERVIEW = "marketOverview" huobiSocketMarketOverview = "marketOverview"
HUOBI_SOCKET_MARKET_STATIC = "marketStatic" huobiSocketMarketStatic = "marketStatic"
// History data API // History data API
HUOBI_SOCKET_REQ_TIMELINE = "reqTimeLine" huobiSocketReqTimeline = "reqTimeLine"
HUOBI_SOCKET_REQ_KLINE = "reqKLine" huobiSocketReqKline = "reqKLine"
HUOBI_SOCKET_REQ_DEPTH_TOP = "reqMarketDepthTop" huobiSocketReqDepthTop = "reqMarketDepthTop"
HUOBI_SOCKET_REQ_DEPTH = "reqMarketDepth" huobiSocketReqDepth = "reqMarketDepth"
HUOBI_SOCKET_REQ_TRADE_DETAIL_TOP = "reqTradeDetailTop" huobiSocketReqTradeDetailTop = "reqTradeDetailTop"
HUOBI_SOCKET_REQ_MARKET_DETAIL = "reqMarketDetail" huobiSocketReqMarketDetail = "reqMarketDetail"
) )
// HuobiSocket is a pointer to a IO Socket
var HuobiSocket *socketio.SocketIO var HuobiSocket *socketio.SocketIO
type HuobiDepth struct { // Depth holds depth information
type Depth struct {
SymbolID string `json:"symbolId"` SymbolID string `json:"symbolId"`
Time float64 `json:"time"` Time float64 `json:"time"`
Version float64 `json:"version"` Version float64 `json:"version"`
@@ -54,24 +56,27 @@ type HuobiDepth struct {
AskAmount []float64 `json:"askAmount"` AskAmount []float64 `json:"askAmount"`
} }
type HuobiWebsocketTrade struct { // WebsocketTrade holds full trade data
type WebsocketTrade struct {
Price []float64 `json:"price"` Price []float64 `json:"price"`
Level []float64 `json:"level"` Level []float64 `json:"level"`
Amount []float64 `json:"amount"` Amount []float64 `json:"amount"`
AccuAmount []float64 `json:"accuAmount"` AccuAmount []float64 `json:"accuAmount"`
} }
type HuobiWebsocketTradeDetail struct { // WebsocketTradeDetail holds specific trade details
SymbolID string `json:"symbolId"` type WebsocketTradeDetail struct {
TradeID []int64 `json:"tradeId"` SymbolID string `json:"symbolId"`
Price []float64 `json:"price"` TradeID []int64 `json:"tradeId"`
Time []int64 `json:"time"` Price []float64 `json:"price"`
Amount []float64 `json:"amount"` Time []int64 `json:"time"`
TopBids []HuobiWebsocketTrade `json:"topBids"` Amount []float64 `json:"amount"`
TopAsks []HuobiWebsocketTrade `json:"topAsks"` TopBids []WebsocketTrade `json:"topBids"`
TopAsks []WebsocketTrade `json:"topAsks"`
} }
type HuobiWebsocketMarketOverview struct { // WebsocketMarketOverview holds market overview data
type WebsocketMarketOverview struct {
SymbolID string `json:"symbolId"` SymbolID string `json:"symbolId"`
Last float64 `json:"priceNew"` Last float64 `json:"priceNew"`
Open float64 `json:"priceOpen"` Open float64 `json:"priceOpen"`
@@ -83,7 +88,8 @@ type HuobiWebsocketMarketOverview struct {
TotalAmount float64 `json:"totalAmount"` TotalAmount float64 `json:"totalAmount"`
} }
type HuobiWebsocketLastTimeline struct { // WebsocketLastTimeline holds timeline data
type WebsocketLastTimeline struct {
ID int64 `json:"_id"` ID int64 `json:"_id"`
SymbolID string `json:"symbolId"` SymbolID string `json:"symbolId"`
Time int64 `json:"time"` Time int64 `json:"time"`
@@ -93,7 +99,8 @@ type HuobiWebsocketLastTimeline struct {
Count int64 `json:"count"` Count int64 `json:"count"`
} }
type HuobiResponse struct { // WebsocketResponse is a general response type for websocket
type WebsocketResponse struct {
Version int `json:"version"` Version int `json:"version"`
MsgType string `json:"msgType"` MsgType string `json:"msgType"`
RequestIndex int64 `json:"requestIndex"` RequestIndex int64 `json:"requestIndex"`
@@ -102,6 +109,7 @@ type HuobiResponse struct {
Payload map[string]interface{} `json:"payload"` Payload map[string]interface{} `json:"payload"`
} }
// BuildHuobiWebsocketRequest packages a new request
func (h *HUOBI) BuildHuobiWebsocketRequest(msgType string, requestIndex int64, symbolRequest []string) map[string]interface{} { func (h *HUOBI) BuildHuobiWebsocketRequest(msgType string, requestIndex int64, symbolRequest []string) map[string]interface{} {
request := map[string]interface{}{} request := map[string]interface{}{}
request["version"] = 1 request["version"] = 1
@@ -118,7 +126,8 @@ func (h *HUOBI) BuildHuobiWebsocketRequest(msgType string, requestIndex int64, s
return request 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 := map[string]interface{}{}
request["version"] = 1 request["version"] = 1
request["msgType"] = msgType request["msgType"] = msgType
@@ -127,10 +136,11 @@ func (h *HUOBI) BuildHuobiWebsocketRequestExtra(msgType string, requestIndex int
request["requestIndex"] = requestIndex request["requestIndex"] = requestIndex
} }
request["symbolList"] = symbolIdList request["symbolList"] = symbolIDList
return request return request
} }
// BuildHuobiWebsocketParamsList packages a parameter list
func (h *HUOBI) BuildHuobiWebsocketParamsList(objectName, currency, pushType, period, count, from, to, percentage string) interface{} { func (h *HUOBI) BuildHuobiWebsocketParamsList(objectName, currency, pushType, period, count, from, to, percentage string) interface{} {
list := map[string]interface{}{} list := map[string]interface{}{}
list["symbolId"] = currency list["symbolId"] = currency
@@ -160,6 +170,7 @@ func (h *HUOBI) BuildHuobiWebsocketParamsList(objectName, currency, pushType, pe
return listCompleted return listCompleted
} }
// OnConnect handles connection establishment
func (h *HUOBI) OnConnect(output chan socketio.Message) { func (h *HUOBI) OnConnect(output chan socketio.Message) {
if h.Verbose { if h.Verbose {
log.Printf("%s Connected to Websocket.", h.GetName()) 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 { for _, x := range h.EnabledPairs {
currency := common.StringToLower(x) 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) result, err := common.JSONEncode(msg)
if err != nil { if err != nil {
log.Println(err) 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) { func (h *HUOBI) OnDisconnect(output chan socketio.Message) {
log.Printf("%s Disconnected from websocket server.. Reconnecting.\n", h.GetName()) log.Printf("%s Disconnected from websocket server.. Reconnecting.\n", h.GetName())
h.WebsocketClient() h.WebsocketClient()
} }
// OnError handles error issues
func (h *HUOBI) OnError() { func (h *HUOBI) OnError() {
log.Printf("%s Error with Websocket connection.. Reconnecting.\n", h.GetName()) log.Printf("%s Error with Websocket connection.. Reconnecting.\n", h.GetName())
h.WebsocketClient() h.WebsocketClient()
} }
// OnMessage handles messages from the exchange
func (h *HUOBI) OnMessage(message []byte, output chan socketio.Message) { func (h *HUOBI) OnMessage(message []byte, output chan socketio.Message) {
} }
// OnRequest handles requests
func (h *HUOBI) OnRequest(message []byte, output chan socketio.Message) { func (h *HUOBI) OnRequest(message []byte, output chan socketio.Message) {
response := HuobiResponse{} response := WebsocketResponse{}
err := common.JSONDecode(message, &response) err := common.JSONDecode(message, &response)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
} }
} }
// WebsocketClient creates a new websocket client
func (h *HUOBI) WebsocketClient() { func (h *HUOBI) WebsocketClient() {
events := make(map[string]func(message []byte, output chan socketio.Message)) events := make(map[string]func(message []byte, output chan socketio.Message))
events["request"] = h.OnRequest events["request"] = h.OnRequest
@@ -211,7 +227,7 @@ func (h *HUOBI) WebsocketClient() {
} }
for h.Enabled && h.Websocket { for h.Enabled && h.Websocket {
err := socketio.ConnectToSocket(HUOBI_SOCKETIO_ADDRESS, HuobiSocket) err := socketio.ConnectToSocket(huobiSocketIOAddress, HuobiSocket)
if err != nil { if err != nil {
log.Printf("%s Unable to connect to Websocket. Err: %s\n", h.GetName(), err) log.Printf("%s Unable to connect to Websocket. Err: %s\n", h.GetName(), err)
continue continue

View File

@@ -20,7 +20,7 @@ func (h *HUOBI) Start() {
// Run implements the HUOBI wrapper // Run implements the HUOBI wrapper
func (h *HUOBI) Run() { func (h *HUOBI) Run() {
if h.Verbose { 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 polling delay: %ds.\n", h.GetName(), h.RESTPollingDelay)
log.Printf("%s %d currencies enabled: %s.\n", h.GetName(), len(h.EnabledPairs), h.EnabledPairs) 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") { if common.StringDataContains(h.BaseCurrencies, "CNY") {
cfg := config.GetConfig() cfg := config.GetConfig()
exchCfg, err := cfg.GetExchangeConfig(h.Name) exchCfg, errCNY := cfg.GetExchangeConfig(h.Name)
if err != nil { 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 return
} }
exchCfg.BaseCurrencies = "USD" exchCfg.BaseCurrencies = "USD"
h.BaseCurrencies = []string{"USD"} h.BaseCurrencies = []string{"USD"}
err = cfg.UpdateExchangeConfig(exchCfg) errCNY = cfg.UpdateExchangeConfig(exchCfg)
if err != nil { if errCNY != nil {
log.Printf("%s failed to update config. %s\n", h.Name, err) log.Printf("%s failed to update config. %s\n", h.Name, errCNY)
return return
} }
} }