IOSTブロックチェーンでスマートコントラクトを書く その1

Takao Wada
Feb 14 · 15 min read

前提条件

テスト環境のインストール

$ docker run -it --rm -d -p 30000-30003:30000-30003 iostio/iost-node:2.5.0-dbb56b7
Unable to find image 'iostio/iost-node:2.5.0-dbb56b7' locally
2.5.0-dbb56b7: Pulling from iostio/iost-node 7b8b6451c85f: Already exists
ab4d1096d9ba: Already exists e6797d1788ac: Already exists e25c5c290bde: Already exists dd95d199336e: Pull complete 633f38155706: Pull complete 8ed9f2869f77: Pull complete 6607d0d46585: Pull complete edfae8fa2cfc: Pull complete 8f987566e055: Pull complete 8bd88b56ce83: Pull complete d3ec982a30ca: Pull complete 63e060801f11: Pull complete b8b436dccc06: Pull complete fc20f66d744a: Pull complete
Digest: sha256:6f8fd924838a96c2fa851488af48c9b3cc1cad1a62f267690776f8cb5ca9a362
Status: Downloaded newer image for iostio/iost-node:2.5.0-dbb56b7 3e90179c24eac3b825fc47da258846ffee69e40db443149d340152a0f6403751
$ docker ps
CONTAINER ID        IMAGE                            COMMAND                  CREATED             STATUS              PORTS                                  NAMES
3e90179c24ea iostio/iost-node:2.5.0-dbb56b7 "iserver -f /var/lib…" 4 minutes ago Up 4 minutes 0.0.0.0:30001-30002->30001-30002/tcp lucid_feynman
$ docker exec -it <表示されたコンテナID> /bin/bash
root@453c72249832:/workdir#
# apt update
Get:1 http://archive.ubuntu.com/ubuntu xenial InRelease [247 kB]
・・・(省略)
# apt install nano
Reading package lists... Done
・・・(省略)
# iwallet -h
An IOST RPC client

Usage:
iwallet [command]
・・・(省略)
# iwallet account import admin 2yquS3ySrGWPEKywCPzX4RTJugqRh7kJSo5aehsLYPEWkUxBWA39oMrZ7ZxuM4fgyXYs2cPwh5n8aNNpH5x2VyK1

HelloWorld

HelloWorldコントラクトの作成

# nano helloworld.js
class HelloWorld {
init() {}
hello(someone) {
return "hello, "+ someone
}
}

module.exports = HelloWorld;

HelloWorldコントラクトのコンパイル

# iwallet compile helloworld.js
Successfully generated abi file as: helloworld.js.abi
# cat helloworld.js.abi
{
"lang": "javascript",
"version": "1.0.0",
"abi": [
{
"name": "hello",
"args": [
"string"
],
"amountLimit": [],
"description": ""
}
]
}

HelloWorldコントラクトのテストノードへのパブリッシュ

# iwallet --account admin publish helloworld.js helloworld.js.abi
Sending transaction...
Transaction has been sent.
The transaction hash is: 6utckwCQXoJQ1wDRdWM4V62hBRuMnmohfYJCUgp9176p
Checking transaction receipt...
SUCCESS!
The contract id is: Contract4nb7JeLK9RJpE4pvbcFmn3TFdmsibnY3Zu22jaPmSSgo

HelloWorldコントラクトの呼び出し

# iwallet --account admin call "<表示されたコントラクトID>" "hello" '["IOST"]'
Sending transaction...
Transaction has been sent.
The transaction hash is: BLJj3zcpnhNZ3ud9FFARYBRiJqsrKdyz6CMFj1eaHsUD
Checking transaction receipt...
SUCCESS!
# iwallet receipt <トランザクションID>
{
"txHash": "BLJj3zcpnhNZ3ud9FFARYBRiJqsrKdyz6CMFj1eaHsUD",
"gasUsage": 33745,
"ramUsage": {
},
"statusCode": "SUCCESS",
"message": "",
"returns": [
"[\"hello, IOST\"]"
],
"receipts": [
]
}

ストレージを使う

Storageコントラクトの作成

# nano storage.js
class Storage {
init() {
storage.put("value1", "TEST")
}
read() {
return storage.get("value1")
}
change(someone) {
storage.put("value1", someone)
}
}
module.exports = Storage;

Storageコントラクトのコンパイル

# iwallet compile storage.js
Successfully generated abi file as: storage.js.abi
# cat storage.js.abi
{
"lang": "javascript",
"version": "1.0.0",
"abi": [
{
"name": "read",
"args": [],
"amountLimit": [],
"description": ""
},
{
"name": "change",
"args": [
"string"
],
"amountLimit": [],
"description": ""
}
]
}

Storageコントラクトのパブリッシュ

# iwallet --account admin publish storage.js storage.js.abi
Sending transaction...
Transaction has been sent.
The transaction hash is: Gyof83SQYsFfSUYMHk5ChqMuoYtn8zTwmfq9aY9XdHoo
Checking transaction receipt...
SUCCESS!
The contract id is: Contract3ugRgjd6AXc8CNLvfCSiPTBacyooUN6syzhrFffRf2Wk

最初の状態のストレージの確認

# iwallet --account admin call "<コントラクトID>" "read" '[]'
Sending transaction...
Transaction has been sent.
The transaction hash is: 7zMhgUuzzeowX9CkTgTffHU5fkSF17g5XBjbqTe8iwHG
Checking transaction receipt...
SUCCESS!
# iwallet receipt "<トランザクションID>"
{
"txHash": "7zMhgUuzzeowX9CkTgTffHU5fkSF17g5XBjbqTe8iwHG",
"gasUsage": 33791,
"ramUsage": {
},
"statusCode": "SUCCESS",
"message": "",
"returns": [
"[\"TEST\"]"
],
"receipts": [
]
}

ストレージの変更

# iwallet --account admin call "<コントラクトID>" "change" '["HELLO"]'
Sending transaction...
Transaction has been sent.
The transaction hash is: G4GNxTWfGwHA1fhZfuUDRnW6nVKMqDhkCwZjfjhxQuLZ
Checking transaction receipt...
SUCCESS!

変更後のストレージの確認

# iwallet --account admin call "<コントラクトID>" "read" '[]'
Sending transaction...
Transaction has been sent.
The transaction hash is: G4GNxTWfGwHA1fhZfuUDRnW6nVKMqDhkCwZjfjhxQuLZ
Checking transaction receipt...
SUCCESS!
# iwallet receipt "<トランザクションID>"
{
"txHash": "G4GNxTWfGwHA1fhZfuUDRnW6nVKMqDhkCwZjfjhxQuLZ",
"gasUsage": 33801,
"ramUsage": {
},
"statusCode": "SUCCESS",
"message": "",
"returns": [
"[\"HELLO\"]"
],
"receipts": [
]
}

関連トピック

Takao Wada

Written by

Co-Founder and CTO of Eversystem Inc., Japan

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