// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
contract SimpleStorage {
// state variables and functions here
}contract SimpleStorage {
uint256 public storedData;
}function setAge(uint256 newAge) public {
// Creates a storage reference - modifies on-chain state
Profile storage ref = profiles[msg.sender];
ref.age = newAge;
// Creates a memory copy - changes lost after function ends
Profile memory copy = profiles[msg.sender];
copy.age = newAge; // This change is NOT persisted!
}uint256 public a; // Unsigned integer
uint8 public b; // Smaller integer
bool public c; // Boolean
address public d; // Ethereum address:160bitsstruct User {
string name;
bool isRegistered;
}
mapping(address => User) public users;
string[] public names;// State-changing function - can modify storage
function write(uint256 v) public {
storedData = v; // Modifies state
}
// Read-only function - cannot modify state
function read() public view returns (uint256) {
return storedData; // Only reads state
}
// Pure computation - no state access at all
function add(uint a, uint b) public pure returns (uint) {
return a + b; // Pure logic, no blockchain interaction
}modifier onlyOwner() {
require(
msg.sender == owner,
"caller is not owner"
);
_; // Function body executes here
}
function withdraw() public onlyOwner {
// This function can only be called by owner
// The modifier check runs first
}contract SimpleWallet {
mapping(address => uint) public balances;
function deposit() public payable {
// msg.value contains the Ether sent with transaction
balances[msg.sender] += msg.value;
}
function getBalance() public view returns (uint) {
return balances[msg.sender];
}
}function withdraw(uint amount) public {
// CHECKS: Verify preconditions
require(balances[msg.sender] >= amount, "Insufficient balance");
// EFFECTS: Update state BEFORE external call
balances[msg.sender] -= amount;
// INTERACTIONS: External call comes last
(bool ok,) = payable(msg.sender).call{value: amount}("");
require(ok, "Transfer failed");
}receive() external payable {
// Called when Ether is sent with empty calldata
emit Received(msg.sender, msg.value);
}fallback() external payable {
// Called when:
// 1. Function selector doesn't match any function
// 2. Ether sent but no receive() exists
}contract EtherWallet {
address payable public owner;
constructor() {
owner = payable(msg.sender);
}
receive() external payable {
// Accept Ether deposits
}
function withdraw(uint amount) external {
require(msg.sender == owner, "Only owner can withdraw");
require(address(this).balance >= amount, "Insufficient balance");
(bool success,) = owner.call{value: amount}("");
require(success, "Transfer failed");
}
function getBalance() external view returns (uint) {
return address(this).balance;
}
}