Hardhat – Transaction Reverted: Deploying a Contract with Too Large Code

chaihardhathardhat-deploytestingtestnets

My smart contract is over 300 lines of code, complete with events, modifiers, internal functions, getters and setters.

I'm trying to run tests using hardhat and chai. All I'm getting is this error:
Error: cannot estimate gas; transaction may fail or may require manual gas limit

"Transaction reverted: trying to deploy a contract whose code is too large"

Like, what the hell? When I run my deploy script to Mumbai testnet, I also get an error although different:

How can I make this smart contract build with so much code in it?

npx hardhat --network polygonMumbai run scripts\runTradable.ts

result:

Deploying contracts with the account: 0x85efe474EfD954021A9562ca6A0AC5eD8679DEbF
Account balance: 3.75318680580021
Network: polygonMumbai
ProviderError: HttpProviderError
    at HttpProvider.request 

What I would like:

  • Make the contract build without all the unnecessary code, if any.
  • Get rid of the error: cannot estimate gas; transaction may fail or may require manual gas limit when deploying
  • make all tests pass and deploy

Best Answer

In this case, the problem was the mal-formed hardhat.config.js file. Here is what should solve the problem:

module.exports = {
  solidity: {
    version: "0.8.9",
    settings: {
      optimizer: {
        enabled: true,
        runs: 1000,
      },
    },
  },
  allowUnlimitedContractSize: true,
  networks: {[...]}
}
Related Topic