[ICON] Deploy Your First Smart Contract on ICON Testnet

Jay | Human Being
Cypher Core
5 min readOct 15, 2018

--

- Install ICONex Wallet

- Switch Wallet Network to Testnet

  • Press F12 to open Chrome DevTools window
  • Navigate to Application->Storage->Local Storage
  • Add a new key/value pair isDev/true
  • Exit Chrome DevTools window and refresh the wallet page
  • Find the MAINET button on the bottom right corner. Click on it and select YEOUIDO.

- Set Up Docker

  • Install Docker via command line
$ curl -sSL https://get.docker.com | sh
  • Check installation
$ docker version
Client:
Version: 18.06.1-ce
API version: 1.38
Go version: go1.10.3
Git commit: e68fc7a
Built: Tue Aug 21 17:24:56 2018
OS/Arch: linux/amd64
Experimental: false
Server:
Engine:
Version: 18.06.1-ce
API version: 1.38 (minimum version 1.12)
Go version: go1.10.3
Git commit: e68fc7a
Built: Tue Aug 21 17:23:21 2018
OS/Arch: linux/amd64
Experimental: false
$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
ca4f61b1923c: Pull complete
Digest: sha256:ca0eeb6fb05351dfc8759c20733c91def84cb8007aa89a5bf606bc8b315b9fc7
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.
...

- Run Docker Container

  • Download Docker Image

The docker images contains ICON SCORE development suite (T-Bears), a Dice Roll DApp and all necessary software (e.g. python3, ICON python SDK etc.)

$ docker pull ibriz/icon-workshop
$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
ibriz/icon-workshop latest a09603ecd862 9 days ago 610MB
smartcontract/devnet latest dd5692e4de40 2 months ago 370MB

Make sure ibriz/icon-workshop is listed on there.

  • Run the container
$ docker run -it -p 127.0.0.1:9000:9000 -p 127.0.0.1:5000:5000 --rm ibriz/icon-workshop:latest
* Starting RabbitMQ Messaging Server rabbitmq-server [ OK ]
Started tbears service successfully

- Start T-Bears

Open a new terminal window.

  • Look up and copy the container id. Mine is db3fb5900ede here.
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
db3fb5900ede ibriz/icon-workshop:latest "/usr/local/bin/entr…" 2 minutes ago Up 2 minutes 127.0.0.1:5000->5000/tcp, 127.0.0.1:9000->9000/tcp happy_bohr
  • Open up /tbears directory
$ docker exec -it <container-id> /bin/bash
/tbears#

- Deploy to Local T-Bears Instance

  • Deploy with test account
$ cd icon-dice-roll/
$ tbears deploy dice-roll -k keystores/keystore_test1.json -c config/tbears_cli_config.json
Input your keystore password:
#password: test1_Account
$ tbears deploy dice-roll -k keystores/keystore_test1.json -c config/tbears_cli_config.jsonSend deploy request successfully.
If you want to check SCORE deployed successfully, execute txresult command
transaction hash: 0xc1956ca841ec57881e405147dba6c5b1d283d67916c06ba01fd2a9422bd51fdc
$ tbears txresult <txhash>Transaction result: {
"jsonrpc": "2.0",
"result": {
"txHash": "0xc1956ca841ec57881e405147dba6c5b1d283d67916c06ba01fd2a9422bd51fdc",
"blockHeight": "0x22",
"blockHash": "0xc653fad7bede10a986b4c8bd72338b1ab11ed7939eca7e9c974343f4336deb58",
"txIndex": "0x0",
"to": "cx0000000000000000000000000000000000000000",
"scoreAddress": "cx39ea6e695672c62475794c803f103b4425e73a03",
"stepUsed": "0x2bf20c8",
"stepPrice": "0x0",
"cumulativeStepUsed": "0x2bf20c8",
"eventLogs": [],
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"status": "0x1"
},
"id": 1
}
  • Save the scoreAddress
  • Edit send_set_treasury.json

Use a text editor of your choice. Here I am using Vim.

$ vi ./testcmdline/send_set_treasury.json

Replace the “to” attribute with the scoreAddress you saved earlier.

  • Request icx_sendTransaction with send_set_treasury.json
$ tbears sendtx -k keystores/keystore_test1.json -c config/tbears_cli_config.json testcmdline/send_set_treasury.jsonInput your keystore password:
#password: test1_Account
Send transaction request successfully.
transaction hash: 0x5ded065ac6a0fe3b214277fe6a37590f90634060e3037900fb10376a1693ee4b
  • Check transaction result
$ tbears txresult <txhash>Transaction result: {
"jsonrpc": "2.0",
"result": {
"txHash": "0x5ded065ac6a0fe3b214277fe6a37590f90634060e3037900fb10376a1693ee4b",
"blockHeight": "0xad",
"blockHash": "0xdd6722f7e152114a472a76a258f5efda78ba7e53dc3cd325b4f8f3707b0a614c",
"txIndex": "0x0",
"to": "cx39ea6e695672c62475794c803f103b4425e73a03",
"stepUsed": "0xf8638",
"stepPrice": "0x0",
"cumulativeStepUsed": "0xf8638",
"eventLogs": [],
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"status": "0x1"
},
"id": 1
}
  • Check balance of given scoreAddress
$ tbears balance <scoreAddress>balance in hex: 0x6f05b59d3b200000
balance in decimal: 8000000000000000000
  • Edit main.py
$ vi ./webapp/main.py

Replace default_score on line 17 with your scoreAddress.

  • Edit send_bet.json
$ vi ./testcmdline/send_bet.json

Replace the “to” attribute with your scoreAddress.

  • Request icx_sendTransaction with send_bet.json
$ tbears sendtx -k keystores/keystore_test1.json -c config/tbears_cli_config.json testcmdline/send_bet.jsonInput your keystore password:
#password: test1_Account
Send transaction request successfully.
transaction hash: 0x16f79e3a450670643325d6e5b553d8cbf86bdef1d622fdb75d8e8e58322f034d
  • Check transaction result
$ tbears txresult <txhash>Transaction result: {
"jsonrpc": "2.0",
"result": {
"txHash": "0x16f79e3a450670643325d6e5b553d8cbf86bdef1d622fdb75d8e8e58322f034d",
"blockHeight": "0x10d",
"blockHash": "0xd39f8e1df6696f98398202beae22b1e7125ff451ffbfdbda05fb0986ea0b8df1",
"txIndex": "0x0",
"to": "cx39ea6e695672c62475794c803f103b4425e73a03",
"stepUsed": "0x103218",
"stepPrice": "0x0",
"cumulativeStepUsed": "0x103218",
"eventLogs": [],
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"status": "0x1"
},
"id": 1
}
  • Check balance of given scoreAddress
$ tbears balance <scoreAddress>balance in hex: 0x7ce66c50e2840000
balance in decimal: 9000000000000000000

- Deploy to Testnet

  • Export wallet backup file

Click on the 3-dot button and select “Backup wallet”. Save the keystore file to a location you prefer.

The file name should look like something like this: UTC — 2018–10–15T01_52_42.607Z — hx04e9f55f5aa3bad0a6f6ae17d45c386e8d2158b2

  • Copy keystore file into container

Open up a new terminal window. Change the working directory to where you saved the keystore file.

$ docker cp <keystore-file> <container-id>:/tbears/icon-dice-roll/keystores/keystore1.json
  • Request testnet coins from faucet

Go back to the previous terminal window with the /tbears/icon-dice-rollworking directory.

  • Deploy with testnet account
$ tbears deploy dice-roll -k keystores/keystore1.json -c config/tbears_cli_config_testnet.jsonInput your keystore password:
#password: your_wallet_password
Send deploy request successfully.
If you want to check SCORE deployed successfully, execute txresult command
transaction hash: 0x8480661ccd0a2ba0b15aa1cc0e2e72a6c3a1e11500b8b561b9e08ee339a8ff14
  • Check transaction result
$ tbears txresult <txhash> -c config/tbears_cli_config_testnet.jsonTransaction result: {
"jsonrpc": "2.0",
"result": {
"txHash": "0x8480661ccd0a2ba0b15aa1cc0e2e72a6c3a1e11500b8b561b9e08ee339a8ff14",
"blockHeight": "0x253d",
"blockHash": "0x7ae59c95a74d1f5723691b78e2c3351a3adacc863761327b26e7c0d3c3343495",
"txIndex": "0x0",
"to": "cx0000000000000000000000000000000000000000",
"scoreAddress": "cxbc2fec3ff5eb40e8075d7f23fea1f47689c7df16",
"stepUsed": "0x3e4921e8",
"stepPrice": "0x2540be400",
"cumulativeStepUsed": "0x3e4921e8",
"eventLogs": [],
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"status": "0x1"
},
"id": 1
}

You can then check on the explorer if it was successfully deployed. https://bicon.tracker.solidwallet.io/contract/<scoreAddress>

Notice scoreAddress is now cxbc2fec3ff5eb40e8075d7f23fea1f47689c7df16

  • Update send_set_treasury.json with the new scoreAddress
$ vi ./testcmdline/send_set_treasury.json

Replace the “to” attribute with the new scoreAddress.

  • Request icx_sendTransaction with send_set_treasury.json
$ tbears sendtx -k keystores/keystore1.json -c config/tbears_cli_config_testnet.json testcmdline/send_set_treasury.json

Check on the explorer if it was successfully deployed. https://bicon.tracker.solidwallet.io/contract/<scoreAddress>

  • Update main.py and run it
$ vi ./webapp/main.py

Replace default_score on line 17 with the new scoreAddress, and http://127.0.0.1:9000/api/v3 on line 18 with http://bicon.net.solidwallet.io/api/v3

$ cd ./webapp
$ python3 main.py
* Serving Flask app "main" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 280-257-458

Contract address generated in this tutorial.

--

--

Jay | Human Being
Cypher Core

Blockchain | Distributed Consensus | Software Architecture | Pitbull | Columbia University SEAS '17.