Adding Symbol and Decimals to the NEP-17 Smart Contract

Apr 29, 2024 11:46:57 PM

What are the Symbol and Decimal methods?

The symbol and decimals methods are part of standards like NEP-17. They are used to provide information about the token to wallets and applications.

The symbol is used by wallets and applications to identify the token. It may also be referred to as the ticker symbol.

  • It must use uppercase letters.
  • The symbol must be a string of 3 to 8 characters.
  • Don’t include special characters or spaces.
  • Changing a after deployment is not recommended.

The decimals method is used by applications to show a human-readable representation of the token amount. One token with two decimal places will display as 0.01. With zero decimal places, one token will display as 1.

  • It must be an integer between 0 and 18.
    • Eight is the most common value.
    • Zero means that the token is indivisible.
    • $ has zero decimals, while $GAS has eight decimals.

Adding the Symbol and Decimals methods

Let’s add the symbol and decimals methods to our smart contract. Open the contract file and add the following code:

Coin.py
Info Icon
Info: Make sure you save the file after making these changes.

Testing the Symbol and Decimals methods

Start testing your contract by running the symbol method. The output should be the string COIN:

Next, run the decimals method. The output should be the integer 8:

Making the Symbol and Decimals methods read-only

The implies that the symbol and decimals methods should be read-only. This means that they should not make changes to the storage. Let’s fix this before we continue.

Update the symbol and decimals methods to be read-only:

Coin.py

Golang users must update the Coin.yml file to include the safemethods attribute:

Coin.yml