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 node
Write a script
Execute 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: