Contributed by Jordan – a Blockchain Developer Building on Celo, an EVM-compatible blockchain
What is Solidity?
Solidity is a contract-oriented, high-level programming language for writing and deploying smart contracts on the Celo, Ethereum, and any EVM-compatible blockchain.
Solidity is designed to target the Ethereum Virtual Machine (EVM) allowing contracts to be executed as bytecode on the blockchain. It also features a number of built-in functions and modifiers that allow contracts to interact with each other and the blockchain, as well as with external data sources.
Solidity is still a relatively new language to developers, and its features are subject to change. However, it is already being used to develop a wide variety of applications on the Celo network and other Ethereum Virtual Machine (EVM) Compatible networks.
Ethereum Virtual Machine (EVM)
Components of a Smart Contract
There are three essential components of a smart contract:
The code: The code is the heart of a smart contract and contains the logic that defines how the contract works
The data: The data is the information that the contract manipulates. This could be anything from the balance of a cryptocurrency account to the details of a property sale
The execution: The execution is the process that runs the code and manipulates the data. This is usually done by a computer, but it could also be done by a human
Solidity Environment Setup
Once the compiler is installed, it can be run from the command line.
$ solc <filename>
This will compile the Solidity source code in <filename> and produce an output file with the same name but with a .bin extension.
Solidity is statically typed, meaning that all variable declarations must be of a specific type. In addition, Solidity is compiled, meaning that it is not interpreted. This allows for increased security and performance.
When writing a smart contract in Solidity, you first need to create a contract object. This object contains the contract’s state and functions. State is information that is stored on the blockchain and functions are the methods that can be called to modify that state.
To create a contract in Solidity, you first need to create a file with a .sol extension. This file contains the contract’s source code. Then, you need to compile the contract into bytecode that can be run on the EVM. You can do this using the Solidity compiler, solc. Finally, you can deploy the contract to the Ethereum blockchain.
Importance of Comments in Solidity
Comments in Solidity are important because they document the code and help people understand it.
In Solidity, comments:
Help developers who will inherit that smart contract to understand it
Help a user of a smart contract that will consume your smart contract
1.) How to Write Comments in Solidity?
You can write single line comments an multi-line comments using // and /** … */
// This is a single line comment
This is a
Variable Types in Solidity
Solidity supports the following variable types:
:uint : Unsigned integer
: Unsigned integer int : Signed integer
: Signed integer bool : Boolean
: Boolean string : String
: String address : Address
: Address enum : Enum
: Enum struct : Struct
: Struct function : Function
: Function variable : Variable
Types of Variables in Solidity
In Solidity, there are 4 types of variables:
2.) Understanding Variables in Solidity
Solidity is a statically typed language. This means that every variable has a specific type, which is declared when the variable is initialized.
The following are the basic variable types in Solidity:
: boolean int: signed integer
: signed integer uint: unsigned integer
: unsigned integer string: string of Unicode characters
array a = [ uint(1), string(“Hello, world!”), uint(2), ];
The following are all invalid assignments in Solidity:
mapping (key: string) myMap;
struct MyStruct; array a;
The reason why these assignments are invalid is that they do not initialize the variables.
Variable Name Rules in Solidity
Solidity has the following variable name rules:
1. Variable names must start with a letter or an underscore
2. Variable names cannot contain spaces
3. Variable names cannot contain numbers
4. Variable names must be unique in a given scope
3.) Common Solidity Use Cases
Celo is a platform that enables developers to build decentralized applications (dApps) with a mobile first approach that enables users to access financial tools remotely.
Solidity is a language that enables developers to write code for these dApps.
Some common use cases for Celo and Solidity include:
Creating a new cryptocurrency – One of the most popular uses of Celo is to create a new cryptocurrency. This can be done by creating a new blockchain or by using Celo as a platform to build a new cryptocurrency.
Building a decentralized application – dApps are applications that are built on top of a blockchain.
Creating a decentralized Finance (DeFi) platform – Another popular use case for Celo is creating Decentralized Finance platforms.
Creating a decentralized file storage system – Ethereum can also be used to create a decentralized file storage system. This can be done by creating a new blockchain or by using Ethereum as a platform.
Solidity Tutorial: Bottom Line
In particular, we have looked at how Solidity supports different data types, variables, arrays, and structs. We have also seen how Solidity supports conditional statements and loops.
Finally, we looked at how Solidity can be used to create contracts.