How to List all Tokens of User in Solidity
Last Updated :
28 Apr, 2025
Ethereum, the main blockchain stage for decentralized applications (DApps), permits designers to make many advanced resources, including tokens. This article focuses on discussing the technique to list all the tokens of a user in Solidity.
Understanding ERC-20 Tokens
ERC-20 tokens adhere to a standard interface, making them interchangeable and widely supported by various platforms and wallets.
ERC-20 tokens come with a set of essential functions:
- balanceOf(address): Returns the token balance of a specific address.
- transfer(address, uint256): Allows the transfer of tokens between addresses.
Let's create a simplified ERC-20 token contract for our examples:
Solidity
// A simplified ERC-20 token contract
contract ERC20Token
{
string public name = "Example Token";
string public symbol = "TOKEN";
uint8 public decimals = 18;
uint256 public totalSupply = 1000000 * 10**uint256(decimals);
mapping(address => uint256) balances;
constructor()
{
balances[msg.sender] = totalSupply;
}
function balanceOf(address _owner) public view returns (uint256)
{
return balances[_owner];
}
function transfer(address _to, uint256 _value) public returns (bool)
{
require(_to != address(0));
require(balances[msg.sender] >= _value);
balances[msg.sender] -= _value;
balances[_to] += _value;
return true;
}
}
Listing All Tokens of a User
Step 1: Import ERC-20 Interfaces
First, we need to import the ERC-20 interface into our contract to access the required functions. Create an interface like this:
Solidity
interface IERC20 {
function balanceOf(address _owner) external view returns (uint256);
}
This step ensures our contract can interact with ERC-20 tokens.
Step 2: Create a Function to List Tokens
Next, we'll implement a function that lists all tokens of a user. This function will loop through known ERC-20 token addresses and call the balanceOf function for each token contract, passing the user's address as an argument.
Solidity
function listUserTokens(address _user, address[] memory _tokenAddresses) public view returns (uint256[] memory) {
uint256[] memory userTokenBalances = new uint256[](_tokenAddresses.length);
for (uint256 i = 0; i < _tokenAddresses.length; i++) {
IERC20 token = IERC20(_tokenAddresses[i]);
userTokenBalances[i] = token.balanceOf(_user);
}
return userTokenBalances;
}
We define a function called listUserTokens, which takes the user's address and an array of token addresses as input. It then loops through the token addresses, calls balanceOf for each token contract, and stores the balances in an array.
Step 3: Deploy Your Contract and Call the Function
Deploy your contract on the Ethereum blockchain, and when calling the listUserTokens function, pass an array of ERC-20 token addresses. This will return an array of token balances for the specified user.
Solidity
pragma solidity ^0.8.0;
// Import the ERC-20 interface
interface IERC20 {
function balanceOf(address _owner) external view returns (uint256);
}
contract TokenLister {
// Function to list user tokens
function listUserTokens(address _user, address[] memory _tokenAddresses) public view returns (uint256[] memory) {
uint256[] memory userTokenBalances = new uint256[](_tokenAddresses.length);
for (uint256 i = 0; i < _tokenAddresses.length; i++) {
IERC20 token = IERC20(_tokenAddresses[i]);
userTokenBalances[i] = token.balanceOf(_user);
}
return userTokenBalances;
}
}
Output:
.png)
Similar Reads
How to List all Users in the Mongo Shell In MongoDB, user management is an essential aspect of database administration, allowing administrators to control access and permissions for different users. The Mongo Shell provides a powerful interface to interact with MongoDB including managing users. In this article, we'll explore how to list al
3 min read
How to List All Users in PostgreSQL If we need to access all the users in our PostgreSQL database, we are in the right place. Whether we are a database administrator or a developer, having a clear method to list all users is essential for managing access and permissions. This article will guide us through a simple and effective way to
4 min read
How to List All Tables in Oracle? In this article, we will discuss all the methods to list all tables in the oracle SQL Database. We have three types of a subset of tables available to use as identifiers which in turn help us to sort the required table names. Here, are the following types of table identifiers in the Oracle SQL Datab
2 min read
How to List all Schemas in PostgreSQL? In PostgreSQL, schemas are used to organize database objects such as tables, views, functions, and indexes into logical groups. Understanding how to list schemas within a PostgreSQL database is essential for effective database management, especially as databases grow in size and complexity.In this a
3 min read
How to Get Session Token in AWS? A session token is a popular concept that is used in AWS for giving access to some user or person for a limited amount of time, in this the user gets to access the AWS resources but only for a limited amount of time only.The purpose of the session token is to have more security in the AWS system so
6 min read
How to Show a List of All Databases in MySQL MySQL is a popular open-source relational database management system (RDBMS) that is uniquely used to construct expandable and high-productivity databases. MySQL, which was created by MySQL AB and later acquired by its current owner Oracle Corporation, was originally introduced in 1995.MySQL is repu
7 min read