Discover the easiest way to get ERC-20 token balances using RPC nodes. Moralis’ Extended RPC Methods allow you to seamlessly fetch any wallet’s token balances with a single RPC-style request. Curious to see how it works? Check out the eth_getTokenBalances endpoint in action:
import fetch from 'node-fetch';
const options = {
method: 'POST',
headers: {
accept: 'application/json',
'content-type': 'application/json'
},
body: JSON.stringify({
"jsonrpc": "2.0",
"id": 1,
"method": "eth_getTokenBalances",
"params": [
{
"address": "0xcB1C1FdE09f811B294172696404e88E658659905",
}
]
})
};
fetch('YOUR_NODE_URL', options)
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));
With one call, you’ll receive the ERC-20 balance of the specified wallet. Here’s a sample response:
{
//...
result: [
{
token_address: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
name: 'Wrapped Ether',
symbol: 'WETH',
decimals: 18,
logo: 'https://logo.moralis.io/0x1_0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2_a578c5277503e547a072ae32517254ca',
thumbnail: 'https://logo.moralis.io/0x1_0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2_a578c5277503e547a072ae32517254ca',
balance: '10000000000000000',
possible_spam: false,
verified_contract: true,
total_supply: '2746607222348759943423350',
total_supply_formatted: '2746607.22234875994342335',
percentage_relative_to_total_supply: 3.64085549569e-7
},
//...
]
}
That’s all it takes – fetching ERC-20 token balances with RPC nodes has never been easier. For a deeper dive, join us in this detailed tutorial or check out our official Extended RPC Methods documentation.
Ready to get started? Sign up with Moralis, and you’ll be able to get ERC-20 token balances with RPC nodes in a heartbeat!
Overview
Whether you’re building a portfolio tracker, cryptocurrency wallet, tax platform, or other Web3 projects, you’ll likely need access to ERC-20 token balances. Traditionally, fetching these balances using RPC nodes has been cumbersome, requiring multiple requests and manual putting together data. But there’s a better way: Moralis’ Extended RPC Methods.
With our Extended RPC Methods, you can easily query decoded, human-readable data through RPC-style requests. Instantly access ERC-20 token balances, decoded transactions, token prices, and more with minimal effort.
What are ERC-20 Token Balances?
Put simply, ERC-20 token balances refer to the number of tokens held by a specific wallet or address. Since various types of ERC-20 tokens exist, a wallet can hold multiple token balances simultaneously. For example, a user might have 5 Wrapped BTC, 100 USDC, and 1,000,000 Shiba Inu tokens.
Introducing Extended RPC Methods – The Easiest Way to Get ERC-20 Token Balances with RPC Nodes
Moralis’ Extended RPC Methods streamline the process of querying decoded, human-readable data using RPC nodes, making dapp development more accessible.
Key data you can access:
- eth_getTokenBalances: Retrieve ERC-20 token balances by wallet.
- eth_getTransactions: Fetch native transactions by wallet address.
- eth_getDecodedTransactions: Access detailed wallet transaction history by address.
- eth_getTokenPrice: Get prices by token addresses.
- eth_getTokenMetadata: Obtain ERC-20 metadata by token address.
- eth_getNFTBalances: Retrieve NFTs by wallet address.
- eth_getNFTCollections: Access NFT collections by wallet.
Why Do You Need Extended RPC Methods?
Querying on-chain data using conventional RPC methods can be cumbersome. These methods aren’t optimized for common queries like, “What ERC-20 tokens does wallet X hold?” To get this information, you typically need to make multiple requests and manually compile the data, which is time-consuming and resource-intensive. Fortunately, that’s where Moralis’ Extended RPC Methods come in.
Our Extended RPC Methods let you easily get ERC-20 token balances, wallet history, NFT balances, and more through simple RPC-style requests, making fetching crypto data via nodes more efficient than ever.
3-Step Tutorial: How to Get ERC-20 Token Balances with RPC Nodes
We’ll now show you how to seamlessly get ERC-20 token balances using RPC nodes. Thanks to the accessibility of our Extended RPC Methods, you can obtain this data in three simple steps:
- Sign up with Moralis & create a node.
- Write a script calling the eth_getTokenBalances endpoint.
- Run the code.
Prerequisites
Before starting the tutorial, ensure you have the following ready:
- Moralis account
- Node URL
- Basic knowledge of RPC methods
Step 1: Sign Up with Moralis & Create a Node
Click the “Start for Free” button at the top right to set up your Moralis account:
From there, log in, go to the “Nodes” tab, and click “+ Create Node”:
Next, select “Ethereum,” followed by “Mainnet,” and click the “Create Node” button. This will provide you with two node URLs. Copy and save one of the URLs, as you’ll need it in the next section.
Step 2: Write a Script Calling the eth_getTokenBalances Endpoint
Start by opening Visual Studio Code or your preferred IDE, then set up a new folder and initialize a project using the terminal command provided:
After that, install the necessary dependencies using the given command:
In your “package.json” file, be sure to add “type”: ”module”:
Next, create a new “index.js” file and insert the provided code. Replace YOUR_NODE_URL with the node URL you copied earlier, and adjust the address parameter as needed to fit your query.
That’s it. From here, you just need to run the code.
Step 3: Run the Code
Open a new terminal and run the provided command in your project’s root folder:
In return, you’ll get the ERC-20 token balances of the specified wallet using your RPC node. Here’s an example of what it might look like:
And that’s it! It’s that easy to get ERC-20 token balances from RPC nodes using Moralis! To learn more about how this works, please check out the eth_getTokenBalances documentation page.
Also, want to see how you can use this data in practice? Check out the Moralis YouTube video below, where one of our developers shows you how to build a portfolio app using just the eth_getTokenBalances endpoint:
Beyond How to Get ERC-20 Token Balances with RPC Nodes – Exploring Other Extended RPC Methods
Fetching ERC-20 token balances with nodes is just one aspect of our Extended RPC Methods. In the following sections, we’ll explore five other methods and highlight the responses you get from them:
- eth_getDecodedTransactions
- eth_getTokenPrice
- eth_getTokenMetadata
- eth_getNFTBalances
- eth_getNFTCollections
So, without further ado, let’s take a closer look at DeFi Daily News!
eth_getDecodedTransactions
With eth_getDecodedTransactions, you can seamlessly fetch the transaction history of a specific wallet address, enriched with decoded data. Moreover, this method encompasses native transfers, ERC-20 transactions, NFT transfers, and more.
Here’s an example of how to call this premier method:
In return for calling the script above, you’ll receive the full transaction history of the wallet. Additionally, each transaction is enriched with a human-readable event summary, category tag, logos, addresses, and more.
eth_getTokenPrice
With eth_getTokenPrice, you can now seamlessly fetch token prices based on contract addresses using RPC-style methods. You can retrieve prices for stablecoins like USDT, meme coins like Shiba Inu, and everything in between.
Here’s an example of what it looks like when calling the endpoint:
In return for running the script above, you receive both the USD and native price of the specified token. Additionally, the response is enriched with price changes over time, a token logo, token decimals, and more.
eth_getTokenMetadata
With eth_getTokenMetadata, you can seamlessly fetch the metadata of an ERC-20 token. Moreover, this endpoint covers everything from meme coins like Shiba Inu to stablecoins like USDC.
Here’s an example of the endpoint in action:
In return for calling the endpoint above, you get the metadata of the specified token. This will include the token name, symbol, decimals, logos, total supply, and much more.
eth_getNFTBalances
Using eth_geNFTBalances, you can effortlessly retrieve a list of NFTs owned by a specific wallet address.
Here’s an example script showing how to call this endpoint:
In return, you’ll get a list of all NFTs owned by the specified address. Moreover, each object is enriched with token addresses, extensive metadata, spam indicators, collection logos, and more.
eth_getNFTCollections
Get a list of all the NFT collections owned by a specific wallet, including collection details and metadata.
Here’s the endpoint in action:
Calling the endpoint above provides an array of all collections owned by the specified wallet. Each object is enriched with data, including a name, collection logo, collection banner, spam indicators, and more.
Moralis Overview – Exploring RPC Nodes & Web3 APIs
Moralis stands out as the industry’s #1 crypto data provider. Our suite of development tools includes top-tier RPC nodes and several use case-specific APIs, such as the Wallet API, Token API, Streams API, and more. With Moralis, you get all crypto data in one place.
What makes Moralis unique?
- One Call – All the Data: Our APIs and Extended RPC Methods are designed with the outcome in mind, giving you more data with fewer calls. Fetch ERC-20 token balances, a wallet’s full history, token prices, and more with single requests.
- Multi-Chain Support: Our nodes and Web3 APIs support all major chains, including Ethereum, Polygon, BSC, Optimism, Base, and others. Enjoy full feature parity across our supported networks, so you only need one data provider for all chains.
- Enterprise-grade Security: Moralis is SOC 2 Type 2 certified, demonstrating our commitment to security and reliability.
RPC Nodes
Moralis is a leading node provider, supporting over 30 blockchains, including Ethereum, Base, and Polygon. With our user-friendly interface, you can effortlessly integrate nodes into your projects at the click of a few buttons.
What sets our nodes apart?
- Speed: Industry-leading response times as low as 70 ms.
- Reliability: 99.9% uptime, ensuring constant data availability.
- Security: SOC 2 Type 2 certified for enterprise-grade protection.
Web3 APIs
Moralis’ Web3 API suite consists of several prominent interfaces. Here are five of them:
To learn more about our interfaces, visit our Web3 API page!
Summary: How to Get ERC-20 Token Balances with RPC Nodes
It doesn’t matter if you’re building a Web3 wallet, portfolio tracker, tax tool, or other similar platforms – chances are, you need access to the ERC-20 token balances of your users. However, querying ERC-20 token balances from RPC nodes has traditionally been a bothersome and time-consuming endeavor. Doing so requires many requests and necessitates stitching together a lot of data yourself. Fortunately, there’s now a more streamlined alternative: Moralis’ Extended RPC Methods.
Our Extended RPC Methods make it possible to query decoded and enriched data seamlessly using RPC-style requests. With this feature, you only need a single call to get ERC-20 token balances, transaction histories, token prices, and more using RPC nodes.
To highlight the accessibility of our Extended RPC Methods, here’s the eth_getTokenBalances endpoint in action:
import fetch from 'node-fetch';
const options = {
method: 'POST',
headers: {
accept: 'application/json',
'content-type': 'application/json'
},
body: JSON.stringify({
"jsonrpc": "2.0",
"id": 1,
"method": "eth_getTokenBalances",
"params": [
{
"address": "0xcB1C1FdE09f811B294172696404e88E658659905",
}
]
})
};
fetch('YOUR_NODE_URL', options)
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));
In return for calling the script above, you’ll get the ERC-20 token balances of the specified address, enriched with token logos, spam indicators, and much more. Here’s a sample response:
{
//...
result: [
{
token_address: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
name: 'Wrapped Ether',
symbol: 'WETH',
decimals: 18,
logo: 'https://logo.moralis.io/0x1_0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2_a578c5277503e547a072ae32517254ca',
thumbnail: 'https://logo.moralis.io/0x1_0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2_a578c5277503e547a072ae32517254ca',
balance: '10000000000000000',
possible_spam: false,
verified_contract: true,
total_supply: '2746607222348759943423350',
total_supply_formatted: '2746607.22234875994342335',
percentage_relative_to_total_supply: 3.64085549569e-7
},
//...
]
}
That’s it! Fetching ERC-20 token balances with RPC nodes doesn’t have to be more challenging than this when using Moralis.
If you liked this tutorial on how to get ERC-20 token balances with RPC nodes, consider checking out more Moralis content. For instance, learn how to get DeFi protocol data or dive into our Alchemy Custom Webhooks guide.
Finally, if you want to get ERC-20 token balances with RPC nodes yourself, don’t forget to sign up with Moralis. You can create an account free of charge and get immediate access to all our premier development tools!