getBalanceOfBatch
The getBalanceOfBatch
method for the ERC-1155 token standard retrieves the balances of multiple token IDs for a specified Ethereum address. This method is efficient for applications that need to check balances for a range of token IDs in a single call. The method is part of the ERC1155Instance
object, which can be acquired through a suitable method of the EdexaClient
or the corresponding library.
Method Signature
getBalanceOfBatch(address: string, ids: string[]): Promise<BigNumber[]>
Parameters
Parameter | Type | Description |
---|---|---|
address | string | The Ethereum address for which you want to retrieve the token balances. |
ids | string[] | An array of specific IDs of the ERC-1155 tokens for which the balances are being retrieved. |
Returns
- A
Promise
resolving to an array ofBigNumber
values. Each value corresponds to the balance of the associated token ID for the provided Ethereum address.
Usage Example
First, instantiate the desired ERC-1155 token contract:
const erc1155 = edexaClient.getERC1155Instance("your ERC-1155 contract address here");
Then, call the getBalanceOfBatch
method for the desired Ethereum address and array of token IDs:
const userAddress = "0x2c360D20cE6b3D8b466511eF093C9177c3817B94";
const tokenIds = ["token-id-1", "token-id-2", "token-id-3"];
const tokenBalances = await erc1155.getBalanceOfBatch(userAddress, tokenIds);
tokenBalances.forEach((balance, index) => {
console.log(`Token Balance for ID ${tokenIds[index]}: ${balance.toString()}`);
});
Notes
The
getBalanceOfBatch
method is particularly useful when dealing with multiple token IDs, as it reduces the need for multiple individual queries, thus optimizing contract interactions.Ensure the Ethereum address and token IDs provided are valid; otherwise, the method might throw an error or return an array of zeroes.