Any contract deployed on the blockchain is immutable. In other words, that code cannot be changed. This is one of the main reasons why security of smart contracts is a fundamental concern from the very start of the coding stage, or perhaps even before that.
Now, while deploying the contract on the blockchain, you can also initiate certain parameters. Once initialized, the values of these parameters will not change, i.e. the values of these variables cannot be changed later. They are called immutable variables.
For instance, when somebody finally deploys a contract code on the blockchain, their wallet address is also passed along with the code. It is often useful to store this address as an immutable variable because wallet addresses can serve as an identity, just like unique email addresses. But instead of sending and receiving emails, wallet addresses are primarily used for sending and receiving digital assets. You may want to store a wallet address as the address of the contract owner to uniquely identify the owner and grant her different access rights if required.
You can then use this immutable variable that stores the owner’s wallet address as a check, so that certain operations in the contract can only be carried out by the owner. The important part about storing the address as an immutable variable is that by doing this, the address of the owner cannot be changed by a third party. (Or else, there is a possibility that a hacker might be able to change the owner address to her own wallet address and then act maliciously as the new owner)
To declare an immutable variable, we need to pay attention to two factors:
Let's tale a look at the format for declaring an immutable variable.
Here, variableName
and variableType
can be replaced by the chosen variable name and type.
If required, we can specify multiple immutable variables separated by commas.
In this example, multiple immutable variables are declared by separating them with commas. The language supports several datatypes, such as Int32
(to represent 32-bit integers), ByStr20
( a sequence of hexadecimal characters that represents 20 bytes. This data type is most convenient to store wallet addresses and will be often used in smart contracts). A detailed list can be seen in the Cheat Sheet in the Task section below.
There are many such data types in Scilla with different types of declaration and that is intentional so that automated checks on the program can be run more easily.
As Robin Milner once said: "Well typed programs don’t go wrong".
We will only be introducing some of the data types through these tutorials. For a detailed list of all the types, kindly refer to the Cheat Sheet in the Task section below.
In our last exercise, we had declared a contract called SocialMediaPayment
.
It should have one immutable variable with the name owner and the variable should be of type ByStr20
.
Show Solution
scilla_version 0
(* Insert the immutable variable declaration in parentheses after the contract name below *)
contract SocialMediaPayment
1234