mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-02 23:16:51 +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:
@@ -3,6 +3,8 @@ package bitfinex
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -2101,22 +2103,21 @@ func (b *Bitfinex) SendAuthenticatedHTTPRequest(ctx context.Context, ep exchange
|
||||
|
||||
maps.Copy(req, params)
|
||||
|
||||
PayloadJSON, err := json.Marshal(req)
|
||||
payloadJSON, err := json.Marshal(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
PayloadBase64 := crypto.Base64Encode(PayloadJSON)
|
||||
hmac, err := crypto.GetHMAC(crypto.HashSHA512_384,
|
||||
[]byte(PayloadBase64),
|
||||
[]byte(creds.Secret))
|
||||
payloadB64 := base64.StdEncoding.EncodeToString(payloadJSON)
|
||||
hmac, err := crypto.GetHMAC(crypto.HashSHA512_384, []byte(payloadB64), []byte(creds.Secret))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
headers := make(map[string]string)
|
||||
headers["X-BFX-APIKEY"] = creds.Key
|
||||
headers["X-BFX-PAYLOAD"] = PayloadBase64
|
||||
headers["X-BFX-SIGNATURE"] = crypto.HexEncodeToString(hmac)
|
||||
headers["X-BFX-PAYLOAD"] = payloadB64
|
||||
headers["X-BFX-SIGNATURE"] = hex.EncodeToString(hmac)
|
||||
|
||||
return &request.Item{
|
||||
Method: method,
|
||||
@@ -2161,15 +2162,11 @@ func (b *Bitfinex) SendAuthenticatedHTTPRequestV2(ctx context.Context, ep exchan
|
||||
headers["bfx-apikey"] = creds.Key
|
||||
headers["bfx-nonce"] = n
|
||||
sig := "/api" + bitfinexAPIVersion2 + path + n + string(payload)
|
||||
hmac, err := crypto.GetHMAC(
|
||||
crypto.HashSHA512_384,
|
||||
[]byte(sig),
|
||||
[]byte(creds.Secret),
|
||||
)
|
||||
hmac, err := crypto.GetHMAC(crypto.HashSHA512_384, []byte(sig), []byte(creds.Secret))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
headers["bfx-signature"] = crypto.HexEncodeToString(hmac)
|
||||
headers["bfx-signature"] = hex.EncodeToString(hmac)
|
||||
|
||||
return &request.Item{
|
||||
Method: method,
|
||||
|
||||
@@ -1703,44 +1703,34 @@ func TestFixCasing(t *testing.T) {
|
||||
require.ErrorIs(t, err, currency.ErrCurrencyPairEmpty)
|
||||
}
|
||||
|
||||
func Test_FormatExchangeKlineInterval(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
func TestFormatExchangeKlineInterval(t *testing.T) {
|
||||
t.Parallel()
|
||||
for _, tc := range []struct {
|
||||
interval kline.Interval
|
||||
output string
|
||||
}{
|
||||
{
|
||||
"OneMin",
|
||||
kline.OneMin,
|
||||
"1m",
|
||||
},
|
||||
{
|
||||
"OneDay",
|
||||
kline.OneDay,
|
||||
"1D",
|
||||
},
|
||||
{
|
||||
"OneWeek",
|
||||
kline.OneWeek,
|
||||
"7D",
|
||||
},
|
||||
{
|
||||
"TwoWeeks",
|
||||
kline.OneWeek * 2,
|
||||
"14D",
|
||||
},
|
||||
}
|
||||
|
||||
for x := range testCases {
|
||||
test := testCases[x]
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
ret, err := b.FormatExchangeKlineInterval(test.interval)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if ret != test.output {
|
||||
t.Fatalf("unexpected result return expected: %v received: %v", test.output, ret)
|
||||
}
|
||||
} {
|
||||
t.Run(tc.interval.String(), func(t *testing.T) {
|
||||
t.Parallel()
|
||||
ret, err := b.FormatExchangeKlineInterval(tc.interval)
|
||||
require.NoError(t, err, "FormatExchangeKlineInterval must not error")
|
||||
assert.Equal(t, tc.output, ret)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -354,22 +354,22 @@ type Withdrawal struct {
|
||||
|
||||
// Order holds order information when an order is in the market
|
||||
type Order struct {
|
||||
ID int64 `json:"id"`
|
||||
Symbol string `json:"symbol"`
|
||||
Exchange string `json:"exchange"`
|
||||
Price float64 `json:"price,string"`
|
||||
AverageExecutionPrice float64 `json:"avg_execution_price,string"`
|
||||
Side string `json:"side"`
|
||||
Type string `json:"type"`
|
||||
Timestamp string `json:"timestamp"`
|
||||
IsLive bool `json:"is_live"`
|
||||
IsCancelled bool `json:"is_cancelled"`
|
||||
IsHidden bool `json:"is_hidden"`
|
||||
WasForced bool `json:"was_forced"`
|
||||
OriginalAmount float64 `json:"original_amount,string"`
|
||||
RemainingAmount float64 `json:"remaining_amount,string"`
|
||||
ExecutedAmount float64 `json:"executed_amount,string"`
|
||||
OrderID int64 `json:"order_id,omitempty"`
|
||||
ID int64 `json:"id"`
|
||||
Symbol currency.Pair `json:"symbol"`
|
||||
Exchange string `json:"exchange"`
|
||||
Price float64 `json:"price,string"`
|
||||
AverageExecutionPrice float64 `json:"avg_execution_price,string"`
|
||||
Side string `json:"side"`
|
||||
Type string `json:"type"`
|
||||
Timestamp types.Time `json:"timestamp"`
|
||||
IsLive bool `json:"is_live"`
|
||||
IsCancelled bool `json:"is_cancelled"`
|
||||
IsHidden bool `json:"is_hidden"`
|
||||
WasForced bool `json:"was_forced"`
|
||||
OriginalAmount float64 `json:"original_amount,string"`
|
||||
RemainingAmount float64 `json:"remaining_amount,string"`
|
||||
ExecutedAmount float64 `json:"executed_amount,string"`
|
||||
OrderID int64 `json:"order_id,omitempty"`
|
||||
}
|
||||
|
||||
// OrderMultiResponse holds order information on the executed orders
|
||||
|
||||
@@ -2,6 +2,7 @@ package bitfinex
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"hash/crc32"
|
||||
@@ -1846,25 +1847,19 @@ func (b *Bitfinex) WsSendAuth(ctx context.Context) error {
|
||||
nonce := strconv.FormatInt(time.Now().Unix(), 10)
|
||||
payload := "AUTH" + nonce
|
||||
|
||||
hmac, err := crypto.GetHMAC(crypto.HashSHA512_384,
|
||||
[]byte(payload),
|
||||
[]byte(creds.Secret))
|
||||
hmac, err := crypto.GetHMAC(crypto.HashSHA512_384, []byte(payload), []byte(creds.Secret))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = b.Websocket.AuthConn.SendJSONMessage(ctx, request.Unset, WsAuthRequest{
|
||||
|
||||
return b.Websocket.AuthConn.SendJSONMessage(ctx, request.Unset, WsAuthRequest{
|
||||
Event: "auth",
|
||||
APIKey: creds.Key,
|
||||
AuthPayload: payload,
|
||||
AuthSig: crypto.HexEncodeToString(hmac),
|
||||
AuthSig: hex.EncodeToString(hmac),
|
||||
AuthNonce: nonce,
|
||||
DeadManSwitch: 0,
|
||||
})
|
||||
if err != nil {
|
||||
b.Websocket.SetCanUseAuthenticatedEndpoints(false)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// WsNewOrder authenticated new order request
|
||||
|
||||
@@ -700,27 +700,16 @@ func (b *Bitfinex) parseOrderToOrderDetail(o *Order) (*order.Detail, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var timestamp float64
|
||||
timestamp, err = strconv.ParseFloat(o.Timestamp, 64)
|
||||
if err != nil {
|
||||
log.Warnf(log.ExchangeSys, "%s Unable to convert timestamp %q, leaving blank", b.Name, o.Timestamp)
|
||||
}
|
||||
|
||||
var pair currency.Pair
|
||||
pair, err = currency.NewPairFromString(o.Symbol)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
orderDetail := &order.Detail{
|
||||
Amount: o.OriginalAmount,
|
||||
Date: time.Unix(int64(timestamp), 0),
|
||||
Date: o.Timestamp.Time(),
|
||||
Exchange: b.Name,
|
||||
OrderID: strconv.FormatInt(o.ID, 10),
|
||||
Side: side,
|
||||
Price: o.Price,
|
||||
RemainingAmount: o.RemainingAmount,
|
||||
Pair: pair,
|
||||
Pair: o.Symbol,
|
||||
ExecutedAmount: o.ExecutedAmount,
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user