Files
gocryptotrader/exchanges/huobi/huobi_websocket.go
Scott 80bc8c7e9e Trade history, recent trades, live trade processing and storage (#558)
* End of day commit moving packages and setting foundation into how trade processing will go

* Conformity

* tdd candle generation based on received trade data, renames orderbookbuffer back to buffer for now...

* Formalises test functions and designs the trade processor

* Theoretical amending old candles to allow any trades that were part of an old processed candle to be more accurate. Saving of candles will only occur on previous cycles, extending memory usage a bit longer

* Figures out sqlboiler for sqlite. Updates websocket entries to process trade data

* One more trade data

* Adds more exchange support

* Adds PSQL stuff

* Begins creating sql implementation

* End of day commit. Helper functions and understanding sql usage in GCT

* Adds delete and cleans up table design

* Finishes trades conceptually. Awaits candle data update in order to translate trades to candles

* Initial handling of trades in coinbene

* Proto

* Fixing of some bugs, attempting to address coinbene asset type ws issues

* Fixes up coinbene websocket implementation for the most part

* finalises coinbene websocket implementation. Adds new ability to parse currencies without a delimiter

* Implements rpc commands and adds testing

* updates the following to be compatible with trade data update: Theoretical amending old candles to allow any trades that were part of an old processed candle to be more accurate. Saving of candles will only occur on previous cycles, extending memory usage a bit longer

* Changes trade to be its own entity rather than attached to a websocket.

* Adds coverage to trades. Changes signature of `AddTradesToBuffer` to return error. Now automatically shuts down without need for channel listening. Will automatically start up again if it gets data

* Implements trade fetching at the wrapper level for a bunch of exchanges. Adds trade id to script updoot. Probably breaking change

* Implements trade fetching for all wrappers hurray hurrah. Updates all the tests

* Adds new interface func to get recent trades. Ensures GetExchangeHistory continues until conditions are met

* Adds new readme, tests all new wrapper endpoints, updates exchange_wrapper_issues to test new endpoints. Updates exchange_wrapper_coverage with new coverage... Fixes lame bug causing wrapper tests to fail from being poorly setup. Adds loopy loop to ensure that all data is captured when requesting exchange history

* Bugfix on psql migrations. Rebases latest changes, updates table design to use base and quote, updates trades to use exchange_name_id

* Adds new config field for saving trades to the database per exchange. Now exits trade processing when trade saving is not enabled. Similarly for wrapper, does not save if not enabled

* Minor bitfinex trade fixes. continues on buffer processing errors, now saves transactionid to the db

* Adds support for generating candles from candlesextended. May extend it further, idk

* Updates trade candles to be able to fill missing data with trades. Adds more tests. Also does a thing where you can forcefully override a candle based on internal trade data instead of API data

* Fixes bug where force deletions did not follow up with insertions. Adds force to candle commands

* Fixes specific exchange based issues. Extends recent trades to 24 hours where possible

* Fixes issue with saved tests. Fixes tests for trades. Adds parallel to tests. Pre-fixes people's nits

* Adds new GRPC functions to find out what data is missing from trades and candles. Fixes some assumptions from missing period code.

* Adds unique constraint. Fixes up niggling issues for wrappers and websockets

* Fixes issues with using unix times in the database trying to retrieve data via the CLI. Reduces save time to 15 seconds

* Updates trades to use timestamps instead of int64 unix

* Adds missing FTX wrapper implementation. Regens docs

* Linting the linters. Updating readme

* Adds new command to set whether an exchange can process trades

* Doc update

* Adds recent trades and historic trade endpoints to grpc

* formats pair_test.go to appease linter gods

* Addresses data race. Removes logging of missing intervals on unrelated function (now that it has its own rpc command). The buffer time isnt customisable, but I don't feel it needs to be at a config level at all really.

* Fixes a few niterinos regarding spacing, type conversion, a weird Bitmex 0 trade value error, unsubscriptions and cli command references

* Reduces map lookups. Adds base func and moves wrappers to use it

* Uses better currency formatter. Adds time based validation to trade history. Reverts configtest.json

* Reverts config and updates test names. Also WAYYYYY LESS SPAMMY

* oopsie doopsie missed a whoopsie

* mint flavoured lint

* Fixes issues caused by rebase

* Fixes issue with timestamps not converting properly from command to RPCServer. Adds new error type. Adds shorthand entries to some commands. Removes os.Exit from tests. Makes Gemini test rolling. Adds enabled exchange check to RPC function. Escapes timestamp on bitstamp. Renames var

* fixes whoopsie oopsie doopsie I forgot to remove code shoopsie

* missed a line

* 🎉 🎉 :tada:Breaks everything in an end of day commit 🎉 🎉 🎉

* Modifies function 'createlocaloffset' to return a string instead. Uses strings for all time based start and end commands. Uses UTC times in RPC server and updates SQLITE to use formatted time based queries

* Adds concurrency-safe way of changing SaveTradeData and checking it. Fixes embarrassing typo

* End of day fix, adds bitfinex update to loop until either the return trades shows no new dates, or meets specifications. Fixes egregious typo

* Improves testing and handling of historical trades function

* Fixes tests after latest changes

* Fix potential fatal err now that db is enabled in test config now

* Fixes up some database settings to use a local engine instead of global var

* DELICIOUS LINT CHOCOLATE FIXES

* Fixes data race by slashing competitor's tyres

* Adds mock test fixes to allow for live and stored data test

* Removes verbosity in engine level tests. Adds new timezone format to highlight the timezone for RPC functions. Removes reference to Preix index fund

* Oopsie doopsie, fixed a whoopsie

* Loggers can no longer do data drag races on my lawn 👴

* Removes bad lock

* Addresses command nits. End of day conceptual commit, trying to calculate spans of time in the context of missing periods. Tests will fail

* Adds new stream response for retrieving trade history as it can take time to do. Unsuccessfully attempts to simplify time range calculation for missing trades response

* Adds new timeperiods package to calculate time periods, time ranges and whether data is in those ranges. Removes kline basic implementation of same concept

* Fixes lint issues. Fixes test. Moves trade cli commands to their own trade subcommands

* Updates lakebtc to no longer have gethistorictrades as it is unsupported. Adds more validation to rpc functions

* Removes requirement to have trades when testing trade wrapper functions. Doesn't really prove it works if there are no trades for a given currency in a time period.

* Addresses nits, runs linting fix and ensures a test is consistent

* Fix merge issues

* Moves sort to timeperiods. Adds test coverage. Fixes typo

* Removes log package in CLI

* Fixes `GetTrades` url

* Reorders all instances of validation occuring after settingup RPC connection

* Fixes test to ensure that it is setup before testing that it is setup

* Fixed issue with bool retrieval. Removes double append

* Fixes Binance times, fixes bitfinex sell sides, fixes huobi times, sorts all responses

* Fixes poloniex trade id consistency. Makes recent trade for poloniex consistent with others (15 minutes). Fixes coinbene. Fixes localbitcoins to use quote currency. Fixes coinut times. Updates huobi trade id, saves okgroup trades. Fixes bid and ask to buy and sell

* Removes websocket trades for lakebtc as it did not meet our requirements for processing. Adds new constraints to the database to ensure we have uniqueness on trades where ID doesn't exist and doesn't trigger errors for trades where the tid does

* Fixes migration for postgres to downscale properly

* Really really fixes the psql index changes

* Fixes broken tests

* Now with working tests and no pocket lint

* Makes the side column nullable with no more constraint for it. adds migrations and runs generation. comments lakebtc

* Lint & Sprüngli

* Updates zb to use more appropriate side

* Fixes oopsie

* Attempts to address a data race from globals

* Fixes build

* Fixes missed regen rpc files

* Updates readme to point to trade readme. Fixes exchange_wrapper_coverage wrapper count and untested panics, tests bitfinex funding pair test for `fUSD`, adds shiny new param `tradeprocessinginterval`

* mint flavoured lint

* Uses the real default to set the default value by default

* Fixes some extra tests surrounding email sending and number incompatibility

* Reverts test config

* re-adds gom2/usdt currency

* Fixes typo, don't look!

* Fixes minor codelingo pickups

* Adds more precision to handling of trade data from Kraken. Expands test

* interface christmas tree

* lint
2020-10-29 13:00:02 +11:00

732 lines
20 KiB
Go

package huobi
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/gorilla/websocket"
"github.com/thrasher-corp/gocryptotrader/common"
"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/ticker"
"github.com/thrasher-corp/gocryptotrader/exchanges/trade"
"github.com/thrasher-corp/gocryptotrader/log"
)
const (
baseWSURL = "wss://api.huobi.pro"
wsMarketURL = baseWSURL + "/ws"
wsMarketKline = "market.%s.kline.1min"
wsMarketDepth = "market.%s.depth.step0"
wsMarketTrade = "market.%s.trade.detail"
wsMarketTicker = "market.%s.detail"
wsAccountsOrdersEndPoint = "/ws/v1"
wsAccountsList = "accounts.list"
wsOrdersList = "orders.list"
wsOrdersDetail = "orders.detail"
wsAccountsOrdersURL = baseWSURL + wsAccountsOrdersEndPoint
wsAccountListEndpoint = wsAccountsOrdersEndPoint + "/" + wsAccountsList
wsOrdersListEndpoint = wsAccountsOrdersEndPoint + "/" + wsOrdersList
wsOrdersDetailEndpoint = wsAccountsOrdersEndPoint + "/" + wsOrdersDetail
wsDateTimeFormatting = "2006-01-02T15:04:05"
signatureMethod = "HmacSHA256"
signatureVersion = "2"
requestOp = "req"
authOp = "auth"
loginDelay = 50 * time.Millisecond
rateLimit = 20
)
// Instantiates a communications channel between websocket connections
var comms = make(chan WsMessage)
// WsConnect initiates a new websocket connection
func (h *HUOBI) WsConnect() error {
if !h.Websocket.IsEnabled() || !h.IsEnabled() {
return errors.New(stream.WebsocketNotEnabled)
}
var dialer websocket.Dialer
err := h.wsDial(&dialer)
if err != nil {
return err
}
err = h.wsAuthenticatedDial(&dialer)
if err != nil {
log.Errorf(log.ExchangeSys,
"%v - authenticated dial failed: %v\n",
h.Name,
err)
}
err = h.wsLogin()
if err != nil {
log.Errorf(log.ExchangeSys,
"%v - authentication failed: %v\n",
h.Name,
err)
h.Websocket.SetCanUseAuthenticatedEndpoints(false)
}
go h.wsReadData()
subs, err := h.GenerateDefaultSubscriptions()
if err != nil {
return err
}
return h.Websocket.SubscribeToChannels(subs)
}
func (h *HUOBI) wsDial(dialer *websocket.Dialer) error {
err := h.Websocket.Conn.Dial(dialer, http.Header{})
if err != nil {
return err
}
go h.wsFunnelConnectionData(h.Websocket.Conn, wsMarketURL)
return nil
}
func (h *HUOBI) wsAuthenticatedDial(dialer *websocket.Dialer) error {
if !h.GetAuthenticatedAPISupport(exchange.WebsocketAuthentication) {
return fmt.Errorf("%v AuthenticatedWebsocketAPISupport not enabled",
h.Name)
}
err := h.Websocket.AuthConn.Dial(dialer, http.Header{})
if err != nil {
return err
}
go h.wsFunnelConnectionData(h.Websocket.AuthConn, wsAccountsOrdersURL)
return nil
}
// wsFunnelConnectionData manages data from multiple endpoints and passes it to
// a channel
func (h *HUOBI) wsFunnelConnectionData(ws stream.Connection, url string) {
h.Websocket.Wg.Add(1)
defer h.Websocket.Wg.Done()
for {
resp := ws.ReadMessage()
if resp.Raw == nil {
return
}
comms <- WsMessage{Raw: resp.Raw, URL: url}
}
}
// wsReadData receives and passes on websocket messages for processing
func (h *HUOBI) wsReadData() {
h.Websocket.Wg.Add(1)
defer h.Websocket.Wg.Done()
for {
resp := <-comms
err := h.wsHandleData(resp.Raw)
if err != nil {
h.Websocket.DataHandler <- err
}
}
}
func stringToOrderStatus(status string) (order.Status, error) {
switch status {
case "submitted":
return order.New, nil
case "canceled":
return order.Cancelled, nil
case "partial-filled":
return order.PartiallyFilled, nil
case "partial-canceled":
return order.PartiallyCancelled, nil
default:
return order.UnknownStatus,
errors.New(status + " not recognised as order status")
}
}
func stringToOrderSide(side string) (order.Side, error) {
switch {
case strings.Contains(side, "buy"):
return order.Buy, nil
case strings.Contains(side, "sell"):
return order.Sell, nil
}
return order.UnknownSide,
errors.New(side + " not recognised as order side")
}
func stringToOrderType(oType string) (order.Type, error) {
switch {
case strings.Contains(oType, "limit"):
return order.Limit, nil
case strings.Contains(oType, "market"):
return order.Market, nil
}
return order.UnknownType,
errors.New(oType + " not recognised as order type")
}
func (h *HUOBI) wsHandleData(respRaw []byte) error {
var init WsResponse
err := json.Unmarshal(respRaw, &init)
if err != nil {
return err
}
if init.Subscribed != "" ||
init.UnSubscribed != "" ||
init.Op == "sub" ||
init.Op == "unsub" {
// TODO handle subs
return nil
}
if init.Ping != 0 {
h.sendPingResponse(init.Ping)
return nil
}
if init.Op == "ping" {
authPing := authenticationPing{
OP: "pong",
TS: init.TS,
}
err := h.Websocket.AuthConn.SendJSONMessage(authPing)
if err != nil {
log.Error(log.ExchangeSys, err)
}
return nil
}
if init.ErrorMessage != "" {
if init.ErrorMessage == "api-signature-not-valid" {
h.Websocket.SetCanUseAuthenticatedEndpoints(false)
return errors.New(h.Name +
" - invalid credentials. Authenticated requests disabled")
}
codes, _ := init.ErrorCode.(string)
return errors.New(h.Name + " Code:" + codes + " Message:" + init.ErrorMessage)
}
if init.ClientID > 0 {
if h.Websocket.Match.IncomingWithData(init.ClientID, respRaw) {
return nil
}
}
switch {
case strings.EqualFold(init.Op, authOp):
h.Websocket.SetCanUseAuthenticatedEndpoints(true)
// Auth captured
return nil
case strings.EqualFold(init.Topic, "accounts"):
var response WsAuthenticatedAccountsResponse
err := json.Unmarshal(respRaw, &response)
if err != nil {
return err
}
h.Websocket.DataHandler <- response
case strings.Contains(init.Topic, "orders") &&
strings.Contains(init.Topic, "update"):
var response WsAuthenticatedOrdersUpdateResponse
err := json.Unmarshal(respRaw, &response)
if err != nil {
return err
}
data := strings.Split(response.Topic, ".")
if len(data) < 2 {
return errors.New(h.Name +
" - currency could not be extracted from response")
}
orderID := strconv.FormatInt(response.Data.OrderID, 10)
var oSide order.Side
oSide, err = stringToOrderSide(response.Data.OrderType)
if err != nil {
h.Websocket.DataHandler <- order.ClassificationError{
Exchange: h.Name,
OrderID: orderID,
Err: err,
}
}
var oType order.Type
oType, err = stringToOrderType(response.Data.OrderType)
if err != nil {
h.Websocket.DataHandler <- order.ClassificationError{
Exchange: h.Name,
OrderID: orderID,
Err: err,
}
}
var oStatus order.Status
oStatus, err = stringToOrderStatus(response.Data.OrderState)
if err != nil {
h.Websocket.DataHandler <- order.ClassificationError{
Exchange: h.Name,
OrderID: orderID,
Err: err,
}
}
var p currency.Pair
var a asset.Item
p, a, err = h.GetRequestFormattedPairAndAssetType(data[1])
if err != nil {
return err
}
h.Websocket.DataHandler <- &order.Detail{
Price: response.Data.Price,
Amount: response.Data.UnfilledAmount + response.Data.FilledAmount,
ExecutedAmount: response.Data.FilledAmount,
RemainingAmount: response.Data.UnfilledAmount,
Exchange: h.Name,
ID: orderID,
Type: oType,
Side: oSide,
Status: oStatus,
AssetType: a,
LastUpdated: time.Unix(response.TS*1000, 0),
Pair: p,
}
case strings.Contains(init.Topic, "orders"):
var response WsOldOrderUpdate
err := json.Unmarshal(respRaw, &response)
if err != nil {
return err
}
h.Websocket.DataHandler <- response
case strings.Contains(init.Channel, "depth"):
var depth WsDepth
err := json.Unmarshal(respRaw, &depth)
if err != nil {
return err
}
data := strings.Split(depth.Channel, ".")
err = h.WsProcessOrderbook(&depth, data[1])
if err != nil {
return err
}
case strings.Contains(init.Channel, "kline"):
var kline WsKline
err := json.Unmarshal(respRaw, &kline)
if err != nil {
return err
}
data := strings.Split(kline.Channel, ".")
var p currency.Pair
var a asset.Item
p, a, err = h.GetRequestFormattedPairAndAssetType(data[1])
if err != nil {
return err
}
h.Websocket.DataHandler <- stream.KlineData{
Timestamp: time.Unix(0, kline.Timestamp*int64(time.Millisecond)),
Exchange: h.Name,
AssetType: a,
Pair: p,
OpenPrice: kline.Tick.Open,
ClosePrice: kline.Tick.Close,
HighPrice: kline.Tick.High,
LowPrice: kline.Tick.Low,
Volume: kline.Tick.Volume,
Interval: data[3],
}
case strings.Contains(init.Channel, "trade.detail"):
if !h.IsSaveTradeDataEnabled() {
return nil
}
var t WsTrade
err := json.Unmarshal(respRaw, &t)
if err != nil {
return err
}
data := strings.Split(t.Channel, ".")
var p currency.Pair
var a asset.Item
p, a, err = h.GetRequestFormattedPairAndAssetType(data[1])
if err != nil {
return err
}
var trades []trade.Data
for i := range t.Tick.Data {
side := order.Buy
if t.Tick.Data[i].Direction != "buy" {
side = order.Sell
}
trades = append(trades, trade.Data{
Exchange: h.Name,
AssetType: a,
CurrencyPair: p,
Timestamp: time.Unix(0,
t.Tick.Data[i].Timestamp*int64(time.Millisecond)),
Amount: t.Tick.Data[i].Amount,
Price: t.Tick.Data[i].Price,
Side: side,
TID: strconv.FormatFloat(t.Tick.Data[i].TradeID, 'f', -1, 64),
})
}
return trade.AddTradesToBuffer(h.Name, trades...)
case strings.Contains(init.Channel, "detail"),
strings.Contains(init.Rep, "detail"):
var wsTicker WsTick
err := json.Unmarshal(respRaw, &wsTicker)
if err != nil {
return err
}
var data []string
if wsTicker.Channel != "" {
data = strings.Split(wsTicker.Channel, ".")
}
if wsTicker.Rep != "" {
data = strings.Split(wsTicker.Rep, ".")
}
var p currency.Pair
var a asset.Item
p, a, err = h.GetRequestFormattedPairAndAssetType(data[1])
if err != nil {
return err
}
h.Websocket.DataHandler <- &ticker.Price{
ExchangeName: h.Name,
Open: wsTicker.Tick.Open,
Close: wsTicker.Tick.Close,
Volume: wsTicker.Tick.Amount,
QuoteVolume: wsTicker.Tick.Volume,
High: wsTicker.Tick.High,
Low: wsTicker.Tick.Low,
LastUpdated: time.Unix(0, wsTicker.Timestamp*int64(time.Millisecond)),
AssetType: a,
Pair: p,
}
default:
h.Websocket.DataHandler <- stream.UnhandledMessageWarning{
Message: h.Name + stream.UnhandledMessage + string(respRaw),
}
return nil
}
return nil
}
func (h *HUOBI) sendPingResponse(pong int64) {
err := h.Websocket.Conn.SendJSONMessage(WsPong{Pong: pong})
if err != nil {
log.Error(log.ExchangeSys, err)
}
}
// WsProcessOrderbook processes new orderbook data
func (h *HUOBI) WsProcessOrderbook(update *WsDepth, symbol string) error {
pairs, err := h.GetEnabledPairs(asset.Spot)
if err != nil {
return err
}
format, err := h.GetPairFormat(asset.Spot, true)
if err != nil {
return err
}
p, err := currency.NewPairFromFormattedPairs(symbol,
pairs,
format)
if err != nil {
return err
}
var bids, asks []orderbook.Item
for i := range update.Tick.Bids {
bids = append(bids, orderbook.Item{
Price: update.Tick.Bids[i][0].(float64),
Amount: update.Tick.Bids[i][1].(float64),
})
}
for i := range update.Tick.Asks {
asks = append(asks, orderbook.Item{
Price: update.Tick.Asks[i][0].(float64),
Amount: update.Tick.Asks[i][1].(float64),
})
}
var newOrderBook orderbook.Base
newOrderBook.Asks = asks
newOrderBook.Bids = bids
newOrderBook.Pair = p
newOrderBook.AssetType = asset.Spot
newOrderBook.ExchangeName = h.Name
return h.Websocket.Orderbook.LoadSnapshot(&newOrderBook)
}
// GenerateDefaultSubscriptions Adds default subscriptions to websocket to be handled by ManageSubscriptions()
func (h *HUOBI) GenerateDefaultSubscriptions() ([]stream.ChannelSubscription, error) {
var channels = []string{wsMarketKline,
wsMarketDepth,
wsMarketTrade,
wsMarketTicker}
var subscriptions []stream.ChannelSubscription
if h.Websocket.CanUseAuthenticatedEndpoints() {
channels = append(channels, "orders.%v", "orders.%v.update")
subscriptions = append(subscriptions, stream.ChannelSubscription{
Channel: "accounts",
})
}
enabledCurrencies, err := h.GetEnabledPairs(asset.Spot)
if err != nil {
return nil, err
}
for i := range channels {
for j := range enabledCurrencies {
enabledCurrencies[j].Delimiter = ""
channel := fmt.Sprintf(channels[i],
enabledCurrencies[j].Lower().String())
subscriptions = append(subscriptions, stream.ChannelSubscription{
Channel: channel,
Currency: enabledCurrencies[j],
})
}
}
return subscriptions, nil
}
// Subscribe sends a websocket message to receive data from the channel
func (h *HUOBI) Subscribe(channelsToSubscribe []stream.ChannelSubscription) error {
var errs common.Errors
for i := range channelsToSubscribe {
if strings.Contains(channelsToSubscribe[i].Channel, "orders.") ||
strings.Contains(channelsToSubscribe[i].Channel, "accounts") {
err := h.wsAuthenticatedSubscribe("sub",
wsAccountsOrdersEndPoint+channelsToSubscribe[i].Channel,
channelsToSubscribe[i].Channel)
if err != nil {
errs = append(errs, err)
continue
}
h.Websocket.AddSuccessfulSubscriptions(channelsToSubscribe[i])
continue
}
err := h.Websocket.Conn.SendJSONMessage(WsRequest{
Subscribe: channelsToSubscribe[i].Channel,
})
if err != nil {
errs = append(errs, err)
continue
}
h.Websocket.AddSuccessfulSubscriptions(channelsToSubscribe[i])
}
if errs != nil {
return errs
}
return nil
}
// Unsubscribe sends a websocket message to stop receiving data from the channel
func (h *HUOBI) Unsubscribe(channelsToUnsubscribe []stream.ChannelSubscription) error {
var errs common.Errors
for i := range channelsToUnsubscribe {
if strings.Contains(channelsToUnsubscribe[i].Channel, "orders.") ||
strings.Contains(channelsToUnsubscribe[i].Channel, "accounts") {
err := h.wsAuthenticatedSubscribe("unsub",
wsAccountsOrdersEndPoint+channelsToUnsubscribe[i].Channel,
channelsToUnsubscribe[i].Channel)
if err != nil {
errs = append(errs, err)
continue
}
h.Websocket.RemoveSuccessfulUnsubscriptions(channelsToUnsubscribe[i])
continue
}
err := h.Websocket.Conn.SendJSONMessage(WsRequest{
Unsubscribe: channelsToUnsubscribe[i].Channel,
})
if err != nil {
errs = append(errs, err)
continue
}
h.Websocket.RemoveSuccessfulUnsubscriptions(channelsToUnsubscribe[i])
}
if errs != nil {
return errs
}
return nil
}
func (h *HUOBI) wsGenerateSignature(timestamp, endpoint string) []byte {
values := url.Values{}
values.Set("AccessKeyId", h.API.Credentials.Key)
values.Set("SignatureMethod", signatureMethod)
values.Set("SignatureVersion", signatureVersion)
values.Set("Timestamp", timestamp)
host := "api.huobi.pro"
payload := fmt.Sprintf("%s\n%s\n%s\n%s",
http.MethodGet, host, endpoint, values.Encode())
return crypto.GetHMAC(crypto.HashSHA256, []byte(payload), []byte(h.API.Credentials.Secret))
}
func (h *HUOBI) wsLogin() error {
if !h.GetAuthenticatedAPISupport(exchange.WebsocketAuthentication) {
return fmt.Errorf("%v AuthenticatedWebsocketAPISupport not enabled", h.Name)
}
h.Websocket.SetCanUseAuthenticatedEndpoints(true)
timestamp := time.Now().UTC().Format(wsDateTimeFormatting)
request := WsAuthenticationRequest{
Op: authOp,
AccessKeyID: h.API.Credentials.Key,
SignatureMethod: signatureMethod,
SignatureVersion: signatureVersion,
Timestamp: timestamp,
}
hmac := h.wsGenerateSignature(timestamp, wsAccountsOrdersEndPoint)
request.Signature = crypto.Base64Encode(hmac)
err := h.Websocket.AuthConn.SendJSONMessage(request)
if err != nil {
h.Websocket.SetCanUseAuthenticatedEndpoints(false)
return err
}
time.Sleep(loginDelay)
return nil
}
func (h *HUOBI) wsAuthenticatedSubscribe(operation, endpoint, topic string) error {
timestamp := time.Now().UTC().Format(wsDateTimeFormatting)
request := WsAuthenticatedSubscriptionRequest{
Op: operation,
AccessKeyID: h.API.Credentials.Key,
SignatureMethod: signatureMethod,
SignatureVersion: signatureVersion,
Timestamp: timestamp,
Topic: topic,
}
hmac := h.wsGenerateSignature(timestamp, endpoint)
request.Signature = crypto.Base64Encode(hmac)
return h.Websocket.AuthConn.SendJSONMessage(request)
}
func (h *HUOBI) wsGetAccountsList() (*WsAuthenticatedAccountsListResponse, error) {
if !h.Websocket.CanUseAuthenticatedEndpoints() {
return nil, fmt.Errorf("%v not authenticated cannot get accounts list", h.Name)
}
timestamp := time.Now().UTC().Format(wsDateTimeFormatting)
request := WsAuthenticatedAccountsListRequest{
Op: requestOp,
AccessKeyID: h.API.Credentials.Key,
SignatureMethod: signatureMethod,
SignatureVersion: signatureVersion,
Timestamp: timestamp,
Topic: wsAccountsList,
}
hmac := h.wsGenerateSignature(timestamp, wsAccountListEndpoint)
request.Signature = crypto.Base64Encode(hmac)
request.ClientID = h.Websocket.AuthConn.GenerateMessageID(true)
resp, err := h.Websocket.AuthConn.SendMessageReturnResponse(request.ClientID, request)
if err != nil {
return nil, err
}
var response WsAuthenticatedAccountsListResponse
err = json.Unmarshal(resp, &response)
if err != nil {
return nil, err
}
code, _ := response.ErrorCode.(int)
if code != 0 {
return nil, errors.New(response.ErrorMessage)
}
return &response, nil
}
func (h *HUOBI) wsGetOrdersList(accountID int64, pair currency.Pair) (*WsAuthenticatedOrdersResponse, error) {
if !h.Websocket.CanUseAuthenticatedEndpoints() {
return nil, fmt.Errorf("%v not authenticated cannot get orders list", h.Name)
}
fpair, err := h.FormatExchangeCurrency(pair, asset.Spot)
if err != nil {
return nil, err
}
timestamp := time.Now().UTC().Format(wsDateTimeFormatting)
request := WsAuthenticatedOrdersListRequest{
Op: requestOp,
AccessKeyID: h.API.Credentials.Key,
SignatureMethod: signatureMethod,
SignatureVersion: signatureVersion,
Timestamp: timestamp,
Topic: wsOrdersList,
AccountID: accountID,
Symbol: fpair.String(),
States: "submitted,partial-filled",
}
hmac := h.wsGenerateSignature(timestamp, wsOrdersListEndpoint)
request.Signature = crypto.Base64Encode(hmac)
request.ClientID = h.Websocket.AuthConn.GenerateMessageID(true)
resp, err := h.Websocket.AuthConn.SendMessageReturnResponse(request.ClientID, request)
if err != nil {
return nil, err
}
var response WsAuthenticatedOrdersResponse
err = json.Unmarshal(resp, &response)
if err != nil {
return nil, err
}
code, _ := response.ErrorCode.(int)
if code != 0 {
return nil, errors.New(response.ErrorMessage)
}
return &response, nil
}
func (h *HUOBI) wsGetOrderDetails(orderID string) (*WsAuthenticatedOrderDetailResponse, error) {
if !h.Websocket.CanUseAuthenticatedEndpoints() {
return nil, fmt.Errorf("%v not authenticated cannot get order details", h.Name)
}
timestamp := time.Now().UTC().Format(wsDateTimeFormatting)
request := WsAuthenticatedOrderDetailsRequest{
Op: requestOp,
AccessKeyID: h.API.Credentials.Key,
SignatureMethod: signatureMethod,
SignatureVersion: signatureVersion,
Timestamp: timestamp,
Topic: wsOrdersDetail,
OrderID: orderID,
}
hmac := h.wsGenerateSignature(timestamp, wsOrdersDetailEndpoint)
request.Signature = crypto.Base64Encode(hmac)
request.ClientID = h.Websocket.AuthConn.GenerateMessageID(true)
resp, err := h.Websocket.AuthConn.SendMessageReturnResponse(request.ClientID, request)
if err != nil {
return nil, err
}
var response WsAuthenticatedOrderDetailResponse
err = json.Unmarshal(resp, &response)
if err != nil {
return nil, err
}
code, _ := response.ErrorCode.(int)
if code != 0 {
return nil, errors.New(response.ErrorMessage)
}
return &response, nil
}