Liqui Package - Fixed linter issues and expanded code cov.

This commit is contained in:
Ryan O'Hara-Reid
2017-09-13 12:36:32 +10:00
parent e8c7bf9af4
commit b3bef011f4
3 changed files with 270 additions and 152 deletions

View File

@@ -16,29 +16,31 @@ import (
)
const (
LIQUI_API_PUBLIC_URL = "https://api.Liqui.io/api"
LIQUI_API_PRIVATE_URL = "https://api.Liqui.io/tapi"
LIQUI_API_PUBLIC_VERSION = "3"
LIQUI_API_PRIVATE_VERSION = "1"
LIQUI_INFO = "info"
LIQUI_TICKER = "ticker"
LIQUI_DEPTH = "depth"
LIQUI_TRADES = "trades"
LIQUI_ACCOUNT_INFO = "getInfo"
LIQUI_TRADE = "Trade"
LIQUI_ACTIVE_ORDERS = "ActiveOrders"
LIQUI_ORDER_INFO = "OrderInfo"
LIQUI_CANCEL_ORDER = "CancelOrder"
LIQUI_TRADE_HISTORY = "TradeHistory"
LIQUI_WITHDRAW_COIN = "WithdrawCoin"
liquiAPIPublicURL = "https://api.Liqui.io/api"
liquiAPIPrivateURL = "https://api.Liqui.io/tapi"
liquiAPIPublicVersion = "3"
liquiAPIPrivateVersion = "1"
liquiInfo = "info"
liquiTicker = "ticker"
liquiDepth = "depth"
liquiTrades = "trades"
liquiAccountInfo = "getInfo"
liquiTrade = "Trade"
liquiActiveOrders = "ActiveOrders"
liquiOrderInfo = "OrderInfo"
liquiCancelOrder = "CancelOrder"
liquiTradeHistory = "TradeHistory"
liquiWithdrawCoin = "WithdrawCoin"
)
// Liqui is the overarching type across the liqui package
type Liqui struct {
exchange.Base
Ticker map[string]LiquiTicker
Info LiquiInfo
Ticker map[string]Ticker
Info Info
}
// SetDefaults sets current default values for liqui
func (l *Liqui) SetDefaults() {
l.Name = "Liqui"
l.Enabled = false
@@ -46,7 +48,7 @@ func (l *Liqui) SetDefaults() {
l.Verbose = false
l.Websocket = false
l.RESTPollingDelay = 10
l.Ticker = make(map[string]LiquiTicker)
l.Ticker = make(map[string]Ticker)
l.RequestCurrencyPairFormat.Delimiter = "_"
l.RequestCurrencyPairFormat.Uppercase = false
l.RequestCurrencyPairFormat.Separator = "-"
@@ -55,6 +57,7 @@ func (l *Liqui) SetDefaults() {
l.AssetTypes = []string{ticker.Spot}
}
// Setup sets exchange configuration parameters for liqui
func (l *Liqui) Setup(exch config.ExchangeConfig) {
if !exch.Enabled {
l.SetEnabled(false)
@@ -79,15 +82,18 @@ 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")
return 0, errors.New("currency does not exist")
}
return val.Fee, nil
}
// GetAvailablePairs returns all available pairs
func (l *Liqui) GetAvailablePairs(nonHidden bool) []string {
var pairs []string
for x, y := range l.Info.Pairs {
@@ -99,79 +105,77 @@ func (l *Liqui) GetAvailablePairs(nonHidden bool) []string {
return pairs
}
func (l *Liqui) GetInfo() (LiquiInfo, error) {
req := fmt.Sprintf("%s/%s/%s/", LIQUI_API_PUBLIC_URL, LIQUI_API_PUBLIC_VERSION, LIQUI_INFO)
resp := LiquiInfo{}
err := common.SendHTTPGetRequest(req, true, l.Verbose, &resp)
// GetInfo provides all the information about currently active pairs, such as
// the maximum number of digits after the decimal point, the minimum price, the
// maximum price, the minimum transaction size, whether the pair is hidden, the
// commission for each pair.
func (l *Liqui) GetInfo() (Info, error) {
resp := Info{}
req := fmt.Sprintf("%s/%s/%s/", liquiAPIPublicURL, liquiAPIPublicVersion, liquiInfo)
if err != nil {
return resp, err
}
return resp, nil
return resp, common.SendHTTPGetRequest(req, true, l.Verbose, &resp)
}
func (l *Liqui) GetTicker(symbol string) (map[string]LiquiTicker, error) {
// GetTicker returns information about currently active pairs, such as: the
// maximum price, the minimum price, average price, trade volume, trade volume
// in currency, the last trade, Buy and Sell price. All information is provided
// over the past 24 hours.
//
// currencyPair - example "eth_btc"
func (l *Liqui) GetTicker(currencyPair string) (map[string]Ticker, error) {
type Response struct {
Data map[string]LiquiTicker
Data map[string]Ticker
}
response := Response{}
req := fmt.Sprintf("%s/%s/%s/%s", LIQUI_API_PUBLIC_URL, LIQUI_API_PUBLIC_VERSION, LIQUI_TICKER, symbol)
err := common.SendHTTPGetRequest(req, true, l.Verbose, &response.Data)
req := fmt.Sprintf("%s/%s/%s/%s", liquiAPIPublicURL, liquiAPIPublicVersion, liquiTicker, currencyPair)
if err != nil {
return nil, err
}
return response.Data, nil
return response.Data,
common.SendHTTPGetRequest(req, true, l.Verbose, &response.Data)
}
func (l *Liqui) GetDepth(symbol string) (LiquiOrderbook, error) {
// GetDepth information about active orders on the pair. Additionally it accepts
// an optional GET-parameter limit, which indicates how many orders should be
// displayed (150 by default). Is set to less than 2000.
func (l *Liqui) GetDepth(currencyPair string) (Orderbook, error) {
type Response struct {
Data map[string]LiquiOrderbook
Data map[string]Orderbook
}
response := Response{}
req := fmt.Sprintf("%s/%s/%s/%s", LIQUI_API_PUBLIC_URL, LIQUI_API_PUBLIC_VERSION, LIQUI_DEPTH, symbol)
req := fmt.Sprintf("%s/%s/%s/%s", liquiAPIPublicURL, liquiAPIPublicVersion, liquiDepth, currencyPair)
err := common.SendHTTPGetRequest(req, true, l.Verbose, &response.Data)
if err != nil {
return LiquiOrderbook{}, err
}
depth := response.Data[symbol]
return depth, nil
return response.Data[currencyPair],
common.SendHTTPGetRequest(req, true, l.Verbose, &response.Data)
}
func (l *Liqui) GetTrades(symbol string) ([]LiquiTrades, error) {
// GetTrades returns information about the last trades. Additionally it accepts
// an optional GET-parameter limit, which indicates how many orders should be
// displayed (150 by default). The maximum allowable value is 2000.
func (l *Liqui) GetTrades(currencyPair string) ([]Trades, error) {
type Response struct {
Data map[string][]LiquiTrades
Data map[string][]Trades
}
response := Response{}
req := fmt.Sprintf("%s/%s/%s/%s", LIQUI_API_PUBLIC_URL, LIQUI_API_PUBLIC_VERSION, LIQUI_TRADES, symbol)
req := fmt.Sprintf("%s/%s/%s/%s", liquiAPIPublicURL, liquiAPIPublicVersion, liquiTrades, currencyPair)
err := common.SendHTTPGetRequest(req, true, l.Verbose, &response.Data)
if err != nil {
return []LiquiTrades{}, err
}
trades := response.Data[symbol]
return trades, nil
return response.Data[currencyPair],
common.SendHTTPGetRequest(req, true, l.Verbose, &response.Data)
}
func (l *Liqui) GetAccountInfo() (LiquiAccountInfo, error) {
var result LiquiAccountInfo
err := l.SendAuthenticatedHTTPRequest(LIQUI_ACCOUNT_INFO, url.Values{}, &result)
// GetAccountInfo returns information about the users current balance, API-key
// privileges, the number of open orders and Server Time. To use this method you
// need a privilege of the key info.
func (l *Liqui) GetAccountInfo() (AccountInfo, error) {
var result AccountInfo
if err != nil {
return result, err
}
return result, nil
return result,
l.SendAuthenticatedHTTPRequest(liquiAccountInfo, url.Values{}, &result)
}
//to-do: convert orderid to int64
// Trade creates orders on the exchange.
// to-do: convert orderid to int64
func (l *Liqui) Trade(pair, orderType string, amount, price float64) (float64, error) {
req := url.Values{}
req.Add("pair", pair)
@@ -179,51 +183,37 @@ func (l *Liqui) Trade(pair, orderType string, amount, price float64) (float64, e
req.Add("amount", strconv.FormatFloat(amount, 'f', -1, 64))
req.Add("rate", strconv.FormatFloat(price, 'f', -1, 64))
var result LiquiTrade
err := l.SendAuthenticatedHTTPRequest(LIQUI_TRADE, req, &result)
var result Trade
if err != nil {
return 0, err
}
return result.OrderID, nil
return result.OrderID, l.SendAuthenticatedHTTPRequest(liquiTrade, req, &result)
}
func (l *Liqui) GetActiveOrders(pair string) (map[string]LiquiActiveOrders, error) {
// GetActiveOrders returns the list of your active orders.
func (l *Liqui) GetActiveOrders(pair string) (map[string]ActiveOrders, error) {
req := url.Values{}
req.Add("pair", pair)
var result map[string]LiquiActiveOrders
err := l.SendAuthenticatedHTTPRequest(LIQUI_ACTIVE_ORDERS, req, &result)
if err != nil {
return result, err
}
return result, nil
var result map[string]ActiveOrders
return result, l.SendAuthenticatedHTTPRequest(liquiActiveOrders, req, &result)
}
func (l *Liqui) GetOrderInfo(OrderID int64) (map[string]LiquiOrderInfo, error) {
// GetOrderInfo returns the information on particular order.
func (l *Liqui) GetOrderInfo(OrderID int64) (map[string]OrderInfo, error) {
req := url.Values{}
req.Add("order_id", strconv.FormatInt(OrderID, 10))
var result map[string]LiquiOrderInfo
err := l.SendAuthenticatedHTTPRequest(LIQUI_ORDER_INFO, req, &result)
if err != nil {
return result, err
}
return result, nil
var result map[string]OrderInfo
return result, l.SendAuthenticatedHTTPRequest(liquiOrderInfo, req, &result)
}
// CancelOrder method is used for order cancelation.
func (l *Liqui) CancelOrder(OrderID int64) (bool, error) {
req := url.Values{}
req.Add("order_id", strconv.FormatInt(OrderID, 10))
var result LiquiCancelOrder
err := l.SendAuthenticatedHTTPRequest(LIQUI_CANCEL_ORDER, req, &result)
var result CancelOrder
err := l.SendAuthenticatedHTTPRequest(liquiCancelOrder, req, &result)
if err != nil {
return false, err
}
@@ -231,38 +221,30 @@ func (l *Liqui) CancelOrder(OrderID int64) (bool, error) {
return true, nil
}
func (l *Liqui) GetTradeHistory(vals url.Values, pair string) (map[string]LiquiTradeHistory, error) {
// GetTradeHistory returns trade history
func (l *Liqui) GetTradeHistory(vals url.Values, pair string) (map[string]TradeHistory, error) {
if pair != "" {
vals.Add("pair", pair)
}
var result map[string]LiquiTradeHistory
err := l.SendAuthenticatedHTTPRequest(LIQUI_TRADE_HISTORY, vals, &result)
if err != nil {
return result, err
}
return result, nil
var result map[string]TradeHistory
return result, l.SendAuthenticatedHTTPRequest(liquiTradeHistory, vals, &result)
}
// WithdrawCoins is designed for cryptocurrency withdrawals.
// API mentions that this isn't active now, but will be soon - you must provide the first 8 characters of the key
// in your ticket to support.
func (l *Liqui) WithdrawCoins(coin string, amount float64, address string) (LiquiWithdrawCoins, error) {
func (l *Liqui) WithdrawCoins(coin string, amount float64, address string) (WithdrawCoins, error) {
req := url.Values{}
req.Add("coinName", coin)
req.Add("amount", strconv.FormatFloat(amount, 'f', -1, 64))
req.Add("address", address)
var result LiquiWithdrawCoins
err := l.SendAuthenticatedHTTPRequest(LIQUI_WITHDRAW_COIN, req, &result)
if err != nil {
return result, err
}
return result, nil
var result WithdrawCoins
return result, l.SendAuthenticatedHTTPRequest(liquiWithdrawCoin, req, &result)
}
// SendAuthenticatedHTTPRequest sends an authenticated http request to liqui
func (l *Liqui) SendAuthenticatedHTTPRequest(method string, values url.Values, result interface{}) (err error) {
if !l.AuthenticatedAPISupport {
return fmt.Errorf(exchange.WarningAuthenticatedRequestWithoutCredentialsSet, l.Name)
@@ -280,7 +262,7 @@ func (l *Liqui) SendAuthenticatedHTTPRequest(method string, values url.Values, r
hmac := common.GetHMAC(common.HashSHA512, []byte(encoded), []byte(l.APISecret))
if l.Verbose {
log.Printf("Sending POST request to %s calling method %s with params %s\n", LIQUI_API_PRIVATE_URL, method, encoded)
log.Printf("Sending POST request to %s calling method %s with params %s\n", liquiAPIPrivateURL, method, encoded)
}
headers := make(map[string]string)
@@ -288,15 +270,14 @@ func (l *Liqui) SendAuthenticatedHTTPRequest(method string, values url.Values, r
headers["Sign"] = common.HexEncodeToString(hmac)
headers["Content-Type"] = "application/x-www-form-urlencoded"
resp, err := common.SendHTTPRequest("POST", LIQUI_API_PRIVATE_URL, headers, strings.NewReader(encoded))
resp, err := common.SendHTTPRequest("POST", liquiAPIPrivateURL, headers, strings.NewReader(encoded))
if err != nil {
return err
}
response := LiquiResponse{}
err = common.JSONDecode([]byte(resp), &response)
response := Response{}
err = common.JSONDecode([]byte(resp), &response)
if err != nil {
return err
}
@@ -306,15 +287,14 @@ func (l *Liqui) SendAuthenticatedHTTPRequest(method string, values url.Values, r
}
jsonEncoded, err := common.JSONEncode(response.Return)
if err != nil {
return err
}
err = common.JSONDecode(jsonEncoded, &result)
if err != nil {
return err
}
return nil
}

View File

@@ -0,0 +1,125 @@
package liqui
import (
"net/url"
"testing"
"github.com/thrasher-/gocryptotrader/config"
)
var l Liqui
const (
apiKey = ""
apiSecret = ""
)
func TestSetDefaults(t *testing.T) {
l.SetDefaults()
}
func TestSetup(t *testing.T) {
cfg := config.GetConfig()
cfg.LoadConfig("../../testdata/configtest.dat")
liquiConfig, err := cfg.GetExchangeConfig("Liqui")
if err != nil {
t.Error("Test Failed - liqui Setup() init error")
}
liquiConfig.AuthenticatedAPISupport = true
liquiConfig.APIKey = apiKey
liquiConfig.APISecret = apiSecret
l.Setup(liquiConfig)
}
func TestGetFee(t *testing.T) {
_, err := l.GetFee("usd")
if err == nil {
t.Error("Test Failed - liqui GetFee() error", err)
}
}
func TestGetAvailablePairs(t *testing.T) {
v := l.GetAvailablePairs(false)
if len(v) != 0 {
t.Error("Test Failed - liqui GetFee() error")
}
}
func TestGetInfo(t *testing.T) {
_, err := l.GetInfo()
if err != nil {
t.Error("Test Failed - liqui GetInfo() error", err)
}
}
func TestGetTicker(t *testing.T) {
_, err := l.GetTicker("eth_btc")
if err != nil {
t.Error("Test Failed - liqui GetTicker() error", err)
}
}
func TestGetDepth(t *testing.T) {
_, err := l.GetDepth("eth_btc")
if err != nil {
t.Error("Test Failed - liqui GetDepth() error", err)
}
}
func TestGetTrades(t *testing.T) {
_, 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 TestTrade(t *testing.T) {
_, 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)
}
}
func TestGetOrderInfo(t *testing.T) {
_, 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)
}
}
func TestGetTradeHistory(t *testing.T) {
_, 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)
}
}

View File

@@ -1,6 +1,23 @@
package liqui
type LiquiTicker struct {
// Info holds the current pair information as well as server time
type Info struct {
ServerTime int64 `json:"server_time"`
Pairs map[string]PairData `json:"pairs"`
}
// PairData is a sub-type for Info
type PairData struct {
DecimalPlaces int `json:"decimal_places"`
MinPrice float64 `json:"min_price"`
MaxPrice float64 `json:"max_price"`
MinAmount float64 `json:"min_amount"`
Hidden int `json:"hidden"`
Fee float64 `json:"fee"`
}
// Ticker contains ticker information
type Ticker struct {
High float64
Low float64
Avg float64
@@ -12,12 +29,14 @@ type LiquiTicker struct {
Updated int64
}
type LiquiOrderbook struct {
// Orderbook references both ask and bid sides
type Orderbook struct {
Asks [][]float64 `json:"asks"`
Bids [][]float64 `json:"bids"`
}
type LiquiTrades struct {
// Trades contains trade information
type Trades struct {
Type string `json:"type"`
Price float64 `json:"bid"`
Amount float64 `json:"amount"`
@@ -25,27 +44,8 @@ type LiquiTrades struct {
Timestamp int64 `json:"timestamp"`
}
type LiquiResponse struct {
Return interface{} `json:"return"`
Success int `json:"success"`
Error string `json:"error"`
}
type LiquiPair struct {
DecimalPlaces int `json:"decimal_places"`
MinPrice float64 `json:"min_price"`
MaxPrice float64 `json:"max_price"`
MinAmount float64 `json:"min_amount"`
Hidden int `json:"hidden"`
Fee float64 `json:"fee"`
}
type LiquiInfo struct {
ServerTime int64 `json:"server_time"`
Pairs map[string]LiquiPair `json:"pairs"`
}
type LiquiAccountInfo struct {
// AccountInfo contains full account details information
type AccountInfo struct {
Funds map[string]float64 `json:"funds"`
Rights struct {
Info bool `json:"info"`
@@ -57,14 +57,8 @@ type LiquiAccountInfo struct {
OpenOrders int `json:"open_orders"`
}
type LiquiTrade struct {
Received float64 `json:"received"`
Remains float64 `json:"remains"`
OrderID float64 `json:"order_id"`
Funds map[string]float64 `json:"funds"`
}
type LiquiActiveOrders struct {
// ActiveOrders holds active order information
type ActiveOrders struct {
Pair string `json:"pair"`
Type string `json:"sell"`
Amount float64 `json:"amount"`
@@ -73,7 +67,8 @@ type LiquiActiveOrders struct {
Status int `json:"status"`
}
type LiquiOrderInfo struct {
// OrderInfo holds specific order information
type OrderInfo struct {
Pair string `json:"pair"`
Type string `json:"sell"`
StartAmount float64 `json:"start_amount"`
@@ -83,12 +78,22 @@ type LiquiOrderInfo struct {
Status int `json:"status"`
}
type LiquiCancelOrder struct {
// CancelOrder holds cancelled order information
type CancelOrder struct {
OrderID float64 `json:"order_id"`
Funds map[string]float64 `json:"funds"`
}
type LiquiTradeHistory struct {
// Trade holds trading information
type Trade struct {
Received float64 `json:"received"`
Remains float64 `json:"remains"`
OrderID float64 `json:"order_id"`
Funds map[string]float64 `json:"funds"`
}
// TradeHistory contains trade history data
type TradeHistory struct {
Pair string `json:"pair"`
Type string `json:"type"`
Amount float64 `json:"amount"`
@@ -98,7 +103,15 @@ type LiquiTradeHistory struct {
Timestamp float64 `json:"timestamp"`
}
type LiquiWithdrawCoins struct {
// Response is a generalized return type
type Response struct {
Return interface{} `json:"return"`
Success int `json:"success"`
Error string `json:"error"`
}
// WithdrawCoins shows the amount of coins withdrawn from liqui not yet available
type WithdrawCoins struct {
TID int64 `json:"tId"`
AmountSent float64 `json:"amountSent"`
Funds map[string]float64 `json:"funds"`