Web3 and Blockchain Development

3 steps to deploy a smart contract

Written by Ivaylo Kirilov | Jun 18, 2020 2:30:00 PM
As a Java developer, it can be a pain to build a system that interacts with Ethereum smart contracts. There are numerous challenges that you need to overcome, not to mention some understanding of what a blockchain is and how it works. The fact that smart contracts are generally written in other languages doesn’t help either. 
 
Wouldn’t it be nice if there were tools at hand that could streamline the process? This is where Epirus CLI comes in, a CLI tool which uses Web3j, the Java library for Ethereum. 

Challenges of working with smart contracts

Ethereum smart contracts are generally written in Solidity, although there are several newer languages like Vyper and Bamboo, and Flint. No matter what language, all smart contracts need to adhere to the specifications of the EVM (Ethereum Virtual Machine) which is similar in concept to the JVM but has many different properties. For example, numeric types are represented as 256 bit numbers in the EVM, while the JVM long (the biggest primitive numeric type) is only 64 bits long. 
 
To interact with a smart contract running in the EVM, one needs to know how to interact with the Ethereum blockchain. This requires sending transactions to a service that implements the Ethereum specification. 
 
There are three types of transactions that one could invoke:
  • a transfer of ether.
  • the deployment of a smart contract.
  • invoking a smart contract function. 
There is also the matter of paying for these transactions. As the blockchain is decentralised, one needs to provide an economic incentive for their transactions to be accepted by the blockchain. Web3j provides abstractions which make integrating with the Ethereum blockchain to work with smart contracts a breeze.

How Web3j can help

 

The diagram above shows a high-level overview of what Web3j does under the hood. Essentially, a transaction can be built using the Web3j API, and then behind the scene, Web3j will sign the transaction and make a request to an Ethereum client. Once the transaction has made it into a block, Web3j will return a transaction receipt which can be used to inspect the outcome of the transaction. For example, when a contract deployment transaction is sent, the transaction receipt will contain the address at which the contract was deployed. 

3 steps to deploy a smart contract

So, someone has dumped a bunch of smart contracts in your VCS and you've been given the task to deploy them on the blockchain. With some Web3j tooling, the scaffolding for working with smart contracts in Java can be generated. 
 
Step 1 is to install and use the Epirus CLI (linked) with a simple command:

 

 

curl -L https://get.epirus.io | sh

We will use the Epirus CLI to generate a Java Gradle project which will have some nifty dependencies built it:

epirus new

This command will take you through a number of interactive steps to set up your project. Alternatively, you could pass the options in via the command line as follows:

epirus new -n <project name> -p <package name> -s <path to solidity sources> [-o <path>] -t

You need to provide a name for the project, the package structure, and the path to the Solidity sources. The Solidity sources is a folder containing Solidity smart contracts. The `-o` option will create the project at the given path, by default it will the directory you run the command in. The `-t` option will generate unit tests for your smart-contracts. 
 
Once the project has been generated it can be opened in an IDE of your choosing.

 

 

 

Step 2 is to resolve your Ethereum client and fund your wallet. There are multiple options to do this. You can run your own Ethereum client and connect to any network, you can connect to an Ethereum client cloud provider like Infura - which manages Ethereum clients on your behalf, or for development purposes you can use an embedded Ethereum client which simulates a blockchain. Fortunately, the generated project will have a dependency on web3j-unit which is a JUnit5 extension that will inject an Ethereum client instance, either in process or in a Docker container. You can head over to the generated test to check it out. 
 
However, in this example, we will be using Chainlens blockchain infrastructure to deploy the contract to a live network. Go ahead and create a free account. After that let’s create a new project and grab the URL for the Rinkeby network. The Rinkeby network is a test network for Ethereum which is a great place to start deploying contracts.

 

 

Step 3 is to run the application. The Epirus CLI comes with a nifty project deployer. Head over to the directory where you generated the project and run: 

epirus deploy rinkeby

This command will take care of everything for you, from funding your testnet wallet to send a transaction to the Ethereum blockchain. The contract will be deployed on the Rinkeby testnet.

 

 

There will be some delay between transactions because we are deploying the application to a live network. Once the application finishes running you can head over to https://goerli.etherscan.io/

 

 

Stay tuned for the next post in this mini-series where we will be discussing how to manage and interact with smart contracts that have already been deployed on the blockchain.