Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

solidity
programming
language
contract
communitycreator

What is Solidity?

Harsh Jain

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Introduction to Solidity

Solidity is a high-level programming language inspired by C++, Python, and JavaScript. This programming language is designed to run on an Ethereum Virtual Machine (EVM). Solidity is used to create smart contractsa very popular concept in Ethereum blockchain that can be used in voting applications, wallets, auctions, etc.

What are smart contracts?

Smart contracts are digital contracts that help verify a transaction’s credibility without the involvement of any third party.

How to set up the environment for Solidity

You need to first install Node.js on your machine. Once this is done, you can run the following command in your terminal to install the Solidity compiler.

npm install -g solc

Now, let’s run the command above and see what happens.

Terminal 1
Terminal

So, we have the Solidity compiler set up on our machine. Let’s now look at what a basic Solidity program looks like.

pragma solidity ^0.5.0;
contract HelloWorld {
bytes32 message;
constructor (bytes32 myMessage) public {
message = myMessage;
}
function getMessage() public view returns(bytes32) {
return message;
}
}
Basic Solidity Program

Explanation

  • In line 1, we define a pragma. A pragma is a directive that tells the compiler which version of Solidity the source code can run on. Here, we have specified that it can run on Solidity version 0.4.0, up to but not including version 0.5.0or anything newer that does not break functionality. This ensures that the smart contract does not behave abnormally due to a newer compiler version.

  • In line 3, we create a contract. A contract is nothing but a collection of functions and data.

  • In line 4, we define a variable message that is of bytes32 type. This will represent a state.

  • In line 5, we define a constructor for our contract.

  • In line 8, we define a getMessage() function that will return the state (message).

This is a default contract that you can relate to as the Hello-World program in other programming languages.

RELATED TAGS

solidity
programming
language
contract
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring