---
tags: Demi
---
# 4/3 Mainnet forking
## Agenda
1. Mainnet forking
2. Full node vs Full archive node
3. 範例
## Mainnet forking
在本地的開發網路中模擬跟主網相同的狀態
讓開發者能夠在本地端與部屬中的合約做複雜的測試互動
## Full Node vs Archive Node
1. 歸檔節點 Archive nodes - Has data since genesis block.
2. 全節點 Full nodes - Receive copies of transactions. Has the current state of the blockchain.
3. Light nodes- Doesn’t have the entirety of the current blockchain state and depends on a full node, useful for low memory and computational devices.
4. Miner nodes - Miner nodes verify transactions and add them to the blocks. They then mine those blocks and secure the blockchain with proof of work.

Hardware requirements to run a full node with fast sync:
A fast Cpu with 4+ cores.
16 GB+ of RAM.
A fast SSD drive with at least **500GB+** of space.
25 MBit/s+ bandwidth.

Hardware requirements to run a full archive node:
A fast Cpu with 4+ cores.
16 GB+ of RAM.
A fast SSD drive with at least **6TB+** of space.
25 MBit/s+ bandwidth.
### Provider
1. QuickNode https://www.quicknode.com/
2. **Alchemy** https://www.alchemy.com/
3. **Moralis** https://moralis.io/
## Let's start
### 安裝hardhat
https://hardhat.org/getting-started/#installation
https://www.youtube.com/watch?v=S0DFNvJu4TQ
### Environment setup
```
$ mkdir hardhat_fork
$ cd hardhat_fork
```
```
$ yarn init # 沒有想更動的部分,就按 Enter 鍵帶過即可
...
$ yarn add dotenv@16.0.0
$ yarn add hardhat@2.8.4
$ yarn add @nomiclabs/hardhat-waffle@2.0.2
$ yarn add @nomiclabs/hardhat-web3@2.0.0
$ yarn add @nomiclabs/hardhat-ethers@2.0.5
$ yarn add web3@1.7.0
$ yarn add ethers@5.5.4
```
```
$ yarn hardhat
▸ Create an empty hardhat.config.js # 請選擇這個選項,並按 Enter
```
### 調整 config
1. 輸入 Alchemy_key
hardhat_fork/.env
```
Mainnet = "https://eth-mainnet.alchemyapi.io/v2/<your_Alchemy_key>"
```
2.
hardhat_fork/hardhat.config.js
```
require("@nomiclabs/hardhat-waffle");
require("@nomiclabs/hardhat-web3");
require("@nomiclabs/hardhat-ethers");
require('dotenv').config();
task("height", "Print the current block height")
.setAction(async (taskArgs) => {
const block_height = await web3.eth.getBlockNumber();
console.log(`The current block height is ${block_height}`);
});
module.exports = {
networks: {
hardhat: {
forking: {
url: process.env.Mainnet,
blockNumber: 14297759
}
}
}
};
// 高度 14297759 發生在 2022-03-01 00:00:18 (UTC+0),選擇此數字方便後續解說
```
### 使用自定義的 Hardhat task
1. 列出所有可用的 Hardhat task
```
$ yarn hardhat --network "hardhat" help
yarn run v1.22.17
Hardhat version 2.8.4
Usage: hardhat [GLOBAL OPTIONS] <TASK> [TASK OPTIONS]
GLOBAL OPTIONS:
--config A Hardhat config file.
--emoji Use emoji in messages.
--help Shows this message, or a task's help if its name is provided
--max-memory The maximum amount of memory that Hardhat can use.
--network The network to connect to.
--show-stack-traces Show stack traces.
--tsconfig A TypeScript config file.
--verbose Enables Hardhat verbose logging
--version Shows hardhat's version.
AVAILABLE TASKS:
check Check whatever you need
clean Clears the cache and deletes all artifacts
compile Compiles the entire project, building all artifacts
console Opens a hardhat console
flatten Flattens and prints contracts and their dependencies
height Print the current block height
help Prints this message
node Starts a JSON-RPC server on top of Hardhat Network
run Runs a user-defined script after compiling the project
test Runs mocha tests
```
2. 運行 height 指令
```
$ yarn hardhat --network "hardhat" height
yarn run v1.22.17
The current block height is 14297759
```
3. 利用 node 指令,開始運作 Hardhat Network 節點
```
$ yarn hardhat --network "hardhat" node
```
## 範例一:與 WETH9 合約互動
https://gist.github.com/a2468834/6101244f5000e467ec8904ac5f0ec41d
WETH9 合約
https://etherscan.io/address/0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2#code
將合約 ABI 儲存到 hardhat_fork/scripts/contract-abi.json
### 範例二:抓取 public 變數的歷史數據
https://gist.github.com/a2468834/71c59d580c1da21337350cdfc47e515b
## 參考資料來源:
1. Chuan-Chun Wang - Hardhat mainnet forking:主網分叉 (1) https://lundao.tech/blog/hardhat-forking-1/
2. Chuan-Chun Wang Hardhat mainnet forking:主網分叉 (2) https://lundao.tech/blog/hardhat-forking-2
3. Hardhat - Mainnet forking
https://hardhat.org/hardhat-network/guides/mainnet-forking.html
3. Moralis - Connect to BSC Node https://docs.moralis.io/speedy-nodes/connecting-to-rpc-nodes/connect-to-bsc-node
4. QuickNode - Ethereum full node vs archive node https://www.quicknode.com/guides/infrastructure/ethereum-full-node-vs-archive-node
## 清交區塊鏈 DAO
https://discord.gg/Uh9QyZCcuv
Blockchain and DeFi Study Group
built with Demi-Chubbytank, Demi-Rabbit, CCWang and other NTHU/NCTU students
## 無情業配
兔子理財小幫手 https://rabbithelpers.com/home
邀請碼: rbt0000343
https://rabbithelpers.com/createUser?c=rbt0000343