NEO Development: Deploy your First Smart Contract

in #neo6 years ago

Hello World

In the previous post, we learned about how to set up an NEO private-net and get all the assets in your wallet ready. Today I'm gonna show you how to write a Hello World smart contract in python, how to compile it and deploy it on your private-net.

在我的上一篇介紹中,我們已經會架設NEO的Private-net了,但架設一個Private Net為的就是要開發智能合約吧!今天我就來非常簡單的介紹,如何寫出自己的第一個Smart Contract!

image.png

Environment Setup

We will need a package calledneo-boa to compile our python script into avm files. An .avm file is something that the NEO virtual machine can understand and run, while we can use different languages to write smart contracts, eventually, they all have to be compiled to an avm file for deployment.
To install neo-boa, just run pip install neo-boa in your terminal, or visit their github for more installation detail

再來關於環境的部份,我們還需要一個叫做neo-boa的python套件來編譯我們的python檔成avm檔。一個.avm檔是NEO虛擬機所認得的代碼,無論我們用何種語言做開發,最後一定需要轉換成.avm檔才能放到NEO的區塊鍊上作為智能合約執行。
要安裝neo-boa很簡單,只要執行pip install neo-boa就可以了。你也可以參考他們官方github的更多教學。

The Smart Contract

Today we're only going to write a very simple hello world smart contract, and we're also using the most easy-understood language: python to implement it, so everything's gonna look simple:

今天要練習的智能合約只是一個Hello World,因此你應該會覺得代碼的部份非常簡單。 如果你懶的自己打一份的話,可以到我的github上面下載

sample1.py

(You can get it through my github if you're too lazy to code it yourself lol)

def Main():
  print("Hello World")
  return True

Save it. Just like the very first python script you wrote, it's just simple.

Test Your Smart Contract

Go to your neo-python directory and run the neo cli through:

我們需要用以下的指令打開CLI,來與NEO區塊鍊互動。

python prompt.py -p

After open the CLI, you will need to open your wallet to test and import your contract. To open your wallet, use: open wallet <path to your .wallet file>.Also you would need to command config sc-events on so we can see the result printed on the CLI.
After that, you can use build function with test argument to test your python script. Now we have to run the following command:

開啟CLI之後,還需要開啟我們的錢包才能執行test以及import contract等指令。要開啟錢包只要使用 open wallet <path to your wallet>就可以了。在打開錢包之後,我們還需要使用config sc-events on的指令,讓我們智能合約中的Print可以顯示在終端機之中。這些準備都做好之後,就可以參考下面的build test指令,測試自己的合約囉!

build <path to py file> test '' 02 False False

The command structure is :

build <path to py file> test {input_params} {return_type} {needs_storage} {needs_dynamic_invoke} param1 param2 etc..
  • The '' after test means we have no input parameters,
  • returns_type is set to 02, which means Integer (You can see Contract Paramenter Type Here)
  • needs_storage and needs_dynamic_invoke were set to False,

After you press Enter, you can see the 'Hello World' String printed

在這個build的指令當中,''留白處代表我們沒有input的參數,return_type設為02代表著整數(看這裡參考更多Type)。最後我們將needs_storageneeds_dynamic_invoke兩個參數設為False,目前階段還不會用到。
確認執行之後,就可以看到我們的'Hello World'被印出來囉。

image.png

A little Test

We can change the output type from Integer to Boolean by changing the return_type to 01:

build <path to py file> test '' 02 False False

We can see that the Result is now True instead of 1

我們在這裡可以做一個小小的測試,將return_type的02換成01,也就是從Interger變成Boolean。在跑一次一樣的指令,我們可以看到Result從1變成True了。

image.png

Build AVM file

After testing your contract, you use the build command without test to convert your .py file to .avm: build <path to your py file>
測試完成之後,我們可以用單純build來將我們的python檔變成avm檔囉~

image.png
Yay! Now we have our sample1.avm ready, then we can put it on the blockchain with the import command.
作到這裡就幾乎完成啦!接著只要把這個avm檔放到blockchain上面,就大功告成囉!

Import Contract onto Blockchain

Please notice that for every contract you deploy, you're gonna be charged a 500 GAS fee, so even if you're using your private-net, make sure to test before your import or you will waste a lot of GAS.
我們將利用import contract的指令將我們的.avm檔deploy到區塊鍊上面,就跟前面的測試時相同,需要給許多的參數來表示我們這個contract的輸入、輸出等等。

import contract <path to your .avm> '' 02 False False

After this you will be asked to enter some basic information about the contract.
接著,我們要再輸入一些關於合約的基本資料。

image.png
Then you will need to enter your password again. (you will also need to hash of your smart contract later)

輸入密碼,準備成功囉!(這裡的hash 值要記起來,是我們以後要call這個合約時不可或缺的id!)

image.png

Now we have your first smart contract on the NEO blockchain!

The hash of our contract is 0x19c73c951d13bdc4a7901947a532f40bc17b3d3b, we will use it to interact with our contract!

到這裡就已經成功啦,記得我們這個智能合約的hash: 0x19c73c951d13bdc4a7901947a532f40bc17b3d3b 等等會用到呦~

Interact with our Smart Contract

Since we don't have any functions within our smart contract, we can only call the main function. To do this, we need the testinvoke command, put the hash after testinvoke and press enter to run.

最後,我們用testinvoke這個指令來測試我們的智能合約。由於我們沒有其他的input參數,所以只要在後面直接加上我們的智能合約hash,就可以call我們的合約啦!

image.png
We can see the 'Hello World' printed with the returned result 1. Congratulations! you have your first smart contract on NEO running!
可以看到我們的 Hello World成功被印出來,並且反傳1回來。成功!我們的第一個智能合約已經被放到NEO blockchain上了,是不是很簡單呢?

To Be Continued

Now you know how to run a smart contract on NEO, start from the next tutorial we can learn more about the contract itself!
現在我們知道怎麼部署NEO智能合約了,之後,我們就能專注於合約本身的開發囉!喜歡我的文章的話,記得Follow我 @antonsteemit喔!

This is only the first Hello World Contract, if you want to learn more about being a NEO developer, please follow @antonsteemit! ;)

image.png

Find your Passion, and it's no longer work

Sort:  

Great info. Marked for future reads. Thanks.

thanks! Glad that it's helpful to someone ;) You can also find more resources in my github repo, I'll keep updating more information

Great post ever, keep it up for success @antonsteemit ✯◡✯

Coin Marketplace

STEEM 0.17
TRX 0.15
JST 0.028
BTC 60385.82
ETH 2321.90
USDT 1.00
SBD 2.51