Skip to main content

Checking Batch Token Balances

This guide explains how to use the getBatchBalance method to check the balance of multiple tokens for a specific account using the ERC1155 API. This functionality is essential for applications that need to display or manage the token holdings of users efficiently.


Overview of the getBatchBalance Method

The getBatchBalance method allows you to query the balances of multiple tokens in a single request, making it highly efficient for applications that deal with various types of tokens and need to monitor their distribution among users.


Prerequisites

  • Installed the edeXa-sdk in your project.
  • An authentication token obtained through the SDK's authentication method. This token is necessary to authorize your requests.

Sample Code Snippet

Here is a JavaScript code snippet demonstrating the use of the getBatchBalance method:

import { ERC1155, Network } from 'edeXa-sdk';

async function batchBalance() {
const settings = {
network: Network.SANDBOX, // Use Network.MAINNET for production environments
};

const erc1155 = new ERC1155({
...settings,
authorization: `Bearer ${token}`, // Replace `${token}` with your actual token
});

// Data required for batch balance inquiry
const dataToBatchBalance = {
userId: 'usernameOfUser', // Username of the user
tokenId: ['tokenId1', 'tokenId2'], // Array of token IDs
chaincode: 'optionalChaincodeName', // Optional chaincode name
channel: 'optionalChannelName', // Optional blockchain channel
};

try {
const batchBalanceResponse = await erc1155.getBatchBalance(
dataToBatchBalance
);
// Handle success
} catch (error) {
// Handle error
}
}

Request Parameters

ParameterTypeRequiredDescription
userIdStringYesThe username of the user for whom to retrieve the balance.
tokenIdArrayYesAn array of token IDs for the tokens you want to check.
chaincodeStringNoThe name of the smart contract or chaincode managing the tokens.
channelStringNoThe name of the blockchain channel where the inquiry occurs.

Response Format

Upon successfully querying the batch balances, the getBatchBalance method returns a response object containing:

KeyTypeDescription
balanceArrayAn array of balances for the specified token IDs.