In this article, I’ll be going over the different types of state variables in Solidity and how to use them. State variables are one of the most important parts of any smart contract, as they allow us to store data that can change over time.
This article is mainly focused on value types of state variables, but I’ll be continuing with another two articles on reference and complex types as well as data location. Let’s dive in!
Basics – A Quick Review
Smart contracts are pieces of code that are deployed in blockchain nodes. They are immutable, meaning they cannot be changed once they have been deployed. This can make it necessary to redeploy the code as a new smart contract or redirect calls from an old contract to new ones.
A smart contract is initiated by a message embedded in a transaction. Ethereum enables these transactions, which may carry out more sophisticated operations like conditional transfers.
A conditional transfer, such as one that depends on the age of the buyer or the value of their bid, could be required.
💡 Example: If the buyer is over 21 and their bid is greater than the minimum bid, then accept the bid. Otherwise reject it.
Smart contracts are executed when predetermined conditions are met to automate the execution of an agreement so that all parties can be immediately certain of the outcome without the need for an intermediary.
How Do You Write a Smart Contract?
Smart contracts are similar to a class definition in an object-oriented programming language.
The smart contracts are:
- data (its state);
- a collection of code (its functions or methods with modifiers public or private with getter and set functions).
What is the structure of a smart contract?
As we have seen in other articles in Finxter, the structure of a smart contract is as follows:
- Contract in the Ethereum blockchain has
pragma
directive; - Name of the contract;
- Data or the state variable that define the state of the contract;
- Collection of functions to carry out the intent of a smart contract;
Note that the identifiers representing these elements are restricted to the ASCII character set. Make sure you select meaningful identifiers and follow camel case convention in naming them.
Variable Declaration
To declare a variable in Solidity, you must first specify its data type. This is followed by an access modifier and the variable name.
Structure
<type> <access modifier> <variable name> ;
Example:
What Categories of Variables Exist in Solidity?
Solidity supports three categories of variables:
(1) State Variables
State variables are variables whose values are permanently stored in a contract storage.
What does this mean?
State variables are an essential part of any contract. They are variables whose values are permanently stored in the contract storage. They can be thought of as a single slot in a database that you can query and alter by calling functions of the code that manages the database. The set
and get
functions can be used to modify and retrieve the value of the variables.
In other words, the data (state variables) are stored contiguously item after item starting with the first state variable, stored in slot 0. For each variable, the size in bytes is determined according to its type. Several contiguous items that require less than 32 bytes are packed into a single storage slot if possible.
To make it easier, if you use other languages and want to store user information for a long time, you would connect your application to a database server and then store the information in the database. In Solidity, however, you do not need to connect, you can simply store the data permanently using state variables.
(2) Local Variables
Local variables are variables whose values exist until the function is executed; the context of local variables is within the function and cannot be accessed outside.
Typically, these variables are used to hold temporary values for processing or computing something. In the following example, “temp” is a local variable that cannot be used outside the “set” function.
(3) Global Variables
Global variables are variables whose values exist in the global namespace to obtain information about the blockchain.
Each function has its own scope, but state variables should always be defined outside the scope, like the attributes of a class.
They are permanently stored in the Ethereum blockchain, more precisely in the storage Merkle-Patricia tree, which is part of the information that forms the state of an account (that’s why we call them state variables).
What Types of Valid State Variables Exist?
💡 Info: Solidity is a statically typed language, meaning each variable’s type must be specified at the time of its declaration.
“Undefined” or “null” values do not exist in Solidity, but newly declared variables always have a default value depending on their type, typically called “zero- state”.
For example, the default value for bool
is false
.
As in other languages (not Python 😀 ), there are two types in Solidity: value types and reference types.
- The value type is a variable that stores its value or its own data directly; it is a value type. If the variable contains a location of the data – it is a reference type.
- The reference types are discussed in a separate article.
For example, consider the integer variable int i = 100;
The system stores 100
in the memory location allocated for the variable i. The following image shows how 100 is stored in a hypothetical location in memory (0x239110
) for “i”:
What are the Modifiers for the State Variables?
Visibility – access modifiers
Access modifiers are the keywords used to specify the declared accessibility of a state variable and functions.
Variables in Solidity have three types of visibility: public, private, and internal. If visibility is not explicitly declared, the compiler considers it internal.
For variables of type public, the compiler automatically creates a method to retrieve them through a call. This does not apply to private or internal variables.
Example:
uint256 public a; is actually exactly the same thing as : uint256 private a; function a() public view returns(uint256) { return a; }
When you create a public variable, it is stored the same way as a private variable, but the compiler automatically creates a getter function for it.
💡 The difference between private and internal variables is that internal variables are inherited by child contracts, while private variables are not.
To learn more about private variables:
contract Addition { uint x; //internal variable uint public y; // contract Child is Addition{ //no need to define x since the child contract inherits the variable //uintx function setX(uint _x) public { x =_x; function getX() public view returns (uint) { return x; } }
Note that the data location (memory, storage, and call data) must be specified for variables of reference type. This is necessary when function arguments are involved. We will cover this in an article on data location.
Other keywords
The following keywords can be used for state variables to restrict changes to their state.
Constant (replaced by “view” and “pure” in functions)
Constant disallows assignment (except at initialization), i.e. they cannot be changed after initialization, but must be initialized at the time of their declaration.
Example:
uint private constant t = 40;
The variable t
has been declared once and therefore cannot be changed.
It is interesting to note that the declaration of a constant variable without initialization is forbidden and the compiler displays an error, e.g.:
Contract Addition { uint private x; uint public y; uint private constant z; //gives an error because constant variables must be initialized when declared.
Immutable
These variables can be declared without being initialized, but the assignment, which is only one, must be done in the constructor. After that, the variable is constant thereafter.
uint private immutable w; //now we declare a constructor for the contract, using the function constructor constructor() { w = 20; //initiate variable }
Override
This keyword states that the public state variables change the behavior of a function.
Value Types
These variables are passed by value. That is, they are copied when they are used either in an assignment or in a function argument.
👉 If this sentence is not clear, you can check here.
Here we will see the basic value types.
Value types are booleans, integers, addresses, enums, and bytes.
Booleans
Boolean values can be true or false
An example of a boolean type:
contract ExampleBool { // example of a bool value type in solidity bool public IsVerified = false; bool public IsSent = true; }
Integers
There are int/uint
(signed and unsigned integers) types of various sizes. It stores the values in a range of 8, int16
, …up to int256
. Int256 is the same as int
, same for uint8
, and uint256
.
💡 Note: uint256
is the same as uint
.
The type uint
stands for positive integers. The type int stands for both positive and negative integers.
👉 Recommended Tutorial: Solidity Data Types – Integer and Boolean
The type uint8
(has 8 bits, which corresponds to 1 byte. This means that it accepts numbers between 0 and 255; bit is a binary digit. So one byte can hold 2 (binary) ^ 8 numbers from 0 to 2^8-1 = 255. This is the same as asking why a three-digit decimal number can represent the values 0 to 999.
The type uint256
accepts numbers between 0 and 2^256.
If we try to assign the value 256 to a variable of type uint8
, the compiler will print an error.
The best practice for integers is to specify the value of the bits at the declaration stage to use as little space as possible and reduce the cost of storage. So use uint8
or uint16
instead of always using int
(uint256
).
contract SimpleContract{ uint32 public uidata = 1234567; //un-signed integer int32 public idata = -1234567; //signed integer }
Fixed Point Numbers
According to the Solidity documents, fixed-point numbers are the type for floating-point numbers. However, the official document states that “Fixed point numbers are not yet fully supported by Solidity”. They can be declared, but cannot be added to or derived from.
However, you can use floating point numbers for calculations, but the value resulting from the calculation should be an integer.
Here is an example,
contract additionContract{ uint8 result; function Addition(uint) public { result = 2/3; //error result = 3.5 + 1.5; // final result will be an integer } }
Let’s do a subtle change,
Address
The address
data type is very specific to Solidity.
On the Ethereum blockchain, every account and smart contract has an address that is used to send and receive Ether from one account to another.
This is your public identity on the blockchain.
Also, when you deploy a smart contract on the blockchain, that contract is assigned an address that you can use to identify and call the smart contract.
There are two variants for the address type, which are identical:
address
– stores a 20-byte value (the size of an Ethereum address or account). The default value for the address is 0x…followed by 40 0’s, or 20 bytes of 0’s.address payable
– like address, but transfer and send with the additional members.
The idea behind this distinction is that the address payable is an address you can send Ether to, while you should not send Ether to a plain address, as it could be a smart contract that was not built to accept Ether.
contract ExampleAddress { address public myAddress = 0xc895t6ea1bc39595cf849612ffta7427f5792987
Enums
What stands for enumerable is a user-defined data type that restricts the variable to have only one of the predefined values.
These values listed in the enumerated list are called enums, and internally these enums are treated like numbers (resource). This makes the contract more readable and maintainable.
contract SampleEnum{ //Creating an enumerator enum animal_classes { Mammals, Fish, Amphibians, Reptiles, Birds } function getFirstEnum() public pure returns(animal_classes){ return animal_classes.Mammals; } // result: // 0: uint8: 0 }
With enums, we can also set a default value;
animal_classes constant defaultValue = animal_classes.Reptiles; function getDefaultValue() public pure returns(animal_classes) { return defaultValue; } } //result // result: // 0: uint8: 2
Bytes and Strings
A byte refers to signed 8-bit integers. Everything in memory is stored in bits with binary values 0 and 1.
Solidity supports string literals that use both double quotes ("
) and single quotes ('
). It provides String as a data type to declare a variable of type String.
Strings are unique in Solidity compared to Python or other programming languages in that there are no functions for manipulating strings, except that you can concatenate strings. The reason for this is that storing strings in a blockchain is very expensive.
Bytes and strings are easy to handle in Solidity because Solidity treats them similarly to an array. The two are very similar. (See Arrays in the Reference Type article).
Conclusion
Smart contracts reside at a specific address in the Ethereum blockchain. In this article, we learned about state variables in Solidity.
We looked at state, local variables, and the different types with a value type.
We tried to understand Boolean, Integers, Enums, Addresses, Bytes, and Strings (although the last ones are treated with more depth in reference types)
Bibliography
- https://docs.soliditylang.org/en/develop/introduction-to-smart-contracts.html
- https://www.tutorialspoint.com/solidity/solidity_variables.htm
- https://medium.com/coinmonks/learn-solidity-lesson-5-uint-types-and-visibility-fbebb221c311
- https://www.tutorialsteacher.com/csharp/csharp-value-type-and-reference-type
- https://www.geeksforgeeks.org/solidity-arrays/
- https://jeancvllr.medium.com/solidity-tutorial-all-about-bytes-9d88fdb22676
- https://www.thecrosschain.co/post/bytes-in-solidity
- https://www.c-sharpcorner.com/article/reference-types-in-solidity/