layout | lang | lang-ref |
---|---|---|
post | en | Implementation_of_NEP5 |
Implementation of NEP-5
Objective : Learn the general idea of NEP5
Main points :
Implementation of each point according to the NEP-5 standard
Use your NEP-5 Token on the Neo-Gui
First of all, we define a readably property owner to present the owner of the contract. The is the Owner
and it is a 20
length byte array.
Now we begin with the main method and the trigger:
Here the main method accept two arguments. The first one is string method
, which is a nep-5 method the user will call to this smart contract. The second one is an array args
, which represents a list of arguments used in the nep-5 method.
Here we also judge the trigger here. When the triggerType is Verification
, it means the end user invoke the transaction with the asset transaction. In other words, end user may want to send global asset such as NEO or GAS to or from this contract. In this condition, we should judge if the invoker ( or the Address that signed the contract ) is owner.
When the triggerType is Application, that means the smart contract is being called by the application (Web/App) with an InvocationTransaction. In this case, we should call other functions according to the method value. We will fill this part later.
Then we define the functions for name, symbol, decimals, which are fixed value for this contract.
We also need define a Transfer event which is also specified in the NEP-5
standard.
Now. Let's define the totalSupply method of the contract. Before that, we should first define a deploy
method. The deploy method is not specified in the NEP-5
standard, but should be the first function that called by smart contract owner and called only once. The purpose of deploy function is to set the totalSupply
value of your NEP-5
token, and move all the token into the Owner's account balance.
It is worth noticing that, in tokenized smart contract, the asset is stored in the storage as the key is the address and the value is the balance. Here is the table which may declare it.
Address | value |
---|---|
address1 | 1000 |
address2 | 200 |
Now , we have the totalSupply defined in the deployment stage, we can fill our totalSupply method, which get the totalSupply value from the storage.
Let's set another method balanceOf
, which get the account NEP-5
balance of a specified address
Now, we have defined almost all the method required in the NEP-5
standard except the transfer method, let us fill the main method first.
Now, the only method left is the transfer method. What the transfer function has to do is first checking the arguments and check if the contract invoker is the owner. If it meets all requirements, get the from
address's balance from the storage, and check if it has enough asset to deal with the transfer. If it has enough amount NEP-5
token, do the calculation and update the new account balance for the from
account and to
account.
Now the NEP-5 Token has been finished and it can be tested on our privateNet . You can also view source code here
After compile the NEP5.cs and get the avm file, deploy it. And if the contract is already deployed on the blockchain, first invoke the deploy method which initial the totalSupply of the token.
Now, in the Neo-GUI click Advanced
-> Option
, add the scriptHash of your contract, you can view the NEP-5 asset you have in the Asset tab。
Let's test another transfer method in the NEP-5 standard. Open the invocation function tab and fill in the arguments. The string part is the method of smart contract you want to call. Here we put transfer
. In the array, the arguments are from
, to
, amount
. The From
address and To
address are in the format of byte array which can be changed by the wallet address. For instance, use this tool link which transfer the wallet address to byte array. For the amount
, do not forget the 108decimal. Here I want to transfer 250000000 my NEP-5 token from my account to other account
After the transaction to be recorded and confirmed, you can open the wallet of the target address and view the updated balance of NEP-5.
Assignment
Define a new NEP5-token by yourself.