Skip to content

Commit

Permalink
Fixed go vet and linter issues for Huobi exchange.
Browse files Browse the repository at this point in the history
  • Loading branch information
shazbert committed Feb 26, 2018
1 parent 9a04d06 commit bfdb6ba
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 51 deletions.
10 changes: 5 additions & 5 deletions exchanges/huobi/huobi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand All @@ -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
Expand Down Expand Up @@ -222,17 +222,17 @@ func (h *HUOBI) GetMarketDetail(symbol string) (Detail, error) {

type response struct {
Response
tick Detail `json:"tick"`
Tick Detail `json:"tick"`
}

var result response
url := fmt.Sprintf("%s/%s", huobiAPIURL, huobiMarketDetail)
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
Expand Down
96 changes: 56 additions & 40 deletions exchanges/huobi/huobi_websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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"`
Expand All @@ -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"`
Expand All @@ -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"`
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -160,14 +170,15 @@ 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())
}

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)
Expand All @@ -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
Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions exchanges/huobi/huobi_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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
}
}
Expand Down

0 comments on commit bfdb6ba

Please sign in to comment.