Solidity: A Programming Language for Smart Contracts

Solidity: A Programming Language for Smart Contracts

4 mins read1.4K Views Comment
Updated on Jul 1, 2022 08:00 IST

Bitcoin is the inception point of Blockchain technology and web3. Similarly, Solidity is seen as a language of web3 and decentralized applications. 

2022_05_solidity.jpg

Contents

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.

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.
soldity_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:

 
 
   
  1. pragma solidity ^0.6.0 ;
  2. contract HotelRoom1 {
  3. //Ethers (ETH) - Payment in smart contracts
  4.  
  5. enum Statuses {Vacant , Occupied }
  6. Statuses currentStatus ;
  7. address payable public owner ;
  8. //The Owner dynamically deploys the smart contract
  9. //payable - allows owner to accept the payments in ethers
  10. event Occupy (address _occupant , uint _value ) ;
  11.  
  12. constructor ( ) public {
  13. owner =msg. sender ;
  14. //msg.sendera allows user to calls the smart contract
  15. currentStatus = Statuses. Vacant ;
  16. }
  17.  
  18. modifier onlyWhileVacant {
  19.  
  20. //require() - ensure the conditions before the code gets executed
  21.  
  22. require (currentStatus ==Statuses. Vacant , "Currently Occupied!!" ) ;
  23. _ ;
  24. //require checks the condition-if true, then proceed with the code flow
  25. //else- display the error message and halt the flow of the code.
  26. }
  27.  
  28. modifier costs (uint _amount ) {
  29. require (msg. value >= _amount , "Not enough Ether Provided!!" ) ;
  30. _ ;
  31. }
  32.  
  33. receive ( ) external payable onlyWhileVacant costs ( 2 ether ) {
  34. //external - called as a smart contract function outside
  35. //check price and status
  36. currentStatus =Statuses. Occupied ;
  37. owner. transfer (msg. value ) ;
  38. //msg.value - for getting the ethers value from the sender account to owner
  39. emit Occupy (msg. sender , msg. value ) ;
  40. //to emit the event(Occupy) created above - address and amount(ether paid)
  41. }
  42. }

Output:

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.

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.

  1. Create a truffle project. Set up a development network using nodejs.
  2. Create and deploy your smart contract.
  3. Keep the ganache open to fetch simulated Blockchain and addresses.
  4. From the Truffle console, interact with your smart contract.
  5. 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.

About the Author

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