From 5dc8ddf404fb70084d3e1e6c792396079b32ee66 Mon Sep 17 00:00:00 2001 From: Ryan O'Hara-Reid Date: Mon, 26 Feb 2018 13:59:12 +1100 Subject: [PATCH] Fixed linter issues, fixed test deployment for auth request, added data types for error returns for Liqui Exchange. --- exchanges/liqui/liqui.go | 28 +++++++---- exchanges/liqui/liqui_test.go | 80 ++++++++++++++++---------------- exchanges/liqui/liqui_types.go | 34 ++++++++++---- exchanges/liqui/liqui_wrapper.go | 2 +- 4 files changed, 83 insertions(+), 61 deletions(-) diff --git a/exchanges/liqui/liqui.go b/exchanges/liqui/liqui.go index c9f8cc2f..573465a5 100644 --- a/exchanges/liqui/liqui.go +++ b/exchanges/liqui/liqui.go @@ -84,7 +84,6 @@ func (l *Liqui) Setup(exch config.ExchangeConfig) { // GetFee returns a fee for a specific currency func (l *Liqui) GetFee(currency string) (float64, error) { - log.Println(l.Info.Pairs) val, ok := l.Info.Pairs[common.StringToLower(currency)] if !ok { return 0, errors.New("currency does not exist") @@ -124,10 +123,12 @@ func (l *Liqui) GetInfo() (Info, error) { // currencyPair - example "eth_btc" func (l *Liqui) GetTicker(currencyPair string) (map[string]Ticker, error) { type Response struct { - Data map[string]Ticker + Data map[string]Ticker + Success int `json:"success"` + Error string `json:"error"` } - response := Response{} + response := Response{Data: make(map[string]Ticker)} req := fmt.Sprintf("%s/%s/%s/%s", liquiAPIPublicURL, liquiAPIPublicVersion, liquiTicker, currencyPair) return response.Data, @@ -139,10 +140,12 @@ func (l *Liqui) GetTicker(currencyPair string) (map[string]Ticker, error) { // displayed (150 by default). Is set to less than 2000. func (l *Liqui) GetDepth(currencyPair string) (Orderbook, error) { type Response struct { - Data map[string]Orderbook + Data map[string]Orderbook + Success int `json:"success"` + Error string `json:"error"` } - response := Response{} + response := Response{Data: make(map[string]Orderbook)} req := fmt.Sprintf("%s/%s/%s/%s", liquiAPIPublicURL, liquiAPIPublicVersion, liquiDepth, currencyPair) return response.Data[currencyPair], @@ -154,10 +157,12 @@ func (l *Liqui) GetDepth(currencyPair string) (Orderbook, error) { // displayed (150 by default). The maximum allowable value is 2000. func (l *Liqui) GetTrades(currencyPair string) ([]Trades, error) { type Response struct { - Data map[string][]Trades + Data map[string][]Trades + Success int `json:"success"` + Error string `json:"error"` } - response := Response{} + response := Response{Data: make(map[string][]Trades)} req := fmt.Sprintf("%s/%s/%s/%s", liquiAPIPublicURL, liquiAPIPublicVersion, liquiTrades, currencyPair) return response.Data[currencyPair], @@ -190,19 +195,21 @@ func (l *Liqui) Trade(pair, orderType string, amount, price float64) (float64, e // GetActiveOrders returns the list of your active orders. func (l *Liqui) GetActiveOrders(pair string) (map[string]ActiveOrders, error) { + result := make(map[string]ActiveOrders) + req := url.Values{} req.Add("pair", pair) - var result map[string]ActiveOrders return result, l.SendAuthenticatedHTTPRequest(liquiActiveOrders, req, &result) } // GetOrderInfo returns the information on particular order. func (l *Liqui) GetOrderInfo(OrderID int64) (map[string]OrderInfo, error) { + result := make(map[string]OrderInfo) + req := url.Values{} req.Add("order_id", strconv.FormatInt(OrderID, 10)) - var result map[string]OrderInfo return result, l.SendAuthenticatedHTTPRequest(liquiOrderInfo, req, &result) } @@ -223,11 +230,12 @@ func (l *Liqui) CancelOrder(OrderID int64) (bool, error) { // GetTradeHistory returns trade history func (l *Liqui) GetTradeHistory(vals url.Values, pair string) (map[string]TradeHistory, error) { + result := make(map[string]TradeHistory) + if pair != "" { vals.Add("pair", pair) } - var result map[string]TradeHistory return result, l.SendAuthenticatedHTTPRequest(liquiTradeHistory, vals, &result) } diff --git a/exchanges/liqui/liqui_test.go b/exchanges/liqui/liqui_test.go index dcb28cbd..ebb91d01 100644 --- a/exchanges/liqui/liqui_test.go +++ b/exchanges/liqui/liqui_test.go @@ -1,6 +1,7 @@ package liqui import ( + "log" "net/url" "testing" @@ -34,6 +35,7 @@ func TestSetup(t *testing.T) { } func TestGetFee(t *testing.T) { + t.Parallel() _, err := l.GetFee("usd") if err == nil { t.Error("Test Failed - liqui GetFee() error", err) @@ -41,6 +43,7 @@ func TestGetFee(t *testing.T) { } func TestGetAvailablePairs(t *testing.T) { + t.Parallel() v := l.GetAvailablePairs(false) if len(v) != 0 { t.Error("Test Failed - liqui GetFee() error") @@ -48,6 +51,7 @@ func TestGetAvailablePairs(t *testing.T) { } func TestGetInfo(t *testing.T) { + t.Parallel() _, err := l.GetInfo() if err != nil { t.Error("Test Failed - liqui GetInfo() error", err) @@ -55,6 +59,7 @@ func TestGetInfo(t *testing.T) { } func TestGetTicker(t *testing.T) { + t.Parallel() _, err := l.GetTicker("eth_btc") if err != nil { t.Error("Test Failed - liqui GetTicker() error", err) @@ -62,64 +67,57 @@ func TestGetTicker(t *testing.T) { } func TestGetDepth(t *testing.T) { - _, err := l.GetDepth("eth_btc") + t.Parallel() + v, err := l.GetDepth("eth_btc") if err != nil { t.Error("Test Failed - liqui GetDepth() error", err) } + log.Println(v) } func TestGetTrades(t *testing.T) { + t.Parallel() _, err := l.GetTrades("eth_btc") if err != nil { t.Error("Test Failed - liqui GetTrades() error", err) } } -func TestGetAccountInfo(t *testing.T) { - _, err := l.GetAccountInfo() - if err == nil { - t.Error("Test Failed - liqui GetAccountInfo() error", err) - } -} +func TestAuthRequests(t *testing.T) { + if l.APIKey != "" && l.APISecret != "" { + _, err := l.GetAccountInfo() + if err == nil { + t.Error("Test Failed - liqui GetAccountInfo() error", err) + } -func TestTrade(t *testing.T) { - _, err := l.Trade("", "", 0, 1) - if err == nil { - t.Error("Test Failed - liqui Trade() error", err) - } -} + _, err = l.Trade("", "", 0, 1) + if err == nil { + t.Error("Test Failed - liqui Trade() error", err) + } -func TestGetActiveOrders(t *testing.T) { - _, err := l.GetActiveOrders("eth_btc") - if err == nil { - t.Error("Test Failed - liqui GetActiveOrders() error", err) - } -} + _, err = l.GetActiveOrders("eth_btc") + if err == nil { + t.Error("Test Failed - liqui GetActiveOrders() error", err) + } -func TestGetOrderInfo(t *testing.T) { - _, err := l.GetOrderInfo(1337) - if err == nil { - t.Error("Test Failed - liqui GetOrderInfo() error", err) - } -} + _, err = l.GetOrderInfo(1337) + if err == nil { + t.Error("Test Failed - liqui GetOrderInfo() error", err) + } -func TestCancelOrder(t *testing.T) { - _, err := l.CancelOrder(1337) - if err == nil { - t.Error("Test Failed - liqui CancelOrder() error", err) - } -} + _, err = l.CancelOrder(1337) + if err == nil { + t.Error("Test Failed - liqui CancelOrder() error", err) + } -func TestGetTradeHistory(t *testing.T) { - _, err := l.GetTradeHistory(url.Values{}, "") - if err == nil { - t.Error("Test Failed - liqui GetTradeHistory() error", err) - } -} + _, err = l.GetTradeHistory(url.Values{}, "") + if err == nil { + t.Error("Test Failed - liqui GetTradeHistory() error", err) + } -func TestWithdrawCoins(t *testing.T) { - _, err := l.WithdrawCoins("btc", 1337, "someaddr") - if err == nil { - t.Error("Test Failed - liqui WithdrawCoins() error", err) + _, err = l.WithdrawCoins("btc", 1337, "someaddr") + if err == nil { + t.Error("Test Failed - liqui WithdrawCoins() error", err) + } } } diff --git a/exchanges/liqui/liqui_types.go b/exchanges/liqui/liqui_types.go index 52a8167d..4adbd0c5 100644 --- a/exchanges/liqui/liqui_types.go +++ b/exchanges/liqui/liqui_types.go @@ -4,6 +4,8 @@ package liqui type Info struct { ServerTime int64 `json:"server_time"` Pairs map[string]PairData `json:"pairs"` + Success int `json:"success"` + Error string `json:"error"` } // PairData is a sub-type for Info @@ -18,15 +20,15 @@ type PairData struct { // Ticker contains ticker information type Ticker struct { - High float64 - Low float64 - Avg float64 - Vol float64 - Vol_cur float64 - Last float64 - Buy float64 - Sell float64 - Updated int64 + High float64 + Low float64 + Avg float64 + Vol float64 + VolumeCurrency float64 + Last float64 + Buy float64 + Sell float64 + Updated int64 } // Orderbook references both ask and bid sides @@ -55,6 +57,8 @@ type AccountInfo struct { ServerTime float64 `json:"server_time"` TransactionCount int `json:"transaction_count"` OpenOrders int `json:"open_orders"` + Success int `json:"success"` + Error string `json:"error"` } // ActiveOrders holds active order information @@ -65,6 +69,8 @@ type ActiveOrders struct { Rate float64 `json:"rate"` TimestampCreated float64 `json:"time_created"` Status int `json:"status"` + Success int `json:"success"` + Error string `json:"error"` } // OrderInfo holds specific order information @@ -76,12 +82,16 @@ type OrderInfo struct { Rate float64 `json:"rate"` TimestampCreated float64 `json:"time_created"` Status int `json:"status"` + Success int `json:"success"` + Error string `json:"error"` } // CancelOrder holds cancelled order information type CancelOrder struct { OrderID float64 `json:"order_id"` Funds map[string]float64 `json:"funds"` + Success int `json:"success"` + Error string `json:"error"` } // Trade holds trading information @@ -90,6 +100,8 @@ type Trade struct { Remains float64 `json:"remains"` OrderID float64 `json:"order_id"` Funds map[string]float64 `json:"funds"` + Success int `json:"success"` + Error string `json:"error"` } // TradeHistory contains trade history data @@ -101,6 +113,8 @@ type TradeHistory struct { OrderID float64 `json:"order_id"` MyOrder int `json:"is_your_order"` Timestamp float64 `json:"timestamp"` + Success int `json:"success"` + Error string `json:"error"` } // Response is a generalized return type @@ -115,4 +129,6 @@ type WithdrawCoins struct { TID int64 `json:"tId"` AmountSent float64 `json:"amountSent"` Funds map[string]float64 `json:"funds"` + Success int `json:"success"` + Error string `json:"error"` } diff --git a/exchanges/liqui/liqui_wrapper.go b/exchanges/liqui/liqui_wrapper.go index ef750603..01325504 100644 --- a/exchanges/liqui/liqui_wrapper.go +++ b/exchanges/liqui/liqui_wrapper.go @@ -59,7 +59,7 @@ func (l *Liqui) UpdateTicker(p pair.CurrencyPair, assetType string) (ticker.Pric tp.Bid = result[currency].Buy tp.Last = result[currency].Last tp.Low = result[currency].Low - tp.Volume = result[currency].Vol_cur + tp.Volume = result[currency].VolumeCurrency ticker.ProcessTicker(l.Name, x, tp, assetType) }