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 size

Why does replacing modifiers with private functions in solidity reduce the contract’s size?

Part I: Beware Solidity Devs, you might just hit your contract size limit (4)Decipher with ZaryabMd Zaryab Afser

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

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 👇

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)

FAQs

What is the maximum size of a Solidity contract? ›

The limit was introduced to prevent denial-of-service (DOS) attacks. When a Solidity developer adds more and more functionality to their contract, at some point, they will reach the limit, and when deploying will see the error: “Warning: Contract code size exceeds 24576 bytes (a limit introduced in Spurious Dragon).

What is the size limit of Solidity event? ›

The size limit of a Solidity event is 320 bytes. This includes the name of the event, the parameters, and the data types of the parameters.

What is the size limit of smart contract in Ethereum? ›

A hard limit of 24,576 bytes was introduced in EIP-170 to prevent DOS (Denial of Service) attacks. This EIP was created because the work required by Ethereum nodes increases disproportionately based on the contract code's size.

How to reduce bytecode size? ›

How to Save on Bytecode Size?
  1. [ MEDIUM ]: Combine Functions: Rather than creating multiple functions, combine the code into a single function.
  2. [ MEDIUM ]: In-line Function Calls: Rather than creating a function, do processing of that function in the same caller function.
Aug 31, 2023

What is the contract size? ›

The term contract size refers to the deliverable quantity of a stock, commodity, or financial instrument that underlies a futures or options contract. It is a standardized amount that tells traders the exact quantities that are being bought or sold based on the terms of the contract.

What is the lot size of a contract? ›

While contract size represents the total value of a trade, lot size determines the quantity of currency units being traded. Contract size remains constant for a specific currency pair, whereas lot size can vary depending on the trader's preferences and trading strategy.

How much storage is in a Solidity contract? ›

Each contract has a finite amount of storage space, which is divided into 32-byte slots. Each slot can hold a 256-bit value, such as an address or an integer. When you declare a variable in Solidity, you must specify its data location as either memory or storage.

What is the maximum stack size in Solidity? ›

It has a maximum size of 1024 elements and contains words of 256 bits. The call stack is utilized when a contract invokes another contract, or itself by means of this. f(), is bounded to 1024 frames. If this limit is reached, then a further invocation will throw an exception.

What is the maximum array size in Solidity? ›

Starting from Solidity version 0.6. 5 the maximum allocation size for dynamic memory arrays is 2**64-1 . Attempting to allocate larger arrays now directly reverts.

How much does it cost to run a smart contract on Ethereum? ›

Smart contract creation cost can be anywhere from $10 to $2,000 assuming Ether costs between $1,500 to $2,000. The biggest factors are 1) Ethereum price, 2) the size of the compiled contract (in bytes), 3) the current gas price on the Ethereum network.

What is the average size of a smart contract? ›

Answers. The maximum size of a smart contract deployed on the TON Network depends on the complexity of the contract and the number of cells it utilizes. In general, the size of a smart contract's Bag-of-Cells (BoC) representation should not exceed the size limit of a single shardchain block, which is 2 MB .

Can bytecode be manipulated? ›

Modifying bytecodes: Bytecode manipulation offers a powerful approach to introduce new behaviors at various stages of the code execution process. It can be done through custom classloaders, the Instrumentation API, among others.

Is bytecode highly optimized? ›

Bytecode is a highly optimized set of instructions designed to be executed by the Java run-time system, which is called the Java Virtual Machine (JVM). In essence, the original JVM was designed as an interpreter for bytecode.

How to reduce data size? ›

There are two methods for reducing dataset size: packing and compression. By packing we mean altering the data in a way that reduces its precision. By compression we mean techniques that store the data more efficiently and result in no precision loss.

What is the maximum storage of a smart contract? ›

If you use cells in a linear single ref tree, you are limited to 512 * 1023 = 523776 bits or near 64 KB. But if you use all cells to store data, it's a very huge tree with a lot of space. Only at the bottom layer it has 4 ** 511 cells.

Top Articles
Why Is This Sacajawea Dollar Worth Thousands of Dollars?
I Messed Up The No contact Rule - What Do I Do To Fix It?
Brittany Dawn James Gofundme
NYPD officers seen in video repeatedly punching a man are placed on modified duty
Mtlsd.schoology
Ads Supplier Portal
Cars for Sale by Owner in Shreveport, LA
Rivals Northwestern
Trauma Care | FMOLHS
Haktuts Free Spins Link 2020
Book a Cruise | Search Cruise Holidays | P&O Cruises
Project Zomboid Dynamic Skybox
GIF by Barstool Sports - Find & Share on GIPHY
Bmp 202 Blue Round Pill
Lohikeitto (Finnish Salmon Soup) Recipe on Food52
Spider Man Wiki Spider Man
Katinakay Leaks
Embassy Suites Wisconsin Dells
Peg Leg Pete's Reservations
Black Men Have Issues
Panama City News Herald Obituary
Farsi1Tv Hd
Omniplex Cinema Dublin - Rathmines | Cinema Listings
Infinite Campus Oldham County Ky
Does Zenni Take Care Credit
Zariajay Erome
Events • Constellation
Walmart Smart Styles Near Me
H1889 007 04 - Local Ppo
Davita.intranet
Volkswagen For Sale Craigslist
Stugats In English
Mike Wirth Net Worth
Eli Lilly Clarifies It’s Not Offering Free Insulin After Tweet From Fake Verified Account—As Chaos Unfolds On Twitter
Hilton Honors Enrollment Rejected
Buffalo Allergy Forecast
Beau Burns Gofundme
How to Use Keyframes in CapCut
Upcoming Events & Tickets | Thompson Boling Arena
Virginia Milestat
Jm White Funeral
What Happened To Ed Hanna Wfmz
Warfare Personified Crossword Clue
763-298-8022
2005 Chevrolet Silverado Radio Wiring Diagram
Effingham Bookings Florence Sc
First United Bank Seminole Texas
Leanbeefpatty tit*
World of Warcraft Bringing Back Old Anniversary Rewards for the First Time in 15 Years
7440 Dean Martin Dr Suite 204 Directions
Www Getelate.com
Derpixon Kemono
Latest Posts
Article information

Author: Rob Wisoky

Last Updated:

Views: 6455

Rating: 4.8 / 5 (48 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Rob Wisoky

Birthday: 1994-09-30

Address: 5789 Michel Vista, West Domenic, OR 80464-9452

Phone: +97313824072371

Job: Education Orchestrator

Hobby: Lockpicking, Crocheting, Baton twirling, Video gaming, Jogging, Whittling, Model building

Introduction: My name is Rob Wisoky, I am a smiling, helpful, encouraging, zealous, energetic, faithful, fantastic person who loves writing and wants to share my knowledge and understanding with you.