Documentation
API Reference
Balances
ℹ️

To interact with the Cede SDK, you need to create an instance of the SDK and register at least one exchange instance. Refer to the General info section for details.

User's fungible balances

To fetch user's balances on a given exchange, use the getBalances method:

method: getBalances
params:

  • exchangeInstanceId - string
    The exchange instance ID for which you want to fetch the balances.
  • auth - The authentication object for the exchange. See authentication section for details.
  • walletTypes - WalletType[], optional
    An optional parameter that allows you to filter the balances by wallet type. If not provided, all wallet types will be fetched. Find supported wallet types with the getSupportedExchanges method and the field supportedWalletTypes.
  • opts.requestPriority - 1 | 2 | 3, optional
    The request priority for the request.
    Default: 2 (medium)
  • opts.onAuthenticationError - VoidFunction, optional
    A callback function that is called when an authentication error occurs.
  • opts.forceRefresh - boolean, optional
    If true, fetches the balances from the exchange and updates the cache.
    Default: false
  • opts.disableHydration - boolean, optional
    If false, returns the cached balances (even if the TTL is expired) and emits an event when the data is refreshed.
    Default: true
  • opts.ttl - number | null, optional
    The time to live of the cache (in ms).
    Default: 30_000

response structure: Promise<BalancesByWalletType>, see BalancesByWalletType for details.

const balances = await cedeSDK.api.getBalances({ exchangeInstanceId, auth });

Balance hydration

The hydration system enhances user experience by delivering cached balance data instantly while asynchronously fetching the latest balances from the exchange. Upon successfully retrieving the updated balances, it triggers an event to notify the client of the changes in real-time, ensuring both speed and accuracy in data delivery.

See the following example to implement hydration for balance fetching:

import CedeSDK, { HydrationItem } from "@cedelabs-private/sdk";
 
cedeSDK.eventEmitter.on(HydrationItem.BALANCES, ({ exchangeInstanceId, walletType, balance }) => {
  // `balance` contains balances freshly fetched from the exchange.
  ...
});
 
const balances = await cedeSDK.api.getBalances({ exchangeInstanceId, opts: { disableHydration: false } });
// Variable `balances` contains the last balances fetched from the exchange and saved in the cache.
// At the same time, balances are being fetched from the exchange if the TTL was expired.
// An event is emitted when the new balances are fetched.
 
const refreshedBalances = await cedeSDK.api.getBalances({ exchangeInstanceId, opts: { disableHydration: false, forceRefresh: true } });
// Same behavior as the previous call, but the cache is updated no matter the TTL.
 
const fetchedBalances = await cedeSDK.api.getBalances({ exchangeInstanceId, opts: { disableHydration: true, forceRefresh: true } });
// This time no event will be emitted, and the cache will be updated no matter the TTL, the balances are returned after the update.