Blog About
Table of Contents
  • เนื้อหา
  • Configuration
  • Available config options
  • Networks configuration
  • Hardhat Network
  • JSON-RPC based networks
  • Solidity configuration
  • Path configuration
  • Mocha configuration
  • อ่านเพิ่ม

hardhat.config.js

Apisit N.
19 Sep 2022

มาตั้งค่า hardhat กัน สิ่งที่ใช้กันบ่อยจะมีไม่กี่อย่าง เช่นการตั้งค่า network เพิ่ม chain ที่เราต้องการ Kovan, Ropsten หรือจะเพิ่ม chain ที่เราเปิดขึ้นมาเองโดยใช้ Ganache ก็ได้ ตอนเขียน smart contract ก็ต้องกำหนด version ของ compiler เราจะกำหนดมันที่ไฟล์ hardhat.config.js นี้แหละ

เนื้อหา

  • เนื้อหา
  • Configuration
  • Available config options
  • Networks configuration
  • Hardhat Network
  • JSON-RPC based networks
  • Solidity configuration
  • Path configuration
  • Mocha configuration
  • อ่านเพิ่ม

Configuration

เมื่อ Hardhat ทำงานจะค้นหาไฟล์ hardhat.config.js ที่ Directory ปัจจุบันที่คำสั่งทำงาน ปกติแล้วไฟล์นี้จะอยู่ที่โฟลเดอร์นอกสุดของโปรเจค

ในไฟล์ hardhat.config.js เป็นการตั้งค่าต่างเช่น config, plugin, custom task จะกำหนดอยู่ในไฟล์นี้

Available config options

ในไฟล์ hardhat.config.js จะต้อง export ตัว object

1
module.exports = {}

ตัว object สามารมี properties เช่น defaultNetwork, networks, solidity, paths และ mocha

1
module.exports = {
2
defaultNetwork: "rinkeby",
3
networks: {
4
hardhat: {
5
},
6
rinkeby: {
7
url: "https://eth-rinkeby.alchemyapi.io/v2/123abc123abc123abc123abc123abcde",
8
accounts: [privateKey1, privateKey2, ...]
9
}
10
},
11
solidity: {
12
version: "0.5.15",
13
settings: {
14
optimizer: {
15
enabled: true,
16
runs: 200
17
}
18
}
19
},
20
paths: {
21
sources: "./contracts",
22
tests: "./test",
23
cache: "./cache",
24
artifacts: "./artifacts"
25
},
26
mocha: {
27
timeout: 40000
28
}
29
}

Networks configuration

networks เป็น optional หมายความว่าจะกำหนดหรือไม่กำหนดก็ได้ ใช้เพื่อเป็นตัวกำหนดว่า network เริ่มต้นจะใช้ network ตัวไหน เช่นเมื่อเราสั่ง npx hardhat run สงสัยไหมว่า Hardhat จะ deploy contracts ของเราไปที่ network หรือ chain ไหนเพราะเราไม่ได้บอก Hardhat เลยว่าให้ deploy ไปที่ network ไหน

Hardhat มี network อยู่ 2 ประเภทคือ JSON-RPC based networks, built-in Hardhat Network

สามารถกำหนด default network ได้ที่ field defaultNetwork หากเราไม่กำหนดโดยค่า default จะเป็น hardhat

Hardhat Network

Hardhat มาพร้อมกับ built-in network ชื่อ hardhat เมื่อมีการเรียกใช้คำสั่งบางอย่าง script, test จะสร้าง instance ของ Hardhat Network โดยอัตโนมัติ

Hardhat network เป็นเหมือน node ที่ run Ethereum network ออกแบบมาเพื่อใช้ development

JSON-RPC based networks

เป็นเหมือนตัวที่ใช้เชื่อมต่อ network กับ node ภายนอก ตัว Nodes สามารถเปิดในเครื่องตัวเองได้โดยใช้ Ganache, remotely, Alchemy หรือ Infura

node ในที่นี้ จะหมายถึง คอมพิวเตอร์ที่ทำการ run หรือเปิด Ethereuem software อยู่

network config ต้องกำหนดอะไรได้บ้าง

  • url: คือ url ของ node ที่เราต้องการเชื่อมต่อ เป็น required
  • chainId: คือ ตัวเลขที่ใช้ตรวจสอบ network ที่ Hardhat จะเชื่อมต่อ เป็น optional
  • from: คือ address ของเรา ถ้าไม่กำหนดไว้จะหยิบจาก account แรกของ node
  • gas: ค่านี้ควรเป็น auto ถ้ากำหนดจะใช้เป็นค่า gas limit ของทุก transaction มี default เป็น auto
  • gasPrice: ค่านี้ควรเป็น auto จะเหมือนกับ gas มี default เป็น auto
  • gasMultiplier: คือ ตัวเลข หรือใช้เป็นตัวคูณค่า gas
  • accounts: คือค่าที่ควบคุมว่าบัญชีใดที่ Hardhat ใช้
  • httpHeaders: คือใช้กำหนด headers ของ HTTP เมื่อส่ง request แบบ JSON-RPC
  • timeout: กำหนด Timeout เป็นหน่วย ms สำหรับส่ง request แบบ JSON-RPC ไปยัง server หากใช้เวลานานกว่านี้ มันจะถูกยกเลิก มี default เป็น 40000 สำหรับใน localhost และ 20000 สำหรับอย่างอื่น

Solidity configuration

solidity config เป็น optional กำหนดได้หลายแบบ

  • กำหนดเลข version 0.7.3 Configuring the compiler
  • กำหนดเป็น object
    • version: คือ version ที่ใช้
    • settings: คือ ตั้งค่าเองได้
  • กำหนด object ที่มี compilers หลายตัว
    • compilers: คือ list รายชื่อของ compiler Multiple Solidity versions
    • overrides: กำหนด compiler ให้ไฟล์ที่ต้องการ เช่นถ้าเขียน pragma solidity >=0.5.0 ตัว compiler จะใช้ version ที่สูงกว่า (0.6.7 ตัวอย่าง) แต่เรากำหนดให้ไฟล์ที่ต้องการให้ใช้ compiler version ที่กำหนดได้ด้วย overrides

Path configuration

สามารถปรับเปลี่ยน path ที่ Hardhat ใช้ได้เอง โดยกำหนดที่ paths

  • root: คือ path เริ่มต้นของโปรเจค มี default ตามที่อยู่ hardhat.config.js (~/my-project)
  • sources: เป็น directory ที่เก็บไฟล์ contract จะอยู่ใน root อีกที มี default เป็น ./contracts (~/my-project/contracts)
  • tests: เป็น directory ที่เก็บไฟล์ test จะอยู่ใน root อีกที มี default เป็น ./test (~/my-project/test)
  • cache: คือ path ที่ Harthad ใช้เก็บช้อมูล cache จะอยู่ใน root อีกที มี default เป็น ./cache (~/my-project/cache)
  • artifacts: คือ directory ที่เก็บผลลัพธ์หรือไฟล์ต่างๆที่ได้หลังจากการ compile จะอยู่ใน root อีกที มี default เป็น ./artifacts (~/my-project/artifacts)

Mocha configuration

ใช้กำหนดค่าตอน test

อ่านเพิ่ม

  • https://hardhat.org/hardhat-runner/docs/config
© 2025 Apisit N.