|
| 1 | +# Using a custom Solidity compiler |
| 2 | + |
| 3 | +To use a custom, local version of the Solidity compiler, you need to override the `TASK_COMPILE_SOLIDITY_GET_SOLC_BUILD` subtask in your Hardhat config. |
| 4 | + |
| 5 | +This subtask returns an object with four properties: |
| 6 | + |
| 7 | +- `compilerPath`: the path to the compiler |
| 8 | +- `isSolcJs`: a flag indicating if the compiler is a javascript module or a native binary |
| 9 | +- `version`: the short version of the compiler (for example, `0.8.24`) |
| 10 | +- `longVersion`: the long version of the compiler (for example, `0.8.24+commit.e11b9ed9`). This property is used as extra metadata in the build-info files, so you shouldn't worry too much about its value. |
| 11 | + |
| 12 | +Here is an example to override the [wasm solc build for version `0.8.24`](https://binaries.soliditylang.org/wasm/soljson-v0.8.24+commit.e11b9ed9.js). Before proceeding, ensure you download the required solc binary to the root of your hardhat project. |
| 13 | + |
| 14 | +::::tabsgroup{options="TypeScript,JavaScript"} |
| 15 | + |
| 16 | +:::tab{value="TypeScript"} |
| 17 | + |
| 18 | +```ts |
| 19 | +// hardhat.config.js |
| 20 | +import { HardhatUserConfig, subtask } from "hardhat/config"; |
| 21 | + |
| 22 | +const { |
| 23 | + TASK_COMPILE_SOLIDITY_GET_SOLC_BUILD, |
| 24 | +} = require("hardhat/builtin-tasks/task-names"); |
| 25 | +const path = require("path"); |
| 26 | + |
| 27 | +subtask( |
| 28 | + TASK_COMPILE_SOLIDITY_GET_SOLC_BUILD, |
| 29 | + async ( |
| 30 | + args: { |
| 31 | + solcVersion: string; |
| 32 | + }, |
| 33 | + hre, |
| 34 | + runSuper |
| 35 | + ) => { |
| 36 | + if (args.solcVersion === "0.8.24") { |
| 37 | + const compilerPath = path.join( |
| 38 | + __dirname, |
| 39 | + "soljson-v0.8.24+commit.e11b9ed9.js" |
| 40 | + ); |
| 41 | + |
| 42 | + return { |
| 43 | + compilerPath, |
| 44 | + isSolcJs: true, // if you are using a native compiler, set this to false |
| 45 | + version: args.solcVersion, |
| 46 | + // This is used as extra information in the build-info files, |
| 47 | + // but other than that is not important |
| 48 | + longVersion: "0.8.24+commit.e11b9ed9", |
| 49 | + }; |
| 50 | + } |
| 51 | + |
| 52 | + // since we only want to override the compiler for version 0.8.24, |
| 53 | + // the runSuper function allows us to call the default subtask. |
| 54 | + return runSuper(); |
| 55 | + } |
| 56 | +); |
| 57 | + |
| 58 | +const config: HardhatUserConfig = { |
| 59 | + solidity: "0.8.24", |
| 60 | +}; |
| 61 | + |
| 62 | +export default config; |
| 63 | +``` |
| 64 | + |
| 65 | +::: |
| 66 | + |
| 67 | +:::tab{value="JavaScript"} |
| 68 | + |
| 69 | +```js |
| 70 | +// hardhat.config.js |
| 71 | +const { |
| 72 | + TASK_COMPILE_SOLIDITY_GET_SOLC_BUILD, |
| 73 | +} = require("hardhat/builtin-tasks/task-names"); |
| 74 | +const path = require("path"); |
| 75 | + |
| 76 | +subtask(TASK_COMPILE_SOLIDITY_GET_SOLC_BUILD, async (args, hre, runSuper) => { |
| 77 | + if (args.solcVersion === "0.8.24") { |
| 78 | + const compilerPath = path.join( |
| 79 | + __dirname, |
| 80 | + "soljson-v0.8.24+commit.e11b9ed9.js" |
| 81 | + ); |
| 82 | + |
| 83 | + return { |
| 84 | + compilerPath, |
| 85 | + isSolcJs: true, // if you are using a native compiler, set this to false |
| 86 | + version: args.solcVersion, |
| 87 | + // This is used as extra information in the build-info files, |
| 88 | + // but other than that is not important |
| 89 | + longVersion: "0.8.24+commit.e11b9ed9", |
| 90 | + }; |
| 91 | + } |
| 92 | + |
| 93 | + // since we only want to override the compiler for version 0.8.24, |
| 94 | + // the runSuper function allows us to call the default subtask. |
| 95 | + return runSuper(); |
| 96 | +}); |
| 97 | + |
| 98 | +module.exports = { |
| 99 | + solidity: "0.8.24", |
| 100 | +}; |
| 101 | +``` |
| 102 | + |
| 103 | +::: |
| 104 | + |
| 105 | +:::: |
| 106 | + |
| 107 | +Check our ["Creating a task" guide](/hardhat-runner/docs/advanced/create-task) to learn more about overriding tasks. |
0 commit comments