Blockchain Technology and Web3 has become a buzz word and a lot of developers are switching or planning to switch to web3. One of the most popular blockchain technology is Ethereum and the programming language used to code its smart contract is solidity. Learning solidity is a step in the right direction in your journey to becoming a Web3 developer. To read more on blockchain checkout Blockchain for Beginners
In this tutorial, you will learn basic concepts in the solidity programming language and take a step to start building decentralized applications.
Prerequisites
Before we continue it is recommended to have the following.
- A basic knowledge in any programming language
Introduction
What is Solidity?
Solidity is an object-oriented programming language for writing smart contracts on Ethereum. It is influenced by JavaScript, Python, and C++, and Solidity targets the Ethereum Virtual Machine (EVM). Solidity is the programming language of Ethereum.
What is Ethereum?
Ethereum is a decentralized opensource blockchain technology that lets you send cryptocurrency to anyone for a small fee. Ether is the native cryptocurrency of Ethereum. It also powers applications that everyone can use and no one can takedown.
What is an Ethereum Virtual Machine (EVM)?
The Ethereum Virtual Machine (EVM) is a powerful, sandboxed virtual stack embedded within each full Ethereum node, responsible for executing contract bytecode. Contracts are typically written in higher-level languages, like Solidity, then compiled to EVM bytecode.
What is a Smart Contract?
Smart contracts are simply programs stored on a blockchain that run when predetermined conditions are met. They typically are used to automate the execution of an agreement so that all participants can be immediately certain of the outcome, without any intermediary’s involvement or time loss. They can also automate a workflow, triggering the next action when conditions are met.
Let's get started!
After creating a solidity file that ends with .sol, the first requirement is to add a comment indicating the license of the code(open source or not).
// SPDX-License-Identifier: MIT
The next line to be added to the file is the pragma indication of the version of solidity that you are using. This will prevent problems during compilation.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;
Contracts
They are starting point of your solidity project. It is the building block of all Ethereum programs. It is similar to classes in object-oriented languages and will contain the variables, functions, and others.
An empty contract looks like this -
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract HelloWorld {
}
State Variables
They are a means of holding values in the smart contract. They are permanently stored in the contract storage, the implication of this is that they are written to the Ethereum blockchain, which is similar to writing to a database. Let's create a state variable named amount.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract HelloWorld {
uint amount = 100;
}
I added uint(unsigned integer) in front of the amount, that is the data type of the state variable
Data Types
Data types in Solidity represent the type of data the program is to use. The type can be numeric, alphanumeric, decimal, bool, etc.
These attributes tell the program compiler how the programmer intends to use the data by constraining the values that a variable or a function might use. Solidity is a statically typed language which means the type of each variable needs to be specified throughout your code.
Data types instruct the compiler to check the usage of the variables in your contract. Declared data types have default values referred to as zero state (0).
For example, a boolean’s default value is False and a uint’s default value is 0. The concept of undefined or null values does not exist in solidity. So a data type will have some value (for example – hi, 123, true, etc.) or 0 as a default value.
Examples of data types found in solidity
String, Integer(signed and unsigned), Address, Fixed Point Number(signed and unsigned), Byte, Structs, and others.
Structs
In some cases, a special data type is needed. Structs are created for this purpose. It allows us to create complicated data types that have properties. The properties inside the struct will also have their types, Let's add a Car Struct to our HelloWorld Contract.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract HelloWorld {
uint amount = 100;
struct Car {
uint amout;
string name;
}
// To create a new Car
Car bmw = Car(10000, "BMW");
}
From the code above we declared our Car struct and created a new Car named bmw. We can also see that we used the Car Struct as a datatype when creating the new car.
Arrays
Arrays or lists are popular in various programming languages, anytime we need to store a collection of items, arrays come to mind. We have two types of array in solidity, fixed arrays, and dynamic arrays.
Fixed arrays have a fixed size.
Dynamic arrays have no fixed
size.
Let's create an array and store a few cars in it.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract HelloWorld {
uint amount = 100;
struct Car {
uint amout;
string name;
}
// To create new Cars
Car bmw = Car(10000, "BMW");
Car Toyota = Car(2000, "Toyota");
Car Honda = Car(3000, "Honda");
//Create a fixed array named fixedCars
Car[2] fixedCars;
//Create a dynamic array named dynamicCars
Car[] public dynamicCars;
function addCars(Car memory newCar) public{
//The push method is used to add items to the array,
dynamicCars.push(newCar);
}
}
we can also declare our arrays as public, this means that other contracts of the blockchain will be able to access it.
i.e lets create a publicCarList
Car[] public publicCarList;
Functions
Functions are very handy when writing code, it helps to break down our code into blocks. Solidity functions are not different. Let's add some functions to our HelloWorld Contract.
In solidity, arguments passed to functions can be done either by value or reference (memory). Passing by reference is compulsory for all reference types such as arrays, structs, mappings, and strings.
By value, which means that the Solidity compiler creates a new copy of the parameter's value and passes it to your function. This allows your function to modify the value without worrying that the value of the initial parameter gets changed.
By reference, which means that your function is called with a... reference to the original variable. Thus, if your function changes the value of the variable it receives, the value of the original variable gets changed.
We will create a createCar function, it will take two parameters, amount and name. Our function will also be public.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract HelloWorld {
uint amount = 100;
struct Car {
uint amout;
string name;
}
// createCar function
// adding underscore to the arguments is a covention
// removing it will have no effect on the code
function createCar(uint _amount, string memory _name) public pure returns (Car memory _car) {
Car memory newCar = Car(_amount,_name);
return newCar;
}
// The above function will return the newCar variable
}
It is not advisable to make our functions public, this will give other contracts access to it, we should make our functions private by default.
function createCar(uint _amount, string memory _name) private pure returns (Car memory _car) {
Car memory newCar = Car(_amount,_name);
return newCar;
}
TypeCasting
This is the conversion of one datatype to another.
uint b = 10;
uint8 c = uint8(b);
Conclusion
The best way to understand the concepts is to practice and build projects.
Part 2 of this article will explain concepts such as events, mappings, data locations, returning multiple values, constructors, function modifiers, solidity time, and import
Link to Part Two - A friendly beginners guide to solidity (Part Two)
I hope you found this tutorial useful.
Thank you for reading.
# References
Top comments (4)
Lovely post Joshua.
But you can improve the readability of the code with syntax highlighting by adding 'solidity' to the code block markdown tag i.e
Rather than this
Done, thanks
I just started out on solidity, I copy and pasted your code onto my IDE and it gave some parsing errors, I wanna know if its my debugger or its something I am missing. Thank you
Hello Tobi, can you point out the exact one, so i can check it and rectify it