Java Tutorial Part 1: Setting Development Environment and Writing Smart Contract
Tech Deep Dives

Java Tutorial Part 1: Setting Development Environment and Writing Smart Contract

год назад

Let's get busy setting up the development environment for writing a smart contract on ICON. By Espanicon

Java Tutorial Part 1: Setting Development Environment and Writing Smart Contract

Содержание

Espanicon is a validator on the ICON network. Their goal is to participate in the chain governance of the network and expand the ICON ecosystem by building projects in it and collaborating with other validators.

Introduction

A smart contract is a self-contained program that is stored and replicated on a blockchain network. When a contract is deployed to the blockchain, it becomes a part of the blockchain and is stored on every node in the network.

In the following guide we are going to explain in detail the process of:

  • Setting up the development environment for writing a smart contract on ICON
  • Writing a sample smart contract that will work as a poll for people to cast a vote “Yes” or “No”.
  • Compile, optimize and deploy the smart contract.
  • And finally we are going to interact with the smart contract via RPC calls.

Setting up the development environment

Prerequisites

For setting the development environment we need to install the following programs:

For the smart contract deployment you can either choose to deploy on a testnet in the ICON Blockchain or run a local network.

If you want to deploy to a testnet you will need to have ICX in your selected testnet, for that you can use the following faucet:

For setting up a local network, you can follow this guide

Creating the project workspace with gradle

We are going to be working on a folder named poll-contract inside our home folder, you can use any folder of your choice in your computer.

-> View code here.
Inside the folder we are going to initialize a project using gradle. If you are unfamiliar with gradle please refer to their documentation.
-> View code here.

The selected options are the following:

  • Select type of project to generate: 2
  • Select implementation language: 3
  • Split functionality across multiple sub-projects?: 1
  • Select build script DSL: 1
  • Generate build using new APIs and behavior (some features may change in the next minor release)? (default: no) [yes, no] yes
  • Select test framework: 4
  • Project name (default: poll-contract): poll-contract
  • Source package (default: poll.contract): poll.contract

The following folder structure will be created:

-> View code here.
The build.gradle file is used to create build scripts. In ICON, we use it to specify dependencies and write tasks.

After creating the workspace, you need to create one build.gradle file at the root of the project with the following code.

-> View code here.
This specifies the project to use the gradle javaee plugin which is specifically created for smart contract development in ICON. Each subproject (in above tree structure, app folder) would have its own build.gradle file. There are 2 additional tasks you would need to add to optimize and deploy the jar file.

Edit the app/build.gradle file to have the following data:

-> View code here.

In the optimizedJar task, mainClassName refers to the class which would be the entry point for the smart contract.

To avoid errors during compilation edit the app/src/test/java/poll/contract/AppTest.java file to have the following code:

-> View code here.

Writing and compiling the smart contract

For this tutorial we are going to be creating a smart contract that would work as a poll, it allows for addresses to vote either “yes” or “no” and has methods to check an address vote and the vote count. After creating the smart contract we compile it, optimize the code for the ICON Blockchain and then deploy it to a local network in our computer.

Writing the smart contract

Edit the app/src/main/java/poll/contract/App.java to have the following code:

-> View code here.

Compiling the smart contract

To build the project and have it ready to deploy into the ICON Blockchain we run the following commands:

-> View code here.

After running these commands the folder ./app/build/libs/ (along with an assortment of other files and folders) will be created by gradle with the following content:

-> View code here.

The file that we are going to deploy into the ICON Blockchain is poll-contract-optimized.jar.

Further Resources

0 people liked this article

Статьи по теме

Crypto News
Trash Can: 5 Cryptocurrencies with Low Performance This Week
COINOTAG is closely examining the biggest altcoin losers in the entire cryptocurrency market, especially between April 21-28.
год назад
Crypto News
Bitcoin remains stable at $29,300 ahead of FOMC Meeting – Analysts speculate whether $35,000 or $...
After two days of volatility, Bitcoin’s daily trading volumes have surged past $30 billion showing strength. Investors will be keeping a watch on the Fed meeting ahead next week and are expec...
год назад
Blog
How We Are Supporting O2 With Their Journey Onboard The Digital Express
Emeldi provides an ‘omni channel’ CRM for all sales and support channels, which is supported by single consolidated customer and product catalogue repositories.
год назад
Crypto News
U.S. government is trying to save First Republic Bank – Here is how
The U.S. government has taken an active role in attempting to save First Republic Bank as private-sector efforts led by the bank’s advisers have not yet produced a deal. According to sources ...
год назад
Crypto News
$168M Worth of AVAX Tokens to be Unlocked in a Month
K33 researchers have removed Avax from their crypto index due to upcoming inflationary pressure. The post $168M Worth of AVAX Tokens to be Unlocked in a Month appeared first on Tokenist.
год назад
Crypto News
Turbos Prepares for Sui Mainnet with Testnet Launch of its Concentrated Liquidity DEX
Tortola`, BVI, April 28th, 2023, ChainwireTurbos Finance, a non-custodial decentralized exchange (DEX) enabling spot and derivatives trading on the Sui Network, has released a Permanent Testnet dep...
год назад