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

@@ -820,17 +820,14 @@ func getOfflineTradeFee(price, amount float64) float64 {
// getMultiplier retrieves account based taker/maker fees
func (e *Exchange) getMultiplier(ctx context.Context, isMaker bool) (float64, error) {
var multiplier float64
account, err := e.GetAccount(ctx)
a, err := e.GetAccount(ctx)
if err != nil {
return 0, err
}
if isMaker {
multiplier = float64(account.MakerCommission)
} else {
multiplier = float64(account.TakerCommission)
return float64(a.MakerCommission), nil
}
return multiplier, nil
return float64(a.TakerCommission), nil
}
// calculateTradingFee returns the fee for trading any currency on Binance

View File

@@ -1707,9 +1707,11 @@ func TestCancelAllExchangeOrders(t *testing.T) {
}
}
func TestGetAccountInfo(t *testing.T) {
func TestUpdateAccountBalances(t *testing.T) {
t.Parallel()
sharedtestvalues.SkipTestIfCredentialsUnset(t, e)
e := new(Exchange) //nolint:govet // Intentional shadow to avoid future copy/paste mistakes
require.NoError(t, testexch.Setup(e), "Test instance Setup must not error")
items := asset.Items{
asset.CoinMarginedFutures,
asset.USDTMarginedFutures,
@@ -1720,10 +1722,8 @@ func TestGetAccountInfo(t *testing.T) {
assetType := items[i]
t.Run(fmt.Sprintf("Update info of account [%s]", assetType.String()), func(t *testing.T) {
t.Parallel()
_, err := e.UpdateAccountInfo(t.Context(), assetType)
if err != nil {
t.Error(err)
}
_, err := e.UpdateAccountBalances(t.Context(), assetType)
require.NoError(t, err)
})
}
}

View File

@@ -444,7 +444,7 @@ type QueryOrderData 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"`
}
@@ -476,12 +476,12 @@ type MarginAccount struct {
// MarginAccountAsset holds each individual margin account asset
type MarginAccountAsset struct {
Asset string `json:"asset"`
Borrowed float64 `json:"borrowed,string"`
Free float64 `json:"free,string"`
Interest float64 `json:"interest,string"`
Locked float64 `json:"locked,string"`
NetAsset float64 `json:"netAsset,string"`
Asset currency.Code `json:"asset"`
Borrowed float64 `json:"borrowed,string"`
Free float64 `json:"free,string"`
Interest float64 `json:"interest,string"`
Locked float64 `json:"locked,string"`
NetAsset float64 `json:"netAsset,string"`
}
// RequestParamsOrderType trade order type

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/collateral"
"github.com/thrasher-corp/gocryptotrader/exchanges/deposit"
@@ -552,113 +552,79 @@ func (e *Exchange) UpdateOrderbook(ctx context.Context, p currency.Pair, a asset
return orderbook.Get(e.Name, p, a)
}
// UpdateAccountInfo retrieves balances for all enabled currencies for the
// Binance exchange
func (e *Exchange) UpdateAccountInfo(ctx context.Context, assetType asset.Item) (account.Holdings, error) {
var info account.Holdings
var acc account.SubAccount
acc.AssetType = assetType
info.Exchange = e.Name
// UpdateAccountBalances retrieves currency balances
func (e *Exchange) UpdateAccountBalances(ctx context.Context, assetType asset.Item) (subAccts accounts.SubAccounts, err error) {
switch assetType {
case asset.Spot:
creds, err := e.GetCredentials(ctx)
if err != nil {
return info, err
return nil, err
}
if creds.SubAccount != "" {
// TODO: implement sub-account endpoints
return info, common.ErrNotYetImplemented
return nil, common.ErrNotYetImplemented
}
raw, err := e.GetAccount(ctx)
resp, err := e.GetAccount(ctx)
if err != nil {
return info, err
return nil, err
}
var currencyBalance []account.Balance
for i := range raw.Balances {
free := raw.Balances[i].Free.InexactFloat64()
locked := raw.Balances[i].Locked.InexactFloat64()
currencyBalance = append(currencyBalance, account.Balance{
Currency: currency.NewCode(raw.Balances[i].Asset),
Total: free + locked,
Hold: locked,
Free: free,
subAccts = accounts.SubAccounts{accounts.NewSubAccount(assetType, "")}
for i := range resp.Balances {
free := resp.Balances[i].Free.InexactFloat64()
locked := resp.Balances[i].Locked.InexactFloat64()
subAccts[0].Balances.Set(resp.Balances[i].Asset, accounts.Balance{
Total: free + locked,
Hold: locked,
Free: free,
})
}
acc.Currencies = currencyBalance
case asset.CoinMarginedFutures:
accData, err := e.GetFuturesAccountInfo(ctx)
resp, err := e.GetFuturesAccountInfo(ctx)
if err != nil {
return info, err
return nil, err
}
var currencyDetails []account.Balance
for i := range accData.Assets {
currencyDetails = append(currencyDetails, account.Balance{
Currency: currency.NewCode(accData.Assets[i].Asset),
Total: accData.Assets[i].WalletBalance,
Hold: accData.Assets[i].WalletBalance - accData.Assets[i].AvailableBalance,
Free: accData.Assets[i].AvailableBalance,
subAccts = accounts.SubAccounts{accounts.NewSubAccount(assetType, "")}
for i := range resp.Assets {
subAccts[0].Balances.Set(resp.Assets[i].Asset, accounts.Balance{
Total: resp.Assets[i].WalletBalance,
Hold: resp.Assets[i].WalletBalance - resp.Assets[i].AvailableBalance,
Free: resp.Assets[i].AvailableBalance,
})
}
acc.Currencies = currencyDetails
case asset.USDTMarginedFutures:
accData, err := e.UAccountBalanceV2(ctx)
resp, err := e.UAccountBalanceV2(ctx)
if err != nil {
return info, err
return nil, err
}
accountCurrencyDetails := make(map[string][]account.Balance)
for i := range accData {
currencyDetails := accountCurrencyDetails[accData[i].AccountAlias]
accountCurrencyDetails[accData[i].AccountAlias] = append(
currencyDetails, account.Balance{
Currency: currency.NewCode(accData[i].Asset),
Total: accData[i].Balance,
Hold: accData[i].Balance - accData[i].AvailableBalance,
Free: accData[i].AvailableBalance,
},
)
}
if info.Accounts, err = account.CollectBalances(accountCurrencyDetails, assetType); err != nil {
return account.Holdings{}, err
subAccts = make(accounts.SubAccounts, 0, len(resp))
for i := range resp {
a := accounts.NewSubAccount(assetType, resp[i].AccountAlias)
a.Balances.Set(resp[i].Asset, accounts.Balance{
Total: resp[i].Balance,
Hold: resp[i].Balance - resp[i].AvailableBalance,
Free: resp[i].AvailableBalance,
})
subAccts = subAccts.Merge(a)
}
case asset.Margin:
accData, err := e.GetMarginAccount(ctx)
resp, err := e.GetMarginAccount(ctx)
if err != nil {
return info, err
return nil, err
}
var currencyDetails []account.Balance
for i := range accData.UserAssets {
currencyDetails = append(currencyDetails, account.Balance{
Currency: currency.NewCode(accData.UserAssets[i].Asset),
Total: accData.UserAssets[i].Free + accData.UserAssets[i].Locked,
Hold: accData.UserAssets[i].Locked,
Free: accData.UserAssets[i].Free,
AvailableWithoutBorrow: accData.UserAssets[i].Free - accData.UserAssets[i].Borrowed,
Borrowed: accData.UserAssets[i].Borrowed,
subAccts = accounts.SubAccounts{accounts.NewSubAccount(assetType, "")}
for i := range resp.UserAssets {
subAccts[0].Balances.Set(resp.UserAssets[i].Asset, accounts.Balance{
Total: resp.UserAssets[i].Free + resp.UserAssets[i].Locked,
Hold: resp.UserAssets[i].Locked,
Free: resp.UserAssets[i].Free,
AvailableWithoutBorrow: resp.UserAssets[i].Free - resp.UserAssets[i].Borrowed,
Borrowed: resp.UserAssets[i].Borrowed,
})
}
acc.Currencies = currencyDetails
default:
return info, fmt.Errorf("%w %v", asset.ErrNotSupported, assetType)
return nil, fmt.Errorf("%w %v", asset.ErrNotSupported, assetType)
}
acc.AssetType = assetType
info.Accounts = append(info.Accounts, acc)
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
@@ -1589,10 +1555,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)
}
@@ -2520,9 +2485,8 @@ func (e *Exchange) GetFuturesPositionSummary(ctx context.Context, req *futures.P
}
var accountAsset *FuturesAccountAsset
for i := range ai.Assets {
// TODO: utilise contract data to discern the underlying currency
// instead of having a user provide it
if ai.Assets[i].Asset != req.UnderlyingPair.Base.Upper().String() {
// TODO: utilise contract data to discern the underlying currency instead of having a user provide it
if !ai.Assets[i].Asset.Equal(req.UnderlyingPair.Base) {
continue
}
accountAsset = &ai.Assets[i]
@@ -2599,7 +2563,7 @@ func (e *Exchange) GetFuturesPositionSummary(ctx context.Context, req *futures.P
MarginType: marginType,
CollateralMode: collateralMode,
ContractSettlementType: contractSettlementType,
Currency: currency.NewCode(accountAsset.Asset),
Currency: accountAsset.Asset,
IsolatedMargin: decimal.NewFromFloat(isolatedMargin),
NotionalSize: decimal.NewFromFloat(positionSize).Mul(decimal.NewFromFloat(markPrice)),
Leverage: decimal.NewFromFloat(leverage),

View File

@@ -396,18 +396,18 @@ type FuturesAccountInformation struct {
// FuturesAccountAsset holds account asset information
type FuturesAccountAsset struct {
Asset string `json:"asset"`
WalletBalance float64 `json:"walletBalance,string"`
UnrealizedProfit float64 `json:"unrealizedProfit,string"`
MarginBalance float64 `json:"marginBalance,string"`
MaintenanceMargin float64 `json:"maintMargin,string"`
InitialMargin float64 `json:"initialMargin,string"`
PositionInitialMargin float64 `json:"positionInitialMargin,string"`
OpenOrderInitialMargin float64 `json:"openOrderInitialMargin,string"`
MaxWithdrawAmount float64 `json:"maxWithdrawAmount,string"`
CrossWalletBalance float64 `json:"crossWalletBalance,string"`
CrossUnPNL float64 `json:"crossUnPnl,string"`
AvailableBalance float64 `json:"availableBalance,string"`
Asset currency.Code `json:"asset"`
WalletBalance float64 `json:"walletBalance,string"`
UnrealizedProfit float64 `json:"unrealizedProfit,string"`
MarginBalance float64 `json:"marginBalance,string"`
MaintenanceMargin float64 `json:"maintMargin,string"`
InitialMargin float64 `json:"initialMargin,string"`
PositionInitialMargin float64 `json:"positionInitialMargin,string"`
OpenOrderInitialMargin float64 `json:"openOrderInitialMargin,string"`
MaxWithdrawAmount float64 `json:"maxWithdrawAmount,string"`
CrossWalletBalance float64 `json:"crossWalletBalance,string"`
CrossUnPNL float64 `json:"crossUnPnl,string"`
AvailableBalance float64 `json:"availableBalance,string"`
}
// GenericAuthResponse is a general data response for a post auth request

View File

@@ -239,13 +239,13 @@ type UFuturesOrderData struct {
// UAccountBalanceV2Data stores account balance data for ufutures
type UAccountBalanceV2Data struct {
AccountAlias string `json:"accountAlias"`
Asset string `json:"asset"`
Balance float64 `json:"balance,string"`
CrossWalletBalance float64 `json:"crossWalletBalance,string"`
CrossUnrealizedPNL float64 `json:"crossUnPnl,string"`
AvailableBalance float64 `json:"availableBalance,string"`
MaxWithdrawAmount float64 `json:"maxWithdrawAmount,string"`
AccountAlias string `json:"accountAlias"`
Asset currency.Code `json:"asset"`
Balance float64 `json:"balance,string"`
CrossWalletBalance float64 `json:"crossWalletBalance,string"`
CrossUnrealizedPNL float64 `json:"crossUnPnl,string"`
AvailableBalance float64 `json:"availableBalance,string"`
MaxWithdrawAmount float64 `json:"maxWithdrawAmount,string"`
}
// UAccountInformationV2Data stores account info for ufutures