Solidity 0.8.x: what changed and why it matters
The jump from Solidity 0.7 to 0.8 was more significant than most version bumps. The biggest change was built-in overflow checking, but there were others that affected both how you write contracts and how you reason about security. Here's a practical rundown of the 0.8.x series changes that are still relevant.
Overflow and underflow checking
Before 0.8, arithmetic on uint and int types would silently wrap around on overflow. This was the source of real vulnerabilities. The infamous BEC token exploit in 2018 used integer overflow to mint billions of tokens from thin air.
In 0.8.0, overflow and underflow cause a revert automatically:
uint256 a = type(uint256).max;
uint256 b = a + 1; // Reverts in 0.8, wraps to 0 in 0.7
The SafeMath library from OpenZeppelin was the standard way to handle this in 0.7 and earlier. In 0.8.x, you don't need it for basic arithmetic. OpenZeppelin still maintains it for compatibility but it's functionally redundant now.
If you intentionally need wrapping arithmetic (for bit manipulation or certain hashing patterns), use the unchecked block:
function increment(uint256 x) external pure returns (uint256) {
unchecked {
return x + 1; // Will wrap on overflow, no revert
}
}
Using unchecked is also a gas optimization. The overflow check has a cost. For loops where you can guarantee no overflow, unchecked { i++; } is a common pattern.
Custom errors (0.8.4)
Described in an earlier post, but worth repeating here for completeness. Custom errors are significantly cheaper than string reverts and more informative than empty reverts:
error Unauthorized(address caller, bytes32 requiredRole);
function adminFunction() external {
if (!hasRole(ADMIN_ROLE, msg.sender)) {
revert Unauthorized(msg.sender, ADMIN_ROLE);
}
}
Error selectors are 4 bytes. A string revert encoding "Unauthorized" is much larger.
Immutable variables (0.8.0, improved in 0.8.6)
immutable was introduced before 0.8 but solidified in this series. Variables declared immutable can be set in the constructor and nowhere else. They're read from bytecode rather than storage, saving gas:
contract MyContract {
address public immutable owner;
uint256 public immutable deploymentBlock;
constructor() {
owner = msg.sender;
deploymentBlock = block.number;
}
}
From 0.8.8 onward, immutable variables don't need to be initialized in every code path of the constructor. Earlier versions required initialization on all paths.
User-defined value types (0.8.8)
You can now define types that wrap primitive types:
type Price is uint128;
type TokenId is uint256;
These are distinct types at the Solidity level. You can't accidentally pass a Price where a TokenId is expected. Zero runtime overhead.
function transfer(TokenId id, address to) external { ... }
Price p = Price.wrap(100);
TokenId t = TokenId.wrap(1);
transfer(p, msg.sender); // Compile error: wrong type
transfer(t, msg.sender); // Works
Useful for contracts that deal with many different numeric types and where accidental type confusion would be a bug.
using A for B global (0.8.13)
Previously, using SafeERC20 for IERC20 only applied within the contract that declared it. From 0.8.13, you can declare library usage at the file level and it applies throughout:
// At file level, not inside a contract
using SafeERC20 for IERC20 global;
Mostly a convenience feature for large codebases.
Named parameters in function calls (0.8.18)
You can name arguments in function calls, similar to named parameters in Python:
function createOrder(address token, uint256 amount, uint256 deadline) external { }
// Named call, order doesn't matter
createOrder({ deadline: block.timestamp + 1 days, token: USDC, amount: 1000e6 });
Helpful for functions with many parameters where the order isn't obvious.
Checking your version
The Solidity changelog is the authoritative record. For a specific feature, it's the fastest way to check which version introduced it. When inheriting a codebase, look at the pragma version and cross-reference against the changelog to understand which features and behaviors apply.
Most active projects target 0.8.20 or later. Older codebases sometimes pin to 0.6 or 0.7 for stability or because they rely on pre-0.8 behavior. Always check the pragma before assuming 0.8 defaults apply.
