(Engine) Variety of engine updates (#390)

* drop common uuid v4 func and imported package as needed

* removed common functions regarding json marshal and unmarshal and used the json package directly. WRT unmarshal it was calling reflect and converted to string which is also checked in the JSON package so it was doing a double up, this will be a tiny gain as it was directly used in the requester package for all our outbound requests.

* add in string

* explicitly throw away return error value

* atleast return the error that websocket initialise returns

* return error when not connected

* fix comment

* Adds comments

* move package declarations

* drop append whenever we call supported

* remove unused import

* Change incorrect spelling

* fix tests

* fix go import issue
This commit is contained in:
Ryan O'Hara-Reid
2019-12-03 10:06:08 +11:00
committed by Adrian Gallagher
parent c27b8657e2
commit 0c5d75b22c
70 changed files with 393 additions and 462 deletions

View File

@@ -2,6 +2,7 @@ package alphapoint
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
@@ -9,7 +10,6 @@ import (
"strings"
"github.com/gorilla/websocket"
"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/common/crypto"
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
@@ -513,7 +513,7 @@ func (a *Alphapoint) SendHTTPRequest(method, path string, data map[string]interf
headers["Content-Type"] = "application/json"
path = fmt.Sprintf("%s/ajax/v%s/%s", a.API.Endpoints.URL, alphapointAPIVersion, path)
PayloadJSON, err := common.JSONEncode(data)
PayloadJSON, err := json.Marshal(data)
if err != nil {
return errors.New("unable to JSON request")
}
@@ -548,7 +548,7 @@ func (a *Alphapoint) SendAuthenticatedHTTPRequest(method, path string, data map[
data["apiSig"] = strings.ToUpper(crypto.HexEncodeToString(hmac))
path = fmt.Sprintf("%s/ajax/v%s/%s", a.API.Endpoints.URL, alphapointAPIVersion, path)
PayloadJSON, err := common.JSONEncode(data)
PayloadJSON, err := json.Marshal(data)
if err != nil {
return errors.New("unable to JSON request")
}

View File

@@ -1,6 +1,7 @@
package alphapoint
import (
"encoding/json"
"testing"
"github.com/thrasher-corp/gocryptotrader/common"
@@ -63,7 +64,7 @@ func TestGetTicker(t *testing.T) {
string(`{"high":253.101,"last":249.76,"bid":248.8901,"volume":5.813354,"low":231.21,"ask":248.9012,"Total24HrQtyTraded":52.654968,"Total24HrProduct2Traded":569.05762,"Total24HrNumTrades":4,"sellOrderCount":7,"buyOrderCount":11,"numOfCreateOrders":0,"isAccepted":true}`),
)
err = common.JSONDecode(mockResp, &ticker)
err = json.Unmarshal(mockResp, &ticker)
if err != nil {
t.Fatal("Alphapoint GetTicker unmarshalling error: ", err)
}
@@ -100,7 +101,7 @@ func TestGetTrades(t *testing.T) {
string(`{"isAccepted":true,"dateTimeUtc":635507981548085938,"ins":"BTCUSD","startIndex":0,"count":10,"trades":[{"tid":0,"px":231.8379,"qty":4.913,"unixtime":1399951989,"utcticks":635355487898355234,"incomingOrderSide":0,"incomingServerOrderId":2598,"bookServerOrderId":2588},{"tid":1,"px":7895.1487,"qty":0.25,"unixtime":1403143708,"utcticks":635387405087297421,"incomingOrderSide":0,"incomingServerOrderId":284241,"bookServerOrderId":284235},{"tid":2,"px":7935.058,"qty":0.25,"unixtime":1403195348,"utcticks":635387921488684140,"incomingOrderSide":0,"incomingServerOrderId":575845,"bookServerOrderId":574078},{"tid":3,"px":7935.0448,"qty":0.25,"unixtime":1403195378,"utcticks":635387921780090390,"incomingOrderSide":0,"incomingServerOrderId":576028,"bookServerOrderId":575946},{"tid":4,"px":7933.9566,"qty":0.1168,"unixtime":1403195510,"utcticks":635387923108371640,"incomingOrderSide":0,"incomingServerOrderId":576974,"bookServerOrderId":576947},{"tid":5,"px":7961.0856,"qty":0.25,"unixtime":1403202307,"utcticks":635387991073850156,"incomingOrderSide":0,"incomingServerOrderId":600547,"bookServerOrderId":600338},{"tid":6,"px":7961.1388,"qty":0.011,"unixtime":1403202307,"utcticks":635387991073850156,"incomingOrderSide":0,"incomingServerOrderId":600547,"bookServerOrderId":600418},{"tid":7,"px":7961.2451,"qty":0.02,"unixtime":1403202307,"utcticks":635387991073850156,"incomingOrderSide":0,"incomingServerOrderId":600547,"bookServerOrderId":600428},{"tid":8,"px":7947.1437,"qty":0.09,"unixtime":1403202749,"utcticks":635387995498225156,"incomingOrderSide":0,"incomingServerOrderId":602183,"bookServerOrderId":601745},{"tid":9,"px":7818.5073,"qty":0.25,"unixtime":1403219720,"utcticks":635388165206506406,"incomingOrderSide":0,"incomingServerOrderId":661909,"bookServerOrderId":661620}]}`),
)
err = common.JSONDecode(mockResp, &trades)
err = json.Unmarshal(mockResp, &trades)
if err != nil {
t.Fatal("GetTrades unmarshalling error: ", err)
}
@@ -140,7 +141,7 @@ func TestGetTradesByDate(t *testing.T) {
string(`{"isAccepted":true,"dateTimeUtc":635504540880633671,"ins":"BTCUSD","startDate":1414799400,"endDate":1414800000,"trades":[{"tid":11505,"px":334.669,"qty":0.1211,"unixtime":1414799403,"utcticks":635503962032459843,"incomingOrderSide":1,"incomingServerOrderId":5185651,"bookServerOrderId":5162440},{"tid":11506,"px":334.669,"qty":0.1211,"unixtime":1414799405,"utcticks":635503962058446171,"incomingOrderSide":1,"incomingServerOrderId":5186245,"bookServerOrderId":5162440},{"tid":11507,"px":336.498,"qty":0.011,"unixtime":1414799407,"utcticks":635503962072967656,"incomingOrderSide":0,"incomingServerOrderId":5186530,"bookServerOrderId":5178944},{"tid":11508,"px":335.948,"qty":0.011,"unixtime":1414799410,"utcticks":635503962108055546,"incomingOrderSide":0,"incomingServerOrderId":5187260,"bookServerOrderId":5186531}]}`),
)
err = common.JSONDecode(mockResp, &trades)
err = json.Unmarshal(mockResp, &trades)
if err != nil {
t.Fatal("GetTradesByDate unmarshalling error: ", err)
}
@@ -188,7 +189,7 @@ func TestGetOrderbook(t *testing.T) {
string(`{"bids":[{"qty":725,"px":66},{"qty":1289,"px":65},{"qty":1266,"px":64}],"asks":[{"qty":1,"px":67},{"qty":1,"px":69},{"qty":2,"px":70}],"isAccepted":true}`),
)
err = common.JSONDecode(mockResp, &orderBook)
err = json.Unmarshal(mockResp, &orderBook)
if err != nil {
t.Fatal("TestGetOrderbook unmarshalling error: ", err)
}
@@ -228,7 +229,7 @@ func TestGetProductPairs(t *testing.T) {
string(`{"productPairs":[{"name":"LTCUSD","productPairCode":100,"product1Label":"LTC","product1DecimalPlaces":8,"product2Label":"USD","product2DecimalPlaces":6}, {"name":"BTCUSD","productPairCode":99,"product1Label":"BTC","product1DecimalPlaces":8,"product2Label":"USD","product2DecimalPlaces":6}],"isAccepted":true}`),
)
err = common.JSONDecode(mockResp, &products)
err = json.Unmarshal(mockResp, &products)
if err != nil {
t.Fatal("TestGetProductPairs unmarshalling error: ", err)
}
@@ -268,7 +269,7 @@ func TestGetProducts(t *testing.T) {
string(`{"products": [{"name": "USD","isDigital": false,"productCode": 0,"decimalPlaces": 4,"fullName": "US Dollar"},{"name": "BTC","isDigital": true,"productCode": 1,"decimalPlaces": 6,"fullName": "Bitcoin"}],"isAccepted": true}`),
)
err = common.JSONDecode(mockResp, &products)
err = json.Unmarshal(mockResp, &products)
if err != nil {
t.Fatal("TestGetProducts unmarshalling error: ", err)
}

View File

@@ -1,10 +1,10 @@
package alphapoint
import (
"encoding/json"
"net/http"
"github.com/gorilla/websocket"
"github.com/thrasher-corp/gocryptotrader/common"
log "github.com/thrasher-corp/gocryptotrader/logger"
)
@@ -49,7 +49,7 @@ func (a *Alphapoint) WebsocketClient() {
}
msgType := MsgType{}
err := common.JSONDecode(resp, &msgType)
err := json.Unmarshal(resp, &msgType)
if err != nil {
log.Error(log.ExchangeSys, err)
continue
@@ -57,7 +57,7 @@ func (a *Alphapoint) WebsocketClient() {
if msgType.MessageType == "Ticker" {
ticker := WebsocketTicker{}
err = common.JSONDecode(resp, &ticker)
err = json.Unmarshal(resp, &ticker)
if err != nil {
log.Error(log.ExchangeSys, err)
continue