0Pricing
Web3 & DApp Development Fundamentals · 课时

编译合约

构建产物

编译合约 是 CoddyKit 上的免费 Web3 & DApp Development Fundamentals 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Web3 & DApp Development Fundamentals 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Web3 & DApp Development Fundamentals 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Why Compile

Solidity source code cannot run on the EVM directly. The compiler turns .sol files into bytecode and an ABI that tools and clients use.

  • Bytecode — the low-level instructions deployed on-chain.
  • ABI — a JSON interface describing functions and events.

The Compile Task

Hardhat compiles every contract under contracts/ with a single command:

npx hardhat compile

Hardhat downloads the right solc version automatically, so you do not need to install the compiler manually.

npx hardhat compile

Choosing the Solidity Version

The compiler version is set in hardhat.config.js and must satisfy the pragma in your contracts:

module.exports = { solidity: "0.8.24", };

If your contract says pragma solidity ^0.8.24;, the configured version must be compatible or compilation fails.

module.exports = {
  solidity: "0.8.24",
};

Build Artifacts

After compiling, Hardhat writes output to the artifacts/ folder. Each contract gets a JSON file containing:

  • abi — the application binary interface.
  • bytecode — deployment bytecode.
  • deployedBytecode — runtime bytecode.

These artifacts are what ethers.js loads to interact with your contract.

The Cache Folder

Hardhat keeps a cache/ folder to track what changed. On the next compile, only modified files are recompiled.

This incremental compilation makes large projects build quickly. If something seems stale, you can force a fresh build.

Cleaning the Build

To remove cached and generated files, run the clean task:

npx hardhat clean

This deletes artifacts/ and cache/. Use it when you suspect a stale artifact is causing odd behavior, then recompile.

npx hardhat clean

Compiler Optimizer

The optimizer reduces gas costs and bytecode size. Enable it in the config:

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

A higher runs value optimizes for frequent calls; a lower value optimizes for deployment size.

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

Multiple Compiler Versions

If different contracts need different Solidity versions, list several compilers:

module.exports = { solidity: { compilers: [ { version: "0.8.24" }, { version: "0.7.6" }, ], }, };

Hardhat picks a matching compiler for each file based on its pragma.

module.exports = {
  solidity: {
    compilers: [
      { version: "0.8.24" },
      { version: "0.7.6" },
    ],
  },
};

Reading Compiler Warnings

The compiler reports errors (which stop the build) and warnings (which do not). Common warnings include:

  • Unused local variables.
  • Shadowed declarations.
  • Functions that could be marked view or pure.

Treat warnings seriously — they often hint at real bugs.

Using the ABI Later

Once compiled, you import the artifact in scripts or tests to attach to a contract:

const artifact = require("./artifacts/contracts/Greeter.sol/Greeter.json"); console.log(artifact.abi);

Tools like ethers.js use this ABI to encode and decode calls correctly.

const artifact = require("./artifacts/contracts/Greeter.sol/Greeter.json");
console.log(artifact.abi);

Forcing a Recompile

By default Hardhat skips files that have not changed. To rebuild everything without cleaning, pass the force flag:

npx hardhat compile --force

This is useful after changing compiler settings, which the incremental cache may not detect on its own.

npx hardhat compile --force

Quick Check

Test your understanding of compilation in Hardhat.

Recap

You learned how Hardhat compiles contracts.

  • npx hardhat compile builds all contracts and downloads the right solc.
  • Artifacts (ABI + bytecode) land in artifacts/; incremental builds use cache/.
  • npx hardhat clean removes stale output.
  • The optimizer and multiple compiler versions are configured in hardhat.config.js.
  • Always fix warnings — they often reveal bugs.

常见问题解答

「编译合约」课时是免费的吗?

是的 — 「编译合约」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Web3 & DApp Development Fundamentals 课程的其余内容,请升级到 CoddyKit PRO。 Web3 & DApp Development Fundamentals 课程共包含 4 节课。

「编译合约」这节课中我会学到什么?

构建产物 你通过在浏览器中直接运行的动手代码来练习 Web3 & DApp Development Fundamentals,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Web3 & DApp Development Fundamentals 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Web3 & DApp Development Fundamentals 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「编译合约」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Web3 & DApp Development Fundamentals 课中编写并运行代码吗?

能。每节 Web3 & DApp Development Fundamentals 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 设置 Hardhat
  2. 编译合约
  3. 脚本与任务
  4. 本地网络与分叉
← 返回 Web3 & DApp Development Fundamentals