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
Parameter | Type | Required | Description |
---|---|---|---|
userId | String | Yes | The username of the user for whom to retrieve the balance. |
tokenId | Array | Yes | An array of token IDs for the tokens you want to check. |
chaincode | String | No | The name of the smart contract or chaincode managing the tokens. |
channel | String | No | The 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:
Key | Type | Description |
---|---|---|
balance | Array | An array of balances for the specified token IDs. |