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, if I need it to be ready to work I’ve to wait for syncing with the network, which takes a lot of time (~ 1 hour in my case) which I can’t tolerate every time I am getting down the client or get disconnected for some network issues.
- Sometimes I am disappointed even after syncing with the network, it takes a lot of time to see my transaction receipts.
While scanning through the GitHub page of web3j, I came across Infura which provides Ethreum networks as a Service to connect with the main or test networks with just a click.
What is Infura?
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 on which network it will run
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 from (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 like 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 which provides dApps integration with Ethereum clients. Here is an example code 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!!