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

@@ -21,9 +21,9 @@ import (
"github.com/stretchr/testify/require"
"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/exchange/accounts"
"github.com/thrasher-corp/gocryptotrader/exchange/order/limits"
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/kline"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
@@ -227,31 +227,30 @@ func TestLoadPrivKey(t *testing.T) {
e.SetDefaults()
require.ErrorIs(t, e.loadPrivKey(t.Context()), exchange.ErrCredentialsAreEmpty)
ctx := account.DeployCredentialsToContext(t.Context(), &account.Credentials{Key: "test", Secret: "errortest"})
ctx := accounts.DeployCredentialsToContext(t.Context(), &accounts.Credentials{Key: "test", Secret: "errortest"})
assert.ErrorIs(t, e.loadPrivKey(ctx), errPEMBlockIsNil)
key, err := rsa.GenerateKey(rand.Reader, 2048)
require.NoError(t, err)
der := x509.MarshalPKCS1PrivateKey(key)
ctx = account.DeployCredentialsToContext(t.Context(), &account.Credentials{Key: "test", Secret: base64.StdEncoding.EncodeToString(der)})
ctx = accounts.DeployCredentialsToContext(t.Context(), &accounts.Credentials{Key: "test", Secret: base64.StdEncoding.EncodeToString(der)})
require.ErrorIs(t, e.loadPrivKey(ctx), errUnableToParsePrivateKey)
ecdsaKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
require.NoError(t, err)
der, err = x509.MarshalPKCS8PrivateKey(ecdsaKey)
require.NoError(t, err)
ctx = account.DeployCredentialsToContext(t.Context(), &account.Credentials{Key: "test", Secret: base64.StdEncoding.EncodeToString(der)})
ctx = accounts.DeployCredentialsToContext(t.Context(), &accounts.Credentials{Key: "test", Secret: base64.StdEncoding.EncodeToString(der)})
require.ErrorIs(t, e.loadPrivKey(ctx), common.ErrTypeAssertFailure)
key, err = rsa.GenerateKey(rand.Reader, 2048)
require.NoError(t, err)
der, err = x509.MarshalPKCS8PrivateKey(key)
require.NoError(t, err)
ctx = account.DeployCredentialsToContext(t.Context(), &account.Credentials{Key: "test", Secret: base64.StdEncoding.EncodeToString(der)})
ctx = accounts.DeployCredentialsToContext(t.Context(), &accounts.Credentials{Key: "test", Secret: base64.StdEncoding.EncodeToString(der)})
assert.NoError(t, e.loadPrivKey(ctx), "loadPrivKey should not error")
sharedtestvalues.SkipTestIfCredentialsUnset(t, e)
assert.NoError(t, e.loadPrivKey(t.Context()), "loadPrivKey should not error")
}
@@ -350,8 +349,8 @@ func TestGetAccountInfo(t *testing.T) {
t.Parallel()
sharedtestvalues.SkipTestIfCredentialsUnset(t, e)
_, err := e.UpdateAccountInfo(t.Context(), asset.Spot)
assert.NoError(t, err, "UpdateAccountInfo should not error")
_, err := e.UpdateAccountBalances(t.Context(), asset.Spot)
assert.NoError(t, err, "UpdateAccountBalances should not error")
}
func TestGetActiveOrders(t *testing.T) {

View File

@@ -11,8 +11,8 @@ 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"
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"
@@ -224,42 +224,27 @@ 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
// Lbank exchange
func (e *Exchange) UpdateAccountInfo(ctx context.Context, assetType asset.Item) (account.Holdings, error) {
var info account.Holdings
data, err := e.GetUserInfo(ctx)
// UpdateAccountBalances retrieves currency balances
func (e *Exchange) UpdateAccountBalances(ctx context.Context, assetType asset.Item) (accounts.SubAccounts, error) {
resp, err := e.GetUserInfo(ctx)
if err != nil {
return info, err
return nil, err
}
acc := account.SubAccount{AssetType: assetType}
for key, val := range data.Info.Asset {
hold, ok := data.Info.Freeze[key]
subAccts := accounts.SubAccounts{accounts.NewSubAccount(assetType, "")}
for k, val := range resp.Info.Asset {
hold, ok := resp.Info.Freeze[k]
if !ok {
return info, fmt.Errorf("hold data not found with %s", key)
return nil, fmt.Errorf("hold data not found with %s", k)
}
totalVal := val.Float64()
totalHold := hold.Float64()
acc.Currencies = append(acc.Currencies, account.Balance{
Currency: currency.NewCode(key),
Total: totalVal,
Hold: totalHold,
Free: totalVal - totalHold,
subAccts[0].Balances.Set(currency.NewCode(k), accounts.Balance{
Total: totalVal,
Hold: totalHold,
Free: totalVal - totalHold,
})
}
info.Accounts = append(info.Accounts, acc)
info.Exchange = e.Name
creds, err := e.GetCredentials(ctx)
if err != nil {
return account.Holdings{}, err
}
err = account.Process(&info, creds)
if err != nil {
return account.Holdings{}, err
}
return info, nil
return subAccts, e.Accounts.Save(ctx, subAccts, true)
}
// GetAccountFundingHistory returns funding history, deposits and
@@ -771,10 +756,9 @@ func (e *Exchange) getAllOpenOrderID(ctx context.Context) (map[string][]string,
return resp, 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)
}