Ethereum Middleware for Java

Sravan Kumar Katta
4 min readJan 10, 2022

--

Introduction

In the present era of web technology, we are highly reliant on the internet to seek information starting from a simple “how to turn a knob” to a complicated algorithm. The Web became a common utility in our daily lives. Adapting to the demands and requirements, the web evolved from version 1.0 to the current 3.0. Web 3.0 focuses on decentralization and data protection.

In the decentralization concept, data is stored in blocks which are connected each other using key. This series of joining the blocks and storing the data are named as Blockchain Technology. The companies/networks like Bitcoin, Ethereum are using this technology.

Discussing about the development process of Ethereum, there are two kinds of developer roles. Core Blockchain developers, who are involved in upgrading the protocols of Decentralized applications. Another type is Blockchain software developers, they are involved in developing the (Decentralized Applications) Daaps with contracts to run on blockchain technology.

This article provides an introduction about Daaps development under Ethereum using the traditional programming language Java.

Architecture

Web3j Middleware for Java Application

The core element to start working with Ethereum is Smart Contract. The interaction and data storage in Ethereum is done based on the Smart contract, which uses Solidity programming language. But to use it as a complete Daap, we need other features like Authentication, User Interface etc. For this reason, it should be converted into the other programming language that satisfies the requirements.

Using web3j package, the wrapper class is built in Java. This will act as a middleware for interacting with Ethereum. Installation details can be found here.

Solidity

Solidity is an Object-oriented programming, which is used to develop Daaps. This can be done with a simple notepad by following the installation that is mentioned here. But to make the process quick, in this article we are following the Remix ide, which is ready for the programming environment.

Sample Helloworld smart contract can be found here.

After completing the smart contract development, it is supposed to compile the code to generate Application Binary Interface (abi) and Binary (bin) data, which are used in creating the wrapper class. Collect the abi and bin code and place them in separate files as Helloworld.abi and Helloworld.bin.

ABI and ByteCode location.

Below command will be used to generate the wrapper class using the above files that are mentioned.

web3j generate solidity -a=Helloworld.abi -b=Helloworld.bin -o=output_directory -p=java_package

Hint: After generating the wrapper class, the BINARY variable is filled with json that contains functionDebungData, generateSources, linkReference, object variables etc. Replace complete json with object variable value alone. This will avoid errors while deploying the contract.

Place the wrapper class inside the Java Application. To start using the wrapper class, we need to collect ethereum network url, account key and private key. After successfully creating an account on ethereum network all these information will be provided. But for deploying the contract and doing operation ether needs to be spent, which is an expensive one. For the development process, we use a test network (Rinkby) using the wallet named Metamask. It is available as a browser extension.

Metamask

After collecting all the information, initialize for connecting to the network using the below command.

Web3j web3j = Web3j.build(new HttpService(network_url));

Credentials credentials = Credentials.create(private_key);

Hint: Use web3j and credentials objects as public variables, as they are used in every call.

Deploying contract:

By default wrapper class provides methods for deploying and loading back the contract.

HelloworldClass helloworld = HelloworldClass.deploy(web3j, credentials, (new ContractGasProviderClass)).send();

helloworld object by default contains getAddress() method that contains the contract address, which is also used in loading the contract.

Hint: If we can store the contract address in Database, we can just load the contract without re-deploying it again.

HelloworldClass helloworld = HelloworldClass.load(helloworld.getAddress(), web3j, credentials, (new ContractGasProviderClass));

After loading/deploying the contract, the rest of the interaction with methods that are defined in the contract is done by using the HelloworldClass object. For instance, there is a method getName() inside the contract. It is interacted as below.

String name = helloworld.getName().send();

As in the contract, getName() method contains the return type as String, the value that responds can be assured as String. But if there is no return type like setName() method, then by default the response would be in TransactionReceipt. The method send() is to specify request to contract.

TransactionReceipt transactionReceipt = helloworld.setName(“Jerry”).send();

TransactionReceipt contains the complete log information about the transaction. To get an overview of the transaction log, here is an example.

--

--