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

@@ -17,7 +17,7 @@ import (
"github.com/thrasher-corp/gocryptotrader/database"
"github.com/thrasher-corp/gocryptotrader/database/drivers"
"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/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
)
@@ -855,7 +855,7 @@ func TestGenerateConfigForDCALiveCandles(t *testing.T) {
ExchangeCredentials: []Credentials{
{
Exchange: mainExchange,
Keys: account.Credentials{
Keys: accounts.Credentials{
Key: "",
Secret: "",
},
@@ -1388,7 +1388,7 @@ func TestGenerateConfigForLiveCashAndCarry(t *testing.T) {
ExchangeCredentials: []Credentials{
{
Exchange: mainExchange,
Keys: account.Credentials{
Keys: accounts.Credentials{
Key: "",
Secret: "",
SubAccount: "",

View File

@@ -7,7 +7,7 @@ import (
"github.com/shopspring/decimal"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/database"
"github.com/thrasher-corp/gocryptotrader/exchanges/account"
"github.com/thrasher-corp/gocryptotrader/exchange/accounts"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
)
@@ -201,6 +201,6 @@ type LiveData struct {
// Credentials holds each exchanges credentials
type Credentials struct {
Exchange string `json:"exchange"`
Keys account.Credentials `json:"credentials"`
Exchange string `json:"exchange"`
Keys accounts.Credentials `json:"credentials"`
}

View File

@@ -39,8 +39,8 @@ import (
"github.com/thrasher-corp/gocryptotrader/database"
"github.com/thrasher-corp/gocryptotrader/database/drivers"
"github.com/thrasher-corp/gocryptotrader/engine"
"github.com/thrasher-corp/gocryptotrader/exchange/accounts"
gctexchange "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/binance"
"github.com/thrasher-corp/gocryptotrader/exchanges/binanceus"
@@ -337,7 +337,7 @@ func TestLoadDataLive(t *testing.T) {
ExchangeCredentials: []config.Credentials{
{
Exchange: testExchange,
Keys: account.Credentials{
Keys: accounts.Credentials{
Key: "test",
Secret: "test",
ClientID: "test",
@@ -1392,7 +1392,7 @@ func TestSetExchangeCredentials(t *testing.T) {
// enter them here
cfg.DataSettings.LiveData.ExchangeCredentials = []config.Credentials{{
Exchange: testExchange,
Keys: account.Credentials{
Keys: accounts.Credentials{
Key: "test",
Secret: "test",
},

View File

@@ -18,7 +18,7 @@ import (
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/signal"
"github.com/thrasher-corp/gocryptotrader/backtester/funding"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/exchanges/account"
"github.com/thrasher-corp/gocryptotrader/exchange/accounts"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/futures"
gctkline "github.com/thrasher-corp/gocryptotrader/exchanges/kline"
@@ -219,7 +219,7 @@ func (f fakeFunding) UpdateFundingFromLiveData(bool) error {
return nil
}
func (f fakeFunding) SetFunding(string, asset.Item, *account.Balance, bool) error {
func (f fakeFunding) SetFunding(string, asset.Item, *accounts.Balance, bool) error {
return nil
}

View File

@@ -23,7 +23,7 @@ import (
"github.com/thrasher-corp/gocryptotrader/database"
"github.com/thrasher-corp/gocryptotrader/database/drivers"
gctengine "github.com/thrasher-corp/gocryptotrader/engine"
"github.com/thrasher-corp/gocryptotrader/exchanges/account"
"github.com/thrasher-corp/gocryptotrader/exchange/accounts"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
gctkline "github.com/thrasher-corp/gocryptotrader/exchanges/kline"
"github.com/thrasher-corp/gocryptotrader/gctrpc/auth"
@@ -547,7 +547,7 @@ func (s *GRPCServer) ExecuteStrategyFromConfig(_ context.Context, request *btrpc
for i := range request.Config.DataSettings.LiveData.Credentials {
creds[i] = config.Credentials{
Exchange: request.Config.DataSettings.LiveData.Credentials[i].Exchange,
Keys: account.Credentials{
Keys: accounts.Credentials{
Key: request.Config.DataSettings.LiveData.Credentials[i].Keys.Key,
Secret: request.Config.DataSettings.LiveData.Credentials[i].Keys.Secret,
ClientID: request.Config.DataSettings.LiveData.Credentials[i].Keys.ClientId,

View File

@@ -15,8 +15,8 @@ import (
gctcommon "github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/currency"
"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/futures"
gctkline "github.com/thrasher-corp/gocryptotrader/exchanges/kline"
@@ -564,31 +564,21 @@ func (f *FundManager) UpdateFundingFromLiveData(initialFundsSet bool) error {
if err != nil {
return err
}
for x := range exchanges {
var creds *account.Credentials
creds, err = exchanges[x].GetCredentials(context.TODO())
if err != nil {
return err
}
assets := exchanges[x].GetAssetTypes(false)
for y := range assets {
if assets[y].IsFutures() {
for _, e := range exchanges {
eName := e.GetName()
for _, a := range e.GetAssetTypes(false) {
if a.IsFutures() {
// we set all holdings as spot
// futures currency holdings are collateral in the collateral currency
continue
}
var acc account.Holdings
acc, err = exchanges[x].UpdateAccountInfo(context.TODO(), assets[y])
subAccts, err := e.UpdateAccountBalances(context.TODO(), a)
if err != nil {
return err
}
for z := range acc.Accounts {
if !acc.Accounts[z].Credentials.Equal(creds) {
continue
}
for i := range acc.Accounts[z].Currencies {
err = f.SetFunding(exchanges[x].GetName(), assets[y], &acc.Accounts[z].Currencies[i], initialFundsSet)
if err != nil {
for _, subAcct := range subAccts {
for _, bal := range subAcct.Balances {
if err := f.SetFunding(eName, a, &bal, initialFundsSet); err != nil {
return err
}
}
@@ -799,7 +789,7 @@ func (f *FundManager) HasExchangeBeenLiquidated(ev common.Event) bool {
// As external sources may have additional currencies and balances
// versus the strategy currencies, they must be appended to
// help calculate collateral
func (f *FundManager) SetFunding(exchName string, item asset.Item, balance *account.Balance, initialFundsSet bool) error {
func (f *FundManager) SetFunding(exchName string, item asset.Item, balance *accounts.Balance, initialFundsSet bool) error {
if exchName == "" {
return gctcommon.ErrExchangeNameNotSet
}

View File

@@ -15,8 +15,8 @@ import (
gctcommon "github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/currency"
"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/binance"
gctkline "github.com/thrasher-corp/gocryptotrader/exchanges/kline"
@@ -717,7 +717,7 @@ func TestSetFunding(t *testing.T) {
err = f.SetFunding(exchName, asset.Spot, nil, false)
assert.ErrorIs(t, err, gctcommon.ErrNilPointer)
bal := &account.Balance{}
bal := &accounts.Balance{}
err = f.SetFunding(exchName, asset.Spot, bal, false)
assert.ErrorIs(t, err, currency.ErrCurrencyCodeEmpty)

View File

@@ -9,7 +9,7 @@ import (
"github.com/thrasher-corp/gocryptotrader/backtester/data/kline"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/engine"
"github.com/thrasher-corp/gocryptotrader/exchanges/account"
"github.com/thrasher-corp/gocryptotrader/exchange/accounts"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
)
@@ -51,7 +51,7 @@ type IFundingManager interface {
HasFutures() bool
HasExchangeBeenLiquidated(handler common.Event) bool
RealisePNL(receivingExchange string, receivingAsset asset.Item, receivingCurrency currency.Code, realisedPNL decimal.Decimal) error
SetFunding(string, asset.Item, *account.Balance, bool) error
SetFunding(string, asset.Item, *accounts.Balance, bool) error
}
// IFundingTransferer allows for funding amounts to be transferred