An Introduction to Contract Development on Neo
In this tutorial, we will work you through an example contract on Neo blockchain, which includes the tasks of setting up and configuring the development environment, compiling, deploying, and invoking the smart contract on a private chain and testnet. This one is simply meant to get us up and running with a basic development and testing environment, highlighting some key technical concepts along the way.
This tutorial contains the following topics:
Development environment
System environment
You are recommended to run Neo-CLI in the following environments that we have tested: - Windows 10 - Ubuntu 16.04/18.04 - CentOS 7.4/7.6 - macOS Big Sur/version 11.1
Connecting to network
Neo provides the Test network where you can develop, debug, and test your programs before publishing them on the live network. To connect your node to test net, see detailed instructions here .
Alternatively, you can also set up your own private chain where you can get more flexibility with plenty of test tokens.
Setting up a private chain
You can pick one of the following options:
-
Build a private chain with one node
Neo-CLI supports generating blocks without consensus nodes, which means you can set up a private chain with one node. There are two ways to achieve this, you can choose either of them of your will.
-
Directly download the project neo-express to run the private chain quickly.
-
Or build a private chain with one node from scratch, more details please check at the guide .
-
-
Build a private chain with multiple nodes
You can also choose to build a private chain with multiple nodes to practise the consensus mechanism. The guide here shows you the way to build a private chain on a windows system computer.
Refer to the instructions from above links to set up your private chain and withdraw NEO and GAS from genesis block.
Preparing a wallet file
Now let's create a new wallet file used for deploying smart contracts:
-
Create a new wallet file named 0.json and copy the default address for later use.
-
Open the wallet where you have withdrawn NEO and GAS from genesis block, transfer all the assets in the wallet to 0.json and wait for the transaction to be confirmed.
-
Open 0.json to check if the assets are received.
Developing a contract
Configuring the development tool
Refer to the official guide to install tools and contract template, and then create a contract project named Contract1 with the following command:
Editing the contract
Here we provide a simple smart contract written in C#. The source code is here:
In the example contact above, note these methods:
-
Update
:This method enables the contract update function. When it is invoked, the contract is upgraded and neither the contract hash nor the storage area is changed. You can refer to here to update your contract. -
Destroy
: This method takes no arguments. When it is invoked, the contract and its storage, if any, are deleted. Then the contract is no longer available.
Compiling the contract
Run the following command to build your contract:
Related contract files are outputted under \bin\Debug\net6.0
path in the contract project directory.
Deploying the contract
Previously, we have compiled an example contract file (Contract1.nef) and contract descriptive file (Contract1.manifest.json), next we will move on to deployment and invocation of the contract with Neo-CLI.
In Neo-CLI, input the deploy command deploy <nefFilePath> [manifestFile]
, for example:
or
After the command is executed, the contract is deployed and the related fee is charged by the system automatically. For more information, refer to the official guide .
Invoking the contract
After you deployed a smart contract on the blockchain, you can then invoke it by its script hash. You can query contract details, such as the contract general information, methods, notifications, etc.
In Neo-CLI
, invoke the contract using the command line:
For example:
After executed successfully, the following information is printed:
Where:
-
VM State:
HALT
indicates the vm executed successfully;FAULT
indicates the vm exited during execution due to an exception. -
Evaluation Stack: the result of contract execution, where the value is encoded with Base64 when it is a string or ByteArray.
-
You can do the data format conversion here
VG9rZW5TeW1ib2w= => TokenSymbol
For more details, please check at the guide here.
Interfacing using NeoLine wallet
The interaction between smart contracts deployed on the blockchain and its users can happen in many different devices and platforms.
In our example, we'll use the NeoLine extension plugin wallet to interface with the chain and expose the results in a simple user interface.
Installation
The following options are available for you to install the NeoLine plugin, you can choose one of your will.
-
Directly download the NeoLine extension plugin wallet to run the desired chain quickly.
-
Get the latest
crx
release from here . -
Or build the project. For more details check at README
Using NeoLine
After you installed the NeoLine and pined it to the browser shortcut, then see the gif below to create a wallet.
The following shows that how to transfer N3 Gas
. you can check the transaction at N3 NeoTube .
Calling the dAPI
After you completed the instructions above, you can call the dAPI of NeoLine and run it directly on the browser, without deploying the full node of Neo.
Initialize NeoLine N3 dAPI example.
For more details check the following links: