0

When I run npx hardhat compile, Hardhat says one of my contracts is above the size limit.

But hardhat-contract-sizer says it's not.

See below: enter image description here

enter image description here

enter image description here

Versions:

  • hardhat-contract-sizer version: ^2.5.1
  • solidity version: 0.8.9
  • hardhat version: 2.9.3

Help?

4 Answers4

0

Regardless of the reasons, I would suggest you assume that your contract size is above the limit to be on the safe side. If you were to compile with truffle, it could also give you a different contract size.

See: EIP-170 | How do you get the real contract size using hardhat and avoid contract code size warning?

You can see the following resource to downsize your contract:

See: EIP170 - Which contract features explain its size the most?

There are also reports of issues with hardhat-contract-sizer regarding that if internal functions are not been called in the contract, they will not be compiled into the contract, so it's hard to analyze the size of internal functions.

See: https://github.com/ItsNickBarry/hardhat-contract-sizer/issues/22

Yongjian P.
  • 4,170
  • 1
  • 3
  • 10
0

For those finding this thread because of test bloat. Commands like npx hardhat coverage add instrumentation to the code that has a large impact on contract size. Doing something without tests like npx hardhat compile avoids that bloat.

0

include optimizer object in settings of solidity compiler like this:-

module.exports = {
  solidity: {
    version: "0.8.9",
    settings: {
      optimizer: {
        enabled: true,
        runs: 1000,
      },
    },
    allowUnlimitedContractSize: true,
  },
};
-1

You can get contract size in hardhat without any libraries

e.g. test.js in /scripts of hardhat project

const hre = require("hardhat")

async function test() { // Use Hardhats provider (locally) or your own provider const provider = ethers.provider

const bytecode = await provider.getCode(contractAddress)
const size = bytecode.length / 2

}

blockson
  • 47
  • 2