This Solidity tutorial will guide you on how to write your first smart contract.
Prerequisites
Before you start with this Solidity tutorial, you need to have a basic understanding of programming concepts like functions, variables, and know what an IDE is.
What are Smart Contracts?
Smart contracts are functions that are deployed and executed on the blockchain only when a specific condition is satisfied, without the involvement of any third parties.
Because smart contracts are immutable and distributed by nature, they cannot be modified or updated after they’ve been written and deployed. Also, distributed in the sense that anyone can check and look at the smart contract status and transaction histories on the blockchain.
How to Build Smart Contracts?
Smart contracts may be written in a variety of programming languages, including Javascript, Rust, Go, and Yul, although Solidity is the most widely used and official smart contract language.
What is Solidity?
Solidity is an object-oriented, high-level, and compiled programming language for writing smart contracts. Solidity is easier for anyone with JavaScript knowledge because it’s syntactically similar to JavaScript.
Solidity Syntax
The following is an example of a simple Solidity smart contract:
pragma solidity ^ 0.8.13; contract My_Smart_Contract { string public myName; constructor() { myName = "Samuel"; } function showMyName() public view returns (string memory) { return myName; } }
1. Solidity Smart Contract License
Every developer is encouraged to add a machine readable license at the top of their Solidity Source file, as shown below:
The MIT license is similar to the license you’ll find on GitHub. You can add the UNLICENSED
value if you don’t want to specify a license on your Solidity source file, but this should not be left blank.
You can check out the complete list of Solidity Licenses supported by SPDX here.
2. Solidity Pragma
A pragma directive instructs the Solidity compiler on the version a smart contract should run on.
The pragma directive below shows that the smart contract is written for Solidity version 0.8.13. The caret symbol indicates that the Solidity program should not work with versions less than 0.8.0 or versions beginning with 0.9.0.
pragma solidity ^ 0.8.13;
A pragma directive is always local to the source file, which means you have to add it to all of your source files.
3. Solidity Contract
A contract is a collection of states and functions that is deployed on the blockchain at a specified address.
contract My_Smart_Contract {}
4. Variables in Solidity
Solidity is a statically-typed programming language, meaning that the state and local variables in a Solidity program must be declared by the programmer before compiling the smart contract.
Here’s an example of declaring a variable in Solidity:
string public myName;
The defined variable is initialized as follows:
myName = "Samuel"
The above variable can be declared and initialized like this:
string myName = "Samuel";
There are 3 main types of variables in Solidity: local, state, and global variables.
1. Local variable
These are variables declared inside of a solidity function, and they’re not stored on the blockchain.
2. State variables
The state variables are variables that are declared outside of a solidity function, and they’re permanently stored on the blockchain.
3. Global variables
Solidity global variables are variables that are accessible by other functions. They hold the information about the blockchain and its transaction properties.
5. Solidity Constructor
In Solidity, a constructor is a special keyword that’s used to create an optional function that initializes state variables in a smart contract.
constructor() { myName = "Samuel"; }
A smart contract can only have a single constructor, and it only executes once a smart contract has been compiled.
6. Solidity Function
In programming, a function is a block of code that…
Read More: web3.hashnode.com