modernise: Run new gopls modernise tool against the codebase and fix minor issues (#1826)

* modernise: Run new gopls modernise tool against codebase

* Address shazbert's nits

* apichecker, gctcli: Simplify HTML scraping functions and improve depth limit handling

* refactor: Create minSyncInterval const and update order book limit handling for binance and binanceUS

* refactor: Various slice usage improvements and rename TODO

* tranches: Revert deleteByID changes due to performance decrease

Shazbert was a F1 driver in a past lifetime 🏎️

* tranches: Simply retrieve copy

Thanks to shazbert

* documentation: Sort contributors list by contributions

* tranches: Remove deadcode in deleteByID
This commit is contained in:
Adrian Gallagher
2025-03-21 09:17:10 +11:00
committed by GitHub
parent d857d704e3
commit 4651af5767
223 changed files with 1504 additions and 1752 deletions

View File

@@ -26,9 +26,7 @@ import (
// Binance is the overarching type across the Binance package
type Binance struct {
exchange.Base
// Valid string list that is required by the exchange
validLimits []int
obm *orderbookManager
obm *orderbookManager
}
const (
@@ -124,10 +122,6 @@ func (b *Binance) GetExchangeInfo(ctx context.Context) (ExchangeInfo, error) {
// symbol: string of currency pair
// limit: returned limit amount
func (b *Binance) GetOrderBook(ctx context.Context, obd OrderBookDataRequestParams) (*OrderBook, error) {
if err := b.CheckLimit(obd.Limit); err != nil {
return nil, err
}
params := url.Values{}
symbol, err := b.FormatSymbol(obd.Symbol, asset.Spot)
if err != nil {
@@ -406,7 +400,7 @@ func (b *Binance) GetSpotKline(ctx context.Context, arg *KlinesRequestParams) ([
}
path := candleStick + "?" + params.Encode()
var resp interface{}
var resp any
err = b.SendHTTPRequest(ctx,
exchange.RestSpotSupplementary,
@@ -416,16 +410,16 @@ func (b *Binance) GetSpotKline(ctx context.Context, arg *KlinesRequestParams) ([
if err != nil {
return nil, err
}
responseData, ok := resp.([]interface{})
responseData, ok := resp.([]any)
if !ok {
return nil, common.GetTypeAssertError("[]interface{}", resp)
return nil, common.GetTypeAssertError("[]any", resp)
}
klineData := make([]CandleStick, len(responseData))
for x := range responseData {
individualData, ok := responseData[x].([]interface{})
individualData, ok := responseData[x].([]any)
if !ok {
return nil, common.GetTypeAssertError("[]interface{}", responseData[x])
return nil, common.GetTypeAssertError("[]any", responseData[x])
}
if len(individualData) != 12 {
return nil, errors.New("unexpected kline data length")
@@ -796,7 +790,7 @@ func (b *Binance) GetMarginAccount(ctx context.Context) (*MarginAccount, error)
}
// SendHTTPRequest sends an unauthenticated request
func (b *Binance) SendHTTPRequest(ctx context.Context, ePath exchange.URL, path string, f request.EndpointLimit, result interface{}) error {
func (b *Binance) SendHTTPRequest(ctx context.Context, ePath exchange.URL, path string, f request.EndpointLimit, result any) error {
endpointPath, err := b.API.Endpoints.GetURL(ePath)
if err != nil {
return err
@@ -817,7 +811,7 @@ func (b *Binance) SendHTTPRequest(ctx context.Context, ePath exchange.URL, path
// SendAPIKeyHTTPRequest is a special API request where the api key is
// appended to the headers without a secret
func (b *Binance) SendAPIKeyHTTPRequest(ctx context.Context, ePath exchange.URL, path string, f request.EndpointLimit, result interface{}) error {
func (b *Binance) SendAPIKeyHTTPRequest(ctx context.Context, ePath exchange.URL, path string, f request.EndpointLimit, result any) error {
endpointPath, err := b.API.Endpoints.GetURL(ePath)
if err != nil {
return err
@@ -846,7 +840,7 @@ func (b *Binance) SendAPIKeyHTTPRequest(ctx context.Context, ePath exchange.URL,
}
// SendAuthHTTPRequest sends an authenticated HTTP request
func (b *Binance) SendAuthHTTPRequest(ctx context.Context, ePath exchange.URL, method, path string, params url.Values, f request.EndpointLimit, result interface{}) error {
func (b *Binance) SendAuthHTTPRequest(ctx context.Context, ePath exchange.URL, method, path string, params url.Values, f request.EndpointLimit, result any) error {
creds, err := b.GetCredentials(ctx)
if err != nil {
return err
@@ -912,21 +906,6 @@ func (b *Binance) SendAuthHTTPRequest(ctx context.Context, ePath exchange.URL, m
return json.Unmarshal(interim, result)
}
// CheckLimit checks value against a variable list
func (b *Binance) CheckLimit(limit int) error {
for x := range b.validLimits {
if b.validLimits[x] == limit {
return nil
}
}
return errors.New("incorrect limit values - valid values are 5, 10, 20, 50, 100, 500, 1000")
}
// SetValues sets the default valid values
func (b *Binance) SetValues() {
b.validLimits = []int{5, 10, 20, 50, 100, 500, 1000, 5000}
}
// GetFee returns an estimate of fee based on type of transaction
func (b *Binance) GetFee(ctx context.Context, feeBuilder *exchange.FeeBuilder) (float64, error) {
var fee float64

View File

@@ -268,7 +268,7 @@ func (b *Binance) GetFuturesKlineData(ctx context.Context, symbol currency.Pair,
params.Set("endTime", strconv.FormatInt(endTime.UnixMilli(), 10))
}
var data [][10]interface{}
var data [][10]any
rateBudget := getKlineRateBudget(limit)
err := b.SendHTTPRequest(ctx, exchange.RestCoinMargined, cfuturesKlineData+params.Encode(), rateBudget, &data)
if err != nil {
@@ -388,7 +388,7 @@ func (b *Binance) GetContinuousKlineData(ctx context.Context, pair, contractType
}
rateBudget := getKlineRateBudget(limit)
var data [][10]interface{}
var data [][10]any
err := b.SendHTTPRequest(ctx, exchange.RestCoinMargined, cfuturesContinuousKline+params.Encode(), rateBudget, &data)
if err != nil {
return nil, err
@@ -503,7 +503,7 @@ func (b *Binance) GetIndexPriceKlines(ctx context.Context, pair, interval string
}
rateBudget := getKlineRateBudget(limit)
var data [][10]interface{}
var data [][10]any
err := b.SendHTTPRequest(ctx, exchange.RestCoinMargined, cfuturesIndexKline+params.Encode(), rateBudget, &data)
if err != nil {
return nil, err
@@ -621,7 +621,7 @@ func (b *Binance) GetMarkPriceKline(ctx context.Context, symbol currency.Pair, i
params.Set("endTime", strconv.FormatInt(endTime.UnixMilli(), 10))
}
var data [][10]interface{}
var data [][10]any
rateBudget := getKlineRateBudget(limit)
err = b.SendHTTPRequest(ctx, exchange.RestCoinMargined, cfuturesMarkPriceKline+params.Encode(), rateBudget, &data)
if err != nil {

View File

@@ -18,7 +18,6 @@ import (
var mockTests = false
func TestMain(m *testing.M) {
b = new(Binance)
if err := testexch.Setup(b); err != nil {
log.Fatal(err)

View File

@@ -1969,8 +1969,7 @@ func BenchmarkWsHandleData(bb *testing.B) {
<-b.Websocket.DataHandler
}
}()
bb.ResetTimer()
for range bb.N {
for bb.Loop() {
for x := range lines {
assert.NoError(bb, b.wsHandleData(lines[x]))
}
@@ -1990,7 +1989,7 @@ func TestSubscribe(t *testing.T) {
var req WsPayload
require.NoError(tb, json.Unmarshal(msg, &req), "Unmarshal should not error")
require.ElementsMatch(tb, req.Params, exp, "Params should have correct channels")
return w.WriteMessage(websocket.TextMessage, []byte(fmt.Sprintf(`{"result":null,"id":%d}`, req.ID)))
return w.WriteMessage(websocket.TextMessage, fmt.Appendf(nil, `{"result":null,"id":%d}`, req.ID))
}
b = testexch.MockWsInstance[Binance](t, mockws.CurryWsMockUpgrader(t, mock))
} else {
@@ -2012,7 +2011,7 @@ func TestSubscribeBadResp(t *testing.T) {
var req WsPayload
err := json.Unmarshal(msg, &req)
require.NoError(tb, err, "Unmarshal should not error")
return w.WriteMessage(websocket.TextMessage, []byte(fmt.Sprintf(`{"result":{"error":"carrots"},"id":%d}`, req.ID)))
return w.WriteMessage(websocket.TextMessage, fmt.Appendf(nil, `{"result":{"error":"carrots"},"id":%d}`, req.ID))
}
b := testexch.MockWsInstance[Binance](t, mockws.CurryWsMockUpgrader(t, mock)) //nolint:govet // Intentional shadow to avoid future copy/paste mistakes
err := b.Subscribe(channels)

View File

@@ -47,7 +47,7 @@ type ExchangeInfo struct {
Interval string `json:"interval"`
Limit int `json:"limit"`
} `json:"rateLimits"`
ExchangeFilters interface{} `json:"exchangeFilters"`
ExchangeFilters any `json:"exchangeFilters"`
Symbols []*struct {
Symbol string `json:"symbol"`
Status string `json:"status"`
@@ -125,7 +125,7 @@ type CoinInfo struct {
// OrderBookDataRequestParams represents Klines request data.
type OrderBookDataRequestParams struct {
Symbol currency.Pair `json:"symbol"` // Required field; example LTCBTC,BTCUSDT
Limit int `json:"limit"` // Default 100; max 1000. Valid limits:[5, 10, 20, 50, 100, 500, 1000]
Limit int `json:"limit"` // Default 100; max 5000. If limit > 5000, then the response will truncate to 5000
}
// OrderbookItem stores an individual orderbook item
@@ -157,7 +157,7 @@ type OrderBook struct {
type DepthUpdateParams []struct {
PriceLevel float64
Quantity float64
ignore []interface{}
ignore []any
}
// WebsocketDepthStream is the difference for the update depth stream

View File

@@ -177,8 +177,8 @@ func (b *Binance) URecentTrades(ctx context.Context, symbol currency.Pair, fromI
}
// UFuturesHistoricalTrades gets historical public trades for USDTMarginedFutures
func (b *Binance) UFuturesHistoricalTrades(ctx context.Context, symbol currency.Pair, fromID string, limit int64) ([]interface{}, error) {
var resp []interface{}
func (b *Binance) UFuturesHistoricalTrades(ctx context.Context, symbol currency.Pair, fromID string, limit int64) ([]any, error) {
var resp []any
params := url.Values{}
symbolValue, err := b.FormatSymbol(symbol, asset.USDTMarginedFutures)
if err != nil {
@@ -253,7 +253,7 @@ func (b *Binance) UKlineData(ctx context.Context, symbol currency.Pair, interval
rateBudget = uFuturesKlineMaxRate
}
var data [][10]interface{}
var data [][10]any
err = b.SendHTTPRequest(ctx, exchange.RestUSDTMargined, ufuturesKlineData+params.Encode(), rateBudget, &data)
if err != nil {
return nil, err

View File

@@ -66,7 +66,6 @@ func (b *Binance) SetDefaults() {
b.Verbose = true
b.API.CredentialsValidator.RequiresKey = true
b.API.CredentialsValidator.RequiresSecret = true
b.SetValues()
for a, ps := range defaultAssetPairStores {
if err := b.SetAssetPairStore(a, ps); err != nil {

View File

@@ -623,7 +623,7 @@ type UFuturesSymbolInfo struct {
// CExchangeInfo stores exchange info for cfutures
type CExchangeInfo struct {
ExchangeFilters []interface{} `json:"exchangeFilters"`
ExchangeFilters []any `json:"exchangeFilters"`
RateLimits []struct {
Interval string `json:"interval"`
IntervalNum int64 `json:"intervalNul"`