9

I am trying to deploy an upgradeable smart contract on bsc testnet using hardhat. But after running npx hardhat run --network testnet scripts/deploy.js it gives me the following error.

ProviderError: max code size exceeded
    at HttpProvider.request (D:\rarible-hardhat\protocol-contracts\tokens\node_modules\hardhat\src\internal\core\providers\http.ts:46:19)ts:46:19)                                                                                                                       ccounts.ts:142:34)
    at HDWalletProvider.request (D:\rarible-hardhat\protocol-contracts\tokens\node_modules\hardhat\src\internal\core\providers\accounts.ts:142:34)                                                                                                              nal\ethers-provider-wrapper.ts:13:20)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at EthersProviderWrapper.send (D:\rarible-hardhat\protocol-contracts\tokens\node_modules\@nomiclabs\hardhat-ethers\src\internal\ethers-provider-wrapper.ts:13:20)

How can I adjust the code size or any other method to resolve this error.

3 Answers 3

12

How can I adjust the code size or any other method to resolve this error.

The contract code size limit is 24kB (24,576 bytes).

You'll need to reduce the size of the contract before deploying, or split it into several smaller contracts and libraries.

Low hanging fruits would be any string literals (e.g. error strings in require() statements), and removing any unnecessary functions or variables.

2
  • I reduced the optimization runs from 200 to 1 and its working fine now
    – mzaidi
    May 5, 2021 at 12:01
  • Yep, that's another way. However, that will increase the gas costs for the users when they execute the functions at a later date. If that's not a consideration, then changing the optimisation is probably the way to go. May 5, 2021 at 12:10
5

you can try Above mentioned steps but before doing any of that, Please try compilerOptimization setting in hardhat.config.js. IMO, use at least 1000 runs which will optimize your deployment a lot increasing the deployment costs but your users will pay less gas fees.

module.exports = {
  solidity: {
    version: "0.8.6",
    settings: {
      optimizer: {
        enabled: true,
        runs: 200
      }
    }
  },
  networks: {
    hardhat: {
    },
    localhost: {
    },
    fuji: {
     
    }
  },
  paths: {
    sources: "./contracts",
    tests: "./test",
    cache: "./cache",
    artifacts: "./artifacts"
  }

};
2

How can I adjust the code size or any other method to resolve this error.

  1. Get rid of any library (like that of OpenZeppelin's Enumerable or Ownable) whose only few functions you would need. Instead, define those specific functions yourself.

  2. Identify any public variables that can be made private, the compiler adds a getter function by itself which adds to the code size.

  3. If you have a lot of comments, get rid of them or shorten them.

  4. The last resort: try to shorten the error message, variable names.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.