Introducing Infura: Connecting DApps With Ethereum Without Setting up Ethereum Nodes
Want to learn more about how we can connect DApps with Ethereum without having to set up nodes? Check out this tutorial to learn how in Java.
Join the DZone community and get the full member experience.Join For Free
If you’ve been working on decentralized applications based on Ethereum, you might have come across setting up the Local Ethereum Node or your own private Ethereum node to test your application. In my case, setting up Ethereum nodes and connecting DApps with it has been a problem for me for the following reasons:
- If I use any of the Ethereum clients (geth or parity) to connect to networks or I need it to be ready to work, I have to wait for syncing with the network, which approximately one hour, in my case.
- Sometimes, I am disappointed even after syncing with the network, it takes a lot of time to see my transaction receipts.
What Is Infura?
Infura is a platform as a service for Ethereum networks. Using Infura, you can connect to Ethereum Test or Main networks by exposing an URL.
When to Use Infura?
1. If you want to connect your DApp with any of the Ethereum networks (test or Main) without setting the Ethereum Network
2. If you want to focus only on the application features for a while without having to care about which network it will run on
Steps to Use Infura to Connect With Ethereum
- Visit https://infura.io/ and click “GET STARTED FOR FREE” and that will lead you to sign up page. Complete the signup and a dashboard will appear.
- Click on “CREATE NEW PROJECT” and provide a name. It will create a project like below:
- As you can see in the side panel, there is a dropdown called ENDPOINT, which is there to choose between different kinds of public network available on Infura.
- To get the testnet endpoint, choose one of the testnets (ROPSTEN, RINKEBY, KOVAN) as per your use case. Since I’ve got an account on Ropsten, I chose “ROPSTEN.”
- Select the endpoint URL displayed after you select a network in the dropdown as highlighted below:
Once you have the URL, you can create a client using web3j to interact with the network. Web3j is a reactive and functional API that provides DApps integration with Ethereum clients. Here is an example in Scala where you can create an HTTP web3j client:
Web3j web3 = Web3j.build(new HttpService("https://rinkeby.infura.io/")); Web3ClientVersion web3ClientVersion = web3.web3ClientVersion().send(); System.out.println(web3ClientVersion.getWeb3ClientVersion());
The above code snippet is a simple example of creating a web3j client in Java. In the coming blogs, I will be discussing how we can use web3j with Scala and Play to create a simple DApp. If you are feeling curious, you can visit https://github.com/knoldus/play-ether-scala for a quick reference. Thanks for reading! Stay tuned!!
Published at DZone with permission of Manish Mishra, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.