In week 5 ICONick update, we released some of the tech spec of INS, ICONick Name Service. This week, we are releasing our tutorial on deploying SCORE on ICON. Please note that this week’s update is more developer focused, so we will be using terms and languages that regular users might not be familiar with.

ICON is often refer as the Ethereum of South Korea. this tutorial is to teach people how to start a local node and deploy a SCORE contract on it.

Requirements

OS: MacOS

Language: Python 3.6

Library need to be installed:

RabbitMQ

LevelDB

Libsecp256k1

Set up Environment

$ brew install leveldb $ brew install rabbitmq

Open up an python virtualenv

$ mkdir "work" $ cd "work"

Open a folder and start an virtual machine

$ virtualenv -p python3 . $ source bin/activate

Before you can install t-bears kit, you will require to have following libraries:

(work)$ brew install automake (work)$ brew install pkg-config (work)$ brew install libtool (work)$ brew install libffi (work)$ brew install gmp

Now you can install ICON SCORE dev — tbears now!

(work)$ pip install tbears

After tbears successfully installed, now we can using tbears

(work)$ tbears -h

It will shows all the command tbears has

command start: Start tbears serivce stop: Stop tbears service deploy: Deploy the SCORE clear: Clear all SCOREs deployed on tbears service init: Initialize tbears project samples: Create two SCORE samples (standard_crowd_sale, standard_token) genconf: Generate tbears config files. (tbears_cli_config.json and tbears_cli_config.json) txresult: Get transaction result by transaction hash transfer: Transfer ICX coin. keystore: Create keystore file balance: Get balance of given address totalsupply: Query total supply of ICX scoreapi: Get score's api using given score address txbyhash Get transaction by transaction hash lastblock Get last block's info blockbyhash Get last block's info blockbyheight Get block's info using given block height sendtx Request icx_sendTransaction with user input json file call Request icx_call with user input json file.

In order to begin to deploy a SCORE contract, we need to start a local node first

Open another terminal

(work)$ brew services start rabbitmq (work)$ tbears start

We have start a local loopchain node, now lets try out how to deploy a SCORE contract on it

(work) $ tbears init abc ABCToken (work) $ tbears ls abc #these is the files inside abc folder abc.py __init__.py package.json tests

This command line is about to create a folder “abc” and create empty contract “ABCToken” inside it.

Take a look into this contract itself

from iconservice import * class ABCToken(IconScoreBase): def __init__(self, db: IconScoreDatabase) -> None: super().__init__(db) def on_install(self) -> None: super().on_install() def on_update(self) -> None: super().on_update() @external(readonly=True) def hello(self) -> str: print(f'Hello, world!') return "Hello"

lets trying deploy it on the local loopchain node

(work) $ tbears deploy -t tbears abc Send deploy request successfully. If you want to check SCORE deployed successfully, execute txresult command transaction hash: 0x5ff4fffab72117b66fdcf6d07578a58dc5f26c41bb29133ab5681cf058026dd9

if you deploy the contract right, the terminal will shows :

Send deploy request successfully.

If you want to check SCORE deployed successfully, execute txresult command

transaction hash: 0x5ff4fffab72117b66fdcf6d07578a58dc5f26c41bb29133ab5681cf058026dd9

Notice that even if the terminal shows that the deploy request successfully , it doesn’t mean that the contract is deployed successfully. So before we can interact with the contract, one more thing is still needed to be done.

We can use tbears command: txresult, to see if the contract has already deployed onto the chain.

(work) $ tbears txresult 0x5ff4fffab72117b66fdcf6d07578a58dc5f26c41bb29133ab5681cf058026dd9 Transaction result: { "jsonrpc": "2.0", "result": { "txHash": "0x5ff4fffab72117b66fdcf6d07578a58dc5f26c41bb29133ab5681cf058026dd9", "blockHeight": "0x50b", "blockHash": "0x99c767c15cf9804534557b45ea0b946f34fc7f6466dc9ed6382c93ab13bb4cf0", "txIndex": "0x0", "to": "cx0000000000000000000000000000000000000000", "scoreAddress": "cx6bd390bd855f086e3e9d525b46bfe24511431532", "stepUsed": "0x10a7c", "stepPrice": "0x0", "cumulativeStepUsed": "0x10a7c", "eventLogs": [], "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "status": "0x1" }, "id": 1 }

The “status”: “0x1” shows that we have successfully deployed this contract!

As we can see the contract address on loopchain is begin with cx

"scoreAddress": "cx6bd390bd855f086e3e9d525b46bfe24511431532"

Let’s use other tbears command scoreapi to see what kind of functions this contract has

(work) $ tbears scoreapi cx6bd390bd855f086e3e9d525b46bfe24511431532

And the result:

(work) johnnyhsieh@Johnnyde-MacBook-Pro-3:~/desktop/work/t-bears$ tbears scoreapi cx6bd390bd855f086e3e9d525b46bfe24511431532 SCORE API: [ { "type": "fallback", "name": "fallback", "inputs": [] }, { "type": "function", "name": "hello", "inputs": [], "outputs": [ { "type": "str" } ], "readonly": "0x1" } ]

The api of this has only one called “hello”, and this function doesn’t require input parameters.

Sounds easy, but how does it works?

According to ICON’s example, we need to create a json file, then use the tbears’ call function to interact with the contract.

Here is the json file that is created for testing this contract.

{ "jsonrpc": "2.0", "method": "icx_call", "params": { "from": "hxef73db5d0ad02eb1fadb37d0041be96bfa56d4e6", "to": "cx6bd390bd855f086e3e9d525b46bfe24511431532", "dataType": "call", "data": { "method": "hello", "params": { } } }, "id": 1 }

“jsonrpc” default is 2.0, method using “icx_call”, this setting can’t be changed

“params”: fill in certain style:

"from": "your address", "to": "contract address", "dataType": "call", "data": { "method": "function name", "params": { } }

lets init “hello” function with .json

(work) johnnyhsieh@Johnnyde-MacBook-Pro-3:~/desktop/work/t-bears$ tbears call hello.json

And we will get result :

response : { "jsonrpc": "2.0", "result": "Hello", "id": 1 }

Thank you for reading.