Skip to content

Configuring the compiler

Solidity compilation in Hardhat is fully customizable. This guide covers the main ways you can configure the compiler to suit your project’s needs.

Configuring the compiler version and settings

Section titled “Configuring the compiler version and settings”

The simplest way to configure compilation is to set the Solidity compiler version and, optionally, its settings:

hardhat.config.ts
import { defineConfig } from "hardhat/config";
export default defineConfig({
//...
solidity: {
version: "0.8.29",
settings: {
/* solc settings, as expected by solc */
},
},
});

One common use of settings is enabling the optimizer. When you enable it, you can also define the number of runs, which affects how the compiler balances between deployment cost and execution cost:

hardhat.config.ts
import { defineConfig } from "hardhat/config";
export default defineConfig({
//...
solidity: {
version: "0.8.29",
settings: {
optimizer: {
enabled: true,
runs: 200,
},
},
},
});

You can also enable the IR-based code generator. While this compilation mode is slower, it enables more powerful optimizations:

hardhat.config.ts
import { defineConfig } from "hardhat/config";
export default defineConfig({
//...
solidity: {
version: "0.8.29",
settings: {
viaIR: true,
},
},
});

The settings property accepts the same options supported by the chosen compiler version. For the full details, see the Solidity compiler documentation.

You now know how to configure the Solidity compiler version, enable the optimizer, and adjust compiler settings. For more advanced configuration options, see the Solidity Configuration reference or these guides: