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

@@ -224,9 +224,9 @@ type OneDayNotionalVolume struct {
// Balance is a simple balance type
type Balance struct {
Currency string `json:"currency"`
Amount float64 `json:"amount,string"`
Available float64 `json:"available,string"`
Currency currency.Code `json:"currency"`
Amount float64 `json:"amount,string"`
Available float64 `json:"available,string"`
}
// DepositAddress holds assigned deposit address for a specific currency

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"
@@ -215,41 +215,21 @@ func (e *Exchange) UpdateTradablePairs(ctx context.Context) error {
return e.EnsureOnePairEnabled()
}
// UpdateAccountInfo Retrieves balances for all enabled currencies for the
// Gemini 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.GetBalances(ctx)
// UpdateAccountBalances retrieves currency balances
func (e *Exchange) UpdateAccountBalances(ctx context.Context, assetType asset.Item) (accounts.SubAccounts, error) {
resp, err := e.GetBalances(ctx)
if err != nil {
return response, err
return nil, err
}
currencies := make([]account.Balance, len(accountBalance))
for i := range accountBalance {
currencies[i] = account.Balance{
Currency: currency.NewCode(accountBalance[i].Currency),
Total: accountBalance[i].Amount,
Hold: accountBalance[i].Amount - accountBalance[i].Available,
Free: accountBalance[i].Available,
}
subAccts := accounts.SubAccounts{accounts.NewSubAccount(assetType, "")}
for i := range resp {
subAccts[0].Balances.Set(resp[i].Currency, accounts.Balance{
Total: resp[i].Amount,
Hold: resp[i].Amount - resp[i].Available,
Free: resp[i].Available,
})
}
response.Accounts = append(response.Accounts, account.SubAccount{
AssetType: assetType,
Currencies: currencies,
})
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)
}
// UpdateTickers updates the ticker for all currency pairs of a given asset type
@@ -758,10 +738,9 @@ func (e *Exchange) GetOrderHistory(ctx context.Context, req *order.MultiOrderReq
return req.Filter(e.Name, orders), 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)
}