Contract code size exceeds 24576 bytes

Hey everyone, I recently came across this error "Contract code size exceeds 24576 bytes ". I googled and read a few things. There are 2 solutions that I get.

  1. Using a truffle plugin.
  2. Refactor code to be modular.

I can't do the first one as I am using Hardhat. And secondly, I can't really refactor much here. What I have here is a governance contract that I made from reading the openzeppelin blog. I am going to make this upgradable and add features late..... I can see how to make the modular but what I have so far is just very basic/necessary. I have just imported contracts and overridden functions. That's it,there isn't really much to refactor.

I have added config to help me but nothing was affected.

module.exports = {
  solidity: "0.8.0",
  settings: {
    optimizer: {
      enabled: true,
      runs: 1
    },
    evmVersion: "byzantium"
  }
};

Any help or suggestion is appreciated.

Maybe this helps:

Generally try to remove functions you/your contract won't ever use.
When copy and pasting contract code from somewhere else, you often introduce functions that never will be used.

Can you share the code? There are different tricks you can try depending on the code itself.

I have already spent some time on that but my contract is the bare minimum at this point.

Slr, I lost my tab on this one. It's the minimum governance contract from https://docs.openzeppelin.com/contracts/4.x/governance. For reference, here is my code https://github.com/DeFipedia/defipedia-protocol/blob/develop/contracts/DefipediaGovernor.sol.

I have future plans to modify this, and I can see how I will refactor them, but here I am just importing, setting up required, and overriding required functions. I have seen a common suggestion of using a truffle tool, but I am on hardhat.

I am still struggling to figure out what I did wrong from the article.

Your contract compiles without warnings with optimizations on. It's below the contract size limit.

Can you please share that optimization snippet, please? Because, I still have it for some reason.

1 Like

Your config is wrong. settings goes under solidity. See Hardhat docs: Configuration.

1 Like

Oh, that was an idiotic mistake on my part. Thanks for helping me with it.

1 Like