Part I: Beware Solidity Devs, you might just hit your contract size limit (2024)

It's quite imperative for you as a Smart Contract Developer to always be within the pre-defined Smart Contract Bytecode size limit.

Wait, What is Bytecode? 🤔

In very simpler terms,

Humans understand Solidity,
but
EVM understands Bytecode.

Bytecode is the low-level language that our solidity smart contracts (a high-level programming language) get translated to.

It technically represents a long sequence of machine codes or opcodes which are pieces of instructions that defines how a particular smart contract is supposed to behave.

Most importantly, these instructions are easily understandable by the EVM and thus allow them to interpret and execute smart contracts accurately.

Alright, What's Bytecode Size Limit? 🤔

Every blockchain has a limit on the maximum size of Smart Contract bytecode that can be deployed.

In the world of Ethereum, after the implementation of EIP170, the maximum bytecode size is 24.576 kilobytes.

Why do we even have a limit? 😫

This limit was introduced in Ethereum with EIP-170 to prevent excessive resource usage and potential network congestion caused by deploying and executing excessively large smart contracts on the blockchain.

The limit was set at 24.576 kilobytes (KB) in order to maintain network performance and prevent spamming attacks that could bloat the blockchain.

By setting a reasonable limit on the size of the contract bytecode, Ethereum nodes can process transactions and execute smart contracts more efficiently, as they have to store and maintain a copy of the entire blockchain on their local machines. Furthermore, the limit discourages the deployment of overly complex and inefficient contracts, which could negatively impact the network as a whole.

In short, this was mainly to prevent any DOS attacks on the network.

Here is a portion of what an article on ethereum.org says about this:

This limit was introduced to prevent denial-of-service (DOS) attacks. Any call to a contract is relatively cheap gas-wise.
However, the impact of a contract call for Ethereum nodes increases disproportionately depending on the called contract code's size (reading the code from disk, pre-processing the code, adding data to the Merkle proof). Whenever you have such a situation where the attacker requires few resources to cause a lot of work for others, you get the potential for DOS attacks.

source: ethereum.org

What does this mean for you as a Solidity Developer?

Two things:

  • You don't just need to manage gas optimizations but also your contract's bytecode size optimization.
  • If you keep adding functions to your smart contract, at some point you will hit the size threshold and you will not be able to deploy that contract on-chain. Trust me, it's not possible.
Part I: Beware Solidity Devs, you might just hit your contract size limit (1)

if you ever try to deploy an extremely bulky contract of contract size more than 24.576 kilobytes, this is what you might get 👇

Part I: Beware Solidity Devs, you might just hit your contract size limit (2)

Therefore it's crucial to learn optimization techniques so that smart contracts can stay within the 24.576 KB size limit without breaking intended functionality.

Part I: Beware Solidity Devs, you might just hit your contract size limit (3)

Well, this is easier said than done since we might need large contracts for our intended use-cases

However, with sufficient knowledge of optimizing contract bytecode size, we can write smart contracts to fit our needs without surpassing the bytecode threshold.

In this short article series, we will mainly learn two things:

  • Solidity development tricks that can help us optimize contract bytecode size.
  • Then, decipher and dive deep to understand why some of these tricks help us optimize contract bytecode size

It's gonna be FUN, as always. 😀

Let’s explore some of these bytecode size optimization tricks to keep in mind while writing solidity code:

  • Minimize code redundancy: Avoid writing repetitive code and reuse existing code as much as possible. This can significantly reduce the size of the Smart Contract bytecode.
  • Start using Libraries: Using a library in a smart contract can help reduce the size of the bytecode because it allows you to reuse code that has already been deployed to the blockchain.
  • Follow ERC-2535, Diamonds, Multi-Facet Proxy: This smart contract system, introduced by @mudgen, is a complete game changer. It effectively allows you to create modular smart contract systems that can be extended after deployment, and thus helps significantly in optimizing contract size.
  • Use smaller data types: Use smaller data types such as uint8, uint16, and uint32 instead of uint256 whenever possible.
  • Shorter error messages should be preferred: Long error messages in require statements can inflate the bytecode size. Consider using shorter and more concise error messages.
  • Use of Custom errors: If you are using solidity versions of 0.8.4 or more, you prefer replacing error messages with custom errors. These can significantly help in contract size reduction as they are ABI-encoded as selectors. Read more about it HERE
  • Wipe out unnecessary functions and variables: Remove any functions or variables that are not required for the Smart Contract to function properly.
  • Optimize code logic: Optimize the code logic to reduce the number of instructions required to execute the Smart Contract.
  • Replacing modifiers with Private Function: Excessive use of modifiers in solidity smart contracts can actually increase contract size. However, replacing modifiers with private functions can actually help reduce contract size.

    Curios about why this happens? Read more about it here 👇

Replace modifiers with private functions and reduce your contract’s sizeWhy does replacing modifiers with private functions in solidity reduce the contract’s size?Decipher with ZaryabMd Zaryab Afser

Tools for Checking Smart Contract Size

Now that you know, the bytecode size limit is a thing, you will definitely need tools that can help track this limit during smart contract development.

Luckily, we do have various tools to monitor their smart contract bytecode size during development, like:

  • hardhat-contract-sizer- Provides details and warnings about contract sizes in a hardhat project
  • truffle-contract-size - displays the contract size of all or a selection of your smart contracts in kilobytes.
  • solc : Provides a built-in bytecode size checker

There you have it devs.

Try using these tricks and never be worried about hitting the threshold for smart contract bytecode size.

Drop your suggestions, in case you have more tricks 😉.

Next Parts of the series 👇

  • Part 2 - Replace modifiers with private functions and reduce your contract's size
  • Part 3 - Custom Errors in solidity are more useful than you think
  • Part 4 - Using libraries to optimize contract size

Join Decipher Club today

Simplifying Web3 and Technology for everyone

Subscribe Now

Part I: Beware Solidity Devs, you might just hit your contract size limit (2024)
Top Articles
The search for Albert Einstein’s IQ
Alibaba to split itself into 6 business groups
30% OFF Jellycat Promo Code - September 2024 (*NEW*)
Kent And Pelczar Obituaries
Riegler & Partner Holding GmbH auf LinkedIn: Wie schätzen Sie die Entwicklung der Wohnraumschaffung und Bauwirtschaft…
Lesson 3 Homework Practice Measures Of Variation Answer Key
Which Is A Popular Southern Hemisphere Destination Microsoft Rewards
What’s the Difference Between Cash Flow and Profit?
Nashville Predators Wiki
George The Animal Steele Gif
Dutchess Cleaners Boardman Ohio
charleston cars & trucks - by owner - craigslist
Conscious Cloud Dispensary Photos
Lancasterfire Live Incidents
Imagetrend Inc, 20855 Kensington Blvd, Lakeville, MN 55044, US - MapQuest
Paychex Pricing And Fees (2024 Guide)
Praew Phat
CANNABIS ONLINE DISPENSARY Promo Code — $100 Off 2024
Joann Ally Employee Portal
Kayky Fifa 22 Potential
The Blind Showtimes Near Amc Merchants Crossing 16
Daytonaskipthegames
Xfinity Cup Race Today
Elbert County Swap Shop
Telegram Voyeur
Craigslist List Albuquerque: Your Ultimate Guide to Buying, Selling, and Finding Everything - First Republic Craigslist
Margaret Shelton Jeopardy Age
Jersey Shore Subreddit
Worthington Industries Red Jacket
Rainfall Map Oklahoma
Pixel Combat Unblocked
Martin Village Stm 16 & Imax
Gasbuddy Lenoir Nc
2012 Street Glide Blue Book Value
Shoreone Insurance A.m. Best Rating
Elizaveta Viktorovna Bout
Myql Loan Login
Heelyqutii
Check From Po Box 1111 Charlotte Nc 28201
Davis Fire Friday live updates: Community meeting set for 7 p.m. with Lombardo
Academy Sports New Bern Nc Coupons
Clima De 10 Días Para 60120
Nu Carnival Scenes
Petra Gorski Obituary (2024)
26 Best & Fun Things to Do in Saginaw (MI)
This Doctor Was Vilified After Contracting Ebola. Now He Sees History Repeating Itself With Coronavirus
Erica Mena Net Worth Forbes
Besoldungstabellen | Niedersächsisches Landesamt für Bezüge und Versorgung (NLBV)
Island Vibes Cafe Exeter Nh
Códigos SWIFT/BIC para bancos de USA
Latest Posts
Article information

Author: Dr. Pierre Goyette

Last Updated:

Views: 5638

Rating: 5 / 5 (50 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Dr. Pierre Goyette

Birthday: 1998-01-29

Address: Apt. 611 3357 Yong Plain, West Audra, IL 70053

Phone: +5819954278378

Job: Construction Director

Hobby: Embroidery, Creative writing, Shopping, Driving, Stand-up comedy, Coffee roasting, Scrapbooking

Introduction: My name is Dr. Pierre Goyette, I am a enchanting, powerful, jolly, rich, graceful, colorful, zany person who loves writing and wants to share my knowledge and understanding with you.