Java Tutorial Part 2: Deploying the Smart Contract and Interacting with the Smart Contract Onchain
Tech Deep Dives

Java Tutorial Part 2: Deploying the Smart Contract and Interacting with the Smart Contract Onchain

3m
1yr ago

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

Java Tutorial Part 2: Deploying the Smart Contract and Interacting with the Smart Contract Onchain

Table of Contents

Deploying the smart contract

Deployment of the smart contract on an ICON Network (Mainnet, testnets or local networks) is done using a special contract creation transaction sent to the contract creation address.

A deployed contract is assigned an ICON address based on the originating account, timestamp, and if the contract address already exists nonce of the contract creation transaction is also used. Salt can also be used to create address. Salt is the transaction index of the transaction in the block. This address can be used to send funds to the contract or call its functions.

-> View code here.

Smart contracts are not associated with private keys like External Owned Accounts (EOAs). However, the smart contract deployer address is considered the “owner.” By default, smart contract owners may upgrade or change the owner of the contract.

-> View code here.
Although this means smart contracts by default are mutable, the contract can be written to specify immutability rules. To make a contract immutable, the owner of the contract address has to be changed to a wallet that no one controls. Some ways to do that are by changing the owner to the contract itself provided there is no method in the contract to do self-upgrade. The owner can also be changed to System contract - cx0000000000000000000000000000000000000000.

Smart contracts are only executed when they are called by a transaction, either directly or as part of a chain of contract calls. They do not run in the background or parallel, and they are single-threaded.

Deploying the smart contract on the terminal using goloop CLI

As shown in the previous RPC JSON example for the deployment of the smart contract we need to encode the compiled smart contract (jar file) into a hex string and also sign the RPC JSON with the private key of the wallet that we are going to assign as the owner of the smart contract. The easiest way to do this in the terminal is with the goloop CLI tool. The following command can be used to deploy the smart contract with the goloop CLI:

-> View code here.

Deploy using icon-sdk-js

We can also deploy the smart contract using the icon-sdk-js, for this example we are going to create a nodejs script to deploy our smart contract.

Inside our project root folder (~/poll-contract) lets create a nodejs project and install the icon-sdk-js package running the following commands:

-> View code here.

Create an index.js file and add the following code in it:

-> View code here.

Execute the file running node index.js and your contract will be deployed to the network.

Post deployment

The RPC call to deploy the contract will return a transaction hash, we need to query the network about the transaction information of that hash to verify that the transaction was processed correctly and get the contract address. This command will output a transaction hash that we can use to make a readonly call using the icx_getTransactionResult method and obtain the contract address:

-> View code here.

Result of the call:

-> View code here.

Interacting with the smart contract on the chain

When interacting with a smart contract a very useful method is the icx_getScoreApi. This readonly method will return the contract ABI which is a JSON formatted object that shows the methods of the contract with the inputs needed when calling each method and the resulting outputs after calling a method.

-> View code here.

The result of calling icx_getScoreApi to the contract we have just created would be the following:

-> View code here.

For calling the methods in a smart contract we have 2 main RPC JSON methods to use:

  • icx_call: for readonly methods (link).
  • icx_sendTransaction: for write methods. These calls require the RPC json to be signed using the private key. (link)

Further Resources

0 people liked this article