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.
Curious how it works? In this tutorial, we’ll show you how to use Moralis to get ERC-20 token balances via RPC nodes. Let’s dive in!
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.
Why are ERC-20 token balances important for developers?
ERC-20 token balances are essential for many Web3 applications. Here are three key examples:
- Wallets: Cryptocurrency wallets need to retrieve and display ERC-20 token balances so users can see which tokens they own and in what amounts.
- Portfolio Trackers: Portfolio trackers rely on ERC-20 token balances to accurately monitor users’ assets, track price changes, and update users on their overall portfolio performance.
- Tax Platforms: To generate precise tax reports, tax platforms must access the ERC-20 token balances of their users.
These are just a few examples; ERC-20 token balances are necessary for most Web3 dapps!
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_getTokenPrices
: 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.
In essence, our Extended RPC Methods make it easy for you to query and integrate decoded, human-readable crypto data into your dapps!
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.
Want to see how it works? Check out the tutorial below to learn how to use our Extended RPC Methods to get ERC-20 token balances!
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.
However, before you can begin, you need to address a few prerequisites.
Prerequisites
Before starting the tutorial, ensure you have the following ready:
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:
npm init
After that, install the necessary dependencies using the given command:
npm install node-fetch --save npm install moralis @moralisweb3/common-evm-utils
In your “package.json” file, be sure to add "type": ”module”
:
Next, create a new “index.js” file and insert the provided code:
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));
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:
node index.js
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:
{ //... 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 }, //... ] }
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.
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_getTokenPrices
eth_getTokenMetadata
eth_getNFTBalances
eth_getNFTCollections
So, without further ado, let’s take a closer look at eth_getDecodedTransactions
!
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:
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_getDecodedTransactions", "params": [ { "address": "0x737Aa707Cbca71C849e230485cAF2a7BB0ad29B8", } ] }) }; 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 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. Here’s an example of what it might look like:
{ //... "result": [ { "block_hash": "0xcbcf4698c22b59ec2a0b9ca50b9c88934ad5574ef526d318813cf88156d85131", "block_number": "20240166", "block_timestamp": "2024-07-05T12:00:23.000Z", "from_address": "0x737aa707cbca71c849e230485caf2a7bb0ad29b8", "from_address_label": null, "from_address_entity": null, "from_address_entity_logo": null, "to_address": "0xfecf6b4ad0140528821a40299d0e85a931d92f99", "to_address_label": null, "to_address_entity": null, "to_address_entity_logo": null, "gas": "21000", "gas_price": "8667201588", "hash": "0x21f9252afaf41b36ee73b2b379d656ae9db0261a475711004685555081f99450", "nonce": "1", "receipt_cumulative_gas_used": "5372558", "receipt_gas_used": "21000", "receipt_status": "1", "transaction_fee": "0.000182011233348", "transaction_index": "42", "value": "20000000000000000", "receipt_contract_address": null, "nft_transfers": [], "erc20_transfers": [], "method_label": null, "native_transfers": [ { "from_address": "0x737aa707cbca71c849e230485caf2a7bb0ad29b8", "from_address_label": null, "from_address_entity": null, "from_address_entity_logo": null, "to_address": "0xfecf6b4ad0140528821a40299d0e85a931d92f99", "to_address_label": null, "to_address_entity": null, "to_address_entity_logo": null, "value": "20000000000000000", "value_formatted": "0.02", "direction": "send", "internal_transaction": false, "token_symbol": "ETH", "token_logo": "https://cdn.moralis.io/eth/0x.png" } ], "summary": "Sent 0.02 ETH to 0xfe...2f99", "possible_spam": false, "category": "send" }, //... } ] }
eth_getTokenPrices
With eth_getTokenPrices
, 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:
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_getTokenPrice", "params": [ { "address": "0xdAC17F958D2ee523a2206206994597C13D831ec7", "include": "percent_change" } ] }) }; fetch(YOUR_NODE_URL, options) .then(response => response.json()) .then(response => console.log(response)) .catch(err => console.error(err));
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. Here’s an example of what the response looks like:
{ //... result: { tokenName: 'Tether USD', tokenSymbol: 'USDT', tokenLogo: 'https://logo.moralis.io/0x1_0xdac17f958d2ee523a2206206994597c13d831ec7_3282f332c2ac2948929f01fe7d921c51', tokenDecimals: '6', nativePrice: { value: '375760131462618', decimals: 18, name: 'Ether', symbol: 'ETH', address: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2' }, usdPrice: 1.0000402502911871, usdPriceFormatted: '1.000040250291187229', '24hrPercentChange': '-0.04543241491797881', exchangeName: 'Uniswap v3', exchangeAddress: '0x1F98431c8aD98523631AE4a59f267346ea31F984', tokenAddress: '0xdac17f958d2ee523a2206206994597c13d831ec7', priceLastChangedAtBlock: '20534105', possibleSpam: false, verifiedContract: true, pairAddress: '0xc7bbec68d12a0d1830360f8ec58fa599ba1b0e9b', pairTotalLiquidityUsd: '7148031.13' } }
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:
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_getTokenMetadata", "params": [ { "addresses": [ "0xdac17f958d2ee523a2206206994597c13d831ec7" ] } ] }) }; fetch('YOUR_NODE_URL', options) .then(response => response.json()) .then(response => console.log(response)) .catch(err => console.error(err));
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. Here’s a sample response:
{ //... result: [ { address: '0xdac17f958d2ee523a2206206994597c13d831ec7', address_label: 'Tether USD (USDT)', name: 'Tether USD', symbol: 'USDT', decimals: '6', logo: 'https://logo.moralis.io/0x1_0xdac17f958d2ee523a2206206994597c13d831ec7_3282f332c2ac2948929f01fe7d921c51', logo_hash: 'ee7aa2cdf100649a3521a082116258e862e6971261a39b5cd4e4354fcccbc54d', thumbnail: 'https://logo.moralis.io/0x1_0xdac17f958d2ee523a2206206994597c13d831ec7_3282f332c2ac2948929f01fe7d921c51', total_supply: '51986637760874451', total_supply_formatted: '51986637760.874451', fully_diluted_valuation: '51930247101.87', block_number: '20519335', validated: 1, created_at: '2017-11-28T00:41:21.000Z', possible_spam: false, verified_contract: true, categories: [ 'Asset-backed Tokens', 'Exchange-Issued Asset Tokens', 'Platform-Based Utility Tokens', 'Stablecoins' ], links: { twitter: 'https://twitter.com/Tether_to', website: 'https://tether.to/', facebook: 'https://www.facebook.com/tether.to', reddit: 'https://www.reddit.com' } } ] }
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:
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_getNFTBalances", "params": [ { "address": "0x3F22FC93143790a1bd11C37C65a0a0f7e7875eA2", "limit": 10, } ] }) }; fetch('YOUR_NODE_URL', options) .then(response => response.json()) .then(response => console.log(response.result, null, 2)) .catch(err => console.error(err));
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. Here’s an example of what it might look like:
{ //... result: [ { amount: '1', token_id: '8545', token_address: '0xbd3531da5cf5857e7cfaa92426877b022e612cf8', contract_type: 'ERC721', owner_of: '0x3f22fc93143790a1bd11c37c65a0a0f7e7875ea2', last_metadata_sync: '2024-08-14T19:47:37.128Z', last_token_uri_sync: '2024-08-14T19:47:32.019Z', metadata: '{"attributes":[{"trait_type":"Background","value":"Red"},{"trait_type":"Skin","value":"Normal"},{"trait_type":"Body","value":"Shirt Red"},{"trait_type":"Face","value":"Beard"},{"trait_type":"Head","value":"Mohawk Green"}],"description":"A collection 8888 Cute Chubby Pudgy Penquins sliding around on the freezing ETH blockchain.","image":"ipfs://QmNf1UsmdGaMbpatQ6toXSkzDpizaGmC9zfunCyoz1enD5/penguin/8545.png","name":"Pudgy Penguin #8545"}', block_number: '18777311', block_number_minted: null, name: 'PudgyPenguins', symbol: 'PPG', token_hash: '5e8faae3c07cd3bdb8bfa817e5a14a1b', token_uri: 'https://ipfs.moralis.io:2053/ipfs/bafybeibc5sgo2plmjkq2tzmhrn54bk3crhnc23zd2msg4ea7a4pxrkgfna/8545', minter_address: null, verified_collection: true, possible_spam: false, collection_logo: 'https://i.seadn.io/gae/yNi-XdGxsgQCPpqSio4o31ygAV6wURdIdInWRcFIl46UjUQ1eV7BEndGe8L661OoG-clRi7EgInLX4LPu9Jfw4fq0bnVYHqg7RFi?w=500&auto=format', collection_banner_image: 'https://i.seadn.io/gcs/files/8a26e3de0f309089cbb1e5ab969fc0bc.png?w=500&auto=format' }, //... ] } }
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:
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_getNFTCollections", "params": [ { "address": "0x3F22FC93143790a1bd11C37C65a0a0f7e7875eA2", "limit": 10 } ] }) }; fetch('YOUR_NODE_URL', options) .then(response => response.json()) .then(response => console.log(response)) .catch(err => console.error(err));
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. Here’s an example of what it might look like:
{ //... result: [ { token_address: '0xbd3531da5cf5857e7cfaa92426877b022e612cf8', possible_spam: false, contract_type: 'ERC721', name: 'PudgyPenguins', symbol: 'PPG', verified_collection: true, collection_logo: 'https://i.seadn.io/gae/yNi-XdGxsgQCPpqSio4o31ygAV6wURdIdInWRcFIl46UjUQ1eV7BEndGe8L661OoG-clRi7EgInLX4LPu9Jfw4fq0bnVYHqg7RFi?w=500&auto=format', collection_banner_image: 'https://i.seadn.io/gcs/files/8a26e3de0f309089cbb1e5ab969fc0bc.png?w=500&auto=format' }, //... ] } }
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.
Let’s dive deeper into our products to highlight the benefits of Moralis!
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.
Discover more on our RPC nodes page!
Web3 APIs
Moralis’ Web3 API suite consists of several prominent interfaces. Here are five of them:
- Wallet API: A leading tool for building wallets and integrating wallet functionality into dapps. Retrieve any wallet’s history, token balances, net worth, profitability, and more with single lines of code.
- Token API: Access ERC-20 data with the Token API. Get balances, prices, metadata, and more.
- NFT API: Ideal for building NFT marketplaces, Web3 games, and other NFT-related platforms. Fetch NFT balances, metadata, prices, and more with minimal code.
- Price API: Get real-time ERC-20 prices, NFT floor prices, OHLCV prices, and more. Integrate price data seamlessly into your projects.
- Streams API: Set up Web3 data pipelines for custom events with just a few clicks. Perfect for real-time alerts, populating databases with current events, and incorporating live insights into your projects.
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!