exchanges/wrappers: Refactor fetch orderbook/ticker/account info funcs (#1440)

* acrost: Pull thread, examine

* fix tests

* linter

* fix_linter

* revert rm ctx param to limit breakages when merging usptream

* linter fix

* Add in priority update grouping so that tests pass

* Update cmd/exchange_wrapper_standards/exchange_wrapper_standards_test.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* glorious nits

* fixed spelling

* whoopsie

* aanother whoops

* glorious: NITTERS!

* glorious: further nitters

* srry linter gods

* glorious: nits continued

* sub test p ara lel

* drop main t.Parallel

* fix whoops

* wrappertests: use context with cancel (test)

* linter: fix

* ensure primary execution

* kucoin test fix

* revert standards test changes and bypass non critical errors

* rm single override

* wrap exported error for accounts

* thrasher: nits ch name

* gk: nits

* gk: nits FetchTickerCached -> GetCachedTicker

* gk: nits rn FetchOrderbookCached -> GetCachedOrderbook

* gk: nits rn FetchAccountInfoCached -> GetCachedAccountInfo

* linter: fix

* gk: nits

* thrasher: nitters 1

* thrasher: nitters tmpls

* gk: nitter

---------

Co-authored-by: shazbert <ryan.oharareid@thrasher.io>
Co-authored-by: Scott <gloriousCode@users.noreply.github.com>
This commit is contained in:
Ryan O'Hara-Reid
2025-02-19 10:47:10 +11:00
committed by GitHub
parent 2fc7e8e3e3
commit 08e015a125
122 changed files with 545 additions and 1962 deletions

View File

@@ -57,13 +57,13 @@ for i := range bot.Exchanges {
// Public calls - wrapper functions
// Fetches current ticker information
tick, err := d.FetchTicker()
tick, err := d.UpdateTicker(...)
if err != nil {
// Handle error
}
// Fetches current orderbook information
ob, err := d.FetchOrderbook()
ob, err := d.UpdateOrderbook(...)
if err != nil {
// Handle error
}

View File

@@ -23,10 +23,8 @@ import (
"github.com/thrasher-corp/gocryptotrader/exchanges/futures"
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-corp/gocryptotrader/exchanges/sharedtestvalues"
"github.com/thrasher-corp/gocryptotrader/exchanges/subscription"
"github.com/thrasher-corp/gocryptotrader/exchanges/ticker"
"github.com/thrasher-corp/gocryptotrader/exchanges/trade"
testexch "github.com/thrasher-corp/gocryptotrader/internal/testing/exchange"
testsubs "github.com/thrasher-corp/gocryptotrader/internal/testing/subscriptions"
@@ -3346,28 +3344,6 @@ func TestChannelName(t *testing.T) {
assert.Panics(t, func() { channelName(&subscription.Subscription{Channel: "wibble"}) }, "Unknown channels should panic")
}
func TestFetchTicker(t *testing.T) {
t.Parallel()
var result *ticker.Price
var err error
for assetType, cp := range assetTypeToPairsMap {
result, err = d.FetchTicker(context.Background(), cp, assetType)
require.NoErrorf(t, err, "expected nil, got %v for asset type %s pair %s", err, assetType, cp)
require.NotNilf(t, result, "expected result not to be nil for asset type %s pair %s", assetType, cp)
}
}
func TestFetchOrderbook(t *testing.T) {
t.Parallel()
var result *orderbook.Base
var err error
for assetType, cp := range assetTypeToPairsMap {
result, err = d.FetchOrderbook(context.Background(), cp, assetType)
require.NoErrorf(t, err, "expected nil, got %v for asset type %s", err, assetType)
require.NotNilf(t, result, "expected result not to be nil for asset type %s", assetType)
}
}
func TestUpdateAccountInfo(t *testing.T) {
t.Parallel()
sharedtestvalues.SkipTestIfCredentialsUnset(t, d)
@@ -3376,17 +3352,6 @@ func TestUpdateAccountInfo(t *testing.T) {
assert.NotNil(t, result)
}
func TestFetchAccountInfo(t *testing.T) {
t.Parallel()
sharedtestvalues.SkipTestIfCredentialsUnset(t, d)
assetTypes := d.GetAssetTypes(true)
for _, assetType := range assetTypes {
result, err := d.FetchAccountInfo(context.Background(), assetType)
require.NoErrorf(t, err, "expected nil, got %v for asset type %s", err, assetType)
require.NotNilf(t, result, "expected result not to be nil for asset type %s", assetType)
}
}
func TestGetFundingHistory(t *testing.T) {
t.Parallel()
sharedtestvalues.SkipTestIfCredentialsUnset(t, d)

View File

@@ -294,24 +294,6 @@ func (d *Deribit) UpdateTicker(ctx context.Context, p currency.Pair, assetType a
return ticker.GetTicker(d.Name, p, assetType)
}
// FetchTicker returns the ticker for a currency pair
func (d *Deribit) FetchTicker(ctx context.Context, p currency.Pair, assetType asset.Item) (*ticker.Price, error) {
tickerNew, err := ticker.GetTicker(d.Name, p, assetType)
if err != nil {
return d.UpdateTicker(ctx, p, assetType)
}
return tickerNew, nil
}
// FetchOrderbook returns orderbook base on the currency pair
func (d *Deribit) FetchOrderbook(ctx context.Context, currencyPair currency.Pair, assetType asset.Item) (*orderbook.Base, error) {
ob, err := orderbook.Get(d.Name, currencyPair, assetType)
if err != nil {
return d.UpdateOrderbook(ctx, currencyPair, assetType)
}
return ob, nil
}
// UpdateOrderbook updates and returns the orderbook for a currency pair
func (d *Deribit) UpdateOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) {
p, err := d.FormatExchangeCurrency(p, assetType)
@@ -391,19 +373,6 @@ func (d *Deribit) UpdateAccountInfo(ctx context.Context, _ asset.Item) (account.
return resp, nil
}
// FetchAccountInfo retrieves balances for all enabled currencies
func (d *Deribit) FetchAccountInfo(ctx context.Context, assetType asset.Item) (account.Holdings, error) {
creds, err := d.GetCredentials(ctx)
if err != nil {
return account.Holdings{}, err
}
accountData, err := account.GetHoldings(d.Name, creds, assetType)
if err != nil {
return d.UpdateAccountInfo(ctx, assetType)
}
return accountData, nil
}
// GetAccountFundingHistory returns funding history, deposits and withdrawals
func (d *Deribit) GetAccountFundingHistory(ctx context.Context) ([]exchange.FundingHistory, error) {
var currencies []CurrencyData