mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-28 15:10:32 +00:00
codebase: Cleanup various things (#1935)
* codebase: Rid base64/hex to string common funcs * codebase: Rid local scope variable usage and other improvements * codebase: Refactor currency pair usage across multiple exchanges - Updated HitBTC tests to use the new currency pair format. - Modified Kraken futures types to use currency.Pair instead of string for Symbol. - Adjusted Kraken wrapper methods to handle currency pairs correctly. - Refined OKX tests and types to utilize currency.Pair for instrument IDs. - Enhanced Poloniex tests to consistently use predefined currency pairs. - Streamlined order and orderbook tests to replace string pairs with currency.NewBTCUSD(). - Improved Yobit tests to utilize a standardized currency pair format. - Updated validator wrapper to use currency pairs directly instead of string conversions. * codebase: Use types.Number where possible * refactor: update PayoutFee type to types.Number for consistency * Refactor: Remove crypto functions to use standard library and other minor changes - Removed custom crypto functions for SHA256, SHA512, and MD5 from the common/crypto package. - Replaced usages of removed functions with standard library implementations in various files including: - cmd/websocket_client/main.go - engine/apiserver.go - exchanges/kraken/kraken.go - exchanges/lbank/lbank.go - exchanges/okx/okx_business_websocket.go - exchanges/kucoin/kucoin_websocket.go - gctscript/vm/vm.go - Updated tests to reflect changes in the crypto functions. - Renamed several functions for clarity, particularly in the context of order book updates across multiple exchanges. * refactor: replace assert with require for consistency in test assertions * refactor: Improve Binance futures candlestick test, standardise orderbook update function names and improve test parallelism * refactor: Replace require.Len with require.Equal for better output in TestGetFuturesKlineData
This commit is contained in:
@@ -2,6 +2,7 @@ package exmo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
@@ -323,16 +324,14 @@ func (e *EXMO) SendAuthenticatedHTTPRequest(ctx context.Context, epath exchange.
|
||||
vals.Set("nonce", n)
|
||||
|
||||
payload := vals.Encode()
|
||||
hash, err := crypto.GetHMAC(crypto.HashSHA512,
|
||||
[]byte(payload),
|
||||
[]byte(creds.Secret))
|
||||
hash, err := crypto.GetHMAC(crypto.HashSHA512, []byte(payload), []byte(creds.Secret))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
headers := make(map[string]string)
|
||||
headers["Key"] = creds.Key
|
||||
headers["Sign"] = crypto.HexEncodeToString(hash)
|
||||
headers["Sign"] = hex.EncodeToString(hash)
|
||||
headers["Content-Type"] = "application/x-www-form-urlencoded"
|
||||
|
||||
return &request.Item{
|
||||
|
||||
@@ -27,7 +27,10 @@ const (
|
||||
canManipulateRealOrders = false
|
||||
)
|
||||
|
||||
var e = &EXMO{}
|
||||
var (
|
||||
e = &EXMO{}
|
||||
testPair = currency.NewBTCUSD().Format(currency.PairFormat{Uppercase: true, Delimiter: "_"})
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
e.SetDefaults()
|
||||
@@ -53,18 +56,14 @@ func TestMain(m *testing.M) {
|
||||
|
||||
func TestGetTrades(t *testing.T) {
|
||||
t.Parallel()
|
||||
_, err := e.GetTrades(t.Context(), "BTC_USD")
|
||||
if err != nil {
|
||||
t.Errorf("Err: %s", err)
|
||||
}
|
||||
_, err := e.GetTrades(t.Context(), testPair.String())
|
||||
assert.NoError(t, err, "GetTrades should not error")
|
||||
}
|
||||
|
||||
func TestGetOrderbook(t *testing.T) {
|
||||
t.Parallel()
|
||||
_, err := e.GetOrderbook(t.Context(), "BTC_USD")
|
||||
if err != nil {
|
||||
t.Errorf("Err: %s", err)
|
||||
}
|
||||
_, err := e.GetOrderbook(t.Context(), testPair.String())
|
||||
assert.NoError(t, err, "GetOrderbook should not error")
|
||||
}
|
||||
|
||||
func TestGetTicker(t *testing.T) {
|
||||
@@ -105,17 +104,15 @@ func TestGetRequiredAmount(t *testing.T) {
|
||||
t.Parallel()
|
||||
sharedtestvalues.SkipTestIfCredentialsUnset(t, e)
|
||||
|
||||
_, err := e.GetRequiredAmount(t.Context(), "BTC_USD", 100)
|
||||
if err != nil {
|
||||
t.Errorf("Err: %s", err)
|
||||
}
|
||||
_, err := e.GetRequiredAmount(t.Context(), testPair.String(), 100)
|
||||
assert.NoError(t, err, "GetRequiredAmount should not error")
|
||||
}
|
||||
|
||||
func setFeeBuilder() *exchange.FeeBuilder {
|
||||
return &exchange.FeeBuilder{
|
||||
Amount: 1,
|
||||
FeeType: exchange.CryptocurrencyTradeFee,
|
||||
Pair: currency.NewPair(currency.BTC, currency.LTC),
|
||||
Pair: testPair,
|
||||
PurchasePrice: 1,
|
||||
FiatCurrency: currency.USD,
|
||||
BankTransactionType: exchange.WireTransfer,
|
||||
@@ -294,12 +291,8 @@ func TestSubmitOrder(t *testing.T) {
|
||||
sharedtestvalues.SkipTestIfCannotManipulateOrders(t, e, canManipulateRealOrders)
|
||||
|
||||
orderSubmission := &order.Submit{
|
||||
Exchange: e.Name,
|
||||
Pair: currency.Pair{
|
||||
Delimiter: "_",
|
||||
Base: currency.BTC,
|
||||
Quote: currency.USD,
|
||||
},
|
||||
Exchange: e.Name,
|
||||
Pair: testPair,
|
||||
Side: order.Buy,
|
||||
Type: order.Limit,
|
||||
Price: 1,
|
||||
@@ -319,11 +312,10 @@ func TestCancelExchangeOrder(t *testing.T) {
|
||||
t.Parallel()
|
||||
sharedtestvalues.SkipTestIfCannotManipulateOrders(t, e, canManipulateRealOrders)
|
||||
|
||||
currencyPair := currency.NewPair(currency.LTC, currency.BTC)
|
||||
orderCancellation := &order.Cancel{
|
||||
OrderID: "1",
|
||||
AccountID: "1",
|
||||
Pair: currencyPair,
|
||||
Pair: testPair,
|
||||
AssetType: asset.Spot,
|
||||
}
|
||||
|
||||
@@ -340,11 +332,10 @@ func TestCancelAllExchangeOrders(t *testing.T) {
|
||||
t.Parallel()
|
||||
sharedtestvalues.SkipTestIfCannotManipulateOrders(t, e, canManipulateRealOrders)
|
||||
|
||||
currencyPair := currency.NewPair(currency.LTC, currency.BTC)
|
||||
orderCancellation := &order.Cancel{
|
||||
OrderID: "1",
|
||||
AccountID: "1",
|
||||
Pair: currencyPair,
|
||||
Pair: testPair,
|
||||
AssetType: asset.Spot,
|
||||
}
|
||||
|
||||
@@ -444,39 +435,20 @@ func TestGetCryptoDepositAddress(t *testing.T) {
|
||||
|
||||
func TestGetRecentTrades(t *testing.T) {
|
||||
t.Parallel()
|
||||
currencyPair, err := currency.NewPairFromString("BTC_USD")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = e.GetRecentTrades(t.Context(), currencyPair, asset.Spot)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
_, err := e.GetRecentTrades(t.Context(), testPair, asset.Spot)
|
||||
assert.NoError(t, err, "GetRecentTrades should not error")
|
||||
}
|
||||
|
||||
func TestGetHistoricTrades(t *testing.T) {
|
||||
t.Parallel()
|
||||
currencyPair, err := currency.NewPairFromString("BTC_USD")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = e.GetHistoricTrades(t.Context(),
|
||||
currencyPair, asset.Spot, time.Now().Add(-time.Minute*15), time.Now())
|
||||
if err != nil && err != common.ErrFunctionNotSupported {
|
||||
t.Error(err)
|
||||
}
|
||||
_, err := e.GetHistoricTrades(t.Context(), testPair, asset.Spot, time.Now().Add(-time.Minute*15), time.Now())
|
||||
assert.ErrorIs(t, err, common.ErrFunctionNotSupported)
|
||||
}
|
||||
|
||||
func TestUpdateTicker(t *testing.T) {
|
||||
t.Parallel()
|
||||
cp, err := currency.NewPairFromString("BTC_USD")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = e.UpdateTicker(t.Context(), cp, asset.Spot)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
_, err := e.UpdateTicker(t.Context(), testPair, asset.Spot)
|
||||
assert.NoError(t, err, "UpdateTicker should not error")
|
||||
}
|
||||
|
||||
func TestUpdateTickers(t *testing.T) {
|
||||
|
||||
@@ -2,6 +2,7 @@ package exmo
|
||||
|
||||
import (
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/types"
|
||||
)
|
||||
|
||||
// Trades holds trade data
|
||||
@@ -17,13 +18,13 @@ type Trades struct {
|
||||
|
||||
// Orderbook holds the orderbook data
|
||||
type Orderbook struct {
|
||||
AskQuantity float64 `json:"ask_quantity,string"`
|
||||
AskAmount float64 `json:"ask_amount,string"`
|
||||
AskTop float64 `json:"ask_top,string"`
|
||||
BidQuantity float64 `json:"bid_quantity,string"`
|
||||
BidTop float64 `json:"bid_top,string"`
|
||||
Ask [][]string `json:"ask"`
|
||||
Bid [][]string `json:"bid"`
|
||||
AskQuantity float64 `json:"ask_quantity,string"`
|
||||
AskAmount float64 `json:"ask_amount,string"`
|
||||
AskTop float64 `json:"ask_top,string"`
|
||||
BidQuantity float64 `json:"bid_quantity,string"`
|
||||
BidTop float64 `json:"bid_top,string"`
|
||||
Asks [][3]types.Number `json:"ask"`
|
||||
Bids [][3]types.Number `json:"bid"`
|
||||
}
|
||||
|
||||
// Ticker holds the ticker data
|
||||
|
||||
@@ -249,42 +249,16 @@ func (e *EXMO) UpdateOrderbook(ctx context.Context, p currency.Pair, assetType a
|
||||
continue
|
||||
}
|
||||
|
||||
book.Asks = make(orderbook.Tranches, len(data.Ask))
|
||||
for y := range data.Ask {
|
||||
var price, amount float64
|
||||
price, err = strconv.ParseFloat(data.Ask[y][0], 64)
|
||||
if err != nil {
|
||||
return book, err
|
||||
}
|
||||
|
||||
amount, err = strconv.ParseFloat(data.Ask[y][1], 64)
|
||||
if err != nil {
|
||||
return book, err
|
||||
}
|
||||
|
||||
book.Asks[y] = orderbook.Tranche{
|
||||
Price: price,
|
||||
Amount: amount,
|
||||
}
|
||||
book.Asks = make(orderbook.Tranches, len(data.Asks))
|
||||
for y := range data.Asks {
|
||||
book.Asks[y].Price = data.Asks[y][0].Float64()
|
||||
book.Asks[y].Amount = data.Asks[y][1].Float64()
|
||||
}
|
||||
|
||||
book.Bids = make(orderbook.Tranches, len(data.Bid))
|
||||
for y := range data.Bid {
|
||||
var price, amount float64
|
||||
price, err = strconv.ParseFloat(data.Bid[y][0], 64)
|
||||
if err != nil {
|
||||
return book, err
|
||||
}
|
||||
|
||||
amount, err = strconv.ParseFloat(data.Bid[y][1], 64)
|
||||
if err != nil {
|
||||
return book, err
|
||||
}
|
||||
|
||||
book.Bids[y] = orderbook.Tranche{
|
||||
Price: price,
|
||||
Amount: amount,
|
||||
}
|
||||
book.Bids = make(orderbook.Tranches, len(data.Bids))
|
||||
for y := range data.Bids {
|
||||
book.Bids[y].Price = data.Bids[y][0].Float64()
|
||||
book.Bids[y].Amount = data.Bids[y][1].Float64()
|
||||
}
|
||||
|
||||
err = book.Process()
|
||||
|
||||
Reference in New Issue
Block a user