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

@@ -2,6 +2,8 @@ package engine
import (
"context"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"net/http"
@@ -15,7 +17,6 @@ import (
"github.com/gorilla/mux"
gws "github.com/gorilla/websocket"
"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/common/crypto"
"github.com/thrasher-corp/gocryptotrader/config"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/encoding/json"
@@ -693,13 +694,8 @@ func wsAuth(client *websocketClient, data any) error {
return err
}
hash, err := crypto.GetSHA256([]byte(client.password))
if err != nil {
return err
}
hashPW := crypto.HexEncodeToString(hash)
if auth.Username == client.username && auth.Password == hashPW {
shasum := sha256.Sum256([]byte(client.password))
if auth.Username == client.username && auth.Password == hex.EncodeToString(shasum[:]) {
client.Authenticated = true
wsResp.Data = WebsocketResponseSuccess
log.Debugln(log.APIServerMgr,

View File

@@ -513,7 +513,7 @@ func TestCompareJobsToData(t *testing.T) {
assert.ErrorIs(t, err, ErrNilSubsystem)
}
func TestRunJob(t *testing.T) { //nolint:tparallel // There is a race condition caused by the DataHistoryJob and it's a big change to fix.
func TestRunJob(t *testing.T) {
t.Parallel()
tt := time.Now().Truncate(kline.OneHour.Duration())
testCases := []*DataHistoryJob{
@@ -586,24 +586,24 @@ func TestRunJob(t *testing.T) { //nolint:tparallel // There is a race condition
m.tradeSaver = dataHistoryTradeSaver
m.candleSaver = dataHistoryCandleSaver
m.tradeLoader = dataHistoryTraderLoader
for x := range testCases {
test := testCases[x]
t.Run(test.Nickname, func(t *testing.T) {
err := m.UpsertJob(test, false)
for _, tc := range testCases {
t.Run(tc.Nickname, func(t *testing.T) {
t.Parallel()
err := m.UpsertJob(tc, false)
assert.NoError(t, err)
test.Status = dataHistoryIntervalIssuesFound
err = m.runJob(test)
tc.Status = dataHistoryIntervalIssuesFound
err = m.runJob(tc)
assert.ErrorIs(t, err, errJobInvalid)
rh := test.rangeHolder
test.Status = dataHistoryStatusActive
test.rangeHolder = nil
err = m.runJob(test)
rh := tc.rangeHolder
tc.Status = dataHistoryStatusActive
tc.rangeHolder = nil
err = m.runJob(tc)
assert.ErrorIs(t, err, errJobInvalid)
test.rangeHolder = rh
err = m.runJob(test)
tc.rangeHolder = rh
err = m.runJob(tc)
assert.NoError(t, err)
})
}

View File

@@ -402,15 +402,10 @@ func TestGetSpecificAvailablePairs(t *testing.T) {
func TestIsRelatablePairs(t *testing.T) {
t.Parallel()
CreateTestBot(t)
xbtusd, err := currency.NewPairFromStrings("XBT", "USD")
if err != nil {
t.Fatal(err)
}
btcusd, err := currency.NewPairFromStrings("BTC", "USD")
if err != nil {
t.Fatal(err)
}
btcusd := currency.NewBTCUSD()
xbtusd := currency.NewPair(currency.XBT, currency.USD)
xbtusdt := currency.NewPair(currency.XBT, currency.USDT)
// Test relational pairs with similar names
result := IsRelatablePairs(xbtusd, btcusd, false)
@@ -424,22 +419,12 @@ func TestIsRelatablePairs(t *testing.T) {
t.Fatal("Unexpected result")
}
btcusdt, err := currency.NewPairFromStrings("BTC", "USDT")
if err != nil {
t.Fatal(err)
}
// Test relational pairs with similar names but with Tether support disabled
result = IsRelatablePairs(xbtusd, btcusdt, false)
result = IsRelatablePairs(xbtusd, currency.NewBTCUSDT(), false)
if result {
t.Fatal("Unexpected result")
}
xbtusdt, err := currency.NewPairFromStrings("XBT", "USDT")
if err != nil {
t.Fatal(err)
}
// Test relational pairs with similar names but with Tether support enabled
result = IsRelatablePairs(xbtusdt, btcusd, true)
if !result {
@@ -505,7 +490,7 @@ func TestIsRelatablePairs(t *testing.T) {
// Test relationl pairs with similar names, different fiat currencies and
// with Tether enabled
result = IsRelatablePairs(usdbtc, btcusdt, true)
result = IsRelatablePairs(usdbtc, currency.NewBTCUSDT(), true)
if !result {
t.Fatal("Unexpected result")
}

View File

@@ -935,7 +935,7 @@ func TestGetOrdersFiltered(t *testing.T) {
}
}
func Test_getFilteredOrders(t *testing.T) {
func TestGetFilteredOrders(t *testing.T) {
m := OrdersSetup(t)
_, err := m.orderStore.getFilteredOrders(nil)
@@ -1014,7 +1014,7 @@ func TestGetOrdersActive(t *testing.T) {
}
}
func Test_processMatchingOrders(t *testing.T) {
func TestProcessMatchingOrders(t *testing.T) {
m := OrdersSetup(t)
exch, err := m.orderStore.exchangeManager.GetExchangeByName(testExchange)
if err != nil {
@@ -1094,7 +1094,7 @@ func TestFetchAndUpdateExchangeOrder(t *testing.T) {
}
}
func Test_getActiveOrders(t *testing.T) {
func TestGetActiveOrders(t *testing.T) {
m := OrdersSetup(t)
var err error
orders := []order.Detail{

View File

@@ -2,6 +2,7 @@ package engine
import (
"context"
"encoding/base64"
"errors"
"fmt"
"net"
@@ -20,7 +21,6 @@ import (
"github.com/shopspring/decimal"
"github.com/thrasher-corp/gct-ta/indicators"
"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/common/crypto"
"github.com/thrasher-corp/gocryptotrader/common/file"
"github.com/thrasher-corp/gocryptotrader/common/file/archive"
"github.com/thrasher-corp/gocryptotrader/common/key"
@@ -102,7 +102,7 @@ func (s *RPCServer) authenticateClient(ctx context.Context) (context.Context, er
return ctx, errors.New("basic not found in authorization header")
}
decoded, err := crypto.Base64Decode(strings.Split(authStr[0], " ")[1])
decoded, err := base64.StdEncoding.DecodeString(strings.Split(authStr[0], " ")[1])
if err != nil {
return ctx, errors.New("unable to base64 decode authorization header")
}