Comments are used to annotate the code to make it easier to understand. They are ignored by the Solidity compiler.
// to comment out a single line./* */ to comment out multiple lines.// This is a single-line comment
/* This is a multi-line comment
that spans several lines */
The pragma directive helps to lock the Solidity compiler version for your smart contract. This ensures that your contract will only compile with a compatible compiler.
pragma solidity ^0.8.0;
The import statement is used to import code from other Solidity files. This is useful for code reusability and separation of concerns.
import "./SomeContract.sol";
Solidity supports various data types:
uint256 x = 42;
bool isTrue = true;
address user = msg.sender;
Enums help in creating custom types with a range of predefined constants, making code more readable and less error-prone.
enum State { Created, Locked, Inactive }
State public state = State.Created;
Structs allow you to create complex data types that group variables under a single name.
struct Person {
string name;
uint age;
}
Person public person = Person("Alice", 30);
Arrays are data structures that can hold more than one value at a time. They can be fixed-size or dynamic.
uint[] public numbers = [1, 2, 3];
Mappings are key-value stores that allow you to link one data type to another.
mapping(address => uint) public balances;
Functions are the building blocks of a Solidity contract. They define executable code and can read and modify contract state.
function add(uint x, uint y) public pure returns (uint) {
return x + y;
}
Modifiers are reusable pieces of code that can change the behavior of functions. They are often used for access control.
modifier onlyOwner() {
require(msg.sender == owner, "Not the owner");
_;
}
Events provide a logging mechanism for the blockchain. They are crucial for frontend applications to "listen" to changes in smart contracts.
event LogData(uint indexed data);
emit LogData(42);
Solidity uses require, assert, and revert for error handling:
require(x > 0, "x must be greater than 0");
Inheritance allows a contract to acquire properties and behavior (i.e., state variables and functions) of a parent contract.
contract Child is Parent {
// code
}
Interfaces define a contract's external functions and enable the interaction between different contracts.
interface IERC20 {
function transfer(address to, uint256 value) external returns (bool);
}
Libraries are reusable pieces of code that can be called from within your contracts. They are deployed only once at a specific address and linked to your contract.
using SafeMath for uint;
uint x = y.add(z);
uint[] storage myArray;
uint[] memory tempArray;
The payable keyword allows a function to receive Ether.
function deposit() public payable {
// code
}
These functions are executed when a contract receives Ether without a function being called.
fallback.external payable.fallback() external payable {
// code
}
receive() external payable {
// code
}
A constructor is a special function that gets executed only once when the contract is deployed.
constructor() {
owner = msg.sender;
}
private but also accessible in derived contracts.uint public x;
uint private y;
function getView() public view returns (uint) {
return x;
}
function getPure() public pure returns (uint) {
return 42;
}
Swap insights and ask questions about “Learn everything about Solidity”.
Ask a question or share your thoughts about this lesson.