Solidity types and variables
Contract would need some state. Variables store the state information.
Solidity is a statically typed language. So you always need to declare the variable type before the variable name.
Types
Boolean
bool : true or false
Operators:
- Logical : ! (logical negation), && (AND), || (OR)
- Comparisons : == (equality), != (inequality)
Integer
- Unsigned : uint8 | uint16 | uint32 | uint64 | uint128 | uint256(uint)
- Signed : int8 | int16 | int32 | int64 | int128 | int256(int)
uint defines an unsigned integer of 256 bits by default.
You can also specify the number of bits, by range of 8 bits.
Here are some examples below:
Type | Number range |
---|---|
uint8 | 0 to 255 |
uint16 | 0 to 65,535 |
uint32 | 0 to 2^32 |
uint64 | 0 to 2^64 |
uint128 | 0 to 2^128 |
uint256 | 0 to 2^256 |
Operators:
- Comparisons: <=, <, ==, !=, >= and >
- Bit operators: &, |, ^ (bitwise exclusive or) and ~ (bitwise negation)
- Arithmetic operators: +, -, unary -, unary +, *, /, %, ** (exponentiation), << (left shift) and >> (right shift)
Address
address: holds an Ethereum address (20 byte value).
address payable : same as address, but includes additional methods transfer and send
Operators:
- Comparisons: <=, <, ==, !=, >= and >
Methods:
<address>.balance (uint256)
: balance of the Address in Wei<address>.transfer(uint256 amount)
: send given amount of Wei to Address, throws on failure<address>.send(uint256 amount) returns (bool)
: send given amount of Wei to Address, returns false on failure<address>.call(...) returns (bool)
: issue low-level CALL, returns false on failure<address>.delegatecall(...) returns (bool)
: issue low-level DELEGATECALL, returns false on failure
delegatecall uses the code of the target address and other aspects (storage, balance, …) from the calling contract. The purpose of delegatecall is to use library code which is stored in another contract. The user has to ensure that the layout of storage in both contracts is compatible for use of delegatecall.
Array
Arrays can be dynamic or have a fixed size.
uint[] dynamicSizeArray;uint[5] fixedSizeArray;
Byte arrays - fixed
bytes1(byte), bytes2, bytes3, …, bytes32
Operators:
- Comparisons: <=, <, ==, !=, >=, > (evaluate to bool) Bit operators: &, |, ^ (bitwise exclusive or), ~ (bitwise negation), << (left shift), >> (right shift) Index access: If x is of type bytesI, then x[k] for 0 <= k < I returns the k th byte (read-only).
Members
- .length : read-only
Byte arrays - dynamic
- bytes: Dynamically-sized byte array.
It is similar to byte[], but it is packed tightly in calldata.
String
string: dynamically-sized UTF-8-encoded string.
It is equal to bytes but does not allow length or index access.
Enum
Enum works just like in every other language.
enum ColorList {Red,Blue,Green,Yellow}ColorList color = ColorList.Red;
Struct
New types can be declared using struct
struct User {address addr;string name;string email;uint age;}User userAna;
Mapping
A list of key and values which each key has one value.
Mappings can be seen as hash tables which are virtually initialized such that every possible key exists and is mapped to a value.
Key can be almost any type. Exceptions: mapping, dynamically sized array, contract, enum, struct.
Value can be any type, including mappings.
Declared as mapping(_KeyType => _ValueType)
Global variables
Transaction variables
- msg.data (bytes): complete calldata
- msg.gas (uint): remaining gas
- msg.sender (address): sender of the message (current call)
- msg.sig (bytes4): first four bytes of the calldata (i.e. function identifier)
- msg.value (uint): number of wei sent with the message
- tx.gasprice (uint): gas price of the transaction
- tx.origin (address): sender of the transaction (full call chain)
Block variables
- block.blockhash(uint blockNumber) returns (bytes32): hash of the given block - only works for the 256 most recent blocks excluding current
- block.coinbase (address): current block miner’s address
- block.difficulty (uint): current block difficulty
- block.gaslimit (uint): current block gaslimit
- block.number (uint): current block number
- block.timestamp (uint): current block timestamp as seconds since unix epoch
- now (uint): current block timestamp (alias for block.timestamp)
Final considerations
The purpose of this lesson was to present the types and variables in Solidity programing language.
In the next lesson you will create a smart contract to use some of them.