From 6962f84748edee120bfbfdd78f5d6be0af675cea Mon Sep 17 00:00:00 2001 From: Ryan O'Hara-Reid Date: Mon, 26 Feb 2018 10:57:35 +1100 Subject: [PATCH] Fixed linter issues for Coinut exchange. --- exchanges/coinut/coinut.go | 75 ++++++++----- exchanges/coinut/coinut_test.go | 1 - exchanges/coinut/coinut_types.go | 157 ++++++++++++++++----------- exchanges/coinut/coinut_websocket.go | 3 +- exchanges/coinut/coinut_wrapper.go | 2 +- 5 files changed, 139 insertions(+), 99 deletions(-) diff --git a/exchanges/coinut/coinut.go b/exchanges/coinut/coinut.go index d9b0a35b..999de2b2 100644 --- a/exchanges/coinut/coinut.go +++ b/exchanges/coinut/coinut.go @@ -84,8 +84,8 @@ func (c *COINUT) Setup(exch config.ExchangeConfig) { } // GetInstruments returns instruments -func (c *COINUT) GetInstruments() (CoinutInstruments, error) { - var result CoinutInstruments +func (c *COINUT) GetInstruments() (Instruments, error) { + var result Instruments params := make(map[string]interface{}) params["sec_type"] = "SPOT" err := c.SendHTTPRequest(coinutInstruments, params, false, &result) @@ -95,8 +95,9 @@ func (c *COINUT) GetInstruments() (CoinutInstruments, error) { return result, nil } -func (c *COINUT) GetInstrumentTicker(instrumentID int) (CoinutTicker, error) { - var result CoinutTicker +// GetInstrumentTicker returns a ticker for a specific instrument +func (c *COINUT) GetInstrumentTicker(instrumentID int) (Ticker, error) { + var result Ticker params := make(map[string]interface{}) params["inst_id"] = instrumentID err := c.SendHTTPRequest(coinutTicker, params, false, &result) @@ -106,8 +107,9 @@ func (c *COINUT) GetInstrumentTicker(instrumentID int) (CoinutTicker, error) { return result, nil } -func (c *COINUT) GetInstrumentOrderbook(instrumentID, limit int) (CoinutOrderbook, error) { - var result CoinutOrderbook +// GetInstrumentOrderbook returns the orderbooks for a specific instrument +func (c *COINUT) GetInstrumentOrderbook(instrumentID, limit int) (Orderbook, error) { + var result Orderbook params := make(map[string]interface{}) params["inst_id"] = instrumentID if limit > 0 { @@ -120,8 +122,9 @@ func (c *COINUT) GetInstrumentOrderbook(instrumentID, limit int) (CoinutOrderboo return result, nil } -func (c *COINUT) GetTrades(instrumentID int) (CoinutTrades, error) { - var result CoinutTrades +// GetTrades returns trade information +func (c *COINUT) GetTrades(instrumentID int) (Trades, error) { + var result Trades params := make(map[string]interface{}) params["inst_id"] = instrumentID err := c.SendHTTPRequest(coinutTrades, params, false, &result) @@ -131,8 +134,9 @@ func (c *COINUT) GetTrades(instrumentID int) (CoinutTrades, error) { return result, nil } -func (c *COINUT) GetUserBalance() (CoinutUserBalance, error) { - result := CoinutUserBalance{} +// GetUserBalance returns the full user balance +func (c *COINUT) GetUserBalance() (UserBalance, error) { + result := UserBalance{} err := c.SendHTTPRequest(coinutBalance, nil, true, &result) if err != nil { return result, err @@ -140,6 +144,7 @@ func (c *COINUT) GetUserBalance() (CoinutUserBalance, error) { return result, nil } +// NewOrder places a new order on the exchange func (c *COINUT) NewOrder(instrumentID int, quantity, price float64, buy bool, orderID uint32) (interface{}, error) { var result interface{} params := make(map[string]interface{}) @@ -159,8 +164,9 @@ func (c *COINUT) NewOrder(instrumentID int, quantity, price float64, buy bool, o return result, nil } -func (c *COINUT) NewOrders(orders []CoinutOrder) ([]CoinutOrdersBase, error) { - var result CoinutOrdersResponse +// NewOrders places multiple orders on the exchange +func (c *COINUT) NewOrders(orders []Order) ([]OrdersBase, error) { + var result OrdersResponse params := make(map[string]interface{}) params["orders"] = orders err := c.SendHTTPRequest(coinutOrders, params, true, &result.Data) @@ -170,8 +176,9 @@ func (c *COINUT) NewOrders(orders []CoinutOrder) ([]CoinutOrdersBase, error) { return result.Data, nil } -func (c *COINUT) GetOpenOrders(instrumentID int) ([]CoinutOrdersResponse, error) { - var result []CoinutOrdersResponse +// GetOpenOrders returns a list of open order and relevant information +func (c *COINUT) GetOpenOrders(instrumentID int) ([]OrdersResponse, error) { + var result []OrdersResponse params := make(map[string]interface{}) params["inst_id"] = instrumentID err := c.SendHTTPRequest(coinutOrdersOpen, params, true, &result) @@ -181,8 +188,9 @@ func (c *COINUT) GetOpenOrders(instrumentID int) ([]CoinutOrdersResponse, error) return result, nil } +// CancelOrder cancels a specific order and returns if it was actioned func (c *COINUT) CancelOrder(instrumentID, orderID int) (bool, error) { - var result CoinutGenericResponse + var result GenericResponse params := make(map[string]interface{}) params["inst_id"] = instrumentID params["order_id"] = orderID @@ -193,8 +201,9 @@ func (c *COINUT) CancelOrder(instrumentID, orderID int) (bool, error) { return true, nil } -func (c *COINUT) CancelOrders(orders []CoinutCancelOrders) (CoinutCancelOrdersResponse, error) { - var result CoinutCancelOrdersResponse +// CancelOrders cancels multiple orders +func (c *COINUT) CancelOrders(orders []CancelOrders) (CancelOrdersResponse, error) { + var result CancelOrdersResponse params := make(map[string]interface{}) params["entries"] = orders err := c.SendHTTPRequest(coinutOrdersCancel, params, true, &result) @@ -204,8 +213,9 @@ func (c *COINUT) CancelOrders(orders []CoinutCancelOrders) (CoinutCancelOrdersRe return result, nil } -func (c *COINUT) GetTradeHistory(instrumentID, start, limit int) (CoinutTradeHistory, error) { - var result CoinutTradeHistory +// GetTradeHistory returns trade history for a specific instrument. +func (c *COINUT) GetTradeHistory(instrumentID, start, limit int) (TradeHistory, error) { + var result TradeHistory params := make(map[string]interface{}) params["inst_id"] = instrumentID if start >= 0 && start <= 100 { @@ -221,8 +231,9 @@ func (c *COINUT) GetTradeHistory(instrumentID, start, limit int) (CoinutTradeHis return result, nil } -func (c *COINUT) GetIndexTicker(asset string) (CoinutIndexTicker, error) { - var result CoinutIndexTicker +// GetIndexTicker returns the index ticker for an asset +func (c *COINUT) GetIndexTicker(asset string) (IndexTicker, error) { + var result IndexTicker params := make(map[string]interface{}) params["asset"] = asset err := c.SendHTTPRequest(coinutIndexTicker, params, false, &result) @@ -232,6 +243,7 @@ func (c *COINUT) GetIndexTicker(asset string) (CoinutIndexTicker, error) { return result, nil } +// GetDerivativeInstruments returns a list of derivative instruments func (c *COINUT) GetDerivativeInstruments(secType string) (interface{}, error) { var result interface{} //to-do params := make(map[string]interface{}) @@ -243,8 +255,9 @@ func (c *COINUT) GetDerivativeInstruments(secType string) (interface{}, error) { return result, nil } -func (c *COINUT) GetOptionChain(asset, secType string, expiry int64) (CoinutOptionChainResponse, error) { - var result CoinutOptionChainResponse +// GetOptionChain returns option chain +func (c *COINUT) GetOptionChain(asset, secType string, expiry int64) (OptionChainResponse, error) { + var result OptionChainResponse params := make(map[string]interface{}) params["asset"] = asset params["sec_type"] = secType @@ -255,8 +268,9 @@ func (c *COINUT) GetOptionChain(asset, secType string, expiry int64) (CoinutOpti return result, nil } -func (c *COINUT) GetPositionHistory(secType string, start, limit int) (CoinutPositionHistory, error) { - var result CoinutPositionHistory +// GetPositionHistory returns position history +func (c *COINUT) GetPositionHistory(secType string, start, limit int) (PositionHistory, error) { + var result PositionHistory params := make(map[string]interface{}) params["sec_type"] = secType if start >= 0 { @@ -272,9 +286,10 @@ func (c *COINUT) GetPositionHistory(secType string, start, limit int) (CoinutPos return result, nil } -func (c *COINUT) GetOpenPositions(instrumentID int) ([]CoinutOpenPosition, error) { +// GetOpenPositions returns all your current opened positions +func (c *COINUT) GetOpenPositions(instrumentID int) ([]OpenPosition, error) { type Response struct { - Positions []CoinutOpenPosition `json:"positions"` + Positions []OpenPosition `json:"positions"` } var result Response params := make(map[string]interface{}) @@ -289,6 +304,7 @@ func (c *COINUT) GetOpenPositions(instrumentID int) ([]CoinutOpenPosition, error //to-do: user position update via websocket +// SendHTTPRequest sends an authenticated HTTP request func (c *COINUT) SendHTTPRequest(apiRequest string, params map[string]interface{}, authenticated bool, result interface{}) (err error) { if !c.AuthenticatedAPISupport && authenticated { return fmt.Errorf(exchange.WarningAuthenticatedRequestWithoutCredentialsSet, c.Name) @@ -299,7 +315,6 @@ func (c *COINUT) SendHTTPRequest(apiRequest string, params map[string]interface{ } else { c.Nonce.Inc() } - payload := []byte("") if params == nil { params = map[string]interface{}{} @@ -307,7 +322,7 @@ func (c *COINUT) SendHTTPRequest(apiRequest string, params map[string]interface{ params["nonce"] = c.Nonce.Get() params["request"] = apiRequest - payload, err = common.JSONEncode(params) + payload, err := common.JSONEncode(params) if err != nil { return errors.New("SenddHTTPRequest: Unable to JSON request") } @@ -333,7 +348,7 @@ func (c *COINUT) SendHTTPRequest(apiRequest string, params map[string]interface{ log.Printf("Received raw: \n%s", resp) } - genResp := CoinutGenericResponse{} + genResp := GenericResponse{} err = common.JSONDecode([]byte(resp), &genResp) if err != nil { return errors.New("unable to JSON Unmarshal generic response") diff --git a/exchanges/coinut/coinut_test.go b/exchanges/coinut/coinut_test.go index ca8e0d5b..09c33b40 100644 --- a/exchanges/coinut/coinut_test.go +++ b/exchanges/coinut/coinut_test.go @@ -21,7 +21,6 @@ func TestSetDefaults(t *testing.T) { func TestSetup(t *testing.T) { t.Parallel() - c := COINUT{} c.Name = "Coinut" cfg := config.GetConfig() cfg.LoadConfig("../../testdata/configtest.json") diff --git a/exchanges/coinut/coinut_types.go b/exchanges/coinut/coinut_types.go index 10db1bf9..131eedba 100644 --- a/exchanges/coinut/coinut_types.go +++ b/exchanges/coinut/coinut_types.go @@ -1,6 +1,7 @@ package coinut -type CoinutGenericResponse struct { +// GenericResponse is the generic response you will get from coinut +type GenericResponse struct { Nonce int64 `json:"nonce"` Reply string `json:"reply"` Status []string `json:"status"` @@ -8,18 +9,21 @@ type CoinutGenericResponse struct { Timestamp int64 `json:"timestamp"` } -type CoinutInstrumentBase struct { +// InstrumentBase holds information on base currency +type InstrumentBase struct { Base string `json:"base"` DecimalPlaces int `json:"decimal_places"` InstID int `json:"inst_id"` Quote string `json:"quote"` } -type CoinutInstruments struct { - Instruments map[string][]CoinutInstrumentBase `json:"SPOT"` +// Instruments holds the full information on base currencies +type Instruments struct { + Instruments map[string][]InstrumentBase `json:"SPOT"` } -type CoinutTicker struct { +// Ticker holds ticker information +type Ticker struct { HighestBuy float64 `json:"highest_buy,string"` InstrumentID int `json:"inst_id"` Last float64 `json:"last,string"` @@ -31,22 +35,25 @@ type CoinutTicker struct { Volume24 float64 `json:"volume24,string"` } -type CoinutOrderbookBase struct { +// OrderbookBase is a sub-type holding price and quantity +type OrderbookBase struct { Count int `json:"count"` Price float64 `json:"price,string"` Quantity float64 `json:"qty,string"` } -type CoinutOrderbook struct { - Buy []CoinutOrderbookBase `json:"buy"` - Sell []CoinutOrderbookBase `json:"sell"` - InstrumentID int `json:"inst_id"` - TotalBuy float64 `json:"total_buy,string"` - TotalSell float64 `json:"total_sell,string"` - TransID int64 `json:"trans_id"` +// Orderbook is the full order book +type Orderbook struct { + Buy []OrderbookBase `json:"buy"` + Sell []OrderbookBase `json:"sell"` + InstrumentID int `json:"inst_id"` + TotalBuy float64 `json:"total_buy,string"` + TotalSell float64 `json:"total_sell,string"` + TransID int64 `json:"trans_id"` } -type CoinutTradeBase struct { +// TradeBase is a sub-type holding information on trades +type TradeBase struct { Price float64 `json:"price,string"` Quantity float64 `json:"quantity,string"` Side string `json:"side"` @@ -54,11 +61,13 @@ type CoinutTradeBase struct { TransID int64 `json:"trans_id"` } -type CoinutTrades struct { - Trades []CoinutTradeBase `json:"trades"` +// Trades holds the full amount of trades associated with API keys +type Trades struct { + Trades []TradeBase `json:"trades"` } -type CoinutUserBalance struct { +// UserBalance holds user balances on the exchange +type UserBalance struct { BTC float64 `json:"btc,string"` ETC float64 `json:"etc,string"` ETH float64 `json:"eth,string"` @@ -71,7 +80,8 @@ type CoinutUserBalance struct { UnrealizedPL float64 `json:"unrealized_pl,string"` } -type CoinutOrder struct { +// Order holds order information +type Order struct { InstrumentID int64 `json:"inst_id"` Price float64 `json:"price,string"` Quantity float64 `json:"qty,string"` @@ -79,7 +89,8 @@ type CoinutOrder struct { Side string `json:"side,string"` } -type CoinutOrderResponse struct { +// OrderResponse is a response for orders +type OrderResponse struct { OrderID int64 `json:"order_id"` OpenQuantity float64 `json:"open_qty,string"` Price float64 `json:"price,string"` @@ -91,40 +102,47 @@ type CoinutOrderResponse struct { Side string `json:"side"` } -type CoinutCommission struct { +// Commission holds trade commision structure +type Commission struct { Currency string `json:"currency"` Amount float64 `json:"amount,string"` } -type CoinutOrderFilledResponse struct { - CoinutGenericResponse - Commission CoinutCommission `json:"commission"` - FillPrice float64 `json:"fill_price,string"` - FillQuantity float64 `json:"fill_qty,string"` - Order CoinutOrderResponse `json:"order"` +// OrderFilledResponse contains order filled response +type OrderFilledResponse struct { + GenericResponse + Commission Commission `json:"commission"` + FillPrice float64 `json:"fill_price,string"` + FillQuantity float64 `json:"fill_qty,string"` + Order OrderResponse `json:"order"` } -type CoinutOrderRejectResponse struct { - CoinutOrderResponse +// OrderRejectResponse holds information on a rejected order +type OrderRejectResponse struct { + OrderResponse Reasons []string `json:"reasons"` } -type CoinutOrdersBase struct { - CoinutGenericResponse - CoinutOrderResponse +// OrdersBase contains generic response and order responses +type OrdersBase struct { + GenericResponse + OrderResponse } -type CoinutOrdersResponse struct { - Data []CoinutOrdersBase +// OrdersResponse holds the full data range on orders +type OrdersResponse struct { + Data []OrdersBase } -type CoinutCancelOrders struct { +// CancelOrders holds information about a cancelled order +type CancelOrders struct { InstrumentID int `json:"int"` OrderID int64 `json:"order_id"` } -type CoinutCancelOrdersResponse struct { - CoinutGenericResponse +// CancelOrdersResponse is response for a cancelled order +type CancelOrdersResponse struct { + GenericResponse Results []struct { OrderID int64 `json:"order_id"` Status string `json:"status"` @@ -132,17 +150,20 @@ type CoinutCancelOrdersResponse struct { } `json:"results"` } -type CoinutTradeHistory struct { - TotalNumber int64 `json:"total_number"` - Trades []CoinutOrderFilledResponse `json:"trades"` +// TradeHistory holds trade history information +type TradeHistory struct { + TotalNumber int64 `json:"total_number"` + Trades []OrderFilledResponse `json:"trades"` } -type CoinutIndexTicker struct { +// IndexTicker holds indexed ticker inforamtion +type IndexTicker struct { Asset string `json:"asset"` Price float64 `json:"price,string"` } -type CoinutOption struct { +// Option holds options information +type Option struct { HighestBuy float64 `json:"highest_buy,string"` InstrumentID int `json:"inst_id"` Last float64 `json:"last,string"` @@ -150,40 +171,43 @@ type CoinutOption struct { OpenInterest float64 `json:"open_interest,string"` } -type CoinutOptionChainResponse struct { +// OptionChainResponse is the response type for options +type OptionChainResponse struct { ExpiryTime int64 `json:"expiry_time"` SecurityType string `json:"sec_type"` Asset string `json:"asset"` Entries []struct { - Call CoinutOption `json:"call"` - Put CoinutOption `json:"put"` - Strike float64 `json:"strike,string"` + Call Option `json:"call"` + Put Option `json:"put"` + Strike float64 `json:"strike,string"` } } -type CoinutOptionChainUpdate struct { - CoinutOption - CoinutGenericResponse +// OptionChainUpdate contains information on the chain update options +type OptionChainUpdate struct { + Option + GenericResponse Asset string `json:"asset"` ExpiryTime int64 `json:"expiry_time"` SecurityType string `json:"sec_type"` Volume float64 `json:"volume,string"` } -type CoinutPositionHistory struct { +// PositionHistory holds the complete position history +type PositionHistory struct { Positions []struct { PositionID int `json:"position_id"` Records []struct { - Commission CoinutCommission `json:"commission"` - FillPrice float64 `json:"fill_price,string,omitempty"` - TransactionID int `json:"trans_id"` - FillQuantity float64 `json:"fill_qty,omitempty"` + Commission Commission `json:"commission"` + FillPrice float64 `json:"fill_price,string,omitempty"` + TransactionID int `json:"trans_id"` + FillQuantity float64 `json:"fill_qty,omitempty"` Position struct { - Commission CoinutCommission `json:"commission"` - Timestamp int64 `json:"timestamp"` - OpenPrice float64 `json:"open_price,string"` - RealizedPL float64 `json:"realized_pl,string"` - Quantity float64 `json:"qty,string"` + Commission Commission `json:"commission"` + Timestamp int64 `json:"timestamp"` + OpenPrice float64 `json:"open_price,string"` + RealizedPL float64 `json:"realized_pl,string"` + Quantity float64 `json:"qty,string"` } `json:"position"` AssetAtExpiry float64 `json:"asset_at_expiry,string,omitempty"` } `json:"records"` @@ -202,12 +226,13 @@ type CoinutPositionHistory struct { TotalNumber int `json:"total_number"` } -type CoinutOpenPosition struct { - PositionID int `json:"position_id"` - Commission CoinutCommission `json:"commission"` - OpenPrice float64 `json:"open_price,string"` - RealizedPL float64 `json:"realized_pl,string"` - Quantity float64 `json:"qty,string"` - OpenTimestamp int64 `json:"open_timestamp"` - InstrumentID int `json:"inst_id"` +// OpenPosition holds information on an open position +type OpenPosition struct { + PositionID int `json:"position_id"` + Commission Commission `json:"commission"` + OpenPrice float64 `json:"open_price,string"` + RealizedPL float64 `json:"realized_pl,string"` + Quantity float64 `json:"qty,string"` + OpenTimestamp int64 `json:"open_timestamp"` + InstrumentID int `json:"inst_id"` } diff --git a/exchanges/coinut/coinut_websocket.go b/exchanges/coinut/coinut_websocket.go index 53e8c9a1..b1d350eb 100644 --- a/exchanges/coinut/coinut_websocket.go +++ b/exchanges/coinut/coinut_websocket.go @@ -8,8 +8,9 @@ import ( "github.com/thrasher-/gocryptotrader/common" ) -const COINUT_WEBSOCKET_URL = "wss://wsapi.coinut.com" +const coinutWebsocketURL = "wss://wsapi.coinut.com" +// WebsocketClient initiates a websocket client func (c *COINUT) WebsocketClient() { for c.Enabled && c.Websocket { var Dialer websocket.Dialer diff --git a/exchanges/coinut/coinut_wrapper.go b/exchanges/coinut/coinut_wrapper.go index feb64f97..74bd87fb 100644 --- a/exchanges/coinut/coinut_wrapper.go +++ b/exchanges/coinut/coinut_wrapper.go @@ -19,7 +19,7 @@ func (c *COINUT) Start() { // Run implements the COINUT wrapper func (c *COINUT) Run() { if c.Verbose { - log.Printf("%s Websocket: %s. (url: %s).\n", c.GetName(), common.IsEnabled(c.Websocket), COINUT_WEBSOCKET_URL) + log.Printf("%s Websocket: %s. (url: %s).\n", c.GetName(), common.IsEnabled(c.Websocket), coinutWebsocketURL) log.Printf("%s polling delay: %ds.\n", c.GetName(), c.RESTPollingDelay) log.Printf("%s %d currencies enabled: %s.\n", c.GetName(), len(c.EnabledPairs), c.EnabledPairs) }