# Contract code size exceeds 24576 bytes

**URL:** <https://forum.openzeppelin.com/t/contract-code-size-exceeds-24576-bytes/17250>\
**Category:** Contracts\
**Created:** [October 20, 2021, 5:57am UTC](https://forum.openzeppelin.com/t/contract-code-size-exceeds-24576-bytes/17250 "2021-10-20T05:57:26Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![PradhumnaPancholi](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/pradhumnapancholi/32/2809_2.png) [@PradhumnaPancholi](https://forum.openzeppelin.com/u/PradhumnaPancholi)\
**Post date:** [October 20, 2021, 5:57am UTC](https://forum.openzeppelin.com/t/contract-code-size-exceeds-24576-bytes/17250/1 "2021-10-20T05:57:26Z")

</div>

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.

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

```

Any help or suggestion is appreciated.

---

<div class="post-metadata">

**Author:** ![kevin](https://avatars.discourse-cdn.com/v4/letter/k/2acd7d/32.png) [@kevin](https://forum.openzeppelin.com/u/kevin)\
**Post date:** [October 20, 2021, 8:58am UTC](https://forum.openzeppelin.com/t/contract-code-size-exceeds-24576-bytes/17250/2 "2021-10-20T08:58:42Z")

</div>

Maybe this helps:

> **[Downsizing contracts to fight the contract size limit](https://soliditydeveloper.com/max-contract-size/)**
>
> There is a maximum size for smart contracts to prevent DOS attacks. What can you do to prevent your contracts from getting over the limit?

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.

---

<div class="post-metadata">

**Author:** ![frangio](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/frangio/32/15251_2.png) [@frangio](https://forum.openzeppelin.com/u/frangio)\
**Post date:** [October 22, 2021, 2:20pm UTC](https://forum.openzeppelin.com/t/contract-code-size-exceeds-24576-bytes/17250/3 "2021-10-22T14:20:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![PradhumnaPancholi](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/pradhumnapancholi/32/2809_2.png) [@PradhumnaPancholi](https://forum.openzeppelin.com/u/PradhumnaPancholi)\
**Post date:** [November 8, 2021, 11:34pm UTC](https://forum.openzeppelin.com/t/contract-code-size-exceeds-24576-bytes/17250/4 "2021-11-08T23:34:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![PradhumnaPancholi](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/pradhumnapancholi/32/2809_2.png) [@PradhumnaPancholi](https://forum.openzeppelin.com/u/PradhumnaPancholi)\
**Post date:** [November 8, 2021, 11:38pm UTC](https://forum.openzeppelin.com/t/contract-code-size-exceeds-24576-bytes/17250/5 "2021-11-08T23:38:12Z")

</div>

Slr, I lost my tab on this one. It's the minimum governance contract from [https://docs.openzeppelin.com/contracts/4.x/governance](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](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.

---

<div class="post-metadata">

**Author:** ![frangio](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/frangio/32/15251_2.png) [@frangio](https://forum.openzeppelin.com/u/frangio)\
**Post date:** [November 15, 2021, 11:59pm UTC](https://forum.openzeppelin.com/t/contract-code-size-exceeds-24576-bytes/17250/6 "2021-11-15T23:59:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![PradhumnaPancholi](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/pradhumnapancholi/32/2809_2.png) [@PradhumnaPancholi](https://forum.openzeppelin.com/u/PradhumnaPancholi)\
**Post date:** [November 17, 2021, 3:55am UTC](https://forum.openzeppelin.com/t/contract-code-size-exceeds-24576-bytes/17250/7 "2021-11-17T03:55:09Z")

</div>

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

 ![Screenshot from 2021-11-16 22-53-37](https://us1.discourse-cdn.com/flex022/uploads/zeppelin/original/2X/9/96dd196c66d508d78442ede82e56f59b427d93ce.png)

---

<div class="post-metadata">

**Author:** ![frangio](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/frangio/32/15251_2.png) [@frangio](https://forum.openzeppelin.com/u/frangio)\
**Post date:** [November 23, 2021, 1:27pm UTC](https://forum.openzeppelin.com/t/contract-code-size-exceeds-24576-bytes/17250/8 "2021-11-23T13:27:56Z")

</div>

Your config is wrong. `settings` goes under `solidity`. See Hardhat docs: [Configuration](https://hardhat.org/config/).

---

<div class="post-metadata">

**Author:** ![PradhumnaPancholi](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/pradhumnapancholi/32/2809_2.png) [@PradhumnaPancholi](https://forum.openzeppelin.com/u/PradhumnaPancholi)\
**Post date:** [November 24, 2021, 3:50am UTC](https://forum.openzeppelin.com/t/contract-code-size-exceeds-24576-bytes/17250/9 "2021-11-24T03:50:33Z")

</div>

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