I'm facing the following error, when compiling my contracts in Hardhat:

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.
  1. I have the optimizer: {enabled: true, runs: N}, and already tested different values for runs: N.
  2. I have also created libraries to reuse as much code as possible, and have followed this suggestions.

None of above worked for me (too much code). So, I have split my code in 3 different contracts that can be deployed independently (with a simple custom deploy.ts script). The problem is that Hardhat is always compiling all the code under /contracts folder altogether, so I always getting the same error of Contract code size exceeds 24576 bytes

I have read the Hardhat compilation documentation in order to find any configuration to compile independently, but nothing stated about this topic.

I have also read the Hardhat documentation about path configuration but it seems that you can only to select one folder to compile all .sol inside, based on the text:

sources: The directory where your contract are stored. This path is resolved from the project's root. Default value: './contracts'.

So, after doing my own research:

  1. Does anyone know if it is possible (and how) to compile contracts independently (grouped in different folders) with Hardhat?
  2. Do I need to create different Hardhat projects for the different contract groups?
  • 2
    I don't think Hardhat gives this warning for all contracts combined when it says contract size exceeding, it means one single contract is independently exceeding the size. because if the case you mentioned was true, then we will always get that error, but we don't. Aug 20, 2023 at 5:41
  • 1
    As @ZartajAfser commented the error is not about the combined size of the contracts but about a single contract. One of your contracts is still too big.
    – Jaime
    Aug 20, 2023 at 14:15
  • But if I split the big contract in 2 new contracts, they will have dependencies with each other, so they will be treated as one by the compile, getting the same error. The only option I see, is to deploy independently the contracts (with public functions), making one contract the owner of the other one. But does not seem the best approach for security concerns. Aug 21, 2023 at 9:22
  • 1
    You have to make your contract(app) modular, like diamond pattern, but you can omit upgradability for example if you want.
    – tenbits
    Aug 21, 2023 at 12:12
  • Yes, I finally came across to this approach. Aug 21, 2023 at 17:54

2 Answers 2


You can allow unlimited contract sizes in your hardhat config file.

const config: HardhatUserConfig = {
  networks: {
    hardhat: {
      chainId: 31337,
      allowUnlimitedContractSize: true,
    localhost: {
      chainId: 31337,
      allowUnlimitedContractSize: true,
  • This is useful and related to op's use case but it doesn't answer the question.
    – Madbreaks
    Nov 14, 2023 at 20:29

After some research & testing, I see that the warning comes from independent contracts, taking into account that if .sol file's contract inherits other contracts, the total size will be sum of all involved contracts.

So, in conclusion, if this error comes from a contract, there are only 2 main approach to reduce the size.

  1. Using libraries
  2. Splitting the contract into independent contracts
  3. Optimize the code

If previous actions are not possible to reduce the size, refactoring to Diamond pattern along with Appstorage, or similar approach, is the only solution.

  • This doesn't answer op's question.
    – Madbreaks
    Nov 14, 2023 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.