0Pricing
Web3 & DApp Development Fundamentals · Pelajaran

Mengompilasi Kontrak

Artefak build

Mengompilasi Kontrak adalah pelajaran Web3 & DApp Development Fundamentals gratis di CoddyKit. Ini adalah pelajaran 2 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Web3 & DApp Development Fundamentals, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Web3 & DApp Development Fundamentals mencakup 4 pelajaran total.

Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.

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.

Pertanyaan yang Sering Diajukan

Apakah pelajaran “Mengompilasi Kontrak” gratis?

Ya — teks lengkap “Mengompilasi Kontrak” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Web3 & DApp Development Fundamentals, upgrade ke CoddyKit PRO. Kursus Web3 & DApp Development Fundamentals mencakup 4 pelajaran total.

Apa yang akan aku pelajari di “Mengompilasi Kontrak”?

Artefak build Kamu berlatih Web3 & DApp Development Fundamentals dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.

Apakah aku perlu pengalaman untuk memulai Web3 & DApp Development Fundamentals?

Tidak diperlukan pengalaman sebelumnya. Web3 & DApp Development Fundamentals di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 2 dari 4.

Berapa lama pelajaran “Mengompilasi Kontrak” memakan waktu?

Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.

Bisakah aku menulis dan menjalankan kode dalam pelajaran Web3 & DApp Development Fundamentals ini?

Ya. Setiap pelajaran Web3 & DApp Development Fundamentals menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.

Semua pelajaran dalam kursus ini

  1. Penyiapan Hardhat
  2. Mengompilasi Kontrak
  3. Skrip dan Tugas
  4. Jaringan Lokal dan Forking
← Kembali ke Web3 & DApp Development Fundamentals