LoopChain with Score tutorial

Johnny Hsieh
Aug 31, 2018 · 4 min read

From Zero to Hero

ICON is also called the Ethereum of South Korea, and it just release the lastest features, 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 tbears kit, you also need some library:

(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

commandstart: Start tbears serivcestop: Stop tbears servicedeploy: Deploy the SCOREclear: Clear all SCOREs deployed on tbears serviceinit: Initialize tbears projectsamples: Create two SCORE samples (standard_crowd_sale,standard_token)genconf: Generate tbears config files. (tbears_cli_config.json andtbears_cli_config.json)txresult: Get transaction result by transaction hashtransfer: Transfer ICX coin.keystore: Create keystore filebalance: Get balance of given addresstotalsupply: Query total supply of ICXscoreapi: Get score's api using given score addresstxbyhash Get transaction by transaction hashlastblock Get last block's infoblockbyhash Get last block's infoblockbyheightGet block's info using given block heightsendtx Request icx_sendTransaction with user input json filecall 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 folderabc.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 abcSend deploy request successfully.If you want to check SCORE deployed successfully, execute txresult commandtransaction 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 thought terminal shows that the deploy request successfully doesn’t mean that we have already deploy the contract successfully.

So before we can interact with the contract, we still got one more thing need to be done.

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

(work) $ tbears txresult 0x5ff4fffab72117b66fdcf6d07578a58dc5f26c41bb29133ab5681cf058026dd9Transaction 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"

lets using other tbears command scoreapi to see what kind of function does this contract have

(work) $ tbears scoreapi cx6bd390bd855f086e3e9d525b46bfe24511431532

And the result:

(work) johnnyhsieh@Johnnyde-MacBook-Pro-3:~/desktop/work/t-bears$ tbears scoreapi cx6bd390bd855f086e3e9d525b46bfe24511431532SCORE 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 don’t need and input parameters.

Sounds easy, but how does it works?

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

Here is the json file that I create 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}

Johnny Hsieh

Written by

Co-founder of Portal Network,Founder of PXTag , Deep learning engineer,Blockchain developer ,Swift & Python engineer, Blockchain & Smart Contract developer.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade