Solidity is an object-oriented programming language with syntax similar to JavaScript. As a decentralized contract language running on blockchain networks, it introduces unique characteristics:
- Account-Based System: Ethereum uses addresses (20-byte
addresstype) instead of UTXO models to locate user/contract accounts. - Native Payment Support: Keywords like
payableenable direct payment handling within the language. - Blockchain Storage: Data persists permanently, requiring explicit memory/blockchain storage designation.
- Decentralized Execution: Emphasizes contract/function invocation methods suitable for distributed networks.
- Atomic Exception Handling: Failures trigger complete transaction rollbacks to ensure data consistency.
From Solidity Code to Smart Contract
- Compilation: Source code is compiled into bytecode with an ABI (Application Binary Interface).
- Deployment: Bytecode is deployed via transactions, creating new contract accounts on Ethereum.
- Interaction: DApps use JavaScript (web3.js + ABI) to call contract functions for data operations.
Compilers
- Remix: Browser-based IDE.
- solcjs: Install via
npm install -g solc.
Key Solidity Concepts
Source File Structure
- Pragma Directive:
pragma solidity ^0.4.0;(restricts compiler versions). Imports:
import "filename"; // Global symbols import * as alias from "filename"; // Namespace import import {symbol1 as alias} from "filename"; // Selective import
Value Types
- Booleans:
bool(true/false). - Integers:
int/uint(signed/unsigned, 8–256 bits). - Fixed-Point:
fixed/ufixed(decimal precision). - Addresses:
address(20 bytes) andaddress payable(supports.transfer()). - Bytes: Fixed-size (
bytes1–bytes32) and dynamic (bytes,string). - Enums: User-defined types (e.g., contract states).
Reference Types
- Arrays: Fixed (
T[k]) or dynamic (T[]). Use.push()to append. - Structs: Custom composite types (cannot contain self-referential members).
- Mappings: Key-value stores (
mapping(KeyType => ValueType)).
Data Locations
- Storage: Persistent (state variables).
- Memory: Temporary (function parameters).
- Calldata: Immutable (external function args).
Functions & Visibility
Function Types
- Internal: Called within contract (
f()). - External: Called via transactions (
this.f()).
Visibility Modifiers
public: Part of contract interface.private: Restricted to defining contract.internal: Accessible by derived contracts.external: Only callable externally.
State Mutability
pure: No state read/write.view: Read-only.payable: Accepts Ether.
Modifiers
modifier onlyOwner {
require(msg.sender == owner);
_; // Function body executes here
}Advanced Features
Fallback Function
fallback() external payable {
// Executes on unmatched calls/Ether receipt
}Events
event LogUpdate(address indexed _from, uint _value);
emit LogUpdate(msg.sender, value); // Stored in transaction logsError Handling
assert(): Tests invariants (e.g., internal errors).require(): Validates conditions (e.g., input checks).revert(): Reverts transactions with custom errors.
Units & Globals
- Ether Units:
1 ether = 1e18 wei. - Time Units:
seconds,minutes,days, etc. - Global Variables:
block.timestamp,msg.sender,tx.origin.
FAQs
What’s the difference between memory and storage?
memory: Temporary, erased between function calls.storage: Persistent, written to blockchain.
How do I safely receive Ether in a contract?
Use a payable fallback function:
fallback() external payable {}Can mappings be iterated?
No—mappings lack iterability. Use auxiliary arrays for iteration patterns.
👉 Explore more Ethereum development tools
👉 Master Solidity with advanced examples