Lesson 2 of 3 Compiling Smart Contracts

Compiler Versions and Settings

The same source can produce different bytecode, gas costs and behaviour depending on compiler settings. This lesson explains how to choose a compiler version, what the optimizer and its runs value do, and why the EVM version must match the target network.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain how to choose and pin a compiler version.
  • Explain the effect of the optimizer's runs setting.
  • Explain why the EVM version must match the target network.
  • Explain why compiler settings matter for verification.

Choosing a compiler version

The version pragma in each file states which compilers may compile it; the tool configuration picks the exact version. In Hardhat 3, a simple configuration is:

solidity: {
  version: "0.8.34",
  settings: { optimizer: { enabled: true, runs: 200 } },
},

Hardhat can also define several compilers for different files, and named build profiles, such as a production profile with the optimizer enabled. Recent patch versions fix bugs and add features, so new projects should use a recent 0.8 release and pin it, so that every developer and the continuous integration server produce identical bytecode. The Solidity team publishes a list of known compiler bugs per version, which auditors check.

The optimizer

The optimizer rewrites bytecode to reduce gas: it removes redundant operations, simplifies expressions, inlines small functions and combines storage operations. Its runs setting tells it how many times each opcode is expected to run over the contract's life. The following comparison shows the effect.

Low runs (for example 1)
Optimises forSmall bytecode, cheap deployment
Deployment costLowest
Cost per callHigher
SuitsRarely used contracts, size-limited contracts
Default (200)
Optimises forA balance
Deployment costMedium
Cost per callMedium
SuitsMost contracts
High runs (for example 10,000)
Optimises forCheap execution
Deployment costHighest
Cost per callLowest
SuitsHeavily used contracts such as tokens and exchanges

Setting viaIR: true compiles through the Yul intermediate representation, which enables more powerful optimisation across functions and avoids some "stack too deep" errors, at the cost of slower compilation.

True or false

Optimizer settings

Exam skills 2.3.2

Check what the optimizer's settings change.

Log in to try this activity Interactive activities are free with an account.

The EVM version

Each Ethereum upgrade can add opcodes; for example, Shanghai added PUSH0 and Cancun added transient storage (TLOAD, TSTORE) and MCOPY. The evmVersion setting tells the compiler which upgrade to target, and therefore which opcodes it may emit. Recent compilers default to osaka, the EVM of the Fusaka upgrade.

Deploying to other EVM chains

Some other EVM chains lag behind Ethereum's upgrades. Bytecode compiled for a newer EVM can fail with "invalid opcode" on such a chain, so set evmVersion to the oldest version among the networks you will deploy to.

Scenario questions

Targeting an EVM version

Exam skills 2.3.2

Choose compiler settings that match the network a contract will run on.

Log in to try this activity Interactive activities are free with an account.

Settings and verification

Compiler version, optimizer settings, runs, viaIR and EVM version all change the bytecode. Source verification recompiles the source with the declared settings and compares the result to the deployed code, so the exact settings used for a deployment must be recorded. Keeping the configuration in version control and deploying from a tagged commit makes this reliable.

Worked example

Tuning runs

A team is deploying (a) an ERC-20 token that will process millions of transfers and (b) a one-time airdrop contract used for a single week. Which runs values fit?

For the token, a high value such as 10,000 or more: deployment costs more once, but every one of millions of transfers becomes cheaper. For the airdrop, a low value such as 1 to 200: deployment cost dominates because the contract runs few times. In both cases, measure with a gas report rather than assume.

Worked example

Verification fails

A contract was deployed from a developer's laptop. Verification on Etherscan with the project's current configuration fails with "bytecode does not match". The source is unchanged. What is the likely cause?

A settings difference: the laptop may have used a different compiler version, optimizer setting, runs value or EVM version from the one now configured. Hardhat's build info files record the exact input and settings of each compilation; using them, or the Ignition deployment's recorded build, allows verification to succeed.

Check yourself

A contract will be called millions of times. Should the optimizer's runs value be low or high?

Show answer

High. A high runs value optimises for cheap execution at the cost of larger bytecode and a more expensive deployment, which pays off when the contract is called often.

Exam Tips

Remember for the exam

  • The pragma limits compiler versions; the configuration picks the exact one. Pin it.
  • Low runs favours cheap deployment; high runs favours cheap calls.
  • evmVersion decides which opcodes may be used; recent solc defaults to osaka.
  • Every setting changes the bytecode, so record settings for verification.