# Solidity

### **What is Solidity?**

Solidity is a high-level **programming language** specifically designed for writing **smart contracts** that run on **Ethereum and other EVM-compatible blockchains**. It is **statically typed**, meaning variables must have a fixed type, and it is influenced by languages like JavaScript, Python, and C++.

Solidity enables developers to build **decentralized applications (dApps)** such as **DeFi protocols, NFTs, DAOs, and token contracts**.

## **Solidity Architecture & Workflow**

Solidity follows a well-structured **Ethereum-based architecture** that ensures security, transparency, and decentralization.

#### **Architecture Diagram**

Here’s how a Solidity smart contract interacts with the blockchain:

```
+------------------+        +------------------+        +--------------------+
|  Solidity Code   | -----> |  EVM Bytecode    | -----> |  Ethereum Virtual  |
|  (Smart Contract)|        |  (Compiled Code) |        |  Machine (EVM)     |
+------------------+        +------------------+        +--------------------+
         |                           |                        |
         v                           v                        v
+------------------+        +------------------+        +--------------------+
|  Web3.js/Ethers.js| -----> |  Ethereum Node  | -----> |  Blockchain Storage |
+------------------+        +------------------+        +--------------------+

```

### **Solidity Workflow**

1. **Write a Smart Contract** in Solidity.
2. **Compile the contract** into EVM Bytecode using tools like **Solidity Compiler (solc)**.
3. **Deploy the contract** to the Ethereum blockchain using Web3.js, Ethers.js, or Hardhat.
4. **Interact with the contract** using a front-end dApp, a wallet like MetaMask, or scripts.

## **Example Solidity Smart Contract**

Let's write a simple **"Hello World"** smart contract that allows users to set and get a message.

#### **1. Create a Solidity Smart Contract**

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract HelloWorld {
    string private message;

    // Constructor sets the initial message
    constructor(string memory _message) {
        message = _message;
    }

    // Function to update the message
    function setMessage(string memory _newMessage) public {
        message = _newMessage;
    }

    // Function to retrieve the current message
    function getMessage() public view returns (string memory) {
        return message;
    }
}

```

#### **2. Compile and Deploy the Contract**

To deploy the contract, you can use **Hardhat, Remix, or Truffle**.

**Using Remix (Easiest)**

1. Open Remix IDE.
2. Create a new Solidity file (`HelloWorld.sol`).
3. Paste the code and **Compile** it.
4. Deploy using **Injected Web3** (MetaMask).

**Using Hardhat (More Advanced)**

1. **Install Hardhat**

```sh
npm install --save-dev hardhat

```

2. Create Hardhat Project:

```sh
npx hardhat

```

3. Compile and deploy the contract

```sh
npx hardhat compile
npx hardhat run scripts/deploy.js --network localhost

```

## **Why Use Solidity Over Other Languages?**

#### **1. Optimized for Smart Contracts**

* Solidity is **purpose-built** for writing **Ethereum smart contracts**, unlike other languages like Python or JavaScript.

#### 2.  **EVM Compatibility**

* Solidity is **designed to run on Ethereum Virtual Machine (EVM)**, making it the **best** choice for Ethereum-based dApps.

#### 3.  **Security-Oriented**

* Solidity offers **features like modifiers, access control, and reentrancy guards** to prevent common vulnerabilities.

#### 4.  **Large Developer Community**

* Solidity has **strong community support**, making it easier to find resources, audits, and libraries.

#### 5.  **Interoperability**

* Solidity contracts can be **integrated with Web3.js, Ethers.js, and other blockchain tools**.

## **Where Solidity is Used?**

* **DeFi (Decentralized Finance)** – Uniswap, Aave, Compound
* **NFTs (Non-Fungible Tokens)** – OpenSea, CryptoPunks
* **DAOs (Decentralized Autonomous Organizations)** – MakerDAO, Aragon
* **Token Contracts** – ERC-20, ERC-721, ERC-1155
* **Gaming & Metaverse** – Axie Infinity, Decentraland
