Adding Token Balances

Apr 29, 2024 11:46:57 PM

Account Balances

Balances must be stored and retrieved from the storage. To do that, we need to create a key for each balance. For simplicity, we are going to use the as the key. The account script hash is a unique identifier for each account on the and is derived from the account’s public key.

Addresses and script hashes are interchangeable in the blockchain. Script hashes are 20 bytes long and are represented as hexadecimal strings. Developer tools and SDKs will automatically convert the script hash to the correct format.

Adding the balanceOf Method

The NEP-17 defines a method to retrieve the balance of an account. The method is called balanceOf and it receives the account script hash as a parameter. The method must be marked as safe and should return an integer.

Add the following code to the smart contract:

If you are using Go, you will need to include the balanceOf in the safemethods property.

Run the balanceOf method by pressing Run. Select any account from the dropdown and press enter. Since we haven’t added any balance to the storage, the method will return 0.