Note

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:

Recap

In our previous article (How to Use Remix with Infura for Ethereum Development), we discussed how to use Infura with Remix for building Ethereum blockchain applications.

In this article, we learn about Ethereum client API.

The Ethereum client API

The Ethereum client API allows you to connect to the Ethereum node and send a transaction to the blockchain. Through the Ethereum client, you can create and run a smart contract. There are many client libraries that have been written by different programming languages. Here is a list of some of them:

Ethereum client API Programming language
web3.js JavaScript Web3
ethers.js Similar to JavaScript Web3
EthereumJS Provide collections of utility functions
Web3.py Python web3 API
web3j Java web3 API
web3.php PHP web3 API
Nethereum .net Web3 API
ethereum.rb Ruby Web3 API
Web3.hs Haskell Web3 API
KEthereum Kotlin Web3 API
Ethereumex Elixir JSON-RPC client for the Ethereum blockchain

Java, as one of the most popular and influential programming languages, has a large number of developers and programmers. The Ethereum Web3j library allows Java developers to interact with the blockchain. In the next section, we will discuss the Web3j API. If you are not familiar with Java, you can skip the next section.

 

One-to-One Live Blockchain Classes

Coding Bootcamps school offers One-to-One Live Blockchain Classes for Beginners.

 

Working with Web3j

Web3j is a Java client library that can connect to an Ethereum node. Similar to Web3 JavaScript, it can be used to interact with a smart contract on the Ethereum node. It can generate a Java smart contract from Solidity contract files.

First, let’s set up the Web3j environment, as shown in the following steps:

1. Install the Solidity compiler, solc, and run the following command to install it:

npm install  -g solc

2. Install web3j using the run brew command (at the time of writing, we used web3j-4.2.0):

brew tap web3j/web3j brew install web3j

Once solc and web3j have been installed, you will see the following screen:

 Ethereum blockchain development in Infura

3. Compile and translate the smart contract into a Java class. We will use the helloWord smart contract as an example. Here is the smart contract:

pragma solidity  >=0.4.22 <0.6.0; contract  helloWorld {
  function getMessage() public pure returns  (string memory) { return "Hello World!";
  }
  }

4. Run the solc command, as shown here, to generate the bin and abi files:

solcjs helloworld.sol --bin --abi --optimize -o compile/

Once the command runs successfully, you will see that the following two files are generated:

helloworld_sol_helloWorld.abi helloworld_sol_helloWorld.bin

5. Generate a Java class from the smart contract by running the following command:

web3j  solidity generate --javaTypes -b helloworld_sol_helloWorld.bin -a helloworld_sol_helloWorld.abi -o .
  -p com.packt.learnethereum

The result of the preceding command will look as follows:

 Ethereum blockchain development in Infura

 

Copy the generated Java class into a Java project. In the next step, we will build out the Java Maven project.

6. Set up a Web3j Maven project. Use your favorite Java IDE (Eclipse or IntelliJ) to create a Maven project and add the following dependency to the Maven project. We used version 4.2.0 in this example:

<!-- https://mvnrepository.com/artifact/org.web3j/core -->
  <dependency>
  <groupId>org.web3j</groupId>
  <artifactId>core</artifactId>
  <version>4.2.0</version>
  </dependency>

 

This will load the Web3j library into your project.

Deploy a smart contract. In our example, we will deploy the helloWorld smart contract to Infura through MetaMask.

7. In MetaMask, select Inject web3 as Environment and connect MetaMask to the Infura Ropsten test network. Then click Deploy. The deployed contract address can be found for the deployed contract by clicking the copy icon, as shown here:

 Ethereum blockchain development in Infura

 

When clicking on Deploy, MetaMask confirms this in a popup. Check and click the Confirm button. This will deploy the contract to the remote Ropsten network. Here is a screenshot of confirming transactions:

 Ethereum blockchain development in Infura

 

Let’s get some basic blockchain information through the Web3j API:

  • Get web3ClientVersion through the Web3j API, as shown here:
Web3j web3j =  Web3j.build(new HttpService("https://ropsten.infura.io/v3/your key")); Web3ClientVersion web3ClientVersion = web3j.web3ClientVersion().send();

This will return the web3 client version. Web3j.build instantiates the Web3j instance. Then, you can use web3j.web3ClientVersion().send() to get the client version. The Web3j Infura API provides the specific Infura-Ethereum- Preferred-Client header. This allows the client to connect to the blockchain network through Infura.

You can get account-, balance-, and transaction-related information through the Web3j API as follows:

  • Get the block number with ethBlockNumber(), as shown here:
web3j.ethBlockNumber().sendAsync().get()
  • Get the account balance with ethGetBalance(), as shown here:
web3j.ethGetBalance(address, DefaultBlockParameter.valueOf("latest")).sendAsync().get()
  • Send 1 ether to another account with sendFunds(), as shown here:
Credentials credentials = Credentials.create("<<your acct private key>>");
  Transfer.sendFunds(web3j, credentials, "<<target acct address>>", BigDecimal.valueOf(1.0),
  Convert.Unit.ETHER).send()

 

  • Call a smart contract with load():

We can use the previous Web3j API to generate a Java class load method by passing the deployed contract address, Web3j instance, credential, and default gas provider. This will connect and invoke a remote contract and call-related contract method, as shown here:

Helloworld_sol_helloWorld  contract = Helloworld_sol_helloWorld.load("<<contract address>>", web3j,  credentials, new DefaultGasProvider());
  String message = contract.getMessage().send();

Once all of the preceding Web3j functions have been executed, you will see the following output:

 Ethereum blockchain development in Infura

 

We just reviewed the Web3j Ethereum API. As a Java developer, I hope you now feel comfortable writing some smart contract client APIs to interact with Ethereum.
Next, we will move on to another important topic—Ethereum decentralized storage.

Next Article
In our next article (How Ethereum IPFS Storage Works), we discuss Ethereum IPFS distributed storage.

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.

Resources

Free Webinars on Blockchain

Here is the list of our free webinars that are highly recommended:

 

Free Courses

Here is the list of our 10 free self-paced courses that are highly recommended:

 

Self-Paced Blockchain Courses

If you like to learn more about Hyperledger Fabric, Hyperledger Sawtooth, Ethereum or Corda, taking the following self-paced classes is highly recommended:

  1. Intro to Blockchain Technology
  2. Blockchain Management in Hyperledger for System Admins
  3. Hyperledger Fabric for Developers
  4. Intro to Blockchain Cybersecurity
  5. Learn Solidity Programming by Examples
  6. Introduction to Ethereum Blockchain Development
  7. Learn Blockchain Dev with Corda R3
  8. Intro to Hyperledger Sawtooth for System Admins

 

Live Blockchain Courses

If you want to master Hyperledger Fabric, Ethereum or Corda, taking the following live classes is highly recommended:

 

Articles and Tutorials on Blockchain Technology

If you like to learn more about blockchain technology and how it works, reading the following articles is highly recommended:

 

Articles and Tutorials on Ethereum and Solidity

If you like to learn more about blockchain development in Ethereum with Solidity, reading the following articles and tutorials is highly recommended:

 

Articles and Tutorials on Hyperledger Family

If you like to learn more about blockchain development with Hyperledger, reading the following articles and tutorials is highly recommended:

 

Articles and Tutorials on R3 Corda

If you like to learn more about blockchain development on Corda , reading the following articles and tutorials is highly recommended:

 

Articles and Tutorials on Other Blockchain Platforms

If you like to learn more about blockchain development in other platforms, reading the following articles and tutorials is highly recommended: