8

I'm using hardhat and when trying to compile the code it shows me a warning containing

@uniswap/v2-periphery/contracts/UniswapV2Router02.sol:12:1: 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.
contract UniswapV2Router02 is IUniswapV2Router02 {
^ (Relevant source part starts here and spans across multiple lines).

I'm not writing any contract with the name UniswapV2Router02 although I'm importing it in contract code

pragma solidity 0.6.6;

import '@uniswap/v2-periphery/contracts/UniswapV2Router02.sol';

How can I downsize the contract that I'm writing and import the uniswap router?

Here is the config file

const config = {
  solidity: "0.6.6",
  paths: {
    artifacts: "./src/artifects",
  },
  networks: {
    goerli: {
      chainID: 5,
      url: INFURA,
      accounts: [
        "0xTHE_ACCOUNT",
      ],
      timeout: 60000,
    },
  },
  settings: {
    optimizer: {
      enabled: true,
      runs: 200,
    },
  },
};

export default config;

4 Answers 4

4

In your hardhat project folder go to file hardhat.config.js and put the following code in the module.exports

module.exports = { solidity: { version: "0.5.15", settings: { optimizer: { enabled: true, runs: 200 } } }

hope it helps you.

2
  • 1
    I added these to config and it gives me the same warning
    – Yaya
    Commented Jul 27, 2022 at 10:06
  • 1
    Make a change to code at place of run :200 change it to run: 1 like this--- module.exports = { solidity: { version: "0.5.15", settings: { optimizer: { enabled: true, runs: 1 } } } Commented Jul 29, 2022 at 1:58
3

There are some methods to reduce your contract size. firstly you should calc your contract size, then enable optimizer (with low run) to reduce smart contract size. if your problem is remains you should use this.

4
  • 1
    I calculated the contracts and the largest of them all was UniswapV2Router02 :) Now even though I enabled the optimizer in config, it doesn't change in size. By the way I now added the config file in the question please check it out
    – Yaya
    Commented Jul 27, 2022 at 10:23
  • 1
    what is size of your contract? Commented Jul 27, 2022 at 12:13
  • 1
    26.302 Kib ......
    – Yaya
    Commented Jul 27, 2022 at 12:30
  • 1
    optimizer doesn't work for any contract!
    – Yaya
    Commented Jul 27, 2022 at 12:31
2

For whatever reason optimizer set in hardhat config didn't work for me as well. But I managed to deploy through remix with the same optimization parameters, could work as a temporary solution.

0

you have to add these all in hardhat.config.js file

 solidity: {
    version: "0.8.15",
    settings: {
      optimizer: {
        enabled: true,
        runs: 1,
      },
      "viaIR": true,
    }
  },
  contractSizer: {
    alphaSort: true,
    disambiguatePaths: false,
    runOnCompile: true,
    strict: true,
    only: [':ERC20$'],
  },

and then you have to install contract size to check contract size with npm.

1
  • it's not working @Ravaga Commented Feb 14, 2023 at 14:47

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.