Solidity: A Programming Language for Smart Contracts
Bitcoin is the inception point of Blockchain technology and web3. Similarly, Solidity is seen as a language of web3 and decentralized applications.
Contents
- What is Solidity?
- How does Solidity work with EVM?
- Benefits of Solidity
- A sample smart contract using Solidity
- Code Execution and Deployment
- Conclusion
The article goes through teaching about Solidity, a programming language for Ethereum smart contracts and DApps in the Blockchain. It covers the basics of Solidity along with its importance, implementation, and execution. Moreover, thereโs also a small smart contract project on hotel room booking in Solidity.
Letโs begin with understanding the need for Solidity.
Also read: How does Blockchain work?
Best-suited Blockchain courses for you
Learn Blockchain with these high-rated online courses
What is Solidity?
Solidity is an object-oriented high-level programming language which uses to write the code for smart contracts in the Ethereum Blockchain. It is greatly inspired by the most popular and developer-friendly languages such as javascript, Python, and C++. The contracts written in Solidity are executed (or run) by Ethereum Virtual Machine (EVM).
Solidity is created to create more functionalities and applications on the Blockchain using simple programming constructs.
How does Solidity work with EVM?
Ethereum is a Blockchain network with a native Cryptocurrency (ETH). Moreover, it provides functionalities like Tokens (ERC20, etc.), Smart contracts, dApps, etc. A smart contract is an automatically executable code deployed on a Blockchain that gets executed when the coded conditions are met. For executing the smart contract on the Ethereum Blockchain, we need Ethereum Virtual Machine (EVM). Itโs a Turing complete system which means it can perform any logical steps with computational functions.
- A developer writes the code for a smart contract using Solidity.
- Solidity codeโ (converted into) โ Opcodes โ (compiles into) โ Byte code.
- The Byte code is a low-level machine language executed and saved by EVM.
To know the further process of a smart contract, read: Smart Contracts in Blockchain.
Benefits of Solidity Programming Language
- A simple, easy-to-use programming language to write smart contracts for dApps
- An open-source, object-oriented high-level programming language
- It provides an Application Binary Interface (ABI) to avoid syntax errors.
- Provides contract inheritance
- The amount to be paid and gas fees can be coded in the smart contract
A sample smart contract using Solidity
Minor Project: Solidity Contract for Hotel Room Booking
Program Code:
pragma solidity ^0.6.0 ; contract HotelRoom1 { //Ethers (ETH) - Payment in smart contracts enum Statuses {Vacant , Occupied } Statuses currentStatus ; address payable public owner ; //The Owner dynamically deploys the smart contract //payable - allows owner to accept the payments in ethers event Occupy (address _occupant , uint _value ) ; constructor ( ) public { owner =msg. sender ; //msg.sendera allows user to calls the smart contract currentStatus = Statuses. Vacant ; } modifier onlyWhileVacant { //require() - ensure the conditions before the code gets executed require (currentStatus ==Statuses. Vacant , "Currently Occupied!!" ) ; _ ; //require checks the condition-if true, then proceed with the code flow //else- display the error message and halt the flow of the code. } modifier costs (uint _amount ) { require (msg. value >= _amount , "Not enough Ether Provided!!" ) ; _ ; } receive ( ) external payable onlyWhileVacant costs ( 2 ether ) { //external - called as a smart contract function outside //check price and status currentStatus =Statuses. Occupied ; owner. transfer (msg. value ) ; //msg.value - for getting the ethers value from the sender account to owner emit Occupy (msg. sender , msg. value ) ; //to emit the event(Occupy) created above - address and amount(ether paid) } }
Output:
You can also read: What is Web3?
Code Execution
There are two common ways to execute the Solidity code:
- Online Mode
- Offline Mode
- Online Mode
For executing and deploying a Solidity smart contract, one of the most popular and easy-to-use IDE would be remix. It has the compilers compatible with almost all the versions of Solidity. Moreover, it has a simulated Blockchain environment with vacant addresses to test, run and deploy your contract.
You can try to run the above hotel room booking smart contract on the remix.
Following are the Alternative IDEs:
- Hardhat
- Microsoft Visual Studio Code
- Tendermint on Microsoft Azure
- Offline Mode
To run the smart contract offline, you need a few software and frameworks to install and set up on your system. You must install Nodejs, Truffle framework, and Ganache (Personal Ethereum Blockchain for test runs). For IDE, you can use Sublime text editor with Ethereum package control.
- Create a truffle project. Set up a development network using nodejs.
- Create and deploy your smart contract.
- Keep the ganache open to fetch simulated Blockchain and addresses.
- From the Truffle console, interact with your smart contract.
- Create tests for evaluating your Solidity smart contract code.
Conclusion
The above article explained the functioning and importance of Solidity along with a tiny smart contract project. Hope you learned something new reading this article. Share your comments and queries in the link below.
Also, read: Top 10 Programming languages to Learn.
Recently completed any professional course/certification from the market? Tell us what liked or disliked in the course for more curated content.
Click here to submit its review with Shiksha Online.
FAQs
What is Ethereum?
It is a decentralized public Blockchain network and a Cryptocurrency with multiple use-cases such as smart contracts, Tokens, dApps, etc.
What is Ethereum Virtual Machine (EVM)?
It is a platform to execute and save smart contracts on the Ethereum network.
What are dApps?
Decentralized applications (dApps) are web or mobile applications (like apps) for users without any central control. dApps are deployed on Blockchains like Ethereum, Solana, etc. It uses the smart contract to execute a set of operations.
This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio