NEOはブロックチェーンプロジェクトの1つです。
今回はスマートコントラクトを実行してみます。
スマートコントラクト実行フロー
スマートコントラクトを実行するフローは次のようになります。
- pythonでコードを書く。
- buildコマンドでpyファイルをコンパイルし、avmファイルを作成する。
- sc deployコマンドでコントラクト(avmファイル)をネットワークにデプロイする。
- sc invokeコマンドでコントラクトのメソッドをコールする。
1. コーディング
所定のディレクトリに移動し、pythonファイルを作成します。
[コマンド]
1 2
| cd ./neo-local/smart-contracts vi test1.py
|
[ソース]
test1.py1 2
| def Main(): print("Hello World")
|
“Hello World”と表示するだけの簡単な処理となります。
2. コンパイル
neo-localを起動し、neo-pythonプロンプト起動します。
[コマンド]
スマートコントラクトのログを表示する設定を行います。
[コマンド]
[結果]
1
| Smart contract event logging is now enabled
|
pyファイルをコンパイルします。
[コマンド]
1
| sc build /smart-contracts/test1.py
|
[結果]
1
| Saved output to /smart-contracts/test1.avm
|
正常にコンパイルできるとsmart-contractsディレクトリ配下にtest1.avmが作成されます。
3. デプロイ
デプロイを行うためにはウォレットが必要となりますので、ウォレットを開いておきます。
パスワードはcozです。
[コマンド]
1
| wallet open neo-privnet.wallet
|
[結果]
1 2
| [password]> *** Opened wallet at neo-privnet.wallet
|
deployコマンドを使ってavmファイルをデプロイします。
いくつか入力を求められますが、[contract name]にはtest1を指定し、そのほかは何も入力しないでエンターを押します。
パスワードはcozです。
[コマンド]
1
| sc deploy /smart-contracts/test1.avm True False False 07 05
|
引数の意味は、順番に次の通りです。
- True コントラクトがストレージを使うかどうか(needs_storage)
- False 他のコントラクトを参照するかどうか(needs_dynamic_invoke)
- False コントラクト内でNEOやNEOGas、他のトークンとの換金が行われるか(is_payable)
- 07 inputがstring型である
- 05 outputがbyte型がである
[結果]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| Please fill out the following contract details: [Contract Name] > test1 [Contract Version] > [Contract Author] > [Contract Email] > [Contract Description] > Creating smart contract.... Name: test1 Version: Author: Email: Description: Needs Storage: True Needs Dynamic Invoke: False Is Payable: False { "hash": "0x5f21886e9c5674ef65f3ba787c45c7a4957621cd", "script": "53c56b0b48656c6c6f20576f726c64680f4e656f2e52756e74696d652e4c6f67006c7566", "parameters": [ "String" ], "returntype": "ByteArray" } [I 210620 01:22:44 EventHub:58] [test_mode][SmartContract.Contract.Create] [5f21886e9c5674ef65f3ba787c45c7a4957621cd] {'type': 'InteropInterface', 'value': {'version': 0, 'hash': '0x5f21886e9c5674ef65f3ba787c45c7a4957621cd', 'script': '53c56b0b48656c6c6f20576f726c64680f4e656f2e52756e74696d652e4c6f67006c7566', 'parameters': ['String'], 'returntype': 'ByteArray', 'name': 'test1', 'code_version': '', 'author': '', 'email': '', 'description': '', 'properties': {'storage': True, 'dynamic_invoke': False, 'payable': False}}} [I 210620 01:22:44 EventHub:58] [test_mode][SmartContract.Execution.Success] [d3e19bd3d6b90ea7af5c8fa0681aed5c8ac9bb71] {'type': 'Array', 'value': [{'type': 'InteropInterface', 'value': {'version': 0, 'hash': '0x5f21886e9c5674ef65f3ba787c45c7a4957621cd', 'script': '53c56b0b48656c6c6f20576f726c64680f4e656f2e52756e74696d652e4c6f67006c7566', 'parameters': ['String'], 'returntype': 'ByteArray', 'name': 'test1', 'code_version': '', 'author': '', 'email': '', 'description': '', 'properties': {'storage': True, 'dynamic_invoke': False, 'payable': False}}}]} Used 500.0 Gas
------------------------------------------------------------------------------------------------------------------------------------- Test deploy invoke successful Total operations executed: 11 Results: [<neo.Core.State.ContractState.ContractState object at 0x7f5da0a520b8>] Deploy Invoke TX GAS cost: 490.0 Deploy Invoke TX Fee: 0.0 -------------------------------------------------------------------------------------------------------------------------------------
Enter your password to continue and deploy this contract [password]> *** [I 210620 01:23:56 Transaction:619] Verifying transaction: b'8d4eb06e2f3344c961b20e87fb89c879449ee4e58961f15e27042d9ff8a3d85b' [I 210620 01:23:57 EventHub:62] [SmartContract.Verification.Success][2843] [9b0ab1768245bae9dd09ff3231acf1667a6aae3d] [tx 8d4eb06e2f3344c961b20e87fb89c879449ee4e58961f15e27042d9ff8a3d85b] {'type': 'Array', 'value': [{'type': 'Boolean', 'value': True}]} Relayed Tx: 8d4eb06e2f3344c961b20e87fb89c879449ee4e58961f15e27042d9ff8a3d85b
|
ネットワークにコンストラクトが反映されるまで少し時間が必要となりますので、少々待ちます。
デプロイしたコントラクトを確認します。
引数にはデプロイ時に指定した[contract name] test1を指定します。
[コマンド]
[結果]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| Found 1 results for test1 { "version": 0, "hash": "0x5f21886e9c5674ef65f3ba787c45c7a4957621cd", "script": "53c56b0b48656c6c6f20576f726c64680f4e656f2e52756e74696d652e4c6f67006c7566", "parameters": [ "String" ], "returntype": "ByteArray", "name": "test1", "code_version": "", "author": "", "email": "", "description": "", "properties": { "storage": true, "dynamic_invoke": false, "payable": false } }
|
4行目の”hash”は、コンストラクトを実行するときに必要となります。
4. 実行
デプロイしたコントラクトを実行します。
引数にはsearchコマンドで表示したhashを指定します。
パスワードはcozです。
[コマンド]
1
| sc invoke 0x5f21886e9c5674ef65f3ba787c45c7a4957621cd
|
[結果]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| neo> sc invoke 0x5f21886e9c5674ef65f3ba787c45c7a4957621cd [I 210620 01:29:21 EventHub:58] [test_mode][SmartContract.Runtime.Log] [5f21886e9c5674ef65f3ba787c45c7a4957621cd] {'type': 'String', 'value': 'Hello World'} [I 210620 01:29:21 EventHub:58] [test_mode][SmartContract.Execution.Success] [5f21886e9c5674ef65f3ba787c45c7a4957621cd] {'type': 'Array', 'value': [{'type': 'ByteArray', 'value': bytearray(b'')}]} Used 0.016 Gas
------------------------------------------------------------------------------------------------------------------------------------- Test invoke successful Total operations: 11 Results [{'type': 'ByteArray', 'value': ''}] Invoke TX GAS cost: 0.0 Invoke TX fee: 0.0001 -------------------------------------------------------------------------------------------------------------------------------------
Enter your password to continue and deploy this contract [password]> *** [I 210620 01:29:25 Transaction:619] Verifying transaction: b'db800ab02631ae8fd05b1676eb14b8fe688acbae8191145459a8677ec6694750' [I 210620 01:29:25 EventHub:62] [SmartContract.Verification.Success][3166] [b2348581314004c5ab7183174a6d021c84c881fa] [tx db800ab02631ae8fd05b1676eb14b8fe688acbae8191145459a8677ec6694750] {'type': 'Array', 'value': [{'type': 'Boolean', 'value': True}]} Relayed Tx: db800ab02631ae8fd05b1676eb14b8fe688acbae8191145459a8677ec6694750
|
結果に’Hello World’(2行目)が表示されていることが確認できます。
また4行目の「Used 0.016 Gas 」よりGasが0.016消費されたことが分かります。