Moving the Total Supply to the Storage
Using the Smart Contract Storage
Smart Contracts can persist information on the blockchain ledger. Any data stored is public and can be accessed by anyone. 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, strings and even objects.
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 Total Supply
method, it will return the value of 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.
Update the _deploy
method 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 correct value of one hundred million followed by eight zeros.