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

@@ -407,11 +407,8 @@ func TestGetLeaderboard(t *testing.T) {
func TestGetAccountFees(t *testing.T) {
t.Parallel()
sharedtestvalues.SkipTestIfCredentialsUnset(t, e)
_, err := e.UpdateAccountInfo(t.Context(), asset.Spot)
if err != nil {
t.Error("GetAccountInfo error", err)
}
_, err := e.UpdateAccountBalances(t.Context(), asset.Spot)
assert.NoError(t, err)
}
func TestGetWithdrawalFee(t *testing.T) {

View File

@@ -341,10 +341,10 @@ type MarginLimits struct {
// Balance holds current balance data
type Balance struct {
Type string `json:"type"`
Currency string `json:"currency"`
Amount float64 `json:"amount,string"`
Available float64 `json:"available,string"`
Type string `json:"type"`
Currency currency.Code `json:"currency"`
Amount float64 `json:"amount,string"`
Available float64 `json:"available,string"`
}
// WalletTransfer holds status of wallet to wallet content transfer on exchange

View File

@@ -14,10 +14,10 @@ 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"
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"
@@ -404,50 +404,23 @@ func (e *Exchange) UpdateOrderbook(ctx context.Context, p currency.Pair, assetTy
return orderbook.Get(e.Name, fPair, assetType)
}
// UpdateAccountInfo retrieves balances for all enabled currencies on the
// Bitfinex exchange
func (e *Exchange) UpdateAccountInfo(ctx context.Context, assetType asset.Item) (account.Holdings, error) {
var response account.Holdings
response.Exchange = e.Name
accountBalance, err := e.GetAccountBalance(ctx)
// UpdateAccountBalances retrieves currency balances
func (e *Exchange) UpdateAccountBalances(ctx context.Context, assetType asset.Item) (accounts.SubAccounts, error) {
resp, err := e.GetAccountBalance(ctx)
if err != nil {
return response, err
return nil, err
}
Accounts := []account.SubAccount{
{ID: "deposit", AssetType: assetType},
{ID: "exchange", AssetType: assetType},
{ID: "trading", AssetType: assetType},
{ID: "margin", AssetType: assetType},
{ID: "funding", AssetType: assetType},
subAccts := accounts.SubAccounts{}
for i := range resp {
a := accounts.NewSubAccount(assetType, resp[i].Type)
a.Balances.Set(resp[i].Currency, accounts.Balance{
Total: resp[i].Amount,
Hold: resp[i].Amount - resp[i].Available,
Free: resp[i].Available,
})
subAccts = subAccts.Merge(a)
}
for x := range accountBalance {
for i := range Accounts {
if Accounts[i].ID == accountBalance[x].Type {
Accounts[i].Currencies = append(Accounts[i].Currencies,
account.Balance{
Currency: currency.NewCode(accountBalance[x].Currency),
Total: accountBalance[x].Amount,
Hold: accountBalance[x].Amount - accountBalance[x].Available,
Free: accountBalance[x].Available,
})
}
}
}
response.Accounts = Accounts
creds, err := e.GetCredentials(ctx)
if err != nil {
return account.Holdings{}, err
}
err = account.Process(&response, creds)
if err != nil {
return account.Holdings{}, err
}
return response, nil
return subAccts, e.Accounts.Save(ctx, subAccts, true)
}
// GetAccountFundingHistory returns funding history, deposits and
@@ -992,10 +965,9 @@ func (e *Exchange) appendOptionalDelimiter(p *currency.Pair) {
}
}
// 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)
}