Moving the Total Supply to the Storage

Using the Smart Contract Storage

can persist information on the . This is done by using the smart contract storage. The storage is a key-value. The storage is public and can be accessed by anyone. The storage is also persistent, which means that the data will be available even after the smart contract is executed. The storage works as a key-value database.

Storage Keys

All information stored in the smart contract storage is identified by a key. Both the key and the values are stored as byte arrays. However, it’s possible to convert bytes from and to other types, such as integers and strings.

Moving the Total Supply to the Storage

To make our contract more flexible, let’s move the total supply to the storage. First, let’s create a key for the total supply and use it to retrieve the total supply value:

If you run the totalSupply method, it will return the value 0. This is because the storage is empty. Let’s fix this by adding the total supply to the storage.

Using the _deploy method

The _deploy method is executed when the smart contract is deployed or updated. It’s a callback method that can be used to initialize the smart contract. Let’s use it to set the total supply in the storage:

Before you test it, clean the blockchain storage by pressing Ctrl+Shift+P and selecting the Linkd: Reset Blockchain command. This will reset the blockchain, making sure that the _deploy method is executed again.

Run the totalSupply method again. It should return the value 10000000000000000. This is the value we expect, since we set the decimals to 8. The total supply has been set to 100,000,000.00000000 (100 million).