How to Get ERC-20 Token Balance at a Given Block
Learn how to get the balance of a single token being held by a specific contract address at a given block or point in time.
This tutorial uses the alchemy_getTokenBalances endpoint.
The token balances for a user address is a critical data point for almost any web3 use case - be it DeFi platforms, wallets, analytics, exchanges, and many others. A common use case would be displaying the balances as below in your wallet app.
Sample wallet screen displaying coin balances
To fetch this data you have 2 options:
- Fetch the Transaction History for the token’s contract, for example take USDT, which is millions of records. Then search through for your user’s address among those records (will take hours) and then show that to your user. Not to forget, do this indexing process every second forever to keep it updated for every user.
- OR just use Alchemy’s
getTokenBalances
endpoint.
If you’re like most web3 developers, you probably want to go for the second option!
About this Tutorial
We will write a simple script in Node to gets us the balance of USDT for a particular wallet address. We will get the latest balance, the balance at a a particular block (in our case, 1000000), and at a particular human-readable time using Alchemy’s Token API Quickstart.
Creating the Token Balances Script
Step 1: Install Node and npm
In case you haven’t already, install node and npm on your local machine.
Make sure that node is at least v14 or higher by typing the following in your terminal:
Step 2: Create an Alchemy app
In case you haven’t already, sign up for a free Alchemy account.
Alchemy’s account dashboard where developers can create a new app on the Ethereum blockchain.
Next, navigate to the Alchemy Dashboard and create a new app.
Make sure you set the chain to Ethereum and the network to Mainnet.
Once the app is created, click on your app’s View Key button on the dashboard.
Take note of the HTTP URL.
The URL will be in this form: https://eth-mainnet.g.alchemy.com/v2/xxxxxxxxx
You will need this later.
Step 3: Create a node project
Let’s now create an empty repository and install all node dependencies.
To make requests to the Token API, use the Alchemy SDK.
You can also use axios
or fetch
alternatively.
This will create a repository named token-balances
that holds all your files and dependencies.
Next, open this repo in your favorite code editor.
We will be writing all our code in the main.js
file.
Step 4: Get the latest USDT balance
To get the USDT balance of a particular wallet, we will use the getTokenBalances method.
This method takes in two arguments:
DATA
: The wallet address whose USDT balance we want to checkArray
: An array containing the list of token contract addresses we are interested in. In our case, it’s just one address (i.e of USDT).
Add the following code to the main.js
file.
You can find the contract address of any token by searching its ticker or name on Etherscan.
In order to format a token into its human-readable format, you simply convert the hexadecimal result received from the API response into a decimal, and then divide by the number of decimal places that that particular token has. USDT, for instance, has 6. You can find out how many decimal places any currency has by reading the decimals
value from the contract’s page on Etherscan.
Solidity does not support decimal point numbers. So, If you want an ERC20 token to have the ability to be subdivided with a precision of 2 decimal places, you need to represent 1 token as 100 (set its decimal places to 2). Similarly if I you want an ERC20 token to have the ability to subdivided with a precision of 18 decimal places, you need to represent 1 token as 1000000000000000000 (set its decimal places to 18).
So, the formula for number of tokens becomes:
tokenSupplyWithDecimalZeros = actualTokenSupply * (10 ** decimals)
That is why we need to divide the decimal value by the number of decimal places, to get the actual token value.
Run the script using the following command:
If all goes well, you should see output that looks something like this:
Step 5: Get USDT balance at a particular block
In order to get the balance of a token at a particular block, we need to leverage the eth_call method. This will allow us to query USDT contract’s balanceOf
method at a particular block number.
Create a new file called historical_balance.js
and add the following code:
Run this script using:
If all goes well, you should see output that looks something like this:
Step 6: Get USDT balance at a particular timestamp
The process to get the USDT balance at a particular timestamp is identical to the previous, except that we need to figure out a way to convert a timestamp into a block number.
In order to do this, we will use a library called ethereum-block-by-date
.
Install this package by running the following command in your terminal:
Replace the contents of historical_balance.js
with the following:
Running the same command as in step 5 should produce the same output.
Conclusion
Congratulations! You now know how to use the Alchemy Ethereum API and Token API to get the token balance of a wallet at present, a particular block height or timestamp.
If you enjoyed this tutorial on how to get ETH balance at a point in time, give us a tweet @Alchemy.
Don’t forget to join our Discord server to meet other blockchain devs, builders, and entrepreneurs.
Ready to start using the Alchemy Token API?
Create a free Alchemy account and share your project with us!