โœ’๏ธGas Saving Technique 9: Use Custom Errors instead of Revert Strings to save Gas

Introduction

Smart contract optimization often involves subtle adjustments that cumulatively result in significant gas savings. An impactful enhancement introduced in Solidity v0.8.4 is the utilization of custom errors. Custom errors provide a more gas-efficient alternative to revert strings for conveying failure reasons in your smart contracts, thereby reducing both deployment and runtime costs.

Impact & Details

Understanding Gas Consumption

  • Expense of Revert Strings: Traditional revert strings are costly in terms of gas, both during the deployment phase and at runtime when a revert condition is triggered. This expense arises because strings are relatively heavy, and each character consumes gas.

  • Efficiency of Custom Errors: Custom errors, introduced in Solidity v0.8.4, provide a more gas-efficient way to handle error messaging. These errors are not only cheaper to deploy but also consume less gas when invoked, making them a superior choice for error handling.

How to Implement Custom Errors for Gas Savings

Practical Example: Using Custom Errors

Consider a contract where you need to ensure that the sent value is above a certain threshold. Below, you can find the contract versions before and after optimization:

Before Optimization:

solidityCopy code// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Fundraiser {
    uint constant minContribution = 100;

    function contribute() public payable {
        require(msg.value >= minContribution, "Contribution too small");
    }
}

After Optimization:

solidityCopy code// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract Fundraiser {
    uint constant minContribution = 100;
    
    error SmallContribution(uint256 valueSent, uint256 minValue);

    function contribute() public payable {
        if (msg.value < minContribution) {
            revert SmallContribution(msg.value, minContribution);
        }
    }
}

In the optimized version, a custom error SmallContribution is defined and used for reverting when the contribution is too small. This approach not only provides clear error messaging but also saves gas compared to the traditional revert string method.

  1. Identify Revert Strings: Review your smart contracts for require or revert statements with string messages.

  2. Implement Custom Errors: Replace identified revert strings with custom errors, taking advantage of this feature in Solidity v0.8.4 and above.

  3. Test: Perform rigorous testing to verify that the updated smart contract maintains its functionality while using less gas for error handling.

Conclusion

The introduction of custom errors in Solidity offers developers a more gas-efficient mechanism for error handling. While the gas saved per transaction might be modest, the aggregate savings across multiple transactions can be substantial. Always ensure to conduct comprehensive testing after implementing these changes, confirming that the smart contract behaves as expected with the added benefit of reduced gas consumption.

Last updated