Lecture
Solidity — is an object-oriented, domain-specific programming language for self-executing contracts on the Ethereum platform.
Smart contract (English: Smart contract) — is a computer algorithm designed to form, control, and provide information about the ownership of something. Most often this refers to the use of blockchain technology. In a narrower sense, a smart contract is understood as a set of functions and data (the current state) located at a specific address on the blockchain .
Ethereum smart contracts are developed in one of the languages designed to compile into the bytecode of the Ethereum virtual machine — Solidity (similar to C or JavaScript), Vyper and Serpent (similar to Python), LLL (a low-level version of Lisp), Mutan (based on Go) .
The parties sign the smart contract using methods similar to signing a transfer of funds in existing cryptocurrency networks. Once signed by the parties, the contract is stored on the blockchain and takes effect. To ensure the automated execution of the contract's obligations, an execution environment (Ethereum blockchain nodes) is absolutely required, which makes it possible to fully automate the execution of the contract's clauses. This means that smart contracts can exist only within an environment that gives the executable code unimpeded access to the smart contract's objects.

Diagram of the architecture of an Ethereum node
All the terms of the contract must have a programmatic description and clear execution logic. For this reason, the first smart contracts had the task of formalizing the simplest relationships, consisting of a small number of conditions. Having unimpeded access to the contract's objects, the smart contract tracks, according to the specified conditions, the fulfillment or violation of clauses and makes independent decisions based on the programmed conditions. Thus, the core principle of a smart contract lies in the full automation and reliability of the execution of contractual relationships .
For smart contracts to be able to exist, certain conditions are required:
The language was proposed in August 2014 by Gavin Wood (Gavin Wood ). Later, development of the language was carried out under the direction of Christian Reitwiessner by the Solidity team as part of the Ethereum project. It is one of four languages (alongside Serpent, LLL, and Mutan) designed to compile into the bytecode of the Ethereum virtual machine. It became widespread with the emergence of blockchain technologies, in particular the Ethereum-based technology stack, for creating smart contract software.
Contract code
Ethereum not only stores transaction data on the chain, but can also store the contract code on the chain.
At the database level, the role of the blockchain is to store transaction data. So what, then, is the logic of voting for candidates or obtaining voting results? In the world of Ethereum, you can use the Solidity language to write business logic / application code (that is, contract: Contract), then compile the contract code into Ethereum bytecode and deploy the bytecode to the blockchain. :

Other languages can be used to write contract code, but Solidity is by far the most popular choice.
Ethereum Virtual Machine
It is important to note that the Ethereum virtual machine is fully isolated. This means that code currently running in the EVM has no access to the network or the file system, and can access other contracts only in a limited way.
The Ethereum blockchain not only stores data and code, every node also contains a virtual machine (EVM: Ethereum Virtual Machine) for executing the contract code - it sounds like a computer's operating system.
In fact, this is the key point that distinguishes Ethereum from Bitcoin: the existence of the virtual machine led to the emergence of blockchain 2.0, and also made the blockchain the first developer-friendly platform for developers. ,
A statically typed, JavaScript-like programming language created for developing self-executing contracts that run on the Ethereum Virtual Machine (EVM). Programs are compiled into EVM bytecode. It allows developers to create self-contained applications containing business logic that results in irreversible blockchain transaction records.
The use of ECMAScript syntax, by Wood's design, was meant to help the language gain acceptance among actual web developers. However, unlike ECMAScript, the language received static typing of variables and dynamic types of return values. Compared to Serpent and Mutan, which compile to the same bytecode, the language has important differences. Complex contract variables are supported, including arbitrary hierarchical mappings and structs. Contracts support inheritance, including multiple inheritance and C3 linearization. A binary application programming interface (ABI) is supported, providing many type-safe functions in each contract (it subsequently also appeared in Serpent). A code documentation system for user-facing explanation of the call sequence has been specified, called the «Ethereum Natural Specification Format». In Solidity, instead of the usual classes, contracts are declared (contract). There are libraries for writing smart contracts, such as: Open Zeppelin, Truffle. The libraries allow you to create your own coin (token) based on ready-made templates, with all the specifications (ERC20) and security checks (the safemath library).
Contracts in Solidity can inherit from one another. This means that the functions and variables of the contract we inherit from will be available in the inheriting contract. In Solidity, just as in C++, there is multiple (diamond) inheritance.
Example of a program in the Solidity language :
DAPP Architecture
The following figure shows the decentralized architecture of an Ethereum-based application:

DAPP architecture diagram
Each client (browser) interacts with the corresponding node application instance, rather than requesting services from a centralized server.
In an ideal decentralized environment, everyone who wants to interact with a DApp should run a full blockchain node on their computer or mobile phone - in short, everyone runs a full node , This means that before actually using the decentralized application, users must download the entire blockchain.
However, we do not live in a utopia. It is unrealistic to expect every user to run a full node before using your application. But the core idea of decentralization is that it does not depend on a centralized server. That is why some solutions have appeared in the blockchain community, such as Infura, which provides publicly accessible blockchain nodes, and Metamask, a browser plugin. Thanks to these solutions, you don't need to spend a lot of hard drive space, memory, and time downloading and running a full blockchain node, and you can still take advantage of decentralization.
all Solidity contracts contain the following elements:
To program smart contracts for production environments, you will need to become more familiar with them, but here you will get general information that will serve as a starting point for studying this technology.
Once you understand these principles, you will be able to start creating your own smart contracts for a wide variety of purposes!
Pragma — is a keyword that the compiler uses to check whether the Solidity version matches the required one. If the versions match, the file can be executed. Otherwise, the compiler returns an error.
The contract definition must always include the latest version of Solidity. Information about the current version can be obtained on the Solidity website. The most recent version must be used in the source file.
The version-defining Pragma directive looks as follows:

This line indicates that a compiler version from 0.7.0 to 0.7.9 will be used to compile the source file. As a result of the changes that will be made in version 0.8.0, compilation of this source file will most likely fail.
State variables are the fundamental elements of any Solidity source file. The values of state variables are permanently stored in the contract's storage.

Note
At the beginning of the contract's source file, the definition contract ContractName is always specified.
This example uses the state variable price with type uint. The integer type uint indicates that this variable can hold a 256-bit unsigned integer. Thus, it can store positive numbers in the range from 0 to 2256–1.
In the definition of any variable, its type and name must be specified.
In addition, you can set the visibility of a state variable as follows:
Within a contract, executable units of code are called functions. Functions are used to describe individual actions needed to accomplish an overall task. Functions can be reused and called from other source files, such as libraries. The behavior of functions in Solidity is similar to other programming languages.
Below is a simple example of a function definition:

This code shows a function named buy, which has public visibility and is therefore accessible from other contracts. The following function visibility specifiers are supported: public, private, internal, and external.
A function call can be either internal or external (from another contract). Functions can accept parameters and return variables, which are used to pass parameters and values between them.
Below is an example of a function that takes the integer parameter price and returns an integer:

Modifiers can be used to change the behavior of functions. If a modifier is set, the corresponding condition is checked before the function executes. For example, in this way a function can check whether the user has seller rights before listing an item for sale.
The following elements are used in this example:
onlySeller, which specifies that only the seller can list an item for sale._;, which indicates where the function body is inserted.onlySeller.The following modifiers can also be used in a function definition:
Events are used to describe actions that take place within a contract. Like functions, events use parameters that must be specified when they are invoked.
To trigger an event, you must use the emit keyword, specifying the event's name and its parameters.
When an event is triggered, it is recorded as a transaction in the transaction log, which is a special data structure on the blockchain. Such logs are linked to the contract's address, included in the blockchain, and remain there forever. The log and the event data it contains are not accessible from contracts and cannot be modified.
Value types pass values and are copied when used. The main value types that will be
used when writing contracts include integers, booleans, string literals, addresses, and enumerations.
Integer types are used in every Solidity source file. They represent whole numbers and can be signed or unsigned. Integer types can hold from 8 to 256 bits.
If the number of bits is not explicitly specified, it defaults to 256 bits.
The following operations can be applied to integers:
<=, <, ==, !=, >=, >& (and), | (or), ^ (bitwise exclusive), ~ (bitwise negation)+ (addition),- (subtraction), * (multiplication), / (division), % (modulo), ** (exponential)Below are examples of integer value definitions:

Boolean values are defined using the bool keyword. They always have either the value true or false.
Below is shown how they can be defined:

Boolean values are usually used in comparison statements. Example:

In addition, boolean values can be used in function parameters and return value types.

String literals are also used in most contract files. They represent a sequence of characters or words enclosed in double or single quotes.

In addition, the following escape characters can be used together with string literals:
\ — escape sequence for a new line\n — new line\r — carriage return\t — tabAddress — is a type with a 20-byte value that represents an Ethereum user account. To define an address, you can use the regular type address or address payable.
The difference between them is that the address payable type defines an address to which "ether" can be sent, for which it uses the additional members transfer and send.

In the Solidity language, you can use enumerations to create a user-defined data type. As the name implies, the values contained in such types are defined by the contract's creator. Enumerations can be used to define a set of choices, one of which is mandatory.
For example, an enumeration can be used to specify a set of possible states of an element. For convenience, an enumeration can be thought of as several predefined answer options to a question, one of which must be chosen. Enumerations can be declared within contract or library definitions.

To write contracts, you will also need to understand the principles of using reference types.
Unlike value types, which always pass an independent copy of the value, reference types point to the location of the data for the value. The following reference types are supported: structs, arrays, and mappings.
When using a reference type, you must explicitly specify the storage location for its data. The data location for a type can be specified in the following ways:
memory:
storage:
calldata:
Reference types always create an independent copy of the data.
Below is an example of using a reference type:

Arrays are used to store similar data in a data set structure. The size of an array can be fixed or set dynamically. Array indices start at 0.
To create a fixed-size array k containing elements of type T, you should use the expression T[k]. Dynamically sized arrays are defined as follows: T[].
Array elements can have any type. For example, an array can contain elements of type uint, memory, or byte. Arrays can also include mappings or structs.
The following example shows how to create an array:

The following members can be used to manage arrays and obtain information about them:
Below are several examples.
Structs — are custom types that a user can use to define a representation of real-world objects. As a rule, structs are used as a schema or to represent records.
Examples:
Mappings represent key-value pairs that are encapsulated or packaged together. The closest analogs of mappings are dictionaries or JavaScript objects. As a rule, mappings are used to model real-world objects and for faster data lookup. Thanks to support for various value types, including complex ones such as structs, this type is universal and easy for humans to understand.
The following code example uses the struct Items_Schema and stores a dictionary with a list of items represented by the struct Items_Schema. Thus, mappings can be used to simulate a database.
Note
In the mapping signature uint256 => Items_Schema, it is specified that the keys are of unsigned integer type, and the values are the struct Items_Schema.
Let's take a closer look at the main components of this smart contract:
buyer, seller and balances;ListItem and PurchasedItem;ItemAvailable and ItemPurchased.msg.sender as the seller and sets the initial state to ItemAvailable. This constructor is called when the contract is created.buy function, which takes three parameters: seller, buyer, and price. Before making the purchase, it is verified that the buyer has sufficient funds. If so, money is transferred from the buyer to the seller, after which a message is issued.
event PurchasedItemEvent(). All required arguments are specified inside the parentheses.
Comments