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 token 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 token symbol 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.
    • $NEO 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
@public
def symbol() -> str:
    return 'COIN'

@public
def decimals() -> int:
    return 8
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:

Success: true
Transaction Hash: 0x4de81e6eb9b8fcb48689fc9b647a3cffaa8e70e34968df4915936fe19c4a87e8
Fees: 0.0216485
 System Fee:  0.0098427 GAS (Burned)
 Network Fee: 0.0118058 GAS (Distributed)
Sender: NMrn3ghpfXVZ3auvirCmVS2VzSkfiSZyw4
Result: [ COIN ]

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

Success: true
Transaction Hash: 0x23153f20dec1d2a5de6e31292784c6b0eff1846562df4a63c969b21e3957553a
Fees: 0.0216664
 System Fee:  0.0098406 GAS (Burned)
 Network Fee: 0.0118258 GAS (Distributed)
Sender: NMrn3ghpfXVZ3auvirCmVS2VzSkfiSZyw4
Result: [ 8 ]

Making the Symbol and Decimals methods read-only

The standard 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
@public(safe=True) # Change this line
def symbol() -> str:
    return 'COIN'

@public(safe=True) # Change this line
def decimals() -> int:
    return 8

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

Coin.yml
name: Coin
safemethods: ["symbol", "decimals"]
supportedstandards: []
events:
    -
permissions:
    - methods: '*'