accounts: Move to instance methods, fix races and isolate tests (#1923)

* Bybit: Fix race in TestUpdateAccountInfo and  TestWSHandleData

* DriveBy rename TestWSHandleData
* This doesn't address running with -race=2+ due to the singleton

* Accounts: Add account.GetService()

* exchange: Assertify TestSetupDefaults

* Exchanges: Add account.Service override for testing

* Exchanges: Remove duplicate IsWebsocketEnabled test from TestSetupDefaults

* Dispatch: Replace nil checks with NilGuard

* Engine: Remove deprecated printAccountHoldingsChangeSummary

* Dispatcher: Add EnsureRunning method

* Accounts: Move singleton accounts service to exchange Accounts

* Move singleton accounts service to exchange Accounts

This maintains the concept of a global store, whilst allowing exchanges
to override it when needed, particularly for testing.

APIServer:

* Remove getAllActiveAccounts from apiserver

Deprecated apiserver only thing using this, so remove it instead of
updating it

* Update comment for UpdateAccountBalances everywhere

* Docs: Add punctuation to function comments

* Bybit: Coverage for wsProcessWalletPushData Save
This commit is contained in:
Gareth Kirwan
2025-10-28 09:52:45 +07:00
committed by GitHub
parent bda9bbec66
commit 73e200e4e7
140 changed files with 3515 additions and 4025 deletions

View File

@@ -421,8 +421,8 @@ type FuturesNotificationData struct {
// FuturesAccountsData stores account data
type FuturesAccountsData struct {
ServerTime string `json:"serverTime"`
Accounts map[string]AccountsData `json:"accounts"`
ServerTime string `json:"serverTime"`
Accounts map[string]*AccountsData `json:"accounts"`
}
// AccountsData stores data of an account

View File

@@ -729,17 +729,17 @@ func TestCancelAllExchangeOrders(t *testing.T) {
assert.Empty(t, resp.Status, "CancelAllOrders Status should not contain any failed order errors")
}
// TestUpdateAccountInfo exercises UpdateAccountInfo
func TestUpdateAccountInfo(t *testing.T) {
// TestUpdateAccountBalances exercises UpdateAccountBalances
func TestUpdateAccountBalances(t *testing.T) {
t.Parallel()
for _, a := range []asset.Item{asset.Spot, asset.Futures} {
_, err := e.UpdateAccountInfo(t.Context(), a)
_, err := e.UpdateAccountBalances(t.Context(), a)
if sharedtestvalues.AreAPICredentialsSet(e) {
assert.NoErrorf(t, err, "UpdateAccountInfo should not error for asset %s", a) // Note Well: Spot and Futures have separate api keys
assert.NoErrorf(t, err, "UpdateAccountBalances should not error for asset %s", a) // Note Well: Spot and Futures have separate api keys
} else {
assert.ErrorIsf(t, err, exchange.ErrAuthenticationSupportNotEnabled, "UpdateAccountInfo should error correctly for asset %s", a)
assert.ErrorIsf(t, err, exchange.ErrAuthenticationSupportNotEnabled, "UpdateAccountBalances should error correctly for asset %s", a)
}
}
}

View File

@@ -14,11 +14,11 @@ import (
"github.com/thrasher-corp/gocryptotrader/common/key"
"github.com/thrasher-corp/gocryptotrader/config"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/exchange/accounts"
"github.com/thrasher-corp/gocryptotrader/exchange/order/limits"
"github.com/thrasher-corp/gocryptotrader/exchange/websocket"
"github.com/thrasher-corp/gocryptotrader/exchange/websocket/buffer"
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
"github.com/thrasher-corp/gocryptotrader/exchanges/account"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/deposit"
"github.com/thrasher-corp/gocryptotrader/exchanges/fundingrate"
@@ -511,69 +511,46 @@ func (e *Exchange) UpdateOrderbook(ctx context.Context, p currency.Pair, assetTy
return orderbook.Get(e.Name, p, assetType)
}
// UpdateAccountInfo retrieves balances for all enabled currencies for the
// Kraken exchange - to-do
func (e *Exchange) UpdateAccountInfo(ctx context.Context, assetType asset.Item) (account.Holdings, error) {
var info account.Holdings
var balances []account.Balance
info.Exchange = e.Name
// UpdateAccountBalances retrieves currency balances
func (e *Exchange) UpdateAccountBalances(ctx context.Context, assetType asset.Item) (subAccts accounts.SubAccounts, err error) {
if !assetTranslator.Seeded() {
if err := e.SeedAssets(ctx); err != nil {
return info, err
return nil, err
}
}
switch assetType {
case asset.Spot:
bal, err := e.GetBalance(ctx)
resp, err := e.GetBalance(ctx)
if err != nil {
return info, err
return nil, err
}
for key := range bal {
translatedCurrency := assetTranslator.LookupAltName(key)
if translatedCurrency == "" {
log.Warnf(log.ExchangeSys, "%s unable to translate currency: %s\n",
e.Name,
key)
subAccts = accounts.SubAccounts{accounts.NewSubAccount(assetType, "")}
for key, bal := range resp {
c := assetTranslator.LookupAltName(key)
if c == "" {
log.Warnf(log.ExchangeSys, "%s unable to translate currency: %s", e.Name, key)
continue
}
balances = append(balances, account.Balance{
Currency: currency.NewCode(translatedCurrency),
Total: bal[key].Total,
Hold: bal[key].Hold,
Free: bal[key].Total - bal[key].Hold,
subAccts[0].Balances.Set(currency.NewCode(c), accounts.Balance{
Total: bal.Total,
Hold: bal.Hold,
Free: bal.Total - bal.Hold,
})
}
info.Accounts = append(info.Accounts, account.SubAccount{
Currencies: balances,
AssetType: assetType,
})
case asset.Futures:
bal, err := e.GetFuturesAccountData(ctx)
resp, err := e.GetFuturesAccountData(ctx)
if err != nil {
return info, err
return nil, err
}
for name := range bal.Accounts {
for code := range bal.Accounts[name].Balances {
balances = append(balances, account.Balance{
Currency: currency.NewCode(code).Upper(),
Total: bal.Accounts[name].Balances[code],
})
for name, v := range resp.Accounts {
a := accounts.NewSubAccount(assetType, name)
for curr, bal := range v.Balances {
a.Balances.Set(currency.NewCode(curr), accounts.Balance{Total: bal})
}
info.Accounts = append(info.Accounts, account.SubAccount{
ID: name,
AssetType: asset.Futures,
Currencies: balances,
})
subAccts = subAccts.Merge(a)
}
}
creds, err := e.GetCredentials(ctx)
if err != nil {
return account.Holdings{}, err
}
if err := account.Process(&info, creds); err != nil {
return account.Holdings{}, err
}
return info, nil
return subAccts, e.Accounts.Save(ctx, subAccts, true)
}
// GetAccountFundingHistory returns funding history, deposits and
@@ -1407,10 +1384,9 @@ func (e *Exchange) AuthenticateWebsocket(ctx context.Context) error {
return nil
}
// ValidateAPICredentials validates current credentials used for wrapper
// functionality
// ValidateAPICredentials validates current credentials used for wrapper functionality
func (e *Exchange) ValidateAPICredentials(ctx context.Context, assetType asset.Item) error {
_, err := e.UpdateAccountInfo(ctx, assetType)
_, err := e.UpdateAccountBalances(ctx, assetType)
return e.CheckTransientError(err)
}