Smart contracts data reading, introduction to Web3 development

Published by Jorge on

Futuristic city NFT

One of the benefits of the blockchain is that all data is freely available. To create web3 applications it’s necessary to ask the state of smart contracts to the blockchain. If all of this sounds obscure to you, don’t worry, I will guide you through the concepts to let you understand what is required and how to think about it.

In this article, you will learn how data is read from the blockchain, what tools are required, how to think about interacting with the blockchain and some resources to get you started.

Understanding the Ethereum system and interfacing requirements

To read data from the blockchain, first of all, you need to connect to it. There are two possible approaches: having your node or using an external one.

The usual approach is using an external one. Setting up a node is a relatively complex task and resource-intensive. You need also to keep it updated with the latest node periodically so you require a server connected to the blockchain, not very practical.

There are external providers such as Infura and Alchemy that offer a generous free tier that will help you to get started with the blockchain. We’ll call them just providers. To get a provider, you need their API endpoints and API keys by registering on their platforms. In your mental model, you could think of them as API providers to an external database (the blockchain) with the particularity that all people have root access to it.

Ok, so now you have a provider. How do you actually read the data?

Ethereum data has a standardized interface JSON-RPC but it’s not very practical to use it for web3 developers. There are javascript libraries that you can use that abstract away the JSON-RPC interface and provides an easier way to interface with the blockchain. Personally, I recommend ethers, but web3js will also work.

Putting together the smart contract details

So, now you have a provider and a web3 library. Now you need to know what data do you actually want to read. Generally, you will want to read data from a specific smart contract of your interest. I.e. supply of the DAI stablecoin.

Before getting into the smart contract, you need to know two major details: network and address. The network is usually Ethereum mainnet which would be like a “production Ethereum” network. But could be the case that you are using for some reason another EVM-compatible blockchain or a testnet. Some examples of EVM-compatible blockchains are Matic or BSC. They would work the same way, you just need to use the appropriate provider endpoint.

After that, you need to know where is the smart contract deployed (which address it has). Generally, all dApps provide you the address of the smart contracts. For example, if you’re using Uniswap for exchanging a token, the token address is provided. If not, you can search it on Coingecko for example. That’s the address where the token smart contract is deployed.

So now you have a provider to connect to the correct blockchain network (i.e. Ethereum mainnet) and the smart contract address. Now, you need the ABI (application-binary interface).

All smart contracts have an ABI in JSON format. The ABI provides the smart contract interface details for interacting with it. You can think of it as including a header file or importing a javascript module. In practice, you can check the smart contract in etherscan.io (a block explorer that indexes block-related data from the blockchain) and download the JSON ABI there by going to the code tab.

Another way of checking if one interface is available is through the interfaceSupported call that checks through the interface ID if it’s available in the contract. If you already know the interface ID and the smart contract implements the interfaceSupported call, you could use that programmatically.

Reading the data

All ready. You have all needed for reading the data of the smart contract instance. Now you just need to code a little to connect to the provider and use the web3 API for calling the desired interface. You could now start creating scripts for downloading specific data from the blockchain.

To get started, I recommend the getting started tutorial of ethers. There you will find sample codes for what I just described. If you’re familiar with javascript and web development, it won’t take you more than 30 minutes to have something working.

If you’re interested in learning more about Solidity and web3 development I recommend cryptozombies.io course as a hands-on introduction or follow my journey on Twitter while I develop stuff.

Summary

The blockchain is similar to a database. You need a provider to get an API endpoint to connect to the blockchain. To read data from the smart contract you need to know the network and smart contract deployment address. Using any web3 library and downloading the smart contract ABI you can call any smart contract interface.


The cover image is an NFT: The Other Side from @jarvinart.


If you want to read more like this, subscribe to my newsletter or follow me on Twitter

    Categories: Web development