sidebar_position |
---|
1 |
Smart Contract Writing Basics
In this tutorial, you will learn the basics of developing a smart contract.
Let's have a look at our basic hello world contract.
Contract property
Inside the contract class, the property defined with static readonly
or const
is the contract property which can be used as constants and can not be changed. For instance, when we want to define a Owner of that contract or the factor number which will be used in the later asset transfer, we can define these constants in this way:
These properties defined in contract property are usually constants that can be used inside the methods of smart contract and every time the smart contract is running on any instance, these properties keep the same value.
In addition, developer can define static method in contract and return a constant, which is exposing the method out of the contract and let end-user can call the method to get the fixed value when they try to query the smart contract. For instance, when you create you own token, you have to define a name which you may want everyone use you contract can check he name with this method.
You can also use the get only property to accomplish the same thing.
[Safe]
represents that this method does not modify the contract data and is safe to access.
Storage property
When you develop the smart contract, you have to store your application data on the blockchain. When a Smart Contract is created or when a transaction awakens it, the Contract’s code can read and write to its storage space. All data stored in the storage of the smart contract are automatically persisted between invocations of the smart contract. Full nodes in the blockchain store the state of every smart contract on the chain.
Neo has provided data access interface based on key-value pairs. Data records may be read or deleted from or written to the smart contracts using keys. Besides, smart contracts may retrieve and send their storage contexts to other contracts, thereby entrusting other contracts to manage their storage areas. In C# development, smart contract can use the Storage
Class to read/write the persistent storage The Storage
class is a static class and does not require a constructor. The methods of Storage
class can be viewed in this API References
For instance, if you want to store the total supply of your token into storage:
Here CurrentContext
Returns the current store context. After obtaining the store context, the object can be passed as an argument to other contracts (as a way of authorization), allowing other contracts to perform read/write operations on the persistent store of the current contract.
Storage
work well for storing primitive values and while you can use an StorageMap
which can be used for storing structured data, this will store the entire container in a single key in smart contract storage.
Data type
When using C# to develop smart contracts, you cannot use the full set of C# features due to the difference between NeoVM and Dotnet IL.
Because NeoVM is more compact, we can only compile limited C# / dotnet features into an NEF file.
NeoVM provides the following basic types:
-
Pointer
-
Boolean
-
Integer
-
ByteString
-
Buffer
-
Array
-
Struct
-
Map
-
InteropInterface
The basic types of C# are:
-
Int8 int16 int32 int64 uint8 uint16 uint32 uint64
-
float double
-
Boolean
-
Char String
Your first Neo contract
After analyzing the basic hello world contract, let us move to your first real-world smart contract. Here we provide a very simple DNS system which was written in C#. The main function of the DNS is store the domain for users. It contains all the points above except the events. We can investigate this smart contract to learn how to make a basic smart contract. The source code is here:
Let's slice it and learn it step by step.
Contract Features
You can declare more features:
-
DisplayName
: The name of the nef and manifest.json files generated by the compiler, and the DisplayName is also written to the name field of manifest.json. -
SupportedStandards
: The NEP standards the contract conform to, such asNepStandard.Nep17
, a token standard on Neo. -
ContractPermission
: The permission requested by the contract, andContractTrust
indicates which contracts trust the contract to call itself. See invocation-permission . -
ContractAuthor
: The author field, which can be filled with the author's name and email address. It will output to the extra json object in manifest.json. -
ContractEmail
: The email field. It will be output to the extra json object in manifest.json. -
ContractSourceCode
: The URL of the contract source code. It will be output to the extra json object in manifest.json. -
ContractVersion
: The version of the contract. It will be output to the extra json object in manifest.json. -
ContractDescription
: The description of the contract. It will be output to the extra json object in manifest.json. -
ManifestExtra
: The extra fields in the Manifest file, where you can addWebSite
,Docs
and etc. It will be output to the extra json object in manifest.json.
The generated manifest is as follows:
Entry function
Theoretically, smart contracts can have any entry points. Methods of the public static type in the contract can be used as an entry function to be invoked externally, properties in the contract can be used as an entry function to be invoked externally, for example:
The compiler marks the offset of First
and Second
in ABI. When invoking the contract, it assigns the value to initialPosition, finds and executes the matching method according to the offset recorded in the ABI.
Trigger
A smart contract trigger is a mechanism that triggers the execution of smart contracts. There are three triggers introduced in the Neo smart contract, Verification
, Application
, and System
. However, for most smart contract development, you only need to implement the Verify method to provide the signature verification logic, without having to decide the trigger.
Verification trigger
A Verification trigger is used to call the contract as a verification function, which can accept multiple parameters and should return a valid Boolean value, indicating the validity of the transaction or block.
CheckWitness
In many, if not all cases, you will probably be wanting to validate whether the address invoking your contract code is really who they say they are.
The Runtime.CheckWitness
method accepts a single parameter which represents the address that you would like to validate against the address used to invoke the contract code. In more deeper detail, it verifies that the transactions / block of the calling contract has validated the required script hashes.
Usually this method is used to check whether an specified address is the the contract caller, and then the address can be used to do store change or something else.
Inside our DNS smart contract
, the Register
function is firstly check if the owner is the same as the one who invoke the contract. Here we use the Runtime.CheckWitness
function. Then we try to fetch the domain owner first to see if the domain is already exists in the storage. If not, we can store our domain->owner pair using the Storage.Put
method.
Similar to the Register method, the Delete function check the owner first and if it exists and it is the same as the one who invoke the contract, delete the pair using the Storage.Delete
method.
Events
In Smart contract, events are a way to communicate that something happened on the blockchain to your app front-end (or back-end), which can be 'listening' for certain events and take action when they happen. You might use this to update an external database, do analytics, or update a UI. In some specified contract standard, it defined some events should be posted. It is not cover in this page, but is very useful for the other smart contracts. For instance, in the NEP-17 Token, the events transfer
should be fired when user invoke the transfer function.
Transfer is the event name.