Skip to main content

Fetching Token Balance

Retrieving the token balance for a specific account is a fundamental feature for applications dealing with Non-Fungible Tokens (NFTs). The edeXa ERC721 SDK simplifies this process through the getBalance method. This guide provides detailed instructions and a code example to fetch token balances.


Overview of the getBalance Method

The getBalance method allows you to query the token balance of any account within the network. This is particularly useful for verifying ownership and managing transactions related to NFTs.


Prerequisites

  • Installation of the edeXa ERC721 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 that demonstrates how to use the getBalance method:

import { ERC721, Network } from 'edeXa-sdk';
async function getBalance() {
const settings = {
network: Network.SANDBOX, // Use Network.MAINNET for production
};
const erc721 = new ERC721({
...settings,
authorization: `Bearer ${token}`, // Replace `${token}` with your authentication token
});

const dataToGetBalance = {
userId: 'Enter the username of the user to retrieve the balance', // Username of the account
};

try {
const getBalanceResponse = await erc721.getBalance(dataToGetBalance);
// Handle success
} catch (error) {
// Handle error
}
}

Important Notes

  • Authentication Token: Ensure you replace the placeholder token in the authorization setup with your actual authentication token.
  • Asynchronous Execution: The getBalance function uses await to pause execution until the promise from erc721.getBalance resolves.
  • Token Minting: Verify that ERC721 tokens have been minted and allocated to the account you're querying.
  • User Identification: The userId should be the username of the account for which you are retrieving the balance.

Request Parameters

ParameterRequiredTypeDescription
userIdYesStringThe username of the user for whom to retrieve the balance.

Response Format

Upon successful execution, the getBalance method returns a response containing the token balance.

AttributeTypeDescription
balanceNumberThe number of tokens in the specified account.