How Ethereum Blockchain Works!
If you are new to the blockchain technology, taking our Introduction to Blockchain Technology self-paced course is highly recommended. Also, for a comprehensive coverage of blockchain development in Ethereum or mastering Solidity programming, taking our below self paced courses is highly recommended:
In our previous article (Review of Infura for Ethereum Development), we reviewed one of popular Ethereum development tools called Infura which provides a collection of full node interfaces that allow the client application to access the Ethereum network.
In this article, we learn how Infura Ethereum API works.
You can build and invoke Ethereum nodes through the Infura load-balanced nodes by calling the Ethereum API. Just write a regular JSON RPC over both HTTPS and WebSockets code; you can use popular Ethereum client libraries and frameworks to access node information.
Let's create a simple Infura DApp to get account and node information from the Gorli testnet; we will use web3.js for this. In our Infura DApp, we will use the newer Gorli test network. Ropsten is not immune to spam attacks, causing the gas prices to inflate due to its 51% attack rate. Gorli is based on the clique consensus and is supported by multiple clients.
Before we start exploring the Ethereum API for Infura, let's get some ether in the Gorli testnet. As shown in the following screenshot, you can get ether and send ether requests from https://goerli-faucet.slock.it/:
Now that we have ethers in our wallet account, it is time to write some simple Infura Ethereum APIs to experience how to interact with the blockchain. Let's write a simple HTML script, as follows:
<html>
<header>
<title>Infura</title>
<script
src="https://cdn.jsdelivr.net/gh/ethereum/web3.js@1.0.0-beta.36/dist/web3.m in.js" integrity="sha256-nWBTbvxhJgjslRyuAKJHK+XcZPlCnmIAAMixz6EefVk=" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<script>
if (typeof web3 !== 'undefined') {
web3 = new Web3(web3.currentProvider);
} else {
// Set the provider you want from Web3.providers
web3 = new Web3(new Web3.providers.HttpProvider("https://goerli.infura.io/v3/<your infura projectID>"));
}
</script>
</header>
</html>
First, we load the web3.min.js and jQuery libraries and then set up providers. In this case, we have MetaMask as our current provider and we connect to the Gorli testnet. The project provides Gorli Infura address information. You can select other environment Infura links as well. This is shown in the following screenshot:
<body>
<div>
<h2>Latest Block</h2><span id="lastblock"></span>
</div>
<script>
web3.eth.getBlockNumber(function (err, res) { if (err) console.log(err)
$( "#lastblock" ).text(res)
})
</script>
</body>
The preceding code will connect to Infura through web3.js and return the most recent block number from the blockchain. We use the web3.getBlockNumber API to achieve this. In the web page, the block number will be displayed on the screen, as shown here:
Similarly, we can use the web3 API to get the balance and query other nodes' information. Here is the code for this:
web3.eth.getBalance("your metamask account address", function (err, res) { if (err) console.log(err)
$( "#balance" ).text(res)
})
web3.eth.getNodeInfo(function (err, res) { if (err) console.log(err)
$( "#nodeInfo" ).text(res)
})
The getBalance() function returns the current account balance, while getNodeInfo()
returns node information. The UI information is shown here:
We just saw how easy it is to use the web3 API, to connecting with the Infura blockchain environment. Remix also provides very easy integration with Infura. Let's have a look.
Next Article
In our next article (How to Use Remix with Infura for Ethereum Development), we discuss how to use Infura with Remix for building Ethereum blockchain applications.
This article is written in collaboration with Brian Wu who is a leading author of “Learn Ethereum: Build your own decentralized applications with Ethereum and smart contracts” book. He has written 7 books on blockchain development.
Here is the list of our free webinars that are highly recommended:
Here is the list of our 10 free self-paced courses that are highly recommended:
If you like to learn more about Hyperledger Fabric, Hyperledger Sawtooth, Ethereum or Corda, taking the following self-paced classes is highly recommended:
If you want to master Hyperledger Fabric, Ethereum or Corda, taking the following live classes is highly recommended:
If you like to learn more about blockchain technology and how it works, reading the following articles is highly recommended:
If you like to learn more about blockchain development in Ethereum with Solidity, reading the following articles and tutorials is highly recommended:
If you like to learn more about blockchain development with Hyperledger, reading the following articles and tutorials is highly recommended:
If you like to learn more about blockchain development on Corda , reading the following articles and tutorials is highly recommended:
If you like to learn more about blockchain development in other platforms, reading the following articles and tutorials is highly recommended:
We offer private custom tutoring classes both online and in DC, MD and VA for almost all of our courses or bootcamps. Give us a call or email us to discuss your needs.
$90 Regular
$50 Limited Offer
REGISTER NOW