Max contract size on Ethereum
A guide about the maximum contract size on Ethereum
Table of Contents for Max contract size on Ethereum
“Spurious Dragon” was the 4th hard fork, at block num 2,675,000, back in 2016. It introduced a new contract code size limit of 24576 bytes.
You can see more about this at https://github.com/ethereum/EIPs/blob/master/EIPS/eip-170.md
If block.number >= FORK_BLKNUM, then if contract creation initialization returns data with length of more than MAX_CODE_SIZE bytes, contract creation fails with an out of gas error.
You may have seen the following error, telling you about this:
Warning: Contract code size exceeds 24576 bytes (a limit introduced in Spurious Dragon). This contract may not be deployable on mainnet. Consider enabling the optimizer (with a low “runs” value!), turning off revert strings, or using libraries.
How to reduce Solidity contract sizes
This is a list of ways to reduce your Soldiity smart contract code size.
- remove unused code/functions
- avoid creating variables (inlining variables can reduce size)
- create smaller error messages
- separate your contracts into their own contracts
- try changing the optimizer settings
- use proxies, use delegatecall
- use libraries
Spotted a typo or have a suggestion to make this crypto dev article better? Please let me know!
📙 Solidity Auditing online quiz
Learn how to audit smart contracts by looking at some example code and trying to find the bugs
⛽ Solidity Gas Optimizations Guide
How to optimize and reduce gas usage in your smart contracts in Solidity
🧪 Guide to testing with Foundry
Guide to adding testing for your Solidity contracts, using the Foundry and Forge tools
📌 Guide to UTXO
UTXO and the UTXO set (used by blockchains such as Bitcoin) explained
📐 Solidity Assembly Guide
Introduction guide to using assembly in your Solidity smart contracts
📦 Ethereum EOF format explained
Information explaining what the upcoming Ethereum EOF format is all about