mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-23 15:10:15 +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:
@@ -3385,10 +3385,10 @@ func TestChannelName(t *testing.T) {
|
||||
assert.Panics(t, func() { channelName(&subscription.Subscription{Channel: "wibble"}) }, "Unknown channels should panic")
|
||||
}
|
||||
|
||||
func TestUpdateAccountInfo(t *testing.T) {
|
||||
func TestUpdateAccountBalances(t *testing.T) {
|
||||
t.Parallel()
|
||||
sharedtestvalues.SkipTestIfCredentialsUnset(t, e)
|
||||
result, err := e.UpdateAccountInfo(t.Context(), asset.Futures)
|
||||
result, err := e.UpdateAccountBalances(t.Context(), asset.Futures)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"regexp"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/encoding/json"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
|
||||
"github.com/thrasher-corp/gocryptotrader/types"
|
||||
@@ -120,13 +121,13 @@ type ContractSizeData struct {
|
||||
|
||||
// CurrencyData stores data for currencies
|
||||
type CurrencyData struct {
|
||||
CoinType string `json:"coin_type"`
|
||||
Currency string `json:"currency"` // TODO: change to currency.Code
|
||||
CurrencyLong string `json:"currency_long"`
|
||||
FeePrecision int64 `json:"fee_precision"`
|
||||
MinConfirmations int64 `json:"min_confirmations"`
|
||||
MinWithdrawalFee float64 `json:"min_withdrawal_fee"`
|
||||
WithdrawalFee float64 `json:"withdrawal_fee"`
|
||||
CoinType string `json:"coin_type"`
|
||||
Currency currency.Code `json:"currency"`
|
||||
CurrencyLong string `json:"currency_long"`
|
||||
FeePrecision int64 `json:"fee_precision"`
|
||||
MinConfirmations int64 `json:"min_confirmations"`
|
||||
MinWithdrawalFee float64 `json:"min_withdrawal_fee"`
|
||||
WithdrawalFee float64 `json:"withdrawal_fee"`
|
||||
WithdrawalPriorities []struct {
|
||||
Value float64 `json:"value"`
|
||||
Name string `json:"name"`
|
||||
|
||||
@@ -14,11 +14,11 @@ 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/order/limits"
|
||||
"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"
|
||||
@@ -337,34 +337,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
|
||||
func (e *Exchange) UpdateAccountInfo(ctx context.Context, _ asset.Item) (account.Holdings, error) {
|
||||
var resp account.Holdings
|
||||
resp.Exchange = e.Name
|
||||
// UpdateAccountBalances retrieves currency balances
|
||||
func (e *Exchange) UpdateAccountBalances(ctx context.Context, _ asset.Item) (accounts.SubAccounts, error) {
|
||||
currencies, err := e.GetCurrencies(ctx)
|
||||
if err != nil {
|
||||
return resp, err
|
||||
return nil, err
|
||||
}
|
||||
resp.Accounts = make([]account.SubAccount, len(currencies))
|
||||
for x := range currencies {
|
||||
var data *AccountSummaryData
|
||||
subAccts := accounts.SubAccounts{accounts.NewSubAccount(asset.All, "")}
|
||||
for i := range currencies {
|
||||
var resp *AccountSummaryData
|
||||
if e.Websocket.IsConnected() && e.Websocket.CanUseAuthenticatedWebsocketForWrapper() {
|
||||
data, err = e.WSRetrieveAccountSummary(ctx, currency.NewCode(currencies[x].Currency), false)
|
||||
resp, err = e.WSRetrieveAccountSummary(ctx, currencies[i].Currency, false)
|
||||
} else {
|
||||
data, err = e.GetAccountSummary(ctx, currency.NewCode(currencies[x].Currency), false)
|
||||
resp, err = e.GetAccountSummary(ctx, currencies[i].Currency, false)
|
||||
}
|
||||
if err != nil {
|
||||
return resp, err
|
||||
return nil, err
|
||||
}
|
||||
var subAcc account.SubAccount
|
||||
subAcc.Currencies = append(subAcc.Currencies, account.Balance{
|
||||
Currency: currency.NewCode(currencies[x].Currency),
|
||||
Total: data.Balance,
|
||||
Hold: data.Balance - data.AvailableFunds,
|
||||
subAccts[0].Balances.Set(currencies[i].Currency, accounts.Balance{
|
||||
Total: resp.Balance,
|
||||
Hold: resp.Balance - resp.AvailableFunds,
|
||||
})
|
||||
resp.Accounts[x] = subAcc
|
||||
}
|
||||
return resp, nil
|
||||
return subAccts, e.Accounts.Save(ctx, subAccts, true)
|
||||
}
|
||||
|
||||
// GetAccountFundingHistory returns funding history, deposits and withdrawals
|
||||
@@ -383,9 +378,9 @@ func (e *Exchange) GetAccountFundingHistory(ctx context.Context) ([]exchange.Fun
|
||||
for x := range currencies {
|
||||
var deposits *DepositsData
|
||||
if e.Websocket.IsConnected() && e.Websocket.CanUseAuthenticatedWebsocketForWrapper() {
|
||||
deposits, err = e.WSRetrieveDeposits(ctx, currency.NewCode(currencies[x].Currency), 100, 0)
|
||||
deposits, err = e.WSRetrieveDeposits(ctx, currencies[x].Currency, 100, 0)
|
||||
} else {
|
||||
deposits, err = e.GetDeposits(ctx, currency.NewCode(currencies[x].Currency), 100, 0)
|
||||
deposits, err = e.GetDeposits(ctx, currencies[x].Currency, 100, 0)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -396,7 +391,7 @@ func (e *Exchange) GetAccountFundingHistory(ctx context.Context) ([]exchange.Fun
|
||||
Status: deposits.Data[y].State,
|
||||
TransferID: deposits.Data[y].TransactionID,
|
||||
Timestamp: deposits.Data[y].UpdatedTimestamp.Time(),
|
||||
Currency: currencies[x].Currency,
|
||||
Currency: currencies[x].Currency.String(),
|
||||
Amount: deposits.Data[y].Amount,
|
||||
CryptoToAddress: deposits.Data[y].Address,
|
||||
TransferType: "deposit",
|
||||
@@ -404,9 +399,9 @@ func (e *Exchange) GetAccountFundingHistory(ctx context.Context) ([]exchange.Fun
|
||||
}
|
||||
var withdrawalData *WithdrawalsData
|
||||
if e.Websocket.IsConnected() && e.Websocket.CanUseAuthenticatedWebsocketForWrapper() {
|
||||
withdrawalData, err = e.WSRetrieveWithdrawals(ctx, currency.NewCode(currencies[x].Currency), 100, 0)
|
||||
withdrawalData, err = e.WSRetrieveWithdrawals(ctx, currencies[x].Currency, 100, 0)
|
||||
} else {
|
||||
withdrawalData, err = e.GetWithdrawals(ctx, currency.NewCode(currencies[x].Currency), 100, 0)
|
||||
withdrawalData, err = e.GetWithdrawals(ctx, currencies[x].Currency, 100, 0)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -417,7 +412,7 @@ func (e *Exchange) GetAccountFundingHistory(ctx context.Context) ([]exchange.Fun
|
||||
Status: withdrawalData.Data[z].State,
|
||||
TransferID: withdrawalData.Data[z].TransactionID,
|
||||
Timestamp: withdrawalData.Data[z].UpdatedTimestamp.Time(),
|
||||
Currency: currencies[x].Currency,
|
||||
Currency: currencies[x].Currency.String(),
|
||||
Amount: withdrawalData.Data[z].Amount,
|
||||
CryptoToAddress: withdrawalData.Data[z].Address,
|
||||
TransferType: "withdrawal",
|
||||
@@ -441,14 +436,14 @@ func (e *Exchange) GetWithdrawalsHistory(ctx context.Context, c currency.Code, _
|
||||
}
|
||||
resp := []exchange.WithdrawalHistory{}
|
||||
for x := range currencies {
|
||||
if !strings.EqualFold(currencies[x].Currency, c.String()) {
|
||||
if !currencies[x].Currency.Equal(c) {
|
||||
continue
|
||||
}
|
||||
var withdrawalData *WithdrawalsData
|
||||
if e.Websocket.IsConnected() && e.Websocket.CanUseAuthenticatedWebsocketForWrapper() {
|
||||
withdrawalData, err = e.WSRetrieveWithdrawals(ctx, currency.NewCode(currencies[x].Currency), 100, 0)
|
||||
withdrawalData, err = e.WSRetrieveWithdrawals(ctx, currencies[x].Currency, 100, 0)
|
||||
} else {
|
||||
withdrawalData, err = e.GetWithdrawals(ctx, currency.NewCode(currencies[x].Currency), 100, 0)
|
||||
withdrawalData, err = e.GetWithdrawals(ctx, currencies[x].Currency, 100, 0)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -458,7 +453,7 @@ func (e *Exchange) GetWithdrawalsHistory(ctx context.Context, c currency.Code, _
|
||||
Status: withdrawalData.Data[y].State,
|
||||
TransferID: withdrawalData.Data[y].TransactionID,
|
||||
Timestamp: withdrawalData.Data[y].UpdatedTimestamp.Time(),
|
||||
Currency: currencies[x].Currency,
|
||||
Currency: currencies[x].Currency.String(),
|
||||
Amount: withdrawalData.Data[y].Amount,
|
||||
CryptoToAddress: withdrawalData.Data[y].Address,
|
||||
TransferType: "deposit",
|
||||
@@ -1027,10 +1022,9 @@ func (e *Exchange) GetFeeByType(ctx context.Context, feeBuilder *exchange.FeeBui
|
||||
return fee, 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