إعداد Hardhat
هيكل المشروع الأساسي
إعداد Hardhat درس مجاني في Web3 & DApp Development Fundamentals على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Web3 & DApp Development Fundamentals، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Web3 & DApp Development Fundamentals 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
What Is Hardhat
Hardhat is a development environment for Ethereum that helps you compile, test, deploy, and debug smart contracts.
- Written in JavaScript/TypeScript and run with Node.js.
- Ships with a built-in local Ethereum network.
- Has a rich plugin ecosystem (ethers, waffle, gas reporter).
It is the most popular alternative to Truffle and Foundry for JS-centric teams.
Prerequisites
Before installing Hardhat you need Node.js (LTS recommended) and a package manager such as npm or yarn.
Check your versions to make sure the toolchain is ready:
node -v
npm -vHardhat targets Node.js LTS releases, so avoid odd-numbered experimental versions.
node -v
npm -vCreating a Project Folder
Each Hardhat project lives in its own directory with a local package.json. Start by creating and initializing the folder:
mkdir my-dapp
cd my-dapp
npm init -yThis package.json will track Hardhat and your other dev dependencies.
mkdir my-dapp
cd my-dapp
npm init -yInstalling Hardhat
Install Hardhat as a dev dependency so it is not bundled into production:
npm install --save-dev hardhatInstalling locally (not globally) pins the exact version per project, which keeps builds reproducible across machines and CI.
npm install --save-dev hardhatScaffolding a Project
Run the Hardhat init wizard to scaffold a starter project:
npx hardhat initYou can choose a JavaScript or TypeScript project, or an empty config. The wizard creates sample contracts, tests, and a config file.
npx hardhat initProject Structure
A typical Hardhat project layout looks like this:
contracts/— your Solidity source files.scripts/— deployment and automation scripts.test/— Mocha/Chai test files.hardhat.config.js— central configuration.artifacts/andcache/— generated build output.
The Config File
The hardhat.config.js file is the heart of your setup. A minimal config just exports the Solidity version:
require("@nomicfoundation/hardhat-toolbox");
module.exports = {
solidity: "0.8.24",
};You will later extend it with networks, plugins, and compiler settings.
require("@nomicfoundation/hardhat-toolbox");
module.exports = {
solidity: "0.8.24",
};The Hardhat Toolbox
The Hardhat Toolbox bundles the most common plugins in one package:
- ethers.js integration.
- Chai matchers for assertions.
- Gas reporter and coverage.
- Etherscan verification.
npm install --save-dev @nomicfoundation/hardhat-toolboxnpm install --save-dev @nomicfoundation/hardhat-toolboxRunning Hardhat Tasks
Hardhat exposes its functionality through tasks. List all available tasks with:
npx hardhatCommon built-in tasks include compile, test, node, and clean. You always run them via npx to use the local install.
npx hardhatA First Contract
Add a simple contract under contracts/Greeter.sol:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
contract Greeter {
string public greeting = "Hello, Hardhat";
}The public state variable automatically gets a getter you can call after deployment.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
contract Greeter {
string public greeting = "Hello, Hardhat";
}Git and Ignore Rules
Generated folders should not be committed. Create a .gitignore:
node_modules
artifacts
cache
.envKeeping .env ignored protects secrets like private keys and RPC URLs from leaking into version control.
node_modules
artifacts
cache
.envQuick Check
Test your understanding of Hardhat project setup.
Recap
You set up a Hardhat project from scratch.
- Hardhat is a Node.js-based Ethereum dev environment.
npx hardhat initscaffolds contracts, scripts, tests, and config.hardhat.config.jsdefines Solidity version, networks, and plugins.- The Toolbox bundles ethers, Chai matchers, gas reporting, and verification.
- Ignore
node_modules,artifacts,cache, and.envin Git.
الأسئلة الشائعة
هل درس «إعداد Hardhat» مجاني؟
نعم — نص درس «إعداد Hardhat» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Web3 & DApp Development Fundamentals، انتقل إلى CoddyKit PRO. تتضمن دورة Web3 & DApp Development Fundamentals 4 دروس في المجموع.
ماذا ستتعلم في «إعداد Hardhat»؟
هيكل المشروع الأساسي تتمرن على Web3 & DApp Development Fundamentals مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Web3 & DApp Development Fundamentals؟
لا تُشترط خبرة سابقة. Web3 & DApp Development Fundamentals على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «إعداد Hardhat»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Web3 & DApp Development Fundamentals هذا؟
نعم. كل درس في Web3 & DApp Development Fundamentals يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- إعداد Hardhat
- ترجمة العقود
- النصوص والمهام
- الشبكة المحلية والتفرع