Want to explore the easiest way to query the Ethereum blockchain for ERC-20 tokens, transactions, NFTs, and other data? With Moralis’ top-tier APIs, you can seamlessly fetch any on-chain data from any blockchain with only a few lines of code. As such, when using Moralis, you can build decentralized applications (dapps) faster and more efficiently!

For a preview of what’s to come, check out the endpoints below for querying ERC-20 tokens, transactions, and NFTs from the Ethereum network: 

  • getWalletTokenBalances() – Query the ERC-20 token balances of any wallet:
const response = await Moralis.EvmApi.token.getWalletTokenBalances({
  "chain": "0x1",
  "address": "0x1f9090aaE28b8a3dCeaDf281B0F12828e676c326"
});
  • getWalletTransactions() – Get the native transaction history of any address: 
const response = await Moralis.EvmApi.transaction.getWalletTransactions({
  "chain": "0x1",
  "address": "0x1f9090aaE28b8a3dCeaDf281B0F12828e676c326"
});
  • getWalletNFTs() – Fetch the NFT balance of any wallet: 
const response = await Moralis.EvmApi.nft.getWalletNFTs({
  "chain": "0x1",
  "address": "0xff3879b8a363aed92a6eaba8f61f1a96a9ec3c1e"
});

If you’d like to learn more about querying data from the Ethereum blockchain and explore additional endpoints, check out the official Moralis documentation page or join us in this article! Also, if you wish to query the Ethereum blockchain yourself, don’t forget to sign up with Moralis. You can create an account for free, and you’ll gain instant access to our premier Web3 APIs! 

Overview 

Whether you’re building an Ethereum-based decentralized exchange (DEX), cryptocurrency wallet, portfolio tracker, or any other Web3 project, you need an easy way to query ERC-20 tokens, transactions, etc., from the blockchain. However, this is easier said than done. And with multiple ways to query blockchain data, how do you find the best option? 

In this guide, we’ll introduce you to the easiest way to query and integrate Ethereum blockchain data into your projects. So, whether you’re looking to prototype your own Web3 project or scale an existing dapp, this read is for you. Let’s dive straight into it! 

What is Ethereum Data? 

Ethereum data – also typically referred to as ”Ethereum on-chain data” – is all the publicly available information stored on the Ethereum blockchain. With this data, it’s possible to verify transactions, track the movement of assets, and gain in-depth market insights. As such, the ability to query blockchain data is an essential part of building dapps and other Web3 projects on Ethereum! 

Graphic art design illustration - Ethereum blockchain data being retrieved using APIs

Ethereum blockchain data can be divided into three main categories: 

  • Transaction Data: Transaction data is information about each Ethereum transaction. This includes sender and receiver addresses, transaction amounts, fees, and more.  
  • Smart Contract Data: Smart contract data refers to all the information about smart contracts deployed on Ethereum, including a contract’s code, logs, state, and events. 
  • Block Data: Block data is all the information about each block in the Ethereum chain. This includes information such as hashes, transactions, miner fees, time stamps, and much more.

All in all, Ethereum data refers to the publicly available information stored on the blockchain, including transaction, smart contract, and block data! 

How Do You Get Data from the Ethereum Blockchain? 

It’s possible to query the Ethereum blockchain in numerous different ways. Below, we’ll explore three prominent examples: 

  • Run a Node: One option for querying the Ethereum blockchain is to run a node yourself. In doing so, you can directly interact with the Ethereum blockchain to query transactions, ERC-20 tokens, and more. 
  • Block Explorers: It’s also possible to leverage block explorers, which are platforms that allow you to seamlessly browse all transactions stored on Ethereum. For Ethereum, the go-to choice is Etherscan.  
  • Web3 APIs: You can also use Web3 API providers to query the Ethereum blockchain. A Web3 API – short for ”application programming interface” – is a collection of rules, methods, and protocols that allow you to interact with a blockchain network.
Art illustration showing developers querying the Ethereum blockchain for data

While you can use nodes, block explores, and Web3 APIs to query the Ethereum blockchain, these three options have their own advantages and disadvantages. For example, running a node requires an underlying infrastructure, which can be bothersome to set up. Furthermore, while block explores give you an intuitive interface to read blockchain transactions, they’re not designed for integrating data into dapps. 

As such, when it comes to Web3 development, your best option is to leverage a Web3 API provider! But which Web3 API provider is the best? For the answer to this question, join us in the following section as we introduce you to Moralis! 

The Easiest Way to Query the Ethereum Blockchain 

The easiest way to query the Ethereum blockchain is to use Moralis – the #1 Web3 API provider. Our premier Web3 APIs are trusted by industry leaders such as MetaMask, Delta, Opera, and many others. As such, Moralis currently powers crypto and blockchain projects for millions of end users across the globe! 

Graphic art design illustration - Moralis Logo and Ethereum

In Moralis’ premier suite of development tools, you’ll find several use-case-specific Web3 APIs, including the Token API, Wallet API, and NFT API. With these interfaces, it becomes possible to query and integrate Ethereum blockchain data into your dapps with only single lines of code. Consequently, when working with Moralis, you can build everything from cryptocurrency wallets to decentralized finance (DeFi) platforms with ease.

For a quick introduction to our suite of development tools, here are three examples of Web3 APIs you can use to query the Ethereum blockchain:

  • Token API: The Token API supports every single token across the biggest blockchains, making it the industry’s most comprehensive API for ERC-20 token data. With this API, you can effortlessly fetch token balances, transactions, prices, and more. 
  • Wallet API: The Wallet API is the industry’s leading tool for building wallets and integrating wallet functionality into your dapps. With single lines of code, you can seamlessly use the Wallet API to fetch balances, transactions, profile data, etc., from any address. 
  • NFT API: The NFT API gives you everything you need to query NFT data from the Ethereum blockchain. Use this API to get NFT balances, metadata, price data, and much more without breaking a sweat. 

If you want to explore all our development tools, please visit our Web3 API page, where you’ll find more than ten additional industry-leading interfaces! 

Benefits of Using Moralis 

With an overview of Moralis, let’s now explore some prominent benefits of using our industry-leading Web3 APIs when querying blockchain data: 

  • Comprehensive: We enrich all our API responses with address labels, marked data, metadata, and more from multiple sources. This allows us to provide the industry’s most comprehensive Web3 APIs. 
  • Simple and Cross-Chain Compatible: Request and response structures follow the same patterns and standards across all our APIs, making it super easy for you to query on-chain data. Moreover, our APIs are cross-chain compatible, meaning you can use the same tools to build on networks like Ethereum, Polygon, Solana, and many others. 
  • Trusted: Moralis is trusted by hundreds of thousands of Web3 developers and large enterprise customers, including MetaMask, Opera, Delta, etc. 
Display highlighting projects using Moralis to query Ethereum blockchain data, including MetaMask, Blockchain.com, Delta, etc.

Also, did you know you can access our premier Web3 APIs for free? All you have to do is sign up with Moralis, and you’ll be able to start building dapps faster and more efficiently today!

Now, let’s dive into the code and look closer at how you can use our APIs to query data from the Ethereum blockchain! 

Ethereum Example Queries: Discover How to Query Ethereum for Data 

Let’s now dive into the main topic and show you how easy it is to query the Ethereum blockchain when using Moralis. To do so, we’ll dive into three examples of endpoints you can utilize to query ERC-20 tokens, transactions, and NFTs directly from the Ethereum blockchain! 

How to Query ERC-20 Tokens?

With Moralis’ Token API, you can effortlessly get any wallet’s ERC-20 token balances. In fact, all you need is a single API call to the getWalletTokenBalances() endpoint: 

const response = await Moralis.EvmApi.token.getWalletTokenBalances({
  "chain": "0x1",
  "address": "0x1f9090aaE28b8a3dCeaDf281B0F12828e676c326"
});

In return for calling the endpoint above, you’ll get an array of all tokens held by the specified address:

[
  {
    "token_address": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
    "symbol": "USDC",
    "name": "USD Coin",
    "logo": "https://cdn.moralis.io/eth/0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48.png",
    "thumbnail": "https://cdn.moralis.io/eth/0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48_thumb.png",
    "decimals": 6,
    "balance": "10",
    "possible_spam": false,
    "verified_contract": true
  },
  //...
]

How to Fetch Wallet Transactions?

With the Wallet API, you can seamlessly query the native transaction history of any address on Ethereum. Simply call the getWalletTransactions() endpoint: 

const response = await Moralis.EvmApi.transaction.getWalletTransactions({
  "chain": "0x1",
  "address": "0x1f9090aaE28b8a3dCeaDf281B0F12828e676c326"
});

In return for calling the getWalletTransactions() endpoint, you’ll get a list containing the native full transaction history of the specified wallet: 

//...
  "result": [
    {
      "hash": "0x3d33b9ff55f9a2e3a3b0dd1df6828fbf6df0db7ae1f5fbd253c31764c39bd970",
      "nonce": "506740",
      "transaction_index": "172",
      "from_address": "0x1f9090aae28b8a3dceadf281b0f12828e676c326",
      "from_address_label": "rsync-builder",
      "to_address": "0x6d2e03b7effeae98bd302a9f836d0d6ab0002766",
      "to_address_label": "Fee Recipient: 0x6d2...766",
      "value": "107843232028388368",
      "gas": "21000",
      "gas_price": "19175358613",
      "input": "0x",
      "receipt_cumulative_gas_used": "21716381",
      "receipt_gas_used": "21000",
      "receipt_contract_address": null,
      "receipt_root": null,
      "receipt_status": "1",
      "block_timestamp": "2024-02-16T07:48:35.000Z",
      "block_number": "19239079",
      "block_hash": "0x699de6e4d3035252f2ca351b95a1bdf7d9be845803a71a34d9d311f9d5a0a618",
      "transfer_index": [
        19239079,
        172
      ]
    },
    //...
  ]
}

How to Get NFTs?

With Moralis’ NFT API, you can easily get the NFT balance of any address. All you need to do is call the getWalletNFTs() endpoint: 

const response = await Moralis.EvmApi.nft.getWalletNFTs({
  "chain": "0x1",
  "address": "0xff3879b8a363aed92a6eaba8f61f1a96a9ec3c1e"
});

Calling the getWalletNFTs() endpoint will give you a response containing a list of all NFTs held by the wallet in question: 

  //...
  "result": [
    {
      "amount": "1",
      "token_id": "5021",
      "token_address": "0xfff54e6fe44fd47c8814c4b1d62c924c54364ad3",
      "contract_type": "ERC721",
      "owner_of": "0xff3879b8a363aed92a6eaba8f61f1a96a9ec3c1e",
      "last_metadata_sync": null,
      "last_token_uri_sync": "2024-02-11T03:14:30.593Z",
      "metadata": null,
      "block_number": "14647390",
      "block_number_minted": "14647390",
      "name": "Youtopia",
      "symbol": "Youtopia",
      "token_hash": "d4719eaf84eabcf443065b0a463f5886",
      "token_uri": "http://api.youtopia-official.xyz/ipfs/5021",
      "minter_address": "0x13f11fd2c7c7be94674651386370d02b7aac9653",
      "verified_collection": false,
      "possible_spam": true,
      "collection_logo": "//...",
      "collection_banner_image": "//..."
    },
    //...
  ]
}

Tutorial: How to Query Ethereum Blockchain Data in 3 Steps 

We’ll now cover a brief tutorial showing you how to call the various endpoints of Moralis. As such, if you follow along, you’ll soon know how to query the Ethereum blockchain yourself for ERC-20 tokens, transactions, NFT balances, and more! 

However, for this walkthrough, we’ll be using the getWalletTokenBalances() endpoint as an example. And we’ll show you how to query the Ethereum blockchain in three steps:

  1. Get a Moralis API Key
  2. Write a Script
  3. Run the Code

However, before proceeding, you must deal with a few prerequisites! 

Prerequisites 

You can query Ethereum blockchain data with Moralis using multiple different programming languages, including Python, Go, etc. However, for this tutorial, we’ll use JavaScript and Node.js. As such, if you haven’t already, ensure you have the following ready: 

Step 1: Get a Moralis API Key 

If you haven’t already, sign up with Moralis by clicking on the ”Start for Free” button at the top right of Moralis’ homepage: 

Step 1 to query Ethereum data: Sign up using the Start for Free button on the Moralis homepage

With an account at hand, go to the ”Settings” tab, scroll down to the ”API Keys” section, and copy your key: 

Step 2 - Copy the Ethereum API key

Keep it for now, as you’ll need it in the next step! 

Step 2: Write a Script 

Open your IDE, set up a new folder, and initialize a project using the following terminal command: 

npm init

Next, install the Moralis SDK with the following terminal input: 

npm install moralis @moralisweb3/common-evm-utils

You can then open your ”package.json” file and add ”type”: ”module” to the list:

Substep - Adding ”type”: ”module” to the code base

From here, create an ”index.js” file and add the following code: 

import Moralis from 'moralis';

try {
  await Moralis.start({
    apiKey: "YOUR_API_KEY"
  });

  const response = await Moralis.EvmApi.token.getWalletTokenBalances({
    "chain": "0x1",
    "address": "0x1f9090aaE28b8a3dCeaDf281B0F12828e676c326"
  });

  console.log(response.raw);
} catch (e) {
  console.error(e);
}

You then need to configure the code by replacing YOUR_API_KEY, which you copied earlier, and configure the address parameter to fit your query: 

Substep to query Ethereum - Paste API key into code

That’s it; all that remains from here is running the code! 

Step 3: Run the Code

Open a new terminal and run the command below to execute the script: 

node index.js

In return, you’ll get a response containing an array of all tokens held by the wallet in question. It will look something like this: 

[
  {
    "token_address": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
    "symbol": "USDC",
    "name": "USD Coin",
    "logo": "https://cdn.moralis.io/eth/0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48.png",
    "thumbnail": "https://cdn.moralis.io/eth/0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48_thumb.png",
    "decimals": 6,
    "balance": "10",
    "possible_spam": false,
    "verified_contract": true
  },
  //...
]

That’s it! Querying Ethereum data doesn’t have to be more challenging than that. From here, you can follow the same steps and simply swap the endpoints and parameters to query transactions, NFTs, or other Ethereum data! 

Query Ethereum with Moralis and Build Next-Gen Dapps 

With Moralis’ diverse suite of APIs, you can seamlessly build any project you want. However, to give you a few examples, we’ll take this section to explore three prominent use cases for Moralis’ APIs: 

  • Token Analytics: Token analytics platforms are websites and apps that give users in-depth market insight into cryptocurrencies. These tools identify, fetch, analyze, and present blockchain data in a readable format, making it easy for traders to make more informed investment decisions.

    A great example of a token analytics platform using our Web3 APIs is Moralis Money. Moralis Money is a leading token explorer, blockchain data analytics tool, and crypto trading platform. As such, it’s essentially a one-stop shop for cryptocurrency traders.

Moralis Money UI Example - Showing what data you can query Ethereum with using Moralis
  • Portfolio Trackers: Portfolio trackers are apps that allow users to monitor the performance of their digital assets across multiple blockchains. Consequently, they essentially allow users to organize and manage their fungible tokens and NFTs in one place.

    An example of a portfolio tracker is Delta, an industry-leading platform for tracking digital assets, including stocks, NFTs, and cryptocurrencies. Delta uses Moralis’ Why Did It Move API to not only tell users when the value of cryptocurrencies changes but also tell users why the price action occurred.

Delta Mobile app with Ethereum data queried by Moralis
  • Cryptocurrency Wallets: Cryptocurrency wallets are digital platforms that allow users to store and manage their digital assets, including fungible tokens and NFTs. The most prominent cryptocurrency wallets also provide functionality for selling, buying, and swapping tokens.

    A great example of a cryptocurrency wallet leveraging Moralis’ Web3 APIs is MetaMask. MetaMask is a leading self-custodial wallet with millions and millions of users worldwide. With this premier cryptocurrency wallet, users can effortlessly store, sell, buy, and trade digital assets.

Crypto portfolio tracker Ethereum example query

Summary: Query the Ethereum Blockchain for ERC-20 Tokens, Transactions, NFTs, and More

When working with Moralis, it’s easy to query the Ethereum blockchain. With our comprehensive APIs, you only need single lines of code to fetch ERC-20 tokens, transactions, and much more straight from the Ethereum blockchain. Just check out the three endpoint examples below: 

  • getWalletTokenBalances() – Fetch the ERC-20 token balances of any address:
const response = await Moralis.EvmApi.token.getWalletTokenBalances({
  "chain": "0x1",
  "address": "0x1f9090aaE28b8a3dCeaDf281B0F12828e676c326"
});
  • getWalletTransactions() – Query the native transaction history of any wallet: 
const response = await Moralis.EvmApi.transaction.getWalletTransactions({
  "chain": "0x1",
  "address": "0x1f9090aaE28b8a3dCeaDf281B0F12828e676c326"
});
  • getWalletNFTs() – get the NFT balance of any address: 
const response = await Moralis.EvmApi.nft.getWalletNFTs({
  "chain": "0x1",
  "address": "0xff3879b8a363aed92a6eaba8f61f1a96a9ec3c1e"
});

So, when using Moralis, you can effortlessly query ERC-20 tokens, transactions, NFTs, and much more from the Ethereum blockchain! 

If you liked this tutorial on how to query the Ethereum blockchain, consider reading more content here on the blog. For instance, check out our most recent guide on how to build on OP Mainnet. Also, if you want to start leveraging the full power of blockchain technology, don’t forget to sign up with Moralis. You can create an account for free, and you’ll gain instant access to our industry-leading Web3 API suite!



Source link