Checking ETH Token Balance: How to Get the Balance of ERC-223 Token?

Checking ETH Token Balance: How to Get the Balance of ERC-223 Token?

ACCESS TO ETHEREUM

In this article, we’ll guide you through the process of fetching the balance of an ERC-223 token using ETH methods like the Web3.js library, Ethers.js library, and direct API calls. 

We also are going to access an Ethereum RPC node with NOWNodes for getting the ERC-223 tokens balance, the node provider that offers access to blockchain nodes without the need to host one yourself.

Before we delve into the process of obtaining your ERC223 token balance, let’s first understand the methods for checking ETH token balance. 

Methods of Retrieving ERC223 Token Balance

ethers.js get token balance

ERC223 standard is an enhanced standard of ERC-20, which includes features that aim to improve security and reduce the risk of losing tokens. To fully utilize ERC223 tokens, it is crucial to understand how to obtain and manage your token balance.

The methods for obtaining the balance of ERC223 tokens are important tools for both developers and users within the Ethereum ecosystem. These ETH methods can range from direct smart contract interaction to methods through libraries or blockchain explorers. 

Starting with the Web3.js library and Ethers.js library, these are JavaScript libraries that provide the tools needed to interact with the Ethereum ecosystem. Speed and accuracy are among Web3.js and Ethers.js get token balance methods strengths; they enable real-time balance checks directly from the blockchain. Ethers.js and Web3.js get token balance methods depend on connecting to an Ethereum RPC node, which can be provided by services like NOWNodes. 

Direct API Calls offer another avenue. These are optimal for those who have a moderate understanding of APIs and RESTful services but perhaps don’t want to dive into the complexities of smart contracts. They offer fast and accurate balance checks, pulling data either directly from the blockchain or Web3 services like the node provider NOWNodes. 

Here are some brief characteristics of these methods:

Feature/MethodWeb3.jsEthers.jsDirect API Calls
Technical ComplexityHighHighModerate to High
Speed and EfficiencyFastFastFast
AccuracyHighHighHigh
FlexibilityHighHighModerate to High
DependencyNode/APINode/APIAPI Service 
SecurityVariableVariableDependent on API
CostVariableVariableVariable
Learning CurveSteepSteepModerate

Choosing a method for checking an ETH token balance of the ERC223 will largely depend on your technical skills and specific needs. 

Now, let’s find out how to retrieve the ERC233 tokens balance through the Web3.js library, Ethers.js library, and direct API calls. 

Retrieve the Balance of an ERC-223 Token 

web3.js get token balance

If you wonder how to retrieve the ERC233 tokens balance through the Web3.js library, Ethers.js library, or direct API calls, you have to be connected to an Ethereum RPC node. 

NOWNodes is a RPC node provider that offers access to various full nodes, including an Ethereum RPC node. It provides a platform where developers can easily access full nodes for multiple blockchain networks through RPC endpoints. This enables developers to interact with blockchain networks without having to set up and maintain their own nodes, which can be both time-consuming and costly.

Moreover, with the node provider like NOWNodes, you can access: 

  • Ethereum WebSocket mainnet connection
  • Goerli WebSocket connection
  • Goerli RPC node
  • Goerli block explorer
  • Ethereum mainnet RPC node
  • Ethereum mainnet explorer
  • Dedicated solutions

Accessing an Ethereum RPC node, Ethereum WebSocket connection, and Ethereum explorer for mainnet and testnet through the NOWNodes involves the simple steps:

1) Sign Up and API Key Generation: First, you’ll need to create a profile on the NOWNodes website (https://nownodes.io/). Choose a tariff plan and blockchains you want to access. Notice that the START plan is free and allows you to pick up to 5 blockchains (including Ethereum mainnet and Ethereum testnet endpoints) through one API key. 

2) After registration, you have to generate an API key. This key is required to access their Ethereum RPC nodes. Always keep your API key secure and never expose it in client-side code to ensure your usage remains secure and within the platform’s guidelines.

3) Explore Ethereum Endpoint Information: NOWNodes will provide you with the Ethereum RPC endpoint for accessing the Ethereum mainnet network. NOWNodes offers documentation and support that can guide you through the functionalities and API calls you can make to the Ethereum RPC node.

The Ethereum RPC endpoint looks like `https://eth.nownodes.io`. You’ll append your API key to this Ethereum RPC URL like so: `https://eth.nownodes.io/your_api_key`, or insert it into the request header. You can test the connection directly from your web browser by pasting the Ethereum endpoint with the API key in Postman. 

Once you have the API key and the Ethereum RPC endpoint, you can plug these into your code to access Ethereum data. Whether you are using the Web3.js library, Ethers.js library, or direct HTTP calls, you’ll specify this Ethereum RPC endpoint for connection. 

Now, let’s dive into ERC-223 Web3.js get token balance method. 

ERC-223 Web3.js Get Token Balance Method

One common way to programmatically get the balance is by using the Web3.js library, a JavaScript library that allows for interaction with the Ethereum blockchain. Follow the steps below required to check the balance of ERC-223 tokens using Web3.js.

Prerequisites

To use the Web3.js get token balance method, you do need to have some basic prerequisites covered:

  • Node.js and npm installed
  • Web3.js library installed
  • An Ethereum wallet address
  • The ETH token contract address and ABI (Application Binary Interface) of the ERC-223 

Once you have met all the prerequisites, you’re all set for following instructions. 

Installation

Install the Web3.js library, which is a JavaScript library that provides a convenient way to interact with the Ethereum ecosystem. If you haven’t installed Web3.js yet, you can install it using npm: npm install web3

Step 1: Initialize Web3 Provider

First, you need to initialize the Web3 provider with the following command: 

const Web3 = require('web3');
const web3 = new Web3(new Web3.providers.HttpProvider('YOUR_NOWNODES_ENDPOINT'));

In this example, we are using the HttpProvider to connect to an Ethereum node via HTTP. If you are using a different connection, such as Ethereum WebSocket, make sure to update the code accordingly. Once the Web3 node provider is initialized, you can proceed to the next steps to load the ERC-223 token contract.

Step 2: Set Up the ERC-223 Contract 

To interact with the ERC-223 contract, you need to set up the contract instance by providing its ABI (Application Binary Interface) and ETH token contract address of ERC-223.

The ABI is a JSON representation of the contract’s functions, events, and variables. It defines how to interact with the contract and what data types to use. You can obtain the ABI from the contract’s source code or a verified source like Etherscan.

The contract address is a unique identifier for the deployed contract on the Ethereum ecosystem. You can find the ETH token contract address after the contract has been deployed.

// Replace the ABI array and contract address with actual values
const contractABI = [...];
const contractAddress = '0xYourERC223ContractAddress';
const contractInstance = new web3.eth.Contract(contractABI, contractAddress);

In the code snippet above, replace contractABI with the actual ABI array of the ERC-223 contract, and contractAddress with the actual ETH token contract address of ERC-223.

Step 3: Fetch the Token Balance

Now that you have set up the ERC-223 contract instance, you can fetch the balance of a specific Ethereum address for this ERC-223 token.

To fetch the ERC-223 token balance, you will use the balanceOf ETH method provided by the ERC-223 contract. This method takes an Ethereum address as a parameter and returns the balance of the specified address.

const address = '0xYourEthereumAddress';
contractInstance.methods.balanceOf(address).call()
  .then(balance => {
    console.log(`The balance of address ${address} is ${balance}`);
  })
  .catch(error => {
    console.log(`An error occurred: ${error}`);
  });

The method will return the balance of the address, which will be logged to the console.

Full Code Example

Putting it all together:

const Web3 = require('web3');
// Initialize web3 instance with NOWNodes Ethereum endpoint
const web3 = new Web3(new Web3.providers.HttpProvider('YOUR_NOWNODES_ENDPOINT'));
// ERC-223 Contract ABI and Address
const contractABI = [...];
const contractAddress = '0xYourERC223ContractAddress';
// Initialize contract instance
const contractInstance = new web3.eth.Contract(contractABI, contractAddress);
// Ethereum address whose balance we want to check
const address = '0xYourEthereumAddress';
// Fetch and log the token balance
contractInstance.methods.balanceOf(address).call()
  .then(balance => {
    console.log(`The balance of address ${address} is ${balance}`);
  })
  .catch(error => {
    console.log(`An error occurred: ${error}`);
  });

Replace placeholders like YOUR_NOWNODES_ENDPOINT, 0xYourERC223ContractAddress, and 0xYourEthereumAddress with your actual NOWNodes endpoint, ERC-223 contract address, and Ethereum address, respectively.

By following these steps, you can easily retrieve the balance of an ERC-223 token using the Web3.js library and NOWNodes. The process is simplified by using NOWNodes as it provides fast and reliable access to the Ethereum RPC endpoint, making it easier for developers to build and maintain their applications.

Now we’re going to explore Ethers.js get token balance method. 

ERC-223 Ethers.js Get Token Balance Method

For developers and token holders looking to manage ERC-223 tokens programmatically, Ethers.js is a popular alternative to Web3.js. This JavaScript library allows for easier interaction with the Ethereum blockchain. Let’s explore how to get the balance of an ERC-223 token using the Ethers.js library.

The prerequisites for receiving ERC-223 tokens balance through the Ethers.js library are the same as for Web3.js, except you need to have the Ethers.js library installed. Notice, if you haven’t installed the Ethers.js library package, use the command: 

npm install --save ethers

As soon as you’ve done all the prerequisites, follow the steps for ERC-223 Ethers.js get token balance method.

Step 1: Initialize Ethers.js

To get started, you’ll need to initialize your Ethers.js library and establish a connection to an Ethereum RPC node. If you haven’t done this already, follow the steps below:

const ethers = require('ethers');
const provider = new ethers.providers.JsonRpcProvider('https://eth.nownodes.io/your_api_key');

Replace ‘https://eth.nownodes.io/your_api_key‘ with the actual Ethereum RPC URL provided by NOWNodes and your API key.

Step 2: Set Up Contract Instance

To interact with a specific ERC-223 token contract, you’ll need to set up a contract instance using the contract’s ABI (Application Binary Interface) and address. Follow the scripts below:

const contractABI = [...] // Replace with your ERC-223 Token's ABI
const contractAddress = "0x...";
// Replace with your ETH ERC-223 Token's address
const contract = new ethers.Contract(contractAddress, contractABI, provider);

The contract object will now represent your ERC-223 token contract and allow you to interact with its functions and events.

Step 3: Query the Token Balance

With the contract instance in hand, you can now query the token balance of an Ethereum wallet address.

const walletAddress = "0x..."; // Replace with your Ethereum wallet address
async function getBalance() {
    const balance = await contract.balanceOf(walletAddress);
    console.log(`The ERC-223 token balance of address ${walletAddress} is: ${balance}`);
}
getBalance();

As you know, the balanceOf ETH method in ERC-223 token contracts (as well as for ERC-20 contracts) retrieves the token balance of a given Ethereum address.

Complete Code Snippet

Here’s how the entire code would look:

const ethers = require('ethers');
const provider = new ethers.providers.JsonRpcProvider('https://eth.nownodes.io/your_api_key');
const contractABI = [...] // Replace with your ERC-223 Token's ABI
const contractAddress = "0x..."; // Replace with your ERC-223 Token's address
const contract = new ethers.Contract(contractAddress, contractABI, provider);
const walletAddress = "0x..."; // Replace with your Ethereum wallet address
async function getBalance() {
    const balance = await contract.balanceOf(walletAddress);
    console.log(`The ERC-223 token balance of address ${walletAddress} is: ${balance}`);
}
getBalance();

Make sure to replace the placeholders […] with the actual ABI and address of your ERC-223 token contract, and the “0x…” and “0x…” placeholders with your Ethereum wallet address and ETH token contract address of ERC-223 respectively. When you run this code, it will connect to the Ethereum network using the Ethereum RPC node provider, set up the contract instance, and retrieve the ERC-223 token balance for the specified wallet address.

Checking the balance of ERC-223 tokens doesn’t always require a full-fledged programming library like Web3.js or Ethers.js. Sometimes, a simple Ethereum JSON RPC call through NOWNodes suffices.  

ERC-223 Tokens Balance with Direct API Calls

In addition to using Web3.js and Ethers.js libraries, you can also retrieve the balance of an ERC-223 token using Ethereum JSON RPC calls with NOWNodes. Connect to the Ethereum RPC endpoint with the node provider and follow the steps below.

To successfully retrieve the balance of an ERC-223 token using direct API calls with NOWNodes, you’ll need the following prerequisites:

  • Access to REST API and JSON-RPC through NOWNodes
  • An Ethereum wallet address
  • The ETH token contract address for the ERC-223 

Once you have met all the prerequisites, follow the steps below. 

Step 1: Prepare the Data Payload for JSON RPC Call

First, create a data payload for the Ethereum JSON RPC call. The balanceOf ETH method usually has an ID of 0x70a08231. You also need to append the Ethereum address you’re interested in (without the ‘0x’ prefix), left-padded with zeros to 64 characters.

wallet_address = '0xYourEthereumAddress'
padded_address = wallet_address[2:].zfill(64)
data_payload = '0x70a08231' + padded_address

By creating this data payload, you’ll be able to include it in the Ethereum JSON-RPC call to retrieve the ERC-223 token balance for the specified wallet address.

Step 2: Make the Ethereum JSON-RPC Call with NOWNodes

With NOWNodes, you’ll make a POST request with the data payload and other JSON-RPC fields required.

Here’s how you could do this using an Ethereum JSON RPC command:

curl -X POST \
https://eth.nownodes.io/your_api_key'
  -H 'Content-Type: application/json' \
  -d '{
        "jsonrpc":"2.0",
        "id": 1,
        "method": "eth_call",
        "params": [
            {
              "to": "0xYourERC223ContractAddress",
              "data": "0x70a08231000000000000000000000000YourPaddedEthereumAddress"
            },
            "latest"
        ]
      }'

Replace 0xYourERC223ContractAddress and 0x70a08231000000000000000000000000YourPaddedEthereumAddress with the actual ETH token contract address of ERC-223 and padded Ethereum address, respectively. Also, replace YOUR_NOWNODES_API_KEY with your actual NOWNodes API key.

Step 3: Parse the Response

The API will return a JSON response that contains the balance in a hexadecimal format. You’ll need to convert this to a decimal number to get the actual balance. You can do this using various programming languages; for example, in Python:

hex_balance = '0x...'  # Replace with the actual hexadecimal balance from the response
decimal_balance = int(hex_balance, 16)

Using NOWNodes, you can easily make Ethereum JSON RPC calls to retrieve the balance of an ERC-223 token. This eliminates the need for third-party libraries and provides a quick and straightforward method for checking token balances. With a service like NOWNodes, you can achieve this in a secure and scalable manner.

Conclusion

In this article, we’ve outlined multiple methods to get ETH token balance, whether you prefer using the Web3.js library, the Ethers.js library, and direct Ethereum JSON RPC calls. Moreover, we demonstrated how to access an Ethereum RPC node through NOWNodes, a service that makes this process even more convenient. NOWNodes takes away the complexity of hosting your own Ethereum node, thereby simplifying the development process and accelerating time-to-market for your blockchain applications.

Whether you’re a developer building a dApp, a token holder wanting to check your balance, or a blockchain enthusiast looking to explore more, these methods provide you with essential tools to interact with the Ethereum mainnet network effectively. By understanding these different approaches, you can more confidently navigate the increasingly complex landscape of ERC-223 tokens and the broader Ethereum mainnet network.

Don’t miss out on valuable content and exciting updates from our blog! Stay up to date by following NOWNodes on Twitter and LinkedIn. Join our Web3 community on Telegram today

GET ACCESS TO ETHEREUM

KEY POINTS:

Checking ETH token balance includes different methods:

  • Utilizing Web3.js Library: Access the Ethereum RPC endpoint through NOWNodes. Install the Web3.js library. Initialize Web3 provider. Set up the ERC-223 contract. Fetch the token balance. 
  • Using Ethers.js: Connect to the Ethereum RPC node through NOWNodes. Install the Ethers.js library. Initialize your Ethers.js library and establish a connection to an Ethereum RPC node. Set up a contract instance. Query the token balance. 
  • By Making Direct API calls: Get your private API key with NOWNodes. Prepare the data payload for the JSON RPC call. Make the Ethereum JSON RPC call. Parse the response.