mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-29 15:10:37 +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:
@@ -202,21 +202,13 @@ func (e *Exchange) GetCandles(ctx context.Context, currencyPair, limit, period s
|
||||
// https://api.hitbtc.com/?python#market-data
|
||||
|
||||
// GetBalances returns full balance for your account
|
||||
func (e *Exchange) GetBalances(ctx context.Context) (map[string]Balance, error) {
|
||||
func (e *Exchange) GetBalances(ctx context.Context) (map[currency.Code]Balance, error) {
|
||||
var result []Balance
|
||||
err := e.SendAuthenticatedHTTPRequest(ctx,
|
||||
exchange.RestSpot,
|
||||
http.MethodGet,
|
||||
apiV2Balance,
|
||||
url.Values{},
|
||||
otherRequests,
|
||||
&result)
|
||||
ret := make(map[string]Balance)
|
||||
|
||||
if err != nil {
|
||||
return ret, err
|
||||
if err := e.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, http.MethodGet, apiV2Balance, url.Values{}, otherRequests, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ret := make(map[currency.Code]Balance)
|
||||
for _, item := range result {
|
||||
ret[item.Currency] = item
|
||||
}
|
||||
|
||||
@@ -96,9 +96,9 @@ type LoanOrders struct {
|
||||
|
||||
// Balance is a simple balance type
|
||||
type Balance struct {
|
||||
Currency string `json:"currency"`
|
||||
Available float64 `json:"available,string"` // Amount available for trading or transfer to main account
|
||||
Reserved float64 `json:"reserved,string"` // Amount reserved for active orders or incomplete transfers to main account
|
||||
Currency currency.Code `json:"currency"`
|
||||
Available float64 `json:"available,string"` // Amount available for trading or transfer to main account
|
||||
Reserved float64 `json:"reserved,string"` // Amount reserved for active orders or incomplete transfers to main account
|
||||
}
|
||||
|
||||
// DepositCryptoAddresses contains address information
|
||||
|
||||
@@ -12,10 +12,10 @@ 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"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchange/websocket"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchange/websocket/buffer"
|
||||
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"
|
||||
@@ -305,41 +305,21 @@ func (e *Exchange) UpdateOrderbook(ctx context.Context, c currency.Pair, assetTy
|
||||
return orderbook.Get(e.Name, c, assetType)
|
||||
}
|
||||
|
||||
// UpdateAccountInfo retrieves balances for all enabled currencies for the
|
||||
// HitBTC exchange
|
||||
func (e *Exchange) UpdateAccountInfo(ctx context.Context, assetType asset.Item) (account.Holdings, error) {
|
||||
var response account.Holdings
|
||||
response.Exchange = e.Name
|
||||
accountBalance, err := e.GetBalances(ctx)
|
||||
// UpdateAccountBalances retrieves currency balances
|
||||
func (e *Exchange) UpdateAccountBalances(ctx context.Context, assetType asset.Item) (accounts.SubAccounts, error) {
|
||||
resp, err := e.GetBalances(ctx)
|
||||
if err != nil {
|
||||
return response, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
currencies := make([]account.Balance, 0, len(accountBalance))
|
||||
for i := range accountBalance {
|
||||
currencies = append(currencies, account.Balance{
|
||||
Currency: currency.NewCode(accountBalance[i].Currency),
|
||||
Total: accountBalance[i].Available + accountBalance[i].Reserved,
|
||||
Hold: accountBalance[i].Reserved,
|
||||
Free: accountBalance[i].Available,
|
||||
subAccts := accounts.SubAccounts{accounts.NewSubAccount(assetType, "")}
|
||||
for i := range resp {
|
||||
subAccts[0].Balances.Set(resp[i].Currency, accounts.Balance{
|
||||
Total: resp[i].Available + resp[i].Reserved,
|
||||
Hold: resp[i].Reserved,
|
||||
Free: resp[i].Available,
|
||||
})
|
||||
}
|
||||
|
||||
response.Accounts = append(response.Accounts, account.SubAccount{
|
||||
AssetType: assetType,
|
||||
Currencies: currencies,
|
||||
})
|
||||
|
||||
creds, err := e.GetCredentials(ctx)
|
||||
if err != nil {
|
||||
return account.Holdings{}, err
|
||||
}
|
||||
err = account.Process(&response, creds)
|
||||
if err != nil {
|
||||
return account.Holdings{}, err
|
||||
}
|
||||
|
||||
return response, nil
|
||||
return subAccts, e.Accounts.Save(ctx, subAccts, true)
|
||||
}
|
||||
|
||||
// GetAccountFundingHistory returns funding history, deposits and
|
||||
@@ -741,10 +721,9 @@ func (e *Exchange) AuthenticateWebsocket(ctx context.Context) error {
|
||||
return e.wsLogin(ctx)
|
||||
}
|
||||
|
||||
// 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