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

@@ -9,9 +9,9 @@ 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/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"
@@ -314,11 +314,11 @@ 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) {
// UpdateAccountBalances retrieves currency balances
func (e *Exchange) UpdateAccountBalances(ctx context.Context, assetType asset.Item) (accounts.SubAccounts, error) {
// If fetching requires more than one asset type please set
// HasAssetTypeAccountSegregation to true in RESTCapabilities above.
return account.Holdings{}, common.ErrNotYetImplemented
return accounts.SubAccounts{}, common.ErrNotYetImplemented
}
// GetAccountFundingHistory returns funding history, deposits and withdrawals
@@ -464,7 +464,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)
}
@@ -525,4 +525,4 @@ func (e *Exchange) SetLeverage(_ context.Context, _ asset.Item, _ currency.Pair,
return common.ErrNotYetImplemented
}
{{end}}
{{end}}

View File

@@ -20,8 +20,8 @@ import (
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/encoding/json"
"github.com/thrasher-corp/gocryptotrader/engine"
"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/collateral"
"github.com/thrasher-corp/gocryptotrader/exchanges/deposit"
@@ -546,17 +546,17 @@ func testWrappers(e exchange.IBotExchange, base *exchange.Base, cfg *Config) []E
})
}
var GetCachedAccountInfoResponse account.Holdings
GetCachedAccountInfoResponse, err = e.GetCachedAccountInfo(context.TODO(), assetTypes[i])
var GetCachedSubAccountsResponse accounts.SubAccounts
GetCachedSubAccountsResponse, err = e.GetCachedSubAccounts(context.TODO(), assetTypes[i])
msg = ""
if err != nil {
msg = err.Error()
responseContainer.ErrorCount++
}
responseContainer.EndpointResponses = append(responseContainer.EndpointResponses, EndpointResponse{
Function: "GetCachedAccountInfo",
Function: "GetCachedSubAccounts",
Error: msg,
Response: jsonifyInterface([]any{GetCachedAccountInfoResponse}),
Response: jsonifyInterface([]any{GetCachedSubAccountsResponse}),
})
var getFundingHistoryResponse []exchange.FundingHistory

View File

@@ -15,11 +15,12 @@ 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/dispatch"
"github.com/thrasher-corp/gocryptotrader/engine"
"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/collateral"
"github.com/thrasher-corp/gocryptotrader/exchanges/deposit"
@@ -50,9 +51,9 @@ func TestAllExchangeWrappers(t *testing.T) {
t.Parallel()
cfg := config.GetConfig()
err := cfg.LoadConfig("../../testdata/configtest.json", true)
if err != nil {
t.Fatal("load config error", err)
}
require.NoError(t, err, "LoadConfig must not error")
err = dispatch.EnsureRunning(dispatch.DefaultMaxWorkers, dispatch.DefaultJobsLimit)
require.NoError(t, err, "dispatch.EnsureRunning must not error")
for i := range cfg.Exchanges {
name := strings.ToLower(cfg.Exchanges[i].Name)
t.Run(name+" wrapper tests", func(t *testing.T) {
@@ -144,6 +145,7 @@ assets:
})
}
assetPairs = append(assetPairs, assetPair{})
return exch, assetPairs
}
@@ -284,7 +286,7 @@ var (
withdrawRequestParam = reflect.TypeOf((**withdraw.Request)(nil)).Elem()
stringParam = reflect.TypeOf((*string)(nil)).Elem()
feeBuilderParam = reflect.TypeOf((**exchange.FeeBuilder)(nil)).Elem()
credentialsParam = reflect.TypeOf((**account.Credentials)(nil)).Elem()
credentialsParam = reflect.TypeOf((**accounts.Credentials)(nil)).Elem()
orderSideParam = reflect.TypeOf((*order.Side)(nil)).Elem()
collateralModeParam = reflect.TypeOf((*collateral.Mode)(nil)).Elem()
marginTypeParam = reflect.TypeOf((*margin.Type)(nil)).Elem()
@@ -334,7 +336,7 @@ func generateMethodArg(ctx context.Context, t *testing.T, argGenerator *MethodAr
Asset: argGenerator.AssetParams.Asset,
})
case argGenerator.MethodInputType.AssignableTo(credentialsParam):
input = reflect.ValueOf(&account.Credentials{
input = reflect.ValueOf(&accounts.Credentials{
Key: "test",
Secret: "test",
ClientID: "test",
@@ -643,7 +645,8 @@ var acceptableErrors = []error{
limits.ErrExchangeLimitNotLoaded, // Is thrown when the limits aren't loaded for a particular exchange, asset, pair
limits.ErrOrderLimitNotFound, // Is thrown when the order limit isn't found for a particular exchange, asset, pair
limits.ErrEmptyLevels, // Is thrown if limits are not provided for the asset
account.ErrExchangeHoldingsNotFound,
accounts.ErrNoBalances,
accounts.ErrNoSubAccounts,
ticker.ErrTickerNotFound,
orderbook.ErrOrderbookNotFound,
websocket.ErrNotConnected,

View File

@@ -575,24 +575,26 @@ func getTickers(c *cli.Context) error {
return nil
}
var getAccountInfoCommand = &cli.Command{
Name: "getaccountinfo",
Usage: "gets the exchange account balance info",
var getAccountBalancesCommand = &cli.Command{
Name: "getaccountbalances",
Usage: "gets the exchange account balances",
ArgsUsage: "<exchange> <asset>",
Action: getAccountInfo,
Action: getAccountBalances,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "exchange",
Usage: "the exchange to get the account info for",
Name: "exchange",
Usage: "the exchange to get the account balances for",
Required: true,
},
&cli.StringFlag{
Name: "asset",
Usage: "the asset type to get the account info for",
Name: "asset",
Usage: "the asset type to get the account balances for",
Required: true,
},
},
}
func getAccountInfo(c *cli.Context) error {
func getAccountBalances(c *cli.Context) error {
if c.NArg() == 0 && c.NumFlags() == 0 {
return cli.ShowSubcommandHelp(c)
}
@@ -621,8 +623,8 @@ func getAccountInfo(c *cli.Context) error {
defer closeConn(conn, cancel)
client := gctrpc.NewGoCryptoTraderServiceClient(conn)
result, err := client.GetAccountInfo(c.Context,
&gctrpc.GetAccountInfoRequest{
result, err := client.GetAccountBalances(c.Context,
&gctrpc.GetAccountBalancesRequest{
Exchange: exchange,
AssetType: assetType,
},
@@ -635,24 +637,24 @@ func getAccountInfo(c *cli.Context) error {
return nil
}
var getAccountInfoStreamCommand = &cli.Command{
Name: "getaccountinfostream",
Usage: "gets the account info stream for a specific exchange",
var getAccountBalancesStreamCommand = &cli.Command{
Name: "getaccountbalancesstream",
Usage: "gets the account balances stream for a specific exchange",
ArgsUsage: "<exchange> <asset>",
Action: getAccountInfoStream,
Action: getAccountBalancesStream,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "exchange",
Usage: "the exchange to get the account info stream from",
Usage: "the exchange to get the account balances stream from",
},
&cli.StringFlag{
Name: "asset",
Usage: "the asset type to get the account info stream for",
Usage: "the asset type to get the account balances stream for",
},
},
}
func getAccountInfoStream(c *cli.Context) error {
func getAccountBalancesStream(c *cli.Context) error {
if c.NArg() == 0 && c.NumFlags() == 0 {
return cli.ShowSubcommandHelp(c)
}
@@ -683,8 +685,8 @@ func getAccountInfoStream(c *cli.Context) error {
defer closeConn(conn, cancel)
client := gctrpc.NewGoCryptoTraderServiceClient(conn)
result, err := client.GetAccountInfoStream(c.Context,
&gctrpc.GetAccountInfoRequest{Exchange: exchangeName, AssetType: assetType})
result, err := client.GetAccountBalancesStream(c.Context,
&gctrpc.GetAccountBalancesRequest{Exchange: exchangeName, AssetType: assetType})
if err != nil {
return err
}
@@ -706,24 +708,24 @@ func getAccountInfoStream(c *cli.Context) error {
}
}
var updateAccountInfoCommand = &cli.Command{
Name: "updateaccountinfo",
Usage: "updates the exchange account balance info",
var updateAccountBalancesCommand = &cli.Command{
Name: "updateaccountbalances",
Usage: "updates the exchange account balances",
ArgsUsage: "<exchange> <asset>",
Action: updateAccountInfo,
Action: updateAccountBalances,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "exchange",
Usage: "the exchange to get the account info for",
Usage: "the exchange to get the account balances for",
},
&cli.StringFlag{
Name: "asset",
Usage: "the asset type to get the account info for",
Usage: "the asset type to get the account balances for",
},
},
}
func updateAccountInfo(c *cli.Context) error {
func updateAccountBalances(c *cli.Context) error {
if c.NArg() == 0 && c.NumFlags() == 0 {
return cli.ShowSubcommandHelp(c)
}
@@ -753,8 +755,8 @@ func updateAccountInfo(c *cli.Context) error {
defer closeConn(conn, cancel)
client := gctrpc.NewGoCryptoTraderServiceClient(conn)
result, err := client.UpdateAccountInfo(c.Context,
&gctrpc.GetAccountInfoRequest{
result, err := client.UpdateAccountBalances(c.Context,
&gctrpc.GetAccountBalancesRequest{
Exchange: exchange,
AssetType: assetType,
},

View File

@@ -12,7 +12,7 @@ import (
"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/core"
"github.com/thrasher-corp/gocryptotrader/encoding/json"
"github.com/thrasher-corp/gocryptotrader/exchanges/account"
"github.com/thrasher-corp/gocryptotrader/exchange/accounts"
"github.com/thrasher-corp/gocryptotrader/gctrpc/auth"
"github.com/thrasher-corp/gocryptotrader/signaler"
"github.com/urfave/cli/v2"
@@ -28,7 +28,7 @@ var (
pairDelimiter string
certPath string
timeout time.Duration
exchangeCreds account.Credentials
exchangeCreds accounts.Credentials
verbose bool
ignoreTimeout bool
)
@@ -172,9 +172,9 @@ func main() {
getExchangeInfoCommand,
getTickerCommand,
getTickersCommand,
getAccountInfoCommand,
getAccountInfoStreamCommand,
updateAccountInfoCommand,
getAccountBalancesCommand,
getAccountBalancesStreamCommand,
updateAccountBalancesCommand,
getConfigCommand,
getPortfolioCommand,
getPortfolioSummaryCommand,