DEVELOPER PERSPECTIVE: A Complete Guide to Solidity

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 Concepts

Solidity is a contract-oriented, high-level programming language for writing and deploying smart contracts on the Celo, Ethereum blockchain. It is an extension of the Javascript programming language with a syntax very similar to that of C++.

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.

_________________

Keywords:

Celo

Ethereum Virtual Machine (EVM)

Smart Contracts

_________________

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.

___________________________________________________________________

SEE ALSODEVELOPER PERSPECTIVE: How to Become a Solidity Blockchain Coding Ninja

___________________________________________________________________

Working of Solidity 

  • 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

multi-line comment

*/

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: 

1. Constants 

2. Variables 

3. Structs 

4. Arrays

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:

bool: boolean 

: boolean int:  signed integer 

: signed integer uint:  unsigned integer 

: unsigned integer string:  string of Unicode characters 

: string of Unicode characters bytes:  byte array 

: byte array address:  Ethereum address

: Ethereum address mapping (key: value): associative array 

: associative array struct: user-defined data structure 

: user-defined data structure array: array of any type 

Initializing Variables 

Variables must be initialized when they are declared.

The following are some examples of variable initialization in Solidity:

bool b = true; 

int i = 123; 

uint u = 456; 

string s = “Hello, world!”; 

bytes bs = bytes32(“Hello, world!”); 

address addr = 0x12345678; 

mapping (key: string) myMap = {“foo”: “bar”, “baz”: “qux”}; 

struct MyStruct { uint a; string b; } 

MyStruct myStruct = { a: uint(1), b: “Hello, world!” };

array a = [ uint(1), string(“Hello, world!”), uint(2), ]; 

The type of a variable can be inferred from the expression on the right-hand side of the assignment operator. For example, the following are all valid assignments in Solidity:

bool b = true; 

int i = 123; 

uint u = 456; 

string s = “Hello, world!”; 

bytes bs = bytes32(“Hello, world!”); 

address addr = 0x12345678; 

mapping (key: string) myMap = { “foo”: “bar”, “baz”: “qux” }; 

struct MyStruct { uint a; string b; } 

MyStruct myStruct = { a: uint(1), b: “Hello, world!” }; 

array a = [ uint(1), string(“Hello, world!”), uint(2), ]; 

The following are all invalid assignments in Solidity: 

bool b; 

int i; 

uint u; 

string s; 

bytes bs; 

address addr; 

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 

We have seen that Solidity is a high-level language that is used to write contracts for the Ethereum Virtual Machine. It is similar to JavaScript but also has some unique features.

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.

___________________________________________________________________

RECOMMENDED READINGDEVELOPER PERSPECTIVE: Why Use NoSQL Together with Blockchain Technology?

___________________________________________________________________

Thank you for your support by helping us create content:

BTC address: 3CW75kjLYu7WpELdaqTv722vbobUswVtxT

ERC20 Address: 0xBc11cFa590C6CC465248D77d399C3106B881cC0E

SOL address: 9cC65AWFHj848kntcoyiT8av3jiRQEqyTTmBR1GvVUb

____________________________________________________________________

Follow us on Twitter for latest posts and updates

Join and interact with our Telegram community

_____________________

Subscribe to our YouTube channel below to learn more:

________________________________________________________________