Governance ProviderError: max code size exceeded

Hi, i am trying to deploy a contract https://wizard.openzeppelin.com/#governor with upgradeability UUPS in mumbai testnet.

My contract (Governor.sol):

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

import "@openzeppelin/contracts-upgradeable/governance/GovernorUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/governance/extensions/GovernorSettingsUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/governance/extensions/GovernorCountingSimpleUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/governance/extensions/GovernorVotesUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/governance/extensions/GovernorVotesQuorumFractionUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/governance/extensions/GovernorTimelockControlUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";

/// @custom:security-contact security@plush.family
contract Governor is Initializable, GovernorUpgradeable, GovernorSettingsUpgradeable, GovernorCountingSimpleUpgradeable, GovernorVotesUpgradeable, GovernorVotesQuorumFractionUpgradeable, GovernorTimelockControlUpgradeable, OwnableUpgradeable, UUPSUpgradeable {
    /// @custom:oz-upgrades-unsafe-allow constructor
    constructor() initializer {}

    function initialize(IVotesUpgradeable _token, TimelockControllerUpgradeable _timelock)
    initializer public
    {
        __Governor_init("Governor");
        __GovernorSettings_init(1 /* 1 block */, 19636 /* 3 days */, 0);
        __GovernorCountingSimple_init();
        __GovernorVotes_init(_token);
        __GovernorVotesQuorumFraction_init(1);
        __GovernorTimelockControl_init(_timelock);
        __Ownable_init();
        __UUPSUpgradeable_init();
    }

    function _authorizeUpgrade(address newImplementation)
    internal
    onlyOwner
    override
    {}

    // The following functions are overrides required by Solidity.

    function votingDelay()
    public
    view
    override(IGovernorUpgradeable, GovernorSettingsUpgradeable)
    returns (uint256)
    {
        return super.votingDelay();
    }

    function votingPeriod()
    public
    view
    override(IGovernorUpgradeable, GovernorSettingsUpgradeable)
    returns (uint256)
    {
        return super.votingPeriod();
    }

    function quorum(uint256 blockNumber)
    public
    view
    override(IGovernorUpgradeable, GovernorVotesQuorumFractionUpgradeable)
    returns (uint256)
    {
        return super.quorum(blockNumber);
    }

    function getVotes(address account, uint256 blockNumber)
    public
    view
    override(IGovernorUpgradeable, GovernorVotesUpgradeable)
    returns (uint256)
    {
        return super.getVotes(account, blockNumber);
    }

    function state(uint256 proposalId)
    public
    view
    override(GovernorUpgradeable, GovernorTimelockControlUpgradeable)
    returns (ProposalState)
    {
        return super.state(proposalId);
    }

    function propose(address[] memory targets, uint256[] memory values, bytes[] memory calldatas, string memory description)
    public
    override(GovernorUpgradeable, IGovernorUpgradeable)
    returns (uint256)
    {
        return super.propose(targets, values, calldatas, description);
    }

    function proposalThreshold()
    public
    view
    override(GovernorUpgradeable, GovernorSettingsUpgradeable)
    returns (uint256)
    {
        return super.proposalThreshold();
    }

    function _execute(uint256 proposalId, address[] memory targets, uint256[] memory values, bytes[] memory calldatas, bytes32 descriptionHash)
    internal
    override(GovernorUpgradeable, GovernorTimelockControlUpgradeable)
    {
        super._execute(proposalId, targets, values, calldatas, descriptionHash);
    }

    function _cancel(address[] memory targets, uint256[] memory values, bytes[] memory calldatas, bytes32 descriptionHash)
    internal
    override(GovernorUpgradeable, GovernorTimelockControlUpgradeable)
    returns (uint256)
    {
        return super._cancel(targets, values, calldatas, descriptionHash);
    }

    function _executor()
    internal
    view
    override(GovernorUpgradeable, GovernorTimelockControlUpgradeable)
    returns (address)
    {
        return super._executor();
    }

    function supportsInterface(bytes4 interfaceId)
    public
    view
    override(GovernorUpgradeable, GovernorTimelockControlUpgradeable)
    returns (bool)
    {
        return super.supportsInterface(interfaceId);
    }
}

My ts hardhat(deploy):

const { ethers, upgrades } = require("hardhat");
import * as args from "../../arguments/plushCoinWalletsArgs";

require('@openzeppelin/hardhat-upgrades');

async function main() {
    const Governor = await ethers.getContractFactory("Governor");
    const governor = await upgrades.deployProxy(Governor);
    await governor.deployed();

    console.log('Governor -> deployed to address:', governor.address);

    if (process.env.NETWORK != 'local') {
        console.log('Waiting 1m before verify contract\n');
        await new Promise(function (resolve) {
            setTimeout(resolve, 60000);
        });
        console.log('Verifying...\n');

        await ethers.run('verify:verify', {
            address: governor.address,
            contract: 'contracts/Governor.sol:Governor'
        });
    }
}
main()
    .then(() => process.exit(0))
    .catch((error) => {
        console.error(error);
        process.exit(1);
    });

And i get this error:

Compiling 1 file with 0.8.9
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.
  --> contracts/Governor.sol:15:1:
   |
15 | contract Governor is Init ... nableUpgradeable, UUPSUpgradeable {
   | ^ (Relevant source part starts here and spans across multiple lines).


Solidity compilation finished successfully
ProviderError: max code size exceeded
    at HttpProvider.request (WorkFolder/plushNew/node_modules/hardhat/src/internal/core/providers/http.ts:49:19)
    at LocalAccountsProvider.request (WorkFolder/plushNew/node_modules/hardhat/src/internal/core/providers/accounts.ts:188:34)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at async EthersProviderWrapper.send (WorkFolder/plushNew/node_modules/@nomiclabs/hardhat-ethers/src/internal/ethers-provider-wrapper.ts:13:20)

Tell me what am I doing wrong? After all, I take the standard code from your constructor...

@Maksim_Gushchyn, are you using the solidity compiler optimizer? Enabling optimization can make a big difference in the size of your programs. Here's an example of how to turn that on in Hardhat from one of my projects.

Yes sure.

settings: {
      optimizer: {
        enabled: true,
        runs: 1,
      },
    },

I tried with different values but it doesn't help. I used to have 200, but after reading a lot of articles, I put "runs: 1", but it does not help, the error remains...

I just tried truffle, same problem

I just copied your contract from above into a new hardhat project and the size of the compiled contract for me came out to 15.3KB.

Here is a link to a minimal git repo to reproduce what I got.

1 Like
solidity: {
    compilers: [
      {
        version: '0.8.9',
      },
      {
        version: '0.8.4',
      },
      {
        version: '0.8.2',
      },
      {
        version: '0.6.6',
      },
      {
        version: '0.6.2',
      },
      {
        version: '0.6.0',
      },
    ],
    overrides: {
      'contracts/PlushCoin.sol': {
        version: '0.8.9',
      },
      'contracts/PlushCoreToken.sol': {
        version: '0.8.9',
      },
      'contracts/PlushForest.sol': {
        version: '0.8.9',
      },
      'contracts/PlushGetTree.sol': {
        version: '0.8.9',
      },
      'contracts/PlushGetCoreToken.sol': {
        version: '0.8.9',
      },
      'contracts/PlushCoinPolygon.sol': {
        version: '0.6.6',
      },
      'contracts/PlushCoinPolygonProxy.sol': {
        version: '0.6.6',
      },
    },
    settings: {
      optimizer: {
        enabled: true,
        runs: 200,
      },
    },
    contractSizer: {
      alphaSort: true,
      runOnCompile: true,
      disambiguatePaths: false,
    },
  },

if i use Multiple Solidity versions - 27 kb
if i use:

solidity: {
    version: '0.8.4',
    settings: {
      optimizer: {
        enabled: true,
        runs: 200,
      },
    },
    contractSizer: {
      alphaSort: true,
      runOnCompile: true,
      disambiguatePaths: false,
    },
  },

all good - 15.3 kb but i have problem with others contracts: how can i fixed that?

so i fixed that!

{
        version: "0.6.7",
        settings: {},
      },

i need to register the optimizer everywhere! Thank you!!!

2 Likes

for example your compiler setting should look like this .

solidity: {

compilers: [

  {

    version: "0.8.13",

    settings: {

      optimizer: {

        enabled: true,

        runs: 200,

      },

    },

  },

  {

    version: "0.8.9",

    settings: {

      optimizer: {

        enabled: true,

        runs: 200,

      },

    },

  },

  {

    version: "0.8.4",

    settings: {

      optimizer: {

        enabled: true,

        runs: 200,

      },

    },

  },

  {

    version: "0.8.2",

    settings: {

      optimizer: {

        enabled: true,

        runs: 200,

      },

    },

  },

  {

    version: "0.6.6",

    settings: {

      optimizer: {

        enabled: true,

        runs: 200,

      },

    },

  },

  {

    version: "0.6.2",

    settings: {

      optimizer: {

        enabled: true,

        runs: 200,

      },

    },

  },

  {

    version: "0.6.0",

    settings: {

      optimizer: {

        enabled: true,

        runs: 200,

      },

    },

  },

],

},

And It takes time to deploy the Governance contract.