How to use fetchBalance() parameters with ccxt

Viewed 3657

I'm new to ccxt and I'm trying to fetch my balance in USDT.

This information is included in what the following code returns to me:

(async () => {
    console.log (await exchange.fetchBalance ())
}) ()

But it also returns a lot of other data that I don't need.

The documentation includes this code: fetchBalance (params = {})

I don't understand how to use "params".

The corresponding extract from the manual is here: https://ccxt.readthedocs.io/en/latest/manual.html#querying-account-balance

1 Answers

The params argument on fetchBalances() is for passing exchange specific data to the request. As each exchange offers different endpoints, CCXT offers a unified API for the most common use cases. It covers a lot, but not everything on every exchange. This is true of most of the methods in the unified API.

So to answer your question, we need more information, like which exchange you're fetching balances from to use the params arg?

Something like this should get you over the line for now though:

const currency = 'USDT';

async function fetchBalance(currency) {
  const balance = await exchange.fetchBalance(); // returns huge object with all balances
  return balance.free[currency]; // returns 'free' or available balance of USDT
}

You can read about the unified API here - https://ccxt.readthedocs.io/en/latest/manual.html#overriding-unified-api-params

If you're not happy with the unified API for your use case, you can use the implicit API. Again I can't offer an example without more info, but this allows you to use the CCXT library and call exchange endpoints as documented on the exchange. There is still a CCXT abstraction over these native endpoints, however this will get you out of a bind if the CCXT integration with your chosen exchange is incomplete.

Check out the implicit API here - https://ccxt.readthedocs.io/en/latest/manual.html#implicit-api-methods

Related