Using multiple versions of Solidity in a single project
Some projects need to compile different files with different Solidity versions. Hardhat lets you define multiple compiler configurations using an extended format of the solidity property, which has a compilers array with different compiler configurations.
import { defineConfig } from "hardhat/config";
export default defineConfig({ // ... solidity: { compilers: [ { version: "0.7.6", }, { version: "0.8.11", }, { version: "0.8.29", settings: { optimizer: { enabled: true }, }, }, ], },});Hardhat compiles each Solidity file in your project using the highest configured Solidity version that’s compatible with the version pragma of the file and its dependencies.
For example, given the configuration above:
- A file with
pragma solidity ^0.8.0will be compiled with Solidity0.8.29, even though0.8.11is also compatible. - A file with
pragma solidity ^0.7.0will use Solidity0.7.6, which is the only valid matching version. - A file with
pragma solidity ^0.8.0that imports another file withpragma solidity <0.8.20will be compiled with0.8.11, because the combination of both restrictions makes it the highest compatible version.
Overriding version selection for a file
Section titled “Overriding version selection for a file”If you need to compile specific files using a different compiler version than Hardhat’s default choice, you can handle this with the overrides property:
import { defineConfig } from "hardhat/config";
export default defineConfig({ //... solidity: { compilers: [ /* configured compilers */ ], overrides: { "contracts/Foo.sol": { version: "0.8.11", }, }, },});In this case, contracts/Foo.sol will always be compiled with Solidity 0.8.11, regardless of the versions defined in solidity.compilers, its pragma, and those of its dependencies.
Each entry in the overrides object maps a file to a custom compiler configuration. Just like the main configuration, only the version field is mandatory.
Using overrides without multiple versions of Solidity
Section titled “Using overrides without multiple versions of Solidity”You can use overrides even if you’re using a single Solidity version. You’ll need to use the extended format of the solidity property, with an array of compilers and an overrides object.
For example, you can enable the optimizer only for a single file:
import { defineConfig } from "hardhat/config";
export default defineConfig({ //... solidity: { compilers: [ { version: "0.8.29", }, ], overrides: { "contracts/Foo.sol": { version: "0.8.29", settings: { optimizer: { enabled: true, }, }, }, }, },});