SmartContract deployment & integration with JAVA SpringBoot

Sourabh Parsekar
CoinsBench
Published in
4 min readApr 1, 2022

--

Oracle says there are 45 Billion active Java virtual machines globally. The question for this blog is —

Is it possible that an Ethereum SmartContract written in Solidity can be deployed and tested with Java ? — YES…

Let’s explore how we can do this in JAVA. It’s easy but not that easy. If you are looking for details on what a SmartContract is or how you can write one, then refer my earlier exploration blog ERC20 — Smart Contract React App

Repository

Let’s start our Java Exploration — The code for this proof of concept can be found in my GitHub repository. For this blog, I have used Web3J Plugin 4.8.7 with Gradle 6.9 to deploy and interact with Smart Contract. Other configuration details can be found in build.gradle.

You can refer to Web3J documentation for how it can be done for Maven Project.

Directory Structure and Gradle Configuration

Below is the directory structure I chose

SmartContract project directory structure
SmartContract Project Directory

The solidity compiler, node configurations are to be added to build.gradle file. For this project, we also need the Solc compiler installed or configured. I have used windows .exe compiler configured in build.gradle. Similarly, node configurations are required to download the node_modules and compile the SmartContract.

Note: okhttp dependency is required as the default one might not work

Gradle Task

Having done the configurations, we can now copy the Solidity contract to solidity folder. Web3J plugin provides us tasks to generate JAVA Wrappers for Smart Contract. After applying the plugin, the base directory for generated code `$buildDir/generated/sources/web3j` will contain a directory for each source set (by default main and test) containing the smart contract wrappers Java classes.

Gradle Plugins and Tasks
Gradle Plugins and Tasks

SmartContract deployment

I have used Swagger to generate a bare minimum UI so that we can deploy and test the SmartContract via Rest APIs. For interacting, we would require the Private Key of the wallet to generate credentials and the Ethereum node URL to initiate the Web3J. For details on Swagger you can refer to below blogs

Open API Specification — Swagger3

OpenAPI Specification — Swagger Authentication

You can use Ganache for local/dev environment or use Alchemy/Infura to generate HTTP Node url in staging and prod environment. You can find more details about it in ERC20 — Smart Contract React App blog.

Below code snippet describes how the SmartContract would be deployed as a standalone program. Token referred in below snippet is the SmartContract token generated via the gradle task.

SmartContract deployment

SmartContract interactions

Now that we know how to deploy the SmartContract, let’s try to interact with the deployed SmartContract to fetch the Token balance for any wallet address.

Instead of deploying the Token we would now load it using the deployed contract address. We then would get the instance of the contract and can query any of the functions available in the SmartContract.

Below is a code snippet to understand how we can fetch the balance for a given wallet address.

Refer to the controller package to understand the other interactions with the deployed SmartContract using Web3J Core library. In case of any issues with the code we can always connect in comments or my mail is available in the github repo as well.

Conclusion

As we come to the end of this short introduction on how we can deploy and integrate with Ethereum SmartContract with JAVA, some items to be kept in mind are

  1. the Gradle tasks to generate wrappers are not compatible with Gradle 7, so we have to use Gradle 6 version.
  2. in addition to Gradle, okhttp dependency 4.9.3 needs to be added as the latest might not compatible with web3j plugin
  3. lastly, the Web3J version used in 4.8.7. This code may not work in other versions.

Basically, if your code does not work, then check the configurations.

Hope, this blog makes you curious to get your Ethereum address and private key if you do not have it already.

As always, do leave your feedback, queries, improvements or shower some claps. For any doubts or issues in setting up your local environment, we can connect via email or comments below.

Until next time, happy coding!!

--

--