Solidity
Fundamentals of Solidity
In this Part, we will deep dive into Ethereum's most popular contract-oriented language, called Solidity. We will cover Solidity's source code file structure and the different data types supported. We will also understand the different units, global variables, and functions it supports and where these should be used, and maany more’s.
Top resource to learn solidty by Example: https://solidity-by-example.org/
Solidity Compilation Process

A successful compilation will generate several outputs depending on the compiler options used:
Bytecode: This is the primary output, consisting of machine code the EVM can execute. It represents the compiled version of your smart contract.
Application Binary Interface (ABI): This defines how to interact with your smart contract's functions, variables, and events from an external application.
Other Outputs (Optional): The compiler can also generate additional outputs like assembly code for debugging or gas usage estimates.
What is Solidity?
Solidity is a statically-typed programming language designed for developing smart contracts that run on the Ethereum Virtual Machine (EVM). It was influenced by C++, Python, and JavaScript and is used to create contracts for voting, crowdfunding, blind auctions, multi-signature wallets, and more
Sample Contract Structure
pragma solidity: Specifies the version of Solidity.contract MyFirstContract: Declares a contract namedMyFirstContract.
2. Data Types and Variables
Solidity supports several basic data types. Variables are used to store data, and every variable in Solidity has a type.
Value Types
Boolean:
trueorfalse
Integer:
Supports both signed (
int) and unsigned (uint) integers. You can specify the bit size, e.g.,uint8,uint256.
Address:
Represents an Ethereum address.
Bytes:
Fixed-size byte arrays from
bytes1tobytes32.
Reference Types
Arrays:
Arrays can be fixed or dynamic in size.
Mappings:
Used for key-value pairs.
3. State and Local Variables
State Variables
State variables are stored on the blockchain and persist between function calls.
Local Variables
Local variables are temporary and exist only within the scope of a function.
4. Constants and Immutable Variables
Constant Variables
These variables' values cannot be modified after initialization.
Immutable Variables
Immutable variables can be set only once, typically in the constructor.
5. Functions
Functions in Solidity contain executable units of code that can be invoked externally or internally.
Basic Function
public: The function can be called both internally and externally.pure: Declares that the function doesn't read or modify state variables.
View Functions
Functions that only read state variables are declared with the view keyword.
Modifiers in Functions
Modifiers control the visibility and behavior of functions, such as:
public: Accessible by everyone.private: Accessible only within the contract.external: Called only from outside the contract.internal: Called only by this contract or derived contracts.
6. Events
Events in Solidity are used for logging. Applications can listen to these events and execute responses.
emit: Keyword to trigger an event.indexed: Marks a field to be searchable in logs.
7. Control Structures
Solidity supports common control structures like if, for, and while loops.
If-Else Statement
For Loop
8. Constructor and Inheritance
Constructor
A constructor is an optional function that runs only once at the time of deployment.
Inheritance
Solidity supports inheritance, enabling contracts to inherit functionality from other contracts.
9. Payable Functions
Payable functions allow a contract to receive Ether.
10. Error Handling
Solidity provides mechanisms to handle errors, including require, assert, and revert.
Require
Used to validate conditions before executing the function.
Assert
Used for internal errors and invariants (conditions that must hold true).
Example Case
Explanation:
License Identifier:
The first line
// SPDX-License-Identifier: MITis a comment that specifies the license under which your code is distributed. In this case, it's the MIT License, a permissive open-source license.
Solidity Version:
pragma solidity 0.8.26;declares the version of Solidity you're using. This version (0.8.26) is relatively recent, ensuring compatibility with newer features and security fixes.
Smart Contract Definition:
contract Demo { ... }defines a smart contract namedDemo. Smart contracts are self-executing programs stored on the blockchain that can hold data and execute code under certain conditions.
State Variable:
int number;declares a state variable namednumberof typeint. State variables store data within the smart contract that persists across function calls and transactions. In this case,numbercan hold whole numbers (positive, negative, or zero).
Constructor:
constructor() public { ... }is a special function that is automatically called once when the contract is deployed (created) on the blockchain. It's used to initialize state variables.number = 5;assigns the initial value of 5 to thenumbervariable.
Getter Function (
getter)function getter() public view returns (int) { ... }defines a function namedgetter.public: This keyword allows anyone to call this function from outside the contract.view: This keyword indicates that the function doesn't modify the contract's state (it only reads data).returns (int): This specifies that the function returns an integer value.
return number;returns the current value of thenumbervariable.
Incrementer Function (
increment)function increment() public { ... }defines a function namedincrement.public: Similar to thegetterfunction.
number = number + 1;increments the value ofnumberby 1.
Commented-Out Line (Optional)
// string public sentence = "hello world";This line is commented out (preceded by//), meaning it's not executed. It's likely included for demonstration purposes, declaring a string variable namedsentencewith the value "hello world".
In Summary:
This Solidity code defines a simple smart contract named Demo that:
Stores an integer value (
number) initialized to 5.Provides a
getterfunction to retrieve the current value ofnumber.Offers an
incrementfunction to increasenumberby 1.
I hope this explanation is clear and helpful! Feel free to ask if you have any further questions.
Last updated