Adding Token Balances

Account Balances

Balances must be stored and retrieved from the storage. To do that, we need to create a key for each account balance. For simplicity, we are going to use the account script hash as the key.

The script hash is a unique identifier for each account. It is calculated by hashing the account’s . The script hash and the are related but they are not the same. The address is a human-readable string that is derived from the script hash.

Adding the balanceOf Method

The standard 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:

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.