Let's get busy setting up the development environment for writing a smart contract on ICON. By Espanicon
Deploying the smart contract
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.
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.
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:
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:
Create an index.js file and add the following code in it:
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:
Result of the call:
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.
The result of calling icx_getScoreApi to the contract we have just created would be the following:
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
- 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