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,8 +21,8 @@ import (
"github.com/thrasher-corp/gocryptotrader/common/key"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/encoding/json"
"github.com/thrasher-corp/gocryptotrader/exchange/accounts"
"github.com/thrasher-corp/gocryptotrader/exchange/websocket"
"github.com/thrasher-corp/gocryptotrader/exchanges/account"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/fill"
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
@@ -489,65 +489,55 @@ func (e *Exchange) processUserPersonalTrades(data []byte) error {
}
func (e *Exchange) processSpotBalances(ctx context.Context, data []byte) error {
var resp []WsSpotBalance
var resp []*WsSpotBalance
if err := json.Unmarshal(data, &resp); err != nil {
return err
}
creds, err := e.GetCredentials(ctx)
if err != nil {
subAccts := accounts.SubAccounts{}
for _, bal := range resp {
a := accounts.NewSubAccount(asset.Spot, bal.User)
a.Balances.Set(bal.Currency, accounts.Balance{
Total: bal.Total.Float64(),
Free: bal.Available.Float64(),
Hold: bal.Freeze.Float64(),
AvailableWithoutBorrow: bal.Available.Float64(),
UpdatedAt: bal.Timestamp.Time(),
})
subAccts = subAccts.Merge(a)
}
if err := e.Accounts.Save(ctx, subAccts, false); err != nil {
return err
}
changes := make([]account.Change, len(resp))
for i := range resp {
changes[i] = account.Change{
Account: resp[i].User,
AssetType: asset.Spot,
Balance: &account.Balance{
Currency: resp[i].Currency,
Total: resp[i].Total.Float64(),
Free: resp[i].Available.Float64(),
Hold: resp[i].Freeze.Float64(),
AvailableWithoutBorrow: resp[i].Available.Float64(),
UpdatedAt: resp[i].Timestamp.Time(),
},
}
}
e.Websocket.DataHandler <- changes
return account.ProcessChange(e.Name, changes, creds)
e.Websocket.DataHandler <- subAccts
return nil
}
func (e *Exchange) processMarginBalances(ctx context.Context, data []byte) error {
resp := struct {
Time types.Time `json:"time"`
Channel string `json:"channel"`
Event string `json:"event"`
Result []WsMarginBalance `json:"result"`
Time types.Time `json:"time"`
Channel string `json:"channel"`
Event string `json:"event"`
Result []*WsMarginBalance `json:"result"`
}{}
err := json.Unmarshal(data, &resp)
if err != nil {
if err := json.Unmarshal(data, &resp); err != nil {
return err
}
creds, err := e.GetCredentials(ctx)
if err != nil {
subAccts := accounts.SubAccounts{}
for _, bal := range resp.Result {
a := accounts.NewSubAccount(asset.Margin, bal.User)
a.Balances.Set(bal.Currency, accounts.Balance{
Total: bal.Available.Float64() + bal.Freeze.Float64(),
Free: bal.Available.Float64(),
Hold: bal.Freeze.Float64(),
UpdatedAt: bal.Timestamp.Time(),
})
subAccts = subAccts.Merge(a)
}
if err := e.Accounts.Save(ctx, subAccts, false); err != nil {
return err
}
changes := make([]account.Change, len(resp.Result))
for x := range resp.Result {
changes[x] = account.Change{
AssetType: asset.Margin,
Balance: &account.Balance{
Currency: currency.NewCode(resp.Result[x].Currency),
Total: resp.Result[x].Available.Float64() + resp.Result[x].Freeze.Float64(),
Free: resp.Result[x].Available.Float64(),
Hold: resp.Result[x].Freeze.Float64(),
UpdatedAt: resp.Result[x].Timestamp.Time(),
},
}
}
e.Websocket.DataHandler <- changes
return account.ProcessChange(e.Name, changes, creds)
e.Websocket.DataHandler <- subAccts
return nil
}
func (e *Exchange) processFundingBalances(data []byte) error {
@@ -567,34 +557,30 @@ func (e *Exchange) processFundingBalances(data []byte) error {
func (e *Exchange) processCrossMarginBalance(ctx context.Context, data []byte) error {
resp := struct {
Time types.Time `json:"time"`
Channel string `json:"channel"`
Event string `json:"event"`
Result []WsCrossMarginBalance `json:"result"`
Time types.Time `json:"time"`
Channel string `json:"channel"`
Event string `json:"event"`
Result []*WsCrossMarginBalance `json:"result"`
}{}
err := json.Unmarshal(data, &resp)
if err != nil {
return err
}
creds, err := e.GetCredentials(ctx)
if err != nil {
subAccts := accounts.SubAccounts{}
for _, bal := range resp.Result {
a := accounts.NewSubAccount(asset.CrossMargin, bal.User)
a.Balances.Set(bal.Currency, accounts.Balance{
Total: bal.Total.Float64(),
Free: bal.Available.Float64(),
UpdatedAt: bal.Timestamp.Time(),
})
subAccts = subAccts.Merge(a)
}
if err := e.Accounts.Save(ctx, subAccts, false); err != nil {
return err
}
changes := make([]account.Change, len(resp.Result))
for x := range resp.Result {
changes[x] = account.Change{
Account: resp.Result[x].User,
AssetType: asset.Margin,
Balance: &account.Balance{
Currency: currency.NewCode(resp.Result[x].Currency),
Total: resp.Result[x].Total.Float64(),
Free: resp.Result[x].Available.Float64(),
UpdatedAt: resp.Result[x].Timestamp.Time(),
},
}
}
e.Websocket.DataHandler <- changes
return account.ProcessChange(e.Name, changes, creds)
e.Websocket.DataHandler <- subAccts
return nil
}
func (e *Exchange) processCrossMarginLoans(data []byte) error {