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

@@ -121,13 +121,11 @@ func TestUpdateTradablePairs(t *testing.T) {
}
}
func TestUpdateAccountInfo(t *testing.T) {
func TestUpdateAccountBalances(t *testing.T) {
t.Parallel()
sharedtestvalues.SkipTestIfCredentialsUnset(t, e)
_, err := e.UpdateAccountInfo(t.Context(), asset.Spot)
if err != nil {
t.Error("Binanceus UpdateAccountInfo() error", err)
}
_, err := e.UpdateAccountBalances(t.Context(), asset.Spot)
require.NoError(t, err)
}
func TestGetRecentTrades(t *testing.T) {

View File

@@ -278,7 +278,7 @@ type Account struct {
// Balance holds query order data
type Balance struct {
Asset string `json:"asset"`
Asset currency.Code `json:"asset"`
Free decimal.Decimal `json:"free"`
Locked decimal.Decimal `json:"locked"`
}

View File

@@ -11,10 +11,10 @@ import (
"github.com/thrasher-corp/gocryptotrader/common"
"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/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"
@@ -335,41 +335,26 @@ func (e *Exchange) UpdateOrderbook(ctx context.Context, pair currency.Pair, asse
return orderbook.Get(e.Name, pair, assetType)
}
// UpdateAccountInfo retrieves balances for all enabled currencies
func (e *Exchange) UpdateAccountInfo(ctx context.Context, assetType asset.Item) (account.Holdings, error) {
var info account.Holdings
var acc account.SubAccount
info.Exchange = e.Name
// UpdateAccountBalances retrieves currency balances
func (e *Exchange) UpdateAccountBalances(ctx context.Context, assetType asset.Item) (accounts.SubAccounts, error) {
if assetType != asset.Spot {
return info, fmt.Errorf("%w %v", asset.ErrNotSupported, assetType)
return nil, fmt.Errorf("%w %v", asset.ErrNotSupported, assetType)
}
theAccount, err := e.GetAccount(ctx)
resp, err := e.GetAccount(ctx)
if err != nil {
return info, err
return nil, err
}
currencyBalance := make([]account.Balance, len(theAccount.Balances))
for i := range theAccount.Balances {
freeBalance := theAccount.Balances[i].Free.InexactFloat64()
locked := theAccount.Balances[i].Locked.InexactFloat64()
currencyBalance[i] = account.Balance{
Currency: currency.NewCode(theAccount.Balances[i].Asset),
Total: freeBalance + locked,
Hold: locked,
Free: freeBalance,
}
subAccts := accounts.SubAccounts{accounts.NewSubAccount(assetType, "")}
for i := range resp.Balances {
freeBalance := resp.Balances[i].Free.InexactFloat64()
locked := resp.Balances[i].Locked.InexactFloat64()
subAccts[0].Balances.Set(resp.Balances[i].Asset, accounts.Balance{
Total: freeBalance + locked,
Hold: locked,
Free: freeBalance,
})
}
acc.Currencies = currencyBalance
acc.AssetType = assetType
info.Accounts = append(info.Accounts, acc)
creds, err := e.GetCredentials(ctx)
if err != nil {
return info, 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 withdrawals
@@ -777,7 +762,7 @@ func (e *Exchange) GetFeeByType(ctx context.Context, feeBuilder *exchange.FeeBui
// ValidateAPICredentials validates current credentials used for wrapper
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)
}