0Pricing
Web3 & DApp Development Fundamentals · 강의

컨트랙트 컴파일하기

빌드 산출물

컨트랙트 컴파일하기은(는) CoddyKit의 무료 Web3 & DApp Development Fundamentals 강의입니다. 이것은 4개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 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.

자주 묻는 질문

“컨트랙트 컴파일하기” 강의는 무료인가요?

네 — “컨트랙트 컴파일하기” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Web3 & DApp Development Fundamentals 강의 전체를 잠금 해제할 수 있습니다. Web3 & DApp Development Fundamentals 강의에는 총 4개의 강의가 포함되어 있습니다.

“컨트랙트 컴파일하기”에서 뭘 배우나요?

빌드 산출물 브라우저에서 직접 실행하는 실습 코드로 Web3 & DApp Development Fundamentals을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Web3 & DApp Development Fundamentals을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Web3 & DApp Development Fundamentals은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 2번째 강의입니다.

“컨트랙트 컴파일하기” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Web3 & DApp Development Fundamentals 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Web3 & DApp Development Fundamentals 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. Hardhat 설정
  2. 컨트랙트 컴파일하기
  3. 스크립트와 작업
  4. 로컬 네트워크와 포크
← Web3 & DApp Development Fundamentals(으)로 돌아가기