Are you searching for the simplest way to get blockchain data using RPC methods? Look no further. In this guide, we’ll introduce you to Moralis’ Extended RPC Methods, enabling you to query the same enhanced, human-readable data our APIs provide, but through RPC-style requests. This includes ERC-20 balances, transaction histories, token metadata, and more. Ready to jump into the code? Here’s a script showing our eth_getTokenBalances method 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));
Running the code above will return the ERC-20 balances of the specified wallet. Here’s an example of what the output 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
},
//…
]
}
Fetching blockchain data via RPC methods doesn’t have to be more complicated than this when using Moralis. For a more detailed tutorial, follow along with this guide or visit our Extended RPC Methods documentation page.
Excited to start using our Extended RPC Methods? Sign up with Moralis today! Create a free account to access our Extended RPC Methods, RPC Nodes, and premier APIs immediately.
Overview
In the context of Web3, RPC refers to communication protocols that streamline the interaction between decentralized applications (dapps) and blockchain networks. There are multiple RPC protocols, and they feature standardized methods, allowing developers to seamlessly read and write blockchain data. However, standard RPC methods are typically quite limited, not allowing for common queries like “What ERC-20 tokens are wallet X holding?” To get this information, you must make several requests and connect the dots yourself. Fortunately, you can now circumvent these challenges with Moralis’ Extended RPC Methods.
Our Extended RPC Methods allow you to query decoded, human-readable blockchain data seamlessly using RPC-style methods: fetch ERC-20 balances, token prices, metadata, and much more with single calls.
But how does this work? If you’d like to learn more, join us in this guide as we lay it all out for you. Let’s dive straight in!
What are RPC Methods?
RPC, short for “Remote Procedure Call,” refers to communication protocols that allow one software system to call and request a service from another software system on a different computer. In the world of crypto, RPC enables dapps and other Web3 platforms (clients) to interact with a blockchain network (server).
There are standardized protocols, one example being JSON-RPC, that feature predefined methods. These so-called RPC methods allow you to seamlessly perform various operations, such as reading blockchain data, sending transactions, and managing wallets.
Here is a list of common RPC methods for Ethereum:
eth_blockNumber: Returns the number of the most recent block.eth_call: Executes a new message call.eth_chainId: Returns the current chain ID.eth_getBalance: Returns the balance of an account.eth_gasPrice: Returns the current gas price.
Overall, RPC methods simplify interaction with blockchain networks, facilitating the effortless development of dapps and other Web3 projects.
Exploring the Limitations of Standard RPC Methods
While RPC protocols simplify dapp development, they come with significant limitations that you need to consider. Typically, standard RPC methods only provide basic blockchain data. As such, common RPC methods are not designed to handle queries such as, “What ERC-20 tokens does wallet X hold?”
To obtain this kind of information using conventional RPC methods, developers must make multiple requests and manually compile the data. This process is cumbersome and time-consuming, demanding considerable development effort and resources.
To address these challenges, Moralis introduces Extended RPC Methods. But what exactly are these methods, and how do they benefit developers?
Find out in the next section!
Introducing Moralis’ Extended RPC Methods
Our Extended RPC Methods significantly improve our node solution, delivering the same enhanced functionalities found in Moralis’ APIs, but through RPC-style methods. These advanced methods simplify the process of querying decoded, human-readable data, streamlining your Web3 developer experience.
What do Moralis’ Enhanced RPC Methods include?
eth_getTransactions: Get all native transactions for a given wallet.eth_getDecodedTransactions: Obtain a detailed transaction history for a specific wallet address.eth_getTokenBalances: Retrieve ERC-20 token balances for a given wallet address.eth_getTokenPrice: Fetch current prices for ERC-20 tokens.eth_getTokenMetadata: Obtain metadata for ERC-20 tokens.eth_getNFTBalances: Fetch all NFTs held by a wallet.eth_getNFTCollections: Obtain all NFT collections owned by a specific wallet address.
In summary, Moralis’ Enhanced RPC Methods significantly extend the capabilities of our nodes, giving you the same enhanced data you would get using our APIs, but via RPC-style requests!
3-Step Tutorial: How to Get All Blockchain Data via Extended RPC Methods
Now, with an overview of what Moralis’ Extended RPC Methods entail, we’ll show you how to use them in practice. More specifically, we’ll demonstrate how you can get the token balance for any given wallet in three simple steps:
Sign up with Moralis & create a nodeWrite a scriptExecute the code
Step 1: Sign Up with Moralis & Create a Node
If you haven’t already, click the “Start for Free” button at the top right to sign up with Moralis:
Once you log in, go to the “Nodes” tab and click the “+ Create Node” button:
Select “Ethereum,” followed by “Mainnet,” and hit “Create Node”:
Copy one of your node URLs and keep it for now, as you’ll need it in the next step:
Step 2: Write a Script
Open your preferred IDE, set up a new folder, and initialize a project with the terminal command below:
npm init
Once initialized, install the necessary dependencies using the provided terminal command:
npm install node-fetch –save
npm install moralis @moralisweb3/common-evm-utils
Next, open package.json and add “type”: “module” to the file:
After that, create an “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));
From here, you need to make some minor configurations. First, add the node URL you copied during the first step by replacing YOUR_NODE_URL. Next, configure the address parameter to fit your query:
That’s it for the code. All that remains now is to execute the script.
Step 3: Execute the Code
To run the code, navigate to the root folder of the project and run the following terminal command:
node index.js
In return, you’ll get the token balances of the specified address, enriched with logos, spam indicators, and much more. Here’s 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
},
//…
]
}
That’s it! Fetching data using Moralis’ Extended RPC Methods doesn’t have to be more challenging than that!
Extended RPC Methods Use Cases
Our Extended RPC Methods significantly improve the Web3 developer experience, enabling you to build a wide range of dapps effortlessly. Here are three common use cases for our enhanced RPC methods, providing the same data as our powerful APIs:
Token Balances: Retrieve token balances for any wallet with a single call, eliminating the need for manual data aggregation. Transaction Histories: Access transaction histories for any address through streamlined RPC calls. Token Prices: Obtain real-time token prices directly from decentralized exchanges (DEXs) effortlessly.
With this comprehensive data, you can develop everything from cryptocurrency wallets to token explorers with minimal effort!
Beyond Extended RPC Methods – Exploring the Ins & Outs of Moralis’ Web3 APIs
In addition to our enhanced RPC methods, Moralis offers a diverse suite of use case-specific APIs, including the Wallet API, Token API, Streams API, and many more. These premier interfaces enable you to effortlessly build everything from Web3 wallets to token explorers.
Why choose Moralis’ APIs?
Comprehensive: Moralis’ Web3 APIs are outcome-focused, providing more data with fewer calls. This allows you to enjoy a seamless developer experience, enabling you to build dapps faster and more efficiently. Cross-Chain: We support full feature parity across over 30 blockchain networks, including Ethereum, Polygon, Base, Optimism, and many others. With Moralis, one provider covers all your data needs. Secure: Moralis is SOC 2 Type 2 certified, ensuring enterprise-grade data security for your applications.
Let’s dive deeper into our API suite to explore the capabilities of our premier interfaces and how they can empower your development process!
Moralis’ Web3 API Suite
Our comprehensive suite of Web3 APIs includes over ten distinct interfaces designed to meet diverse use cases. While this guide won’t cover all of them, we’ll highlight three key examples:
Wallet APIToken APINFT API
Let’s start with an in-depth look at our Wallet API!
Wallet API
The Wallet API offers a comprehensive suite of features, unmatched flexibility, and exceptional scalability, making it the ultimate tool for wallet builders. Use this interface to access any wallet’s history, token balances, DeFi positions, and much more!
Supporting millions of addresses across all major chains, including Ethereum, Polygon, Base, Optimism, and many more, the Wallet API allows you to seamlessly integrate wallet data into your dapp, regardless of the platform you’re building on.
To demonstrate the power of the Wallet API, here’s how easy it is to fetch a wallet’s transaction history:
import fetch from ‘node-fetch’;
const options = {
method: ‘GET’,
headers: {
accept: ‘application/json’,
‘X-API-Key’: ‘YOUR_API_KEY’
},
};
fetch(
‘https://deep-index.moralis.io/api/v2.2/wallets/0xda74Ac6b69Ff4f1B6796cdDf61fBDd4A5f68525f/history?chain=eth&order=DESC’,
options
)
.then((response) => response.json())
.then((response) => console.log(response))
.catch((err) => console.error(err));
Running the above script provides you with the complete transaction history of the specified wallet, enriched with human-readable category tags, summaries, address labels, and more for each event. Here’s an example of what it might look like:
{
//…
“result”: [
{
“hash”: “0xc565260238f59fc3f35b74f3011375c7d637db9b075f77d342c30d19f946272e”,
“nonce”: “14”,
“transaction_index”: “75”,
“from_address”: “0xda74ac6b69ff4f1b6796cddf61fbdd4a5f68525f”,
“from_address_label”: null,
“to_address”: “0xdac17f958d2ee523a2206206994597c13d831ec7”,
“to_address_label”: “Tether USD (USDT)”,
“value”: “0”,
“gas”: “55331”,
“gas_price”: “13623172301”,
“receipt_cumulative_gas_used”: “13917979”,
“receipt_gas_used”: “41309”,
“receipt_status”: “1”,
“block_timestamp”: “2024-05-14T14:00:23.000Z”,
“block_number”: “19868695”,
“block_hash”: “0x660274d577cd20b0b82c1bff5f3c5641ba6027544e005f9256d5add9c7447920”,
“transaction_fee”: “0.000562759624582009”,
“nft_transfers”: [],
“erc20_transfers”: [
{
“token_name”: “Tether USD”,
“token_symbol”: “USDT”,
“token_logo”: “https://logo.moralis.io/0x1_0xdac17f958d2ee523a2206206994597c13d831ec7_0b0d126af6c744c185e112a2c8dc1495”,
“token_decimals”: “6”,
“from_address”: “0xda74ac6b69ff4f1b6796cddf61fbdd4a5f68525f”,
“from_address_label”: null,
“to_address”: “0x28c6c06298d514db089934071355e5743bf21d60”,
“to_address_label”: “Binance 14”,
“address”: “0xdac17f958d2ee523a2206206994597c13d831ec7”,
“log_index”: 338,
“value”: “50000000000”,
“possible_spam”: false,
“verified_contract”: true,
“direction”: “send”,
“value_formatted”: “50000”
}
],
“method_label”: null,
“native_transfers”: [],
“summary”: “Sent 50,000 USDT to Binance 14”,
“possible_spam”: false,
“category”: “token send”
},
//…
]
}
Token API
Moralis’ Token API is your essential tool for fetching and integrating ERC-20 data into your Web3 projects. With this premier interface, you can seamlessly retrieve token balances, prices, metadata, owners, and much more.
The Token API covers every single token across all major chains, including stablecoins like USDT, meme coins like Shiba Inu, LP tokens, and everything in between. This comprehensive coverage allows you to effortlessly build cross-chain token explorers, portfolio trackers, and more.
To demonstrate the power of the Token API, we’ll show you how to fetch token balances with prices for any wallet:
import fetch from ‘node-fetch’;
const options = {
method: ‘GET’,
headers: {
accept: ‘application/json’,
‘X-API-Key’: ‘YOUR_API_KEY’
},
};
fetch(‘https://deep-index.moralis.io/api/v2.2/wallets/0xcB1C1FdE09f811B294172696404e88E658659905/tokens?chain=eth’, options)
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));
By running the provided script, you’ll receive the token balances of the wallet, enriched with prices, price changes over time, and other metadata. Here’s an example of what the response will look like:
{
//…
“result”: [
{
“token_address”: “0xae7ab96520de3a18e5e111b5eaab095312d7fe84”,
“symbol”: “stETH”,
“name”: “Liquid staked Ether 2.0”,
“logo”: “https://logo.moralis.io/0x1_0xae7ab96520de3a18e5e111b5eaab095312d7fe84_cd0f5053ccb543e08f65554bf642d751”,
“thumbnail”: “https://logo.moralis.io/0x1_0xae7ab96520de3a18e5e111b5eaab095312d7fe84_cd0f5053ccb543e08f65554bf642d751”,
“decimals”: 18,
“balance”: “61135846911533523”,
“possible_spam”: false,
“verified_contract”: true,
“total_supply”: “9788873903061556217474312”,
“total_supply_formatted”: “9788873.903061556217474312”,
“percentage_relative_to_total_supply”: 6.2454422763e-7,
“balance_formatted”: “0.061135846911533523”,
“usd_price”: 2731.4812583950234,
“usd_price_24hr_percent_change”: 2.9851365470017,
“usd_price_24hr_usd_change”: 79.1749645169798,
“usd_value”: 166.99142005496108,
“usd_value_24hr_usd_change”: 4.840428509936174,
“native_token”: false,
“portfolio_percentage”: 44.16600478357348
},
//…
]
}
NFT API
The NFT API provides comprehensive NFT data at your fingertips. With just a few lines of code, you can access NFT balances, up-to-date metadata, prices, image previews, and more.
Moralis’ NFT API supports millions of NFT collections across all major blockchain networks, from well-known projects like CryptoPunks to newly minted tokens. Whether you’re developing NFT marketplaces, Web3 games, or similar platforms, this API is an essential tool.
To demonstrate the ease of use, here’s a script that fetches NFT metadata effortlessly:
import fetch from ‘node-fetch’;
const options = {
method: ‘GET’,
headers: {
accept: ‘application/json’,
‘X-API-Key’: ‘YOUR_API_KEY’
},
};
fetch(‘https://deep-index.moralis.io/api/v2.2/nft/0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB/1?chain=eth’, options)
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));
Running this script will return the metadata for the specified NFT, which may look something like this:
{
“amount”: “1”,
“token_id”: “1”,
“token_address”: “0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb”,
“contract_type”: “CRYPTOPUNKS”,
“owner_of”: “0xffa914c83d851b9fe372e4ba6a6e131373aa16ab”,
“last_metadata_sync”: “2024-08-10T14:39:45.042Z”,
“last_token_uri_sync”: “2024-08-10T14:39:44.622Z”,
“metadata”: “{\”image\”:\”https://www.larvalabs.com/cryptopunks/cryptopunk001.png\”,\”name\”:\”CryptoPunk 001\”,\”attributes\”:[\”Smile\”,\”Mohawk\”],\”description\”:\”Male\”}”,
“block_number”: “16079985”,
“block_number_minted”: null,
“name”: “CRYPTOPUNKS”,
“symbol”: “Ͼ”,
“token_hash”: “a99d02058e62e327e79aabd57e0b88a3”,
“token_uri”: “Invalid uri”,
“minter_address”: null,
“rarity_rank”: 7247,
“rarity_percentage”: 72.47,
“rarity_label”: “Top 73%”,
“verified_collection”: true,
“possible_spam”: false,
“collection_logo”: “https://i.seadn.io/gae/BdxvLseXcfl57BiuQcQYdJ64v-aI8din7WPk0Pgo3qQFhAUH-B6i-dCqqc_mCkRIzULmwzwecnohLhrcH8A9mpWIZqA7ygc52Sr81hE?w=500&auto=format”,
“collection_banner_image”: “https://i.seadn.io/gae/48oVuDyfe_xhs24BC2TTVcaYCX7rrU5mpuQLyTgRDbKHj2PtzKZsQ5qC3xTH4ar34wwAXxEKH8uUDPAGffbg7boeGYqX6op5vBDcbA?w=500&auto=format”
}
Visit the official Web3 API page for more information on our interfaces, including the Blockchain API, Streams API, Market Data API, and others!
Summary: Extended RPC Methods – Get All Blockchain Data via RPC Methods
RPC stands for “Remote Procedure Call,” and in the context of Web3, it refers to communication protocols that allow dapps to communicate with blockchain networks. There are standard RPC protocols that feature RPC methods. However, traditional RPC methods are often limited, providing only basic on-chain data. For instance, you can’t use conventional RPC methods to easily get the ERC-20 balance of a wallet. To get comprehensive data like this, you’d typically need to make multiple requests and decode the information manually. Fortunately, we offer a streamlined solution: Extended RPC Methods!
Our Extended RPC Methods deliver the same enhanced functionality provided by our APIs but via RPC-style requests. With these, you can seamlessly query ERC-20 balances, token prices, metadata, and much more using single calls. Here are some of our top methods:
eth_getTransactions: Retrieve the transactions for a wallet address.eth_getDecodedTransactions: Obtain detailed transaction history for a wallet address.eth_getTokenBalances: Retrieve ERC-20 token balances for a wallet address.eth_getTokenPrice: Fetch current prices for ERC-20 tokens.eth_getTokenMetadata: Obtain metadata for ERC-20 tokens by their addresses.eth_getNFTBalances: Retrieve NFTs held by a wallet.eth_getNFTCollections: Obtain NFT collections owned by a wallet address.
All in all, our Extended RPC Methods improve our node solution, giving you access to the same enhanced functionality that you would get with our APIs but via RPC-style requests. In return, you can query decoded, human-readable data straight from your Moralis nodes!
If you enjoyed learning about our Extended RPC Methods, explore more content on our blog. For instance, check out our guides on:
Ready to use our Extended RPC Methods to get the same enhanced data our APIs provide, but via RPC-style requests? Sign up with Moralis for free and gain instant access to our development tools!