Smart Contract Fundamentals
Smart contracts on Rootstock behave similarly to Ethereum because Rootstock is EVM-compatible. This module gives you the foundational concepts you must understand before writing or deploying your first contract.
What Are Smart Contracts?
Smart contracts are programs stored on the blockchain that run exactly as written.
Key traits on Rootstock:
- Immutable once deployed
- Executed by the EVM
- Triggered by transactions or messages
- Store and manage state
- Written primarily in Solidity
The EVM & Rootstock Compatibility
Rootstock uses the Ethereum Virtual Machine (EVM), meaning:
- Solidity code compiles the same way
- Same opcodes
- Same bytecode
- Same transaction structure
- Same ABI encoding
What differs is the network layer (mining, consensus, gas costs), not the programming model.
Solidity File Structure
Every .sol file follows the same high-level structure:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract Example {
uint256 public value;
function setValue(uint256 _v) external {
value = _v;
}
}
State Variables
State variables live on-chain. They cost gas. They persist.
uint256 public supply;
address public owner;
mapping(address => uint256) public balances;
Functions: External, Public, Internal, Private
Function visibility matters.
function update() external {}
function read() public view returns(uint256) {}
function _calc() internal {}
function helper() private {}
Events
Events are your logging system. They don't store data on-chain; they emit logs users and dApps can track.
event Transfer(address indexed from, address indexed to, uint256 amount);
Modifiers
Reusable access control logic.
modifier onlyOwner() {
require(msg.sender == owner, "Not owner");
_;
}
Constructor
Runs one time at deployment.
constructor() {
owner = msg.sender;
}
Error Handling
Use explicit errors for better gas efficiency and debugging.
error NotOwner();
function withdraw() external {
if (msg.sender != owner) revert NotOwner();
}
Contract Structure Best Practices
Follow this pattern for clean, maintainable contracts:
- Imports
- Errors
- Events
- State Variables
- Modifiers
- Constructor
- Core Functions
- Internal Logic