NEP-5 contract

Objective: Learn the general idea of NEP5

Main points:

  1. What is NEP (NEO Enhancement Proposals)

  2. The detail of NEP5

What is NEP?

NEP stands for NEO Enhancement Proposal. An NEP is a design document providing information to the NEO community, or describing a new feature for NEO or its processes or environment. The NEP should provide a concise technical specification of the feature and a rationale for the feature. The NEP author is responsible for building consensus within the community and documenting dissenting opinions. There is already more then 10 NEPS in this github repository

For NEO implementers, NEPs are a convenient way to track the progress of their implementation. Ideally each implementation maintainer would list the NEPs that they have implemented. This will give end users a convenient way to know the current status of a given implementation or library.

If someone are interested in proposing a new NEP, first review NEP-1, then clone the repository and add your NEP to it. There is a template NEP here. Then submit a Pull Request to this repository.

Introduction to NEP-5

The NEP-5 standard is a token standard which represents a tokenized smart contract. This standard can regulated the token which is issued on the NEO blockchain. A standard method for interacting with these tokens relieves the entire ecosystem from maintaining a definition for basic operations that are required by every Smart Contract that employs a token.

In NEP-5 standard, you have some methods and trigger one event

Methods

totalSupply

public static BigInteger totalSupply()

Returns the total token supply deployed in the system.

name

public static string name()

Returns the name of the token. e.g. "MyToken".

This method MUST always return the same value every time it is invoked.

symbol

public static string symbol()

Returns a short string symbol of the token managed in this contract. e.g. "MYT". This symbol SHOULD be short (3-8 characters is recommended), with no whitespace characters or new-lines and SHOULD be limited to the uppercase latin alphabet (i.e. the 26 letters used in English).

This method MUST always return the same value every time it is invoked.

decimals

public static byte decimals()

Returns the number of decimals used by the token - e.g. 8, means to divide the token amount by 100,000,000 to get its user representation.

This method MUST always return the same value every time it is invoked.

balanceOf

public static BigInteger balanceOf(byte[] account)

Returns the token balance of the account.

The parameter account SHOULD be a 20-byte address. If not, this method SHOULD throw an exception.

If the account is an unused address, this method MUST return 0.

transfer

public static bool transfer(byte[] from, byte[] to, BigInteger amount)

Transfers an amount of tokens from the from account to the to account.

The parameters from and to SHOULD be 20-byte addresses. If not, this method SHOULD throw an exception.

The parameter amount MUST be greater than or equal to 0. If not, this method SHOULD throw an exception.

The function MUST return false if the from account balance does not have enough tokens to spend.

If the method succeeds, it MUST fire the transfer event, and MUST return true, even if the amount is 0, or from and to are the same address.

The function SHOULD check whether the from address equals the caller contract hash. If so, the transfer SHOULD be processed; If not, the function SHOULD use the SYSCALL Neo.Runtime.CheckWitness to verify the transfer.

If the to address is a deployed contract, the function SHOULD check the payable flag of this contract to decide whether it should transfer the tokens to this contract.

If the transfer is not processed, the function SHOULD return false.

Events

transfer

public static event transfer(byte[] from, byte[] to, BigInteger amount)

MUST trigger when tokens are transferred, including zero value transfers.

A token contract which creates new tokens MUST trigger a transfer event with the from address set to null when tokens are created.

A token contract which burns tokens MUST trigger a transfer event with the to address set to null when tokens are burned.

Next Step

Now let us implement a NEP5-Token!

Previous Step

If you want to learn the knowledge points of smart contract, click here.