[Solved] Strange wallet balances returned by API

To give some context, I need to obtain the spot value of USDT in an account.

To do that, I use the library github.com/adshao/go-binance/v2, which internally uses /sapi/v1/asset/wallet/balance (at the end you can see the code of how get the balance, is pretty simple).

The problem comes when I see an incorrect value in the API response compared to what I see on the web UI as any other user.

The values are:
In the web UI (these are the correct values), I see:

  • Spot: 1.68598160
  • Funding: 17.93826561
  • Earn: 200.09171085

But when I make the request over the API, I obtain:

  • Spot: 388.0760924
  • Funding: 3565.03679525
  • Earn: 200.09149724

Very different!

At this point, I checked:

  • The API Key used is the same as the account where I see the dashboard.
  • I checked again: the ID of the API key loaded in the environment variable matches the one shown in the web UI.
  • Seeing the docs of the endpoint, I see that it is necessary to enable the Universal Transfer permission. Permission added, verified, and the same result as before.

The next culprit I thought of was the library — might it have a bug parsing the JSON from the API? I enabled the logs to see the raw JSON returned, and the incorrect values come directly from the API!

That’s what I get from the API:

response body: [{"activate":true,"balance":"388.0760924","walletName":"Spot"},{"activate":true,"balance":"3565.03679525","walletName":"Funding"},{"activate":true,"balance":"0","walletName":"Cross Margin"},{"activate":true,"balance":"0","walletName":"Isolated Margin"},{"activate":true,"balance":"0","walletName":"USDⓈ-M Futures"},{"activate":true,"balance":"0","walletName":"COIN-M Futures"},{"activate":true,"balance":"200.09149724","walletName":"Earn"},{"activate":true,"balance":"0","walletName":"Options"},{"activate":false,"balance":"0","walletName":"Trading Bots"},{"activate":true,"balance":"0","walletName":"Copy Trading"}]

The library is OK, the API is who returns the strange values.

After doing some testing, I found some strange behavior. When I transfer between wallets (e.g., from Spot to Funding), the values change without much sense.

An example is:
In time 0 the API returns (bad numbers, by the way), spot omited:

  • Spot: 388.0760924
  • Funding: 3565.03679525

If I transfer 10 USDT from Funding to Spot (using the web UI) and then check the balances again, I get:

  • Spot: 398.07164353 (like only 9.9955511 was transferred)
  • Funding: 3559.78412999 (like 5.252666 was transferred)

In addition to the fact that the initial balance is incorrect compared to the web UI, when I make a transfer from the web UI (everything works perfectly there), the balances returned from the API are even more inconsistent.

Suspicions

Assuming the endpoint is the problem, I think it is important to mention that before, I had been working with subaccounts. Initially, I tried to use a subaccount to get balances, but after a first trade, the values started to differ from web UI vs API as i tell above. I thought it was only a subaccount issue, so I tried with a normal account, but the problem persists.

Questions

  • What else could I be doing wrong to get these fake values?
  • Anyone using the endpoint now, is it working fine?

If any additional information is required, do not hesitate to ask. Thanks!

Misc

Code used to do the request

func (b Binance) balance(ctx context.Context, asset string) (float32, error) {
	const spotWallet = 0

	balances, err := b.client.NewWalletBalanceService().QuoteAsset(asset).Do(ctx)
	if err != nil {
		return 0, err
	}

	balance, err := strconv.ParseFloat(balances[spotWallet].Balance, 32)
	if err != nil {
		return 0, err
	}

	return float32(balance), nil
}

The param asset is “USDT”

Here you can find the definition of Do(): go-binance/v2/wallet_balance_service.go at master · ccxt/go-binance · GitHub

If you what to do some test and see debug logs, define the debug level property in the client as:

import github.com/adshao/go-binance/v2

c := binance.NewClient(apiKey, apiSecret)
c.Debug = true

Where i try to find info

My first try was the docs of the endpoint and my second was a post here titled “Binance v3 account endpoint does not return correct balances” (i can’t add more than 2 urls :stuck_out_tongue:). But i dont have any stacking and in the overview of the account i don’t have the 3k USDT the API response says (i have 219 USDT)

Problem was me. I been using the wrong service. The values that i receive are the sum of all the assets in the account not a specific asset.

With /api/v3/account i can get balance of a specific asset