Zero to Hero: Solidity - I
Solidity is a programming language used to develop smart contracts on blockchain platforms compatible with Ethereum and the Ethereum Virtual Machine (EVM).
EVM-compatible blockchains are platforms that can process similar transactions and data to smart contracts on the Ethereum network. These networks generally offer higher speed, capacity, and lower costs compared to Ethereum. Their energy consumption per transaction is also significantly lower. Some of the well-known EVM-compatible blockchains include:
Avalanche Mainnet / Fuji
Binance Smart Chain
Ethereum Classic
Ontology Mainnet
EOS Mainnet
Polygon Mainnet
Fantom Opera
Theta Mainnet
Celo Mainnet
Harmony Mainnet
xDAI Chain
Evmos (Cosmos Ecosystem)
There are more than 100 EVM-compatible blockchains available. You can view and directly add other EVM-compatible blockchains from chainlist.org. After learning the Solidity language, we can use it to develop smart contracts on all EVM-compatible blockchains.
Solidity was initially developed as a high-level language in 2014 by Gavin Wood (co-founder of Ethereum, founder of Web3 Foundation, and founder of Polkadot) along with Christian Reitwiessner, Alex Beregszaszi, Liana Husikyan, and Yoichi Hirai.
Solidity is an object-oriented language designed to develop smart contracts that run on the Ethereum Virtual Machine (EVM). It uses curly braces {} for block delimiters and is statically typed (the type of a variable is known at compile time). It supports inheritance, libraries (reusable code), and complex user-defined data types. Variable declarations, loops, function overloading, type conversions, and many other features are influenced by C++. Python and JavaScript inspire some features.
The primary component on which we execute Solidity code is the Ethereum Virtual Machine (EVM). We can think of the EVM as a virtual computer on the blockchain that runs the smart contracts we create using the Solidity language.
I'm starting a series of articles delving into the details of Solidity. For now, I'm leaving a sample Solidity code below.
Smart Contract Basics
Contract:
A legal instrument that requires at least two or more parties to express their mutual and corresponding will to create a result, supported by law. A protocol, on the other hand, is a mutually accepted agreement that explains the implementation of contracts in summary. For a contract to be valid, the parties to the contract must have entered into the contract with their free will and consciously.
Smart Contract:
A self-executing software/program managed by a peer-to-peer computer network that can reliably and consistently execute transactions and agreements between parties, with changes not possible retrospectively. Smart contracts provide a coordination and implementation framework for agreements among network participants without the need for traditional legal contracts. In this article, I want to approach smart contracts from a more general perspective, although I will delve into the technical details of smart contracts extensively in the Solidity series.
A smart contract is embedded in software code managed by a blockchain, making it self-executing. The smart contract code contains a set of rules (Protocol) that the parties agree to interact with each other. When these predefined rules are met, the contract is automatically enforced. For example, when I want to stake the tokens I hold, a smart contract locks the specified amount of tokens, and at the end of the designated staking period, it releases them again. Smart contracts provide mechanisms to efficiently manage tokenized assets and access rights between parties. You can think of them as cryptographic boxes that, when specific predefined conditions are met and fulfilled, grant access to the value or any resource specified in the smart contract. The values and access rights managed by smart contracts are stored on the blockchain. Therefore, smart contracts provide a public and verifiable way to embed governance rules and business logic into a few lines of code, which can be audited and enforced by the majority consensus of a peer-to-peer network.
When implemented correctly, smart contracts can provide superior transaction security compared to traditional contract law, reducing coordination costs for contract auditing and enforcement. The parties to the contract can monitor the performance of the smart contract in real time and save costs associated with contract enforcement.
Security:
Smart contract security is still an issue that needs to be addressed at a technical level. We will need to be able to implement more complex contract clauses along with decentralized dispute-resolution mechanisms. Some of these tools include:
Kleros: Decentralized arbitration service
OpenLaw: Project enabling legal contracts on Ethereum
Jur: Decentralized dispute resolution system
It seems that it may take more time for such developments to mature. As technology advances, becomes more widespread, and legal standards are adopted, we will likely see a convergence of legal contracts and smart contracts.
Use Cases: With smart contracts, any agreement, process, task, and payment can have a digital record and signature that can be defined, verified, stored, and shared. Use cases for smart contracts range from simple (transferring money from one user to another) to complex (DAO structures). Formal and semi-formal records (land records, birth certificates, school grades, report cards, and diplomas) can serve as simple technological use cases. Smart contracts can also be used for much more complex agreements that manage a group of people sharing common interests and goals across the supply chain of goods or services without the need for traditional centralized institutions. Decentralized Autonomous Organizations (DAOs) are an example of this and likely represent the most complex smart contracts. A smart contract formalizes governance rules (articles of association, bylaws, procedural rules, or a company's memorandum of association) and replaces daily operational management with self-executing code. For example, a decision made within a DAO is put to a vote, and if the vote is positive, a smart contract is automatically triggered; if it's negative, another smart contract is triggered. Smart contracts can be used in many fields, including banking, insurance, energy, e-government, telecommunications, the music industry, art, sports, and education.
How Smart Contracts Work:
Smart contracts must be written in the programming language used for smart contract development on the blockchain on which they will be executed. As mentioned in this article, with the Solidity programming language, we can write smart contracts on more than 100 EVM-compatible blockchains. With the Rust programming language, we can write smart contracts on Solana, Polkadot, and Near blockchains.
Once smart contracts are written, they are deployed to the relevant blockchain, and the parties to the contract can review the contract code and its current state on the blockchain. For example, to review smart contracts on the Ethereum blockchain, we can use the "Ethereum Blockchain Explorer" service provided by Etherscan. By entering the address of the smart contract we want to review, we can examine the contract code and even interact with the relevant smart contract via Metamask. Smart contracts are triggered and executed by internal or external events and produce an output agreed upon by all parties involved.
Solidity Development Environments
Solidity development is supported by various tools that are essential for programmers during coding sessions. When we look back at the historical progression of these tools, we can observe significant changes over time. Initially, there were no development environments, but now we have AI-powered tools like Github Copilot and Codota in our arsenal. Nowadays, programmers can effortlessly perform various functionalities directly in their development environments, which they previously had to think about and take action on. In my opinion, these auxiliary programs, which are necessary for developing software components, should be the first to learn. They enable us to observe language features more clearly and mentally adapt better, activating our compiler in the brain.
One of the simplest examples of these programs is the text editor, which allows us to edit plain text. Notepad and Sublime Text are common examples of such editors. These tools, by enabling both note-taking and modifications on those notes, are among the most commonly used programs in daily life. These editors allow us to save files in multiple formats, making them suitable for basic coding tasks. For instance, Windows users can code their batch files via these editors and save them with the ".bat" file extension, indicating to Windows that it is a batch file and should be executed accordingly.
Before diving into development environments, let's understand what an Integrated Development Environment (IDE) is. IDEs integrate multiple components to provide capabilities beyond simply performing a single task. As for development environments, they represent the set of tools necessary for the development of a particular software component. IDEs should include components like a text editor, debugging tools, a runtime environment, and tools for automatic documentation generation.
Plugins, on the other hand, enhance the functionalities of a main program, providing extra features that are not inherently available. They streamline tasks that developers could otherwise perform with extra effort or address the needs that developers may have missed during the analysis phase. For further details, you can refer here.
Now, let's delve into some of the most widely used Solidity development environments and explore the concepts we'll use when describing these environments together.
Text Editor:
A text editor is a computer program that allows us to edit plain text. Notepad and Sublime Text are common examples of such editors. These editors are often used in daily life because they enable both note-taking and modifications on those notes. They also allow us to save files in multiple formats, making them suitable for basic coding tasks. For instance, Windows users can code their batch files via these editors and save them with the ".bat" file extension, indicating to Windows that it is a batch file and should be executed accordingly.
IDE:
Before discussing development environments, let's understand what an Integrated Development Environment (IDE) is. As the name suggests, IDEs integrate multiple components to provide capabilities beyond simply performing a single task. Development environments represent the set of tools necessary for the development of a particular software component. IDEs should include components like a text editor, debugging tools, a runtime environment, and tools for automatic documentation generation.
Plugin:
A plugin, as the name suggests, is an add-on software that provides additional features to a main program, enhancing its functionalities. Plugins cannot function independently and require the main program to operate. They streamline tasks that developers could otherwise perform with extra effort or address the needs that developers may have missed during the analysis phase.
These explanations will lay the groundwork for understanding the various Solidity development environments we'll discuss next.
REMIX IDE:
Remix is a browser-based Integrated Development Environment (IDE) that allows us to create and test smart contracts for all blockchain platforms supporting Solidity and the Ethereum Virtual Machine (EVM). It can be used online at the https://remix.ethereum.org/ website or downloaded for offline use. For offline usage, a zip file is available for download, allowing users to run it locally.
Remix provides numerous plugins, such as DGIT and DGIT DIFF, which enable us to send our Solidity code to our GitHub repository.
VISUAL STUDIO CODE (VS CODE):
Visual Studio Code, developed by Microsoft for Windows, Linux, and MacOS, is a source code editor that includes features such as debugging, built-in Git control, syntax highlighting, intelligent code completion (IntelliSense), snippets, and code refactoring support. It combines the simplicity of a source code editor with powerful developer tools like IntelliSense code completion and debugging.
VS Code is the most preferred IDE worldwide, with a high percentage of developers using it, according to the Developer Survey conducted by Stack Overflow. I use VS Code for frontend and backend development, as well as for Solidity and Rust programming in the blockchain domain.
Some key features of VS Code include:
Rich code refactoring support.
Platform-independent code development support.
Rich code recognition (code IntelliSense).
Debugger support.
Speed.
GANACHE:
Ganache creates a local Ethereum blockchain, allowing us to simulate transactions and test our developments in a local environment. It provides a testing environment where we can deploy contracts, develop applications, and run tests.
HARDHAT:
Hardhat is one of the most commonly used Solidity development environments and is highly preferred among developers. It offers various features, including:
Setting up our local blockchain with Ganache to simulate our developments.
Creating our faucet to provide gas fees needed for testing.
Deploying our contracts to testnets like Kovan, Ropsten, Rinkeby, and mainnets like Ethereum, BSC, Polygon, and Avalanche.
Verifying our contracts on Etherscan for public visibility.
Estimating gas consumption for our contracts using the "hardhat-gas-reporter" plugin.