mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 23:16:45 +00:00
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:
@@ -418,12 +418,11 @@ func TestCancelAllExchangeOrders(t *testing.T) {
|
||||
assert.Emptyf(t, resp.Status, "%v orders failed to cancel", len(resp.Status))
|
||||
}
|
||||
|
||||
func TestGetAccountInfo(t *testing.T) {
|
||||
func TestUpdateAccountBalances(t *testing.T) {
|
||||
t.Parallel()
|
||||
sharedtestvalues.SkipTestIfCredentialsUnset(t, e)
|
||||
|
||||
_, err := e.UpdateAccountInfo(t.Context(), asset.Spot)
|
||||
require.NoError(t, err, "UpdateAccountInfo must not error")
|
||||
_, err := e.UpdateAccountBalances(t.Context(), asset.Spot)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestModifyOrder(t *testing.T) {
|
||||
|
||||
@@ -13,10 +13,10 @@ import (
|
||||
"github.com/thrasher-corp/gocryptotrader/common/convert"
|
||||
"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"
|
||||
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/currencystate"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/deposit"
|
||||
@@ -299,52 +299,29 @@ 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
|
||||
// Bithumb exchange
|
||||
func (e *Exchange) UpdateAccountInfo(ctx context.Context, assetType asset.Item) (account.Holdings, error) {
|
||||
var info account.Holdings
|
||||
// UpdateAccountBalances retrieves currency balances
|
||||
func (e *Exchange) UpdateAccountBalances(ctx context.Context, assetType asset.Item) (accounts.SubAccounts, error) {
|
||||
bal, err := e.GetAccountBalance(ctx, "ALL")
|
||||
if err != nil {
|
||||
return info, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
exchangeBalances := make([]account.Balance, 0, len(bal.Total))
|
||||
for key, totalAmount := range bal.Total {
|
||||
hold, ok := bal.InUse[key]
|
||||
subAccts := accounts.SubAccounts{accounts.NewSubAccount(assetType, "")}
|
||||
for k, totalAmount := range bal.Total {
|
||||
hold, ok := bal.InUse[k]
|
||||
if !ok {
|
||||
return info, fmt.Errorf("getAccountInfo error - in use item not found for currency %s",
|
||||
key)
|
||||
return subAccts, fmt.Errorf("currency %s missing from InUse balances", k)
|
||||
}
|
||||
|
||||
avail, ok := bal.Available[key]
|
||||
avail, ok := bal.Available[k]
|
||||
if !ok {
|
||||
avail = totalAmount - hold
|
||||
}
|
||||
|
||||
exchangeBalances = append(exchangeBalances, account.Balance{
|
||||
Currency: currency.NewCode(key),
|
||||
Total: totalAmount,
|
||||
Hold: hold,
|
||||
Free: avail,
|
||||
subAccts[0].Balances.Set(currency.NewCode(k), accounts.Balance{
|
||||
Total: totalAmount,
|
||||
Hold: hold,
|
||||
Free: avail,
|
||||
})
|
||||
}
|
||||
|
||||
info.Accounts = append(info.Accounts, account.SubAccount{
|
||||
Currencies: exchangeBalances,
|
||||
AssetType: assetType,
|
||||
})
|
||||
|
||||
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
|
||||
@@ -731,10 +708,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)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user