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:
Adrian Gallagher
2025-06-12 14:12:36 +10:00
committed by GitHub
parent ce134a0a1d
commit d5ba674fc4
115 changed files with 1327 additions and 3112 deletions

View File

@@ -3,6 +3,7 @@ package bybit
import (
"bytes"
"context"
"encoding/hex"
"errors"
"fmt"
"net/http"
@@ -22,6 +23,7 @@ import (
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-corp/gocryptotrader/exchanges/request"
"github.com/thrasher-corp/gocryptotrader/types"
)
// Bybit is the overarching type across this package
@@ -2527,20 +2529,13 @@ func (by *Bybit) GetBrokerEarning(ctx context.Context, businessType, cursor stri
return resp.List, by.SendAuthHTTPRequestV5(ctx, exchange.RestSpot, http.MethodGet, "/v5/broker/earning-record", params, nil, &resp, defaultEPL)
}
func processOB(ob [][2]string) ([]orderbook.Tranche, error) {
var err error
func processOB(ob [][2]types.Number) []orderbook.Tranche {
o := make([]orderbook.Tranche, len(ob))
for x := range ob {
o[x].Amount, err = strconv.ParseFloat(ob[x][1], 64)
if err != nil {
return nil, err
}
o[x].Price, err = strconv.ParseFloat(ob[x][0], 64)
if err != nil {
return nil, err
}
o[x].Price = ob[x][0].Float64()
o[x].Amount = ob[x][1].Float64()
}
return o, nil
return o
}
// SendHTTPRequest sends an unauthenticated request
@@ -2699,7 +2694,7 @@ func getSign(sign, secret string) (string, error) {
if err != nil {
return "", err
}
return crypto.HexEncodeToString(hmacSigned), nil
return hex.EncodeToString(hmacSigned), nil
}
// FetchAccountType if not set fetches the account type from the API, stores it and returns it. Else returns the stored account type.

View File

@@ -18,11 +18,11 @@ var validCategory = []string{"spot", "linear", "inverse", "option"}
var supportedOptionsTypes = []string{"BTC", "ETH", "SOL"}
type orderbookResponse struct {
Symbol string `json:"s"`
Asks [][2]string `json:"a"`
Bids [][2]string `json:"b"`
Timestamp types.Time `json:"ts"`
UpdateID int64 `json:"u"`
Symbol string `json:"s"`
Asks [][2]types.Number `json:"a"`
Bids [][2]types.Number `json:"b"`
Timestamp types.Time `json:"ts"`
UpdateID int64 `json:"u"`
}
// Authenticate stores authentication variables required
@@ -142,23 +142,14 @@ type MarkPriceKlineResponse struct {
}
func constructOrderbook(o *orderbookResponse) (*Orderbook, error) {
var (
s = Orderbook{
Symbol: o.Symbol,
UpdateID: o.UpdateID,
GenerationTime: o.Timestamp.Time(),
}
err error
)
s.Bids, err = processOB(o.Bids)
if err != nil {
return nil, err
s := Orderbook{
Symbol: o.Symbol,
UpdateID: o.UpdateID,
GenerationTime: o.Timestamp.Time(),
}
s.Asks, err = processOB(o.Asks)
if err != nil {
return nil, err
}
return &s, err
s.Bids = processOB(o.Bids)
s.Asks = processOB(o.Asks)
return &s, nil
}
// TickerData represents a list of ticker detailed information.
@@ -1758,11 +1749,11 @@ type Orderbook struct {
// WsOrderbookDetail represents an orderbook detail information.
type WsOrderbookDetail struct {
Symbol string `json:"s"`
Bids [][]string `json:"b"`
Asks [][]string `json:"a"`
UpdateID int64 `json:"u"`
Sequence int64 `json:"seq"`
Symbol string `json:"s"`
Bids [][2]types.Number `json:"b"`
Asks [][2]types.Number `json:"a"`
UpdateID int64 `json:"u"`
Sequence int64 `json:"seq"`
}
// SubscriptionResponse represents a subscription response.

View File

@@ -2,6 +2,7 @@ package bybit
import (
"context"
"encoding/hex"
"fmt"
"net/http"
"strconv"
@@ -107,12 +108,16 @@ func (by *Bybit) WsConnect() error {
// WsAuth sends an authentication message to receive auth data
func (by *Bybit) WsAuth(ctx context.Context) error {
var dialer gws.Dialer
err := by.Websocket.AuthConn.Dial(&dialer, http.Header{})
creds, err := by.GetCredentials(ctx)
if err != nil {
return err
}
var dialer gws.Dialer
if err := by.Websocket.AuthConn.Dial(&dialer, http.Header{}); err != nil {
return err
}
by.Websocket.AuthConn.SetupPingHandler(request.Unset, websocket.PingHandler{
MessageType: gws.TextMessage,
Message: []byte(`{"op":"ping"}`),
@@ -121,10 +126,7 @@ func (by *Bybit) WsAuth(ctx context.Context) error {
by.Websocket.Wg.Add(1)
go by.wsReadData(asset.Spot, by.Websocket.AuthConn)
creds, err := by.GetCredentials(ctx)
if err != nil {
return err
}
intNonce := time.Now().Add(time.Hour * 6).UnixMilli()
strNonce := strconv.FormatInt(intNonce, 10)
hmac, err := crypto.GetHMAC(
@@ -135,7 +137,7 @@ func (by *Bybit) WsAuth(ctx context.Context) error {
if err != nil {
return err
}
sign := crypto.HexEncodeToString(hmac)
sign := hex.EncodeToString(hmac)
req := Authenticate{
RequestID: strconv.FormatInt(by.Websocket.AuthConn.GenerateMessageID(false), 10),
Operation: "auth",
@@ -694,39 +696,29 @@ func (by *Bybit) wsProcessPublicTrade(assetType asset.Item, resp *WebsocketRespo
func (by *Bybit) wsProcessOrderbook(assetType asset.Item, resp *WebsocketResponse) error {
var result WsOrderbookDetail
err := json.Unmarshal(resp.Data, &result)
if err != nil {
if err := json.Unmarshal(resp.Data, &result); err != nil {
return err
}
if len(result.Bids) == 0 && len(result.Asks) == 0 {
return nil
}
cp, err := by.MatchSymbolWithAvailablePairs(result.Symbol, assetType, hasPotentialDelimiter(assetType))
if err != nil {
return err
}
asks := make([]orderbook.Tranche, len(result.Asks))
for i := range result.Asks {
asks[i].Price, err = strconv.ParseFloat(result.Asks[i][0], 64)
if err != nil {
return err
}
asks[i].Amount, err = strconv.ParseFloat(result.Asks[i][1], 64)
if err != nil {
return err
}
asks[i].Price = result.Asks[i][0].Float64()
asks[i].Amount = result.Asks[i][1].Float64()
}
bids := make([]orderbook.Tranche, len(result.Bids))
for i := range result.Bids {
bids[i].Price, err = strconv.ParseFloat(result.Bids[i][0], 64)
if err != nil {
return err
}
bids[i].Amount, err = strconv.ParseFloat(result.Bids[i][1], 64)
if err != nil {
return err
}
}
if len(asks) == 0 && len(bids) == 0 {
return nil
bids[i].Price = result.Bids[i][0].Float64()
bids[i].Amount = result.Bids[i][1].Float64()
}
if resp.Type == "snapshot" {
return by.Websocket.Orderbook.LoadSnapshot(&orderbook.Base{
Pair: cp,