Zokyo Gas Savings
  • โ›ฝZokyo Gas Savings
  • ๐Ÿ“šTutorials
    • โœ”๏ธGas Saving Technique 1: Unchecked Arithmetic
    • โ›“๏ธGas Saving Technique 2: Immutable Variable
    • โœจGas Saving Technique 3: Double star ** inefficiency
    • ๐Ÿ’ฐGas Saving Technique 4: Cache Array Length
    • โฌ…๏ธGas Saving Technique 5: ++i costs less gas compared to i++
    • โš–๏ธGas Saving Technique 6: NOT operator ! cheaper than boolean FALSE
    • ๐ŸชกGas Saving Technique 7: Using Short Reason Strings
    • ๐ŸชตGas Saving Technique 8: Use Custom Errors instead of Revert Strings to save Gas
    • โœ’๏ธGas Saving Technique 9: Use Custom Errors instead of Revert Strings to save Gas
    • ๐Ÿ‘พGas Saving Technique 10: Calldata cheaper than memory
    • โ›”Gas Saving Technique 11: > 0 is less efficient than != 0 for unsigned integers
    • โž—Gas Saving Technique 12: SafeMath no longer needed
    • ๐Ÿ˜ฎGas Saving Technique 13: variables default to 0
    • ๐ŸงฑGas Saving Technique 14: struct layout/ variable packing
    • ๐Ÿ“žGas Saving Technique 15: Cache External Call
    • โœ๏ธGas Saving Technique 16: Early Validation before external call
    • ๐Ÿ˜ŽGas Saving Technique 17: Donโ€™t cache value that is used once
    • ๐Ÿ˜งGas Saving Technique 18: Redundant code
    • โœ…Gas Saving Technique 19: Early Validation before external call
    • โ›๏ธGas Saving Technique 20: Storage vs Memory read optimizations
    • โœ’๏ธGas Saving Technique 21: Unneeded If statements
    • ๐ŸŒ—Gas Saving Technique 22: >= is cheaper than >
    • ๐ŸŽ’Gas Saving Technique 23: Public to private constants
    • โน๏ธGas Saving Technique 24: Make unchanged variables constant/immutable
    • โฑ๏ธGas Saving Techniques 25: Redundant Access Control Checks
    • โžก๏ธGas Saving Technique 26: Shift Right instead of Dividing by 2
    • ๐ŸชƒGas Saving Tutorial 27: Efficient Boolean Comparison
    • ๐ŸคGas Saving Technique 28: && operator uses more gas
    • ๐Ÿ‘“Gas Saving Technique 29: x = x + y is cheaper than x += y
    • ๐Ÿ‘‚Gas Saving Technique 30: Using 1 and 2 rather than 0 and 1 saves gas
    • โšฝGas Saving Technique 31: Optimize Storage by Avoiding Booleans
    • ๐Ÿ”™Gas Saving Technique 32: Optimal Use of Named Return Variables in Solidity
    • ๐Ÿ›ข๏ธGas Saving Technique 33: Making Functions Payable for Optimized Gas Costs
    • โœ๏ธGas Saving Technique 34: Optimizing Storage References in Smart Contracts
    • โ›ฐ๏ธGas Saving Technique 35: Usage of uints/ints smaller than 32 bytes (256 bits) incurs overhead
    • ๐ŸŒช๏ธGas Saving Technique 36: Inlining Single Use Internal Functions for Savings
    • โ˜„๏ธGas Saving Technique 37: Switching from Public to External Functions for Savings
    • ๐ŸŽ†Gas Saving Technique 38: Upgrading Solidity Compiler to Improve Gas Efficiency and Security
    • ๐Ÿ•ถ๏ธGas Saving Technique 39: Avoiding Duplicated Code for Gas Savings
    • ๐Ÿ˜„Gas Saving Technique 40: Removal of Unused Internal Functions for Gas Savings
    • ๐Ÿ–‹๏ธGas Saving Tutorial 41: In-lining Single Use Modifiers For Gas Saving
    • โ›๏ธGas Saving Technique 42: `require` vs`assert`
Powered by GitBook
On this page
  1. Tutorials

Gas Saving Technique 38: Upgrading Solidity Compiler to Improve Gas Efficiency and Security

PreviousGas Saving Technique 37: Switching from Public to External Functions for SavingsNextGas Saving Technique 39: Avoiding Duplicated Code for Gas Savings

Last updated 1 year ago

Introduction: Solidity, the widely-used language for writing Ethereum smart contracts, continuously evolves. New versions often introduce optimizations and features that can make contracts more gas-efficient and secure. This tutorial focuses on the benefits of upgrading to Solidity version 0.8.4 or newer.


Concept:

  1. Pragma Declaration: Solidity contracts start with a pragma declaration to specify which compiler versions are suitable. For example, pragma solidity ^0.8.0; indicates compatibility with version 0.8.0 and later minor versions.

  2. Compiler Advancements: Newer versions of the Solidity compiler come with improvements that can lead to gas savings and increased security. These advancements can include gas optimizations, additional safety checks, and new features.


Advantages of Upgrading to 0.8.4 or Newer:

  1. Low-Level Inliner (>= 0.8.2):

    • Makes small functions more gas-efficient during runtime.

  2. Optimizer Improvements in Packed Structs (>= 0.8.3):

    • Enhanced handling and storage of packed structs leading to gas savings.

  3. Custom Errors (>= 0.8.4):

    • Allows developers to replace revert strings with custom errors, leading to a reduction in both deployment and runtime gas costs.

    • Especially beneficial when a revert condition is triggered.


Step-by-Step Guide to Upgrading the Compiler Version:

  1. Update Pragma Line: In your smart contract, replace the current pragma line with:

    solidityCopy codepragma solidity ^0.8.4;
  2. Replace Revert Strings: Introduce custom errors and replace traditional revert strings. This makes error handling more efficient:

    solidityCopy codeerror InsufficientFunds(uint256 available, uint256 required);
    
    function withdraw(uint256 amount) external {
        if (amount > balance[msg.sender])
            revert InsufficientFunds(balance[msg.sender], amount);
    }
  3. Review & Test: After updating, test the contract's functionality to ensure there are no regressions and everything works as expected.

  4. Deploy: Once satisfied, deploy your updated contract to the desired network.


Benefits:

  1. Gas Savings: New compiler versions offer optimizations that reduce gas usage.

  2. Enhanced Security: New versions include improved safety checks, reducing the likelihood of vulnerabilities.

  3. Improved Error Handling: Custom errors provide clearer and more gas-efficient error messaging.


Considerations:

  1. Backward Compatibility: Ensure that your contract doesn't rely on behaviors specific to older compiler versions.

  2. External Libraries: If your contract uses external libraries or interfaces, confirm they are compatible with the new compiler version.

  3. Thorough Testing: Always run comprehensive tests after upgrading, especially if relying on compiler-specific behavior.


Conclusion: Upgrading to a newer compiler version, such as 0.8.4 or later, can offer significant benefits in gas efficiency, security, and overall contract quality. Regularly updating to the latest compiler ensures that your contract benefits from the latest advancements in the Solidity ecosystem.

๐Ÿ“š
๐ŸŽ†
Book an audit with Zokyo