Check the 24 KB contract size limit
Ethereum rejects contracts whose deployed code is larger than 24,576 bytes, a rule introduced by EIP-170, and since EIP-3860 it also rejects creation code over 49,152 bytes. Paste the compiled bytecode from Hardhat, Foundry, Remix or solc and this tool tells you how close you are, how many bytes you have to spare, and a floor estimate for the gas needed to deploy it.
Runtime versus creation bytecode
- Runtime (deployed) bytecode is what stays on the chain. It is the one the 24 KB limit applies to. In Hardhat it is the deployedBytecode field, and in Foundry it is the deployedBytecode in the artifact.
- Creation (init) bytecode is runtime code plus the constructor and is what the deployment transaction carries. It is checked against the 49,152 byte initcode limit.
How the gas estimate works
A deployment pays 21,000 base gas, 32,000 for contract creation, and calldata gas of 4 per zero byte and 16 per non-zero byte. Contracts then pay 200 gas for every byte of runtime code stored, and EIP-3860 adds 2 gas per 32-byte word of initcode. The estimate cannot include what your constructor does when it runs, such as writing storage or calling other contracts, so treat it as the minimum and measure the real cost on a test network.
Ways to shrink a large contract
Enable the optimizer with a low runs value, use custom errors in place of revert strings, remove unused code, move shared logic into libraries, and split big systems across several contracts. Other chains and test networks can have different limits, and some allow larger contracts, so check the one you deploy to. Foundry and Hardhat can also print contract sizes during a build.