account: fix balance issue returning only one balance (#1478)

* fix issue with single-pop response

* minor fixes

* simplify return
This commit is contained in:
Scott
2024-02-27 10:40:01 +11:00
committed by GitHub
parent 5758a0e2dd
commit 954aa0239e
2 changed files with 19 additions and 20 deletions

View File

@@ -25,7 +25,6 @@ var (
errAssetHoldingsNotFound = errors.New("asset holdings not found")
errExchangeAccountsNotFound = errors.New("exchange accounts not found")
errNoExchangeSubAccountBalances = errors.New("no exchange sub account balances")
errNoBalanceFound = errors.New("no balance found")
errBalanceIsNil = errors.New("balance is nil")
errNoCredentialBalances = errors.New("no balances associated with credentials")
errCredentialsAreNil = errors.New("credentials are nil")
@@ -106,7 +105,7 @@ func GetHoldings(exch string, creds *Credentials, assetType asset.Item) (Holding
}
var currencyBalances = make([]Balance, 0, len(subAccountHoldings))
accountsHoldings := make([]SubAccount, 0, len(subAccountHoldings))
cpy := *creds
for mapKey, assetHoldings := range subAccountHoldings {
if mapKey.Asset != assetType {
continue
@@ -121,32 +120,24 @@ func GetHoldings(exch string, creds *Credentials, assetType asset.Item) (Holding
Borrowed: assetHoldings.borrowed,
})
assetHoldings.m.Unlock()
if len(currencyBalances) == 0 {
continue
}
cpy := *creds
if cpy.SubAccount == "" {
if cpy.SubAccount == "" && mapKey.SubAccount != "" {
// TODO: fix this backwards population
// the subAccount here may not be associated with the balance across all subAccountHoldings
cpy.SubAccount = mapKey.SubAccount
}
accountsHoldings = append(accountsHoldings, SubAccount{
Credentials: Protected{creds: cpy},
ID: mapKey.SubAccount,
AssetType: mapKey.Asset,
Currencies: currencyBalances,
})
break
}
if len(accountsHoldings) == 0 {
if len(currencyBalances) == 0 {
return Holdings{}, fmt.Errorf("%s %s %w",
exch,
assetType,
errAssetHoldingsNotFound)
}
return Holdings{Exchange: exch, Accounts: accountsHoldings}, nil
return Holdings{Exchange: exch, Accounts: []SubAccount{{
Credentials: Protected{creds: cpy},
ID: cpy.SubAccount,
AssetType: assetType,
Currencies: currencyBalances,
}}}, nil
}
// GetBalance returns the internal balance for that asset item.

View File

@@ -646,6 +646,14 @@ func (b *Binance) UpdateAccountInfo(ctx context.Context, assetType asset.Item) (
info.Exchange = b.Name
switch assetType {
case asset.Spot:
creds, err := b.GetCredentials(ctx)
if err != nil {
return info, err
}
if creds.SubAccount != "" {
// TODO: implement sub-account endpoints
return info, common.ErrNotYetImplemented
}
raw, err := b.GetAccount(ctx)
if err != nil {
return info, err