Solidity
Fundamentals of Solidity
Last updated
Fundamentals of Solidity
Last updated
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:
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.
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
pragma solidity
: Specifies the version of Solidity.
contract MyFirstContract
: Declares a contract named MyFirstContract
.
Solidity supports several basic data types. Variables are used to store data, and every variable in Solidity has a type.
Boolean:
true
or false
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 bytes1
to bytes32
.
Arrays:
Arrays can be fixed or dynamic in size.
Mappings:
Used for key-value pairs.
State variables are stored on the blockchain and persist between function calls.
Local variables are temporary and exist only within the scope of a function.
These variables' values cannot be modified after initialization.
Immutable variables can be set only once, typically in the constructor.
Functions in Solidity contain executable units of code that can be invoked externally or internally.
public
: The function can be called both internally and externally.
pure
: Declares that the function doesn't read or modify state variables.
Functions that only read state variables are declared with the view
keyword.
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.
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.
Solidity supports common control structures like if
, for
, and while
loops.
A constructor is an optional function that runs only once at the time of deployment.
Solidity supports inheritance, enabling contracts to inherit functionality from other contracts.
Payable functions allow a contract to receive Ether.
Solidity provides mechanisms to handle errors, including require
, assert
, and revert
.
Used to validate conditions before executing the function.
Used for internal errors and invariants (conditions that must hold true).
Explanation:
License Identifier:
The first line // SPDX-License-Identifier: MIT
is 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 named Demo
. 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 named number
of type int
. State variables store data within the smart contract that persists across function calls and transactions. In this case, number
can 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 the number
variable.
Getter Function (getter
)
function getter() public view returns (int) { ... }
defines a function named getter
.
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 the number
variable.
Incrementer Function (increment
)
function increment() public { ... }
defines a function named increment
.
public
: Similar to the getter
function.
number = number + 1;
increments the value of number
by 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 named sentence
with 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 getter
function to retrieve the current value of number
.
Offers an increment
function to increase number
by 1.
I hope this explanation is clear and helpful! Feel free to ask if you have any further questions.