Zero to Hero: Solidity - II
Contract Structures
Contract structures are similar to the concept of classes in object-oriented programming languages. Within this structure:
Functions
"Modifier functions" are used to control access to functions (only contract owner, accounts with a certain amount of balance, etc.)
"State variables" are permanently stored in the contract storage.
"Events" are used to inform other applications interacting with the contract about the outcome of an operation, which are logged by the EVM.
"Errors" are used to halt the transaction in case of any error or undesirable situation, and to inform applications or individuals interacting with the contract.
"Struct types" allow the creation of custom data types as needed.
"Enum types" are a data type consisting of a series of values called elements, members, enumerations, or enumerators.
Abstract contracts, on the other hand, have functionalities that can be implemented by other contracts inheriting from them according to their own needs. These contracts are not deployed themselves; instead, contracts inheriting from them can be deployed. Therefore, during development, when there is a need for one or more common features and some features will be used in different contracts with different functionalities, an abstract contract can be written to serve as a parent, and other contracts can inherit from this abstract contract to develop within a common framework.
When contracts have common-named methods and variables, interfaces can be used. Interfaces ensure adherence to a specific standard during development. Interfaces must have the following characteristics:
They cannot be inherited from other contracts; they can only be inherited from interfaces.
They do not have a constructor.
They do not have any state variables.
They do not have any modifiers.
To enable contracts to be coded within a standard framework, Ethereum Improvement Proposals (EIPs) are called by Ethereum. Based on certain criteria and community acceptance, standards beginning with ERC are developed.
Using these structures, many real-life applications can be developed. The most well-known examples are tokens with ERC20, ERC721, and ERC1155 standards.
Data Types
Solidity is a static language with basic data types similar to other languages. These types communicate with each other through operators. Values like 'null' or 'undefined' in languages such as Javascript or Java do not exist in Solidity. Instead, there are default values based on the type of data.
For example, when we want to represent an empty 'address', we use 'address(0)'. For the integer data type, it's '0', and for the boolean data type, it's 'false', and so forth.
Here are the data types:
Boolean data type: It has values of 'true' and 'false'.
Integer data type: It is divided into 'signed' and 'unsigned'.
The range of unsigned integer values is between '0' and '(2^256)-1'.
The range of signed integer values is from '-(2^255)' to '((2^255)-1)' due to allocating 1 bit for the sign.
Address data type: It's a specialized data type with a size of 20 bytes, representing an Ethereum address.
Byte array data type: It is used to store information as a byte array. It can be used in two different forms: fixed-size and dynamically-sized arrays.
Enum data type: It's a data type consisting of a series of values called elements, members, enumerations, or enumerators.
Fixed Point data type: This data type, commonly used in other languages, is not fully supported by Solidity.
Contract data type: Each contract has its own data type and can be converted to each other through inheritance.
String data type: This data type has an ASCII character set, and each character is used as a byte. It is used by writing within single or double quotes.
Unicode and Hexadecimal data types: They are used by adding the expressions 'unicode' or 'hex' in front. For example, 'hex "44556677"' and 'unicode "Hello"'.
User-defined data type: It is defined as 'type C is V'. It is used when creating a new data type with inheritance from a desired data type.
Function data type: It's a special data type for functions.
Reference Types:
These are data types that hold the location of the data and do not directly share the data. Each reference type has an additional explanation called "data location" about where it is stored. These data locations are 'memory', 'storage', and 'call data'.
Array data type: It is used to store information in array form. It can be used in two different forms: fixed-size and dynamically-sized arrays.
Struct data type: It is used to define new data types. Inside, we can use types specific to Solidity, as well as the "data type created via inheritance" or "data type created with struct" that we defined earlier.
Mapping Types:
Mapping data type: These are data types defined as 'mapping(KeyType => ValueType) variableName'. The "data location" value of these data types is used as 'storage'.
Iterable Mapping: Operations cannot be performed on mappings based on indexes as in arrays. For this purpose, an iterable mapping structure is applied, and operations can be performed on mapping data types.