Let's get busy setting up the development environment for writing a smart contract on ICON. By Espanicon
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:
- Install OpenJDK, go, and goloop. Follow these instructions to install goloop (this has instructions for OpenJDK, go and goloop).
- Install gradle
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.
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:
After creating the workspace, you need to create one build.gradle file at the root of the project with the following code.
Edit the app/build.gradle file to have the following data:
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:
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:
Compiling the smart contract
To build the project and have it ready to deploy into the ICON Blockchain we run the following commands:
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:
The file that we are going to deploy into the ICON Blockchain is poll-contract-optimized.jar.
Further Resources
- Smart contracts in the official docs: https://docs.icon.community/icon-stack/smart-contracts
- Java score examples: https://github.com/icon-project/java-score-examples
- Javaee scorex: https://github.com/icon-project/javaee-scorex
- javaee module source: https://github.com/icon-project/goloop/tree/master/javaee/api/src/java/score
- Chain score sample: https://github.com/icon-project/goloop/blob/master/testsuite/java/foundation/icon/test/score/ChainScore.java
- javaee api docs: https://www.javadoc.io/doc/foundation.icon/javaee-api/latest/index.html
- javaee unittest api docs: https://www.javadoc.io/doc/foundation.icon/javaee-unittest/0.9.7/index.html
- javaee unittest source: https://github.com/icon-project/javaee-unittest