Introduction

Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.

I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.

I also build and publish a variety of tools, libraries, and open-source projects.

Solidity version 0.8.26 was released on May 21, 2024. This article summarizes its changes based on the official release announcement.

Changes

Solidity 0.8.26 introduces the following changes.

  • Support for custom errors in require.
  • Optimization of custom errors with small encoded sizes.
  • A faster Yul Optimizer sequence.
  • A change to the JSON library used during compilation.

Custom Errors in require

Custom errors allow developers to define their own errors, as shown below.

solidity
// SPDX-License-Identifier: MIT
pragma solidity 0.8.26;

contract Solidity0826 {
    uint256 requiredAmount = 0.01 ether;

    error InsufficientBalance(uint256 available, uint256 required);

    function deposit() public payable {
        if (msg.value <= 0.001 ether) {
            revert InsufficientBalance(msg.value, requiredAmount);
        }
    }
}
solidity
error InsufficientBalance(uint256 available, uint256 required);

The example defines an error named InsufficientBalance.

solidity
if (msg.value <= 0.001 ether) {
    revert InsufficientBalance(msg.value, requiredAmount);
}

When a condition is met, such as the condition in an if statement, the previously defined error can be returned by reverting execution.

Another commonly used way to enforce conditions is require. It is a familiar and concise alternative and can be gas-efficient.

solidity
// SPDX-License-Identifier: MIT
pragma solidity 0.8.26;

contract Solidity0826 {
    uint256 requiredAmount = 0.01 ether;

    error InsufficientBalance(uint256 available, uint256 required);

    function deposit() public payable {
        require(msg.value > 0.001 ether, "InsufficientBalance");
    }
}

In this example, if msg.value > 0.001 ether is false, execution stops with the message InsufficientBalance.

Up to Solidity 0.8.25, custom errors could not be used as the second argument of require.

Solidity 0.8.26 introduces this capability.

solidity
// SPDX-License-Identifier: MIT
pragma solidity 0.8.26;

contract Solidity0826 {
    uint256 requiredAmount = 0.01 ether;

    error InsufficientBalance(uint256 available, uint256 required);

    function deposit() public payable {
        require(
            msg.value > 0.001 ether ,
            InsufficientBalance(msg.value, requiredAmount)
        );
    }
}

This can provide more gas-efficient error handling.

There is one important limitation in this release: the feature is supported only by the IR-based compilation pipeline (compilation through Yul).

An IR pipeline converts source code into an intermediate representation rather than directly into bytecode, enabling the compiler to perform optimizations before generating bytecode. Solidity uses Yul as its intermediate representation.

See the following article for more information about Yul.

Here is how to enable the IR-based compilation pipeline in Hardhat and Remix.

Hardhat

Add viaIR: true, to hardhat.config.js as shown below.

hardhat.config.js
solidity: {
  version: "0.8.24", // any version you want
  settings: {
+   viaIR: true,
    optimizer: {
      enabled: true,
      details: {
        yulDetails: {
          optimizerSteps: "u",
        },
      },
    },
  },
}

Remix

Add "viaIR": true, to compiler_config.json as shown below.

compiler_config.json

{
	"language": "Solidity",
	"settings": {
+		"viaIR": true,
		"optimizer": {
			"enabled": true,
			"runs": 200
		},
		"outputSelection": {
			"*": {
			"": ["ast"],
			"*": ["abi", "metadata", "devdoc", "userdoc", "storageLayout", "evm.legacyAssembly", "evm.bytecode", "evm.deployedBytecode", "evm.methodIdentifiers", "evm.gasEstimates", "evm.assembly"]
			}
		}
	}
}

Source Code

The source code created in Remix is also available here.

Optimization of Custom Errors with Small Encoded Sizes

Previously, inline assembly was used to optimize code that reverts with custom errors.

https://ethereum.stackexchange.com/questions/142752/yul-inline-assembly-revert-with-a-custom-error-message

https://stackoverflow.com/questions/73373480/solidity-yul-inline-assembly-revert-with-custom-error

https://stackoverflow.com/questions/73373480/solidity-yul-inline-assembly-revert-with-custom-error/74704254

See the following article for more information about inline assembly.

In Solidity 0.8.26, the compiler can automatically optimize the generated code for custom errors without arguments and custom errors whose encoded data is small enough to fit in the applicable memory area.

This removes the need to manually optimize such custom errors with inline assembly.

solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.26;

error ForceFailure();

contract FailureForcer {
    function fail() external pure {
        revert ForceFailure();
    }
}

The example above shows a custom error without arguments.

A Faster Yul Optimizer Sequence

The Yul Optimizer introduces a new default sequence of optimization steps.

The new sequence substantially reduces compilation time while maintaining the quality of code optimization.

Previously, optimization steps were repeated multiple times to expose additional opportunities for optimization. Analysis revealed that the results of the initial optimization and the final results were often nearly identical.

Previous Yul Optimizer sequence and its optimization results Source: https://soliditylang.org/blog/2024/05/21/solidity-0.8.26-release-announcement

The new default sequence therefore reduces repetition to a single optimization pass in the relevant part of the sequence.

New Yul Optimizer sequence Source: https://soliditylang.org/blog/2024/05/21/solidity-0.8.26-release-announcement

Gas-cost optimization appears to have been adjusted similarly.

Previous sequence

Previous sequence for gas-cost optimization Source: https://soliditylang.org/blog/2024/05/21/solidity-0.8.26-release-announcement

New sequence

New sequence for gas-cost optimization Source: https://soliditylang.org/blog/2024/05/21/solidity-0.8.26-release-announcement

The original images are retained, including any original labels.

As illustrated by the following benchmark results, compilation times decreased by up to approximately 65% across the projects tested.

Project Compilation time change Bytecode size change Runtime gas cost change
pool-together -63% -1.29% Not measured
uniswap -53% +1.67% Not measured
zeppelin -47% -0.48% -0.01%
elementfi -42% -1.87% Not measured
euler -34% +1.00% Not measured
yield_liquidator -27% +0.84% +0.14%
ens -22% -1.20% -0.01%
brink -20% +0.61% Not measured
perpetual-pools -16% -0.23% +0.02%
gp2 -12% +0.50% Not measured

An important caveat concerns a vulnerability affecting Solidity 0.8.21 and earlier versions. The new default sequence should be used with Solidity 0.8.22 or later; earlier versions should continue using the old sequence.

Change to the JSON Library Used During Compilation

The compiler's internal JSON library has changed from jsoncpp to nlohmann::json.

https://github.com/open-source-parsers/jsoncpp https://github.com/nlohmann/json

This makes UTF-8 handling stricter: invalid UTF-8 sequences are no longer accepted.

Performance may also improve, although that is not established by the following discussion alone.

Other Changes

See the official announcement for additional fixes and smaller changes.

References

Conclusion

This article summarized the changes introduced in Solidity 0.8.26.

I also publish articles on the following platforms.

https://chaldene.net/