From 970f65ba72bd0b7445df3596ac7069aa1893cd10 Mon Sep 17 00:00:00 2001 From: Gareth Kirwan Date: Tue, 9 May 2023 01:49:23 +0100 Subject: [PATCH] OKX: Fix balance using funding account (#1175) It looks like this was an oversight. No other exchange uses the funding account for balance and it wouldn't make sense. Again, this aims to be minimally invasive and backwards compatible, so GetBalance is left alone to preserve compat. --- exchanges/okx/okx.go | 4 +-- exchanges/okx/okx_types.go | 68 ++++++++++++++++++------------------ exchanges/okx/okx_wrapper.go | 22 ++++++------ 3 files changed, 47 insertions(+), 47 deletions(-) diff --git a/exchanges/okx/okx.go b/exchanges/okx/okx.go index 1c16640d..86ed08a3 100644 --- a/exchanges/okx/okx.go +++ b/exchanges/okx/okx.go @@ -90,7 +90,7 @@ const ( oneClickRepayHistory = "trade/one-click-repay-history" oneClickRepay = "trade/one-click-repay" - // Funding orders routes + // Funding account routes assetCurrencies = "asset/currencies" assetBalance = "asset/balances" assetValuation = "asset/asset-valuation" @@ -1379,7 +1379,7 @@ func (ok *Okx) GetFundingCurrencies(ctx context.Context) ([]CurrencyResponse, er return resp, ok.SendHTTPRequest(ctx, exchange.RestSpot, getCurrenciesEPL, http.MethodGet, assetCurrencies, nil, &resp, true) } -// GetBalance retrieves the balances of all the assets and the amount that is available or on hold. +// GetBalance retrieves the funding account balances of all the assets and the amount that is available or on hold. func (ok *Okx) GetBalance(ctx context.Context, currency string) ([]AssetBalance, error) { var resp []AssetBalance params := url.Values{} diff --git a/exchanges/okx/okx_types.go b/exchanges/okx/okx_types.go index d87d3124..7b6cca57 100644 --- a/exchanges/okx/okx_types.go +++ b/exchanges/okx/okx_types.go @@ -1204,44 +1204,44 @@ type ConvertHistory struct { // Account holds currency account balance and related information type Account struct { - AdjEq string `json:"adjEq"` - Details []AccountDetail `json:"details"` - Imr string `json:"imr"` // Frozen equity for open positions and pending orders in USD level Applicable to Multi-currency margin and Portfolio margin - IsoEq string `json:"isoEq"` - MgnRatio string `json:"mgnRatio"` - Mmr string `json:"mmr"` // Maintenance margin requirement in USD level Applicable to Multi-currency margin and Portfolio margin - NotionalUsd string `json:"notionalUsd"` - OrdFroz string `json:"ordFroz"` // Margin frozen for pending orders in USD level Applicable to Multi-currency margin and Portfolio margin - TotalEquity string `json:"totalEq"` // Total Equity in USD level - UpdateTime okxUnixMilliTime `json:"uTime"` // UpdateTime + AdjEq okxNumericalValue `json:"adjEq"` + Details []AccountDetail `json:"details"` + Imr okxNumericalValue `json:"imr"` // Frozen equity for open positions and pending orders in USD level Applicable to Multi-currency margin and Portfolio margin + IsoEq okxNumericalValue `json:"isoEq"` + MgnRatio okxNumericalValue `json:"mgnRatio"` + Mmr okxNumericalValue `json:"mmr"` // Maintenance margin requirement in USD level Applicable to Multi-currency margin and Portfolio margin + NotionalUsd okxNumericalValue `json:"notionalUsd"` + OrdFroz okxNumericalValue `json:"ordFroz"` // Margin frozen for pending orders in USD level Applicable to Multi-currency margin and Portfolio margin + TotalEquity okxNumericalValue `json:"totalEq"` // Total Equity in USD level + UpdateTime okxUnixMilliTime `json:"uTime"` // UpdateTime } // AccountDetail account detail information. type AccountDetail struct { - AvailableBalance string `json:"availBal"` - AvailableEquity string `json:"availEq"` - CashBalance string `json:"cashBal"` // Cash Balance - Currency string `json:"ccy"` - CrossLiab string `json:"crossLiab"` - DiscountEquity string `json:"disEq"` - EquityOfCurrency string `json:"eq"` - EquityUsd string `json:"eqUsd"` - FrozenBalance string `json:"frozenBal"` - Interest string `json:"interest"` - IsoEquity string `json:"isoEq"` - IsolatedLiabilities string `json:"isoLiab"` - IsoUpl string `json:"isoUpl"` // Isolated unrealized profit and loss of the currency applicable to Single-currency margin and Multi-currency margin and Portfolio margin - LiabilitiesOfCurrency string `json:"liab"` - MaxLoan string `json:"maxLoan"` - MarginRatio string `json:"mgnRatio"` // Equity of the currency - NotionalLever string `json:"notionalLever"` // Leverage of the currency applicable to Single-currency margin - OpenOrdersMarginFrozen string `json:"ordFrozen"` - Twap string `json:"twap"` - UpdateTime okxUnixMilliTime `json:"uTime"` - UnrealizedProfit string `json:"upl"` - UnrealizedCurrencyLiabilities string `json:"uplLiab"` - StrategyEquity string `json:"stgyEq"` // strategy equity - TotalEquity string `json:"totalEq"` // Total equity in USD level + AvailableBalance okxNumericalValue `json:"availBal"` + AvailableEquity okxNumericalValue `json:"availEq"` + CashBalance okxNumericalValue `json:"cashBal"` // Cash Balance + Currency string `json:"ccy"` + CrossLiab okxNumericalValue `json:"crossLiab"` + DiscountEquity okxNumericalValue `json:"disEq"` + EquityOfCurrency okxNumericalValue `json:"eq"` + EquityUsd okxNumericalValue `json:"eqUsd"` + FrozenBalance okxNumericalValue `json:"frozenBal"` + Interest okxNumericalValue `json:"interest"` + IsoEquity okxNumericalValue `json:"isoEq"` + IsolatedLiabilities okxNumericalValue `json:"isoLiab"` + IsoUpl okxNumericalValue `json:"isoUpl"` // Isolated unrealized profit and loss of the currency applicable to Single-currency margin and Multi-currency margin and Portfolio margin + LiabilitiesOfCurrency okxNumericalValue `json:"liab"` + MaxLoan okxNumericalValue `json:"maxLoan"` + MarginRatio okxNumericalValue `json:"mgnRatio"` // Equity of the currency + NotionalLever okxNumericalValue `json:"notionalLever"` // Leverage of the currency applicable to Single-currency margin + OpenOrdersMarginFrozen okxNumericalValue `json:"ordFrozen"` + Twap okxNumericalValue `json:"twap"` + UpdateTime okxUnixMilliTime `json:"uTime"` + UnrealizedProfit okxNumericalValue `json:"upl"` + UnrealizedCurrencyLiabilities okxNumericalValue `json:"uplLiab"` + StrategyEquity okxNumericalValue `json:"stgyEq"` // strategy equity + TotalEquity okxNumericalValue `json:"totalEq"` // Total equity in USD level } // AccountPosition account position. diff --git a/exchanges/okx/okx_wrapper.go b/exchanges/okx/okx_wrapper.go index 253c601e..9d5b0453 100644 --- a/exchanges/okx/okx_wrapper.go +++ b/exchanges/okx/okx_wrapper.go @@ -527,22 +527,22 @@ func (ok *Okx) UpdateAccountInfo(ctx context.Context, assetType asset.Item) (acc if !ok.SupportsAsset(assetType) { return info, fmt.Errorf("%w: %v", asset.ErrNotSupported, assetType) } - balances, err := ok.GetBalance(ctx, "") + accountBalances, err := ok.GetNonZeroBalances(ctx, "") if err != nil { return info, err } - currencyBalance := make([]account.Balance, len(balances)) - for i := range balances { - free := balances[i].AvailBal - locked := balances[i].FrozenBalance - currencyBalance[i] = account.Balance{ - Currency: currency.NewCode(balances[i].Currency), - Total: balances[i].Balance, - Hold: locked, - Free: free, + currencyBalances := []account.Balance{} + for i := range accountBalances { + for j := range accountBalances[i].Details { + currencyBalances = append(currencyBalances, account.Balance{ + Currency: currency.NewCode(accountBalances[i].Details[j].Currency), + Total: accountBalances[i].Details[j].EquityOfCurrency.Float64(), + Hold: accountBalances[i].Details[j].FrozenBalance.Float64(), + Free: accountBalances[i].Details[j].AvailableBalance.Float64(), + }) } } - acc.Currencies = currencyBalance + acc.Currencies = currencyBalances acc.AssetType = assetType info.Accounts = append(info.Accounts, acc) creds, err := ok.GetCredentials(ctx)