Photo by Shubham Dhage / Unsplash

วิธีการรัน Ethereum Blockchain บน Local Node

Ethereum Apr 10, 2023

ขั้นตอนการรัน Local Node สำหรับการพัฒนา Smart Contract บน Ethereum วันนี้ขอนำเสนอ 3 วิธีครับ คือ

  1. ใช้ Hardhat Node
  2. ใช้ Ganache
  3. ใช้ anvil (Foundry)

โดย default ถ้าเราไม่ได้ปรับอะไร ตัว local node จะรันที่ port 8545 คือ http://127.0.0.1:8545

Hardhat Node

Hardhat มาพร้อมกับ built-in ตัว Ethereum local Node

# 1. ติดตั้ง hardhat
npm install hardhat

# 2. run local node
npx hardhat node

จะได้ผลลัพธ์เป็น local Node มี account และ private key (ห้ามใช้เงินจริงเด็ดขาด เพราะเป็น public private key ไปแล้ว ใครๆ ก็รู้)

Started HTTP and WebSocket JSON-RPC server at http://127.0.0.1:8545/

Accounts
========

WARNING: These accounts, and their private keys, are publicly known.
Any funds sent to them on Mainnet or any other live network WILL BE LOST.

Account #0: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 (10000 ETH)
Private Key: 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80

Account #1: 0x70997970C51812dc3A010C7d01b50e0d17dc79C8 (10000 ETH)
Private Key: 0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d

Account #2: 0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC (10000 ETH)
Private Key: 0x5de4111afa1a4b94908f83103eb1f1706367c2e68ca870fc3fb9a804cdab365a

Account #3: 0x90F79bf6EB2c4f870365E785982E1f101E93b906 (10000 ETH)
Private Key: 0x7c852118294e51e653712a81e05800f419141751be58f605c371e15141b007a6

...
...

WARNING: These accounts, and their private keys, are publicly known.
Any funds sent to them on Mainnet or any other live network WILL BE LOST.
Hardhat Network | Ethereum development environment for professionals by Nomic Foundation
Hardhat Network

Ganache

ตัว Ganache เป็นหนึ่งใน Truffle Suite และเป็น GUI ที่สามารถรัน Local Node ได้ (นอกจาก Ethereum ก็รัน Filecoin ได้ แต่บทความนี้ขอพูดเฉพาะ Ethereum) รวมถึงมีมากกว่านั้น คือสามารถจัดการ Account, Transaction มีหน้า Explorer

วิธีการติดตั้งง่ายมาก เข้าเว็บ Ganache แล้วก็ทำการ Download รองรับทั้ง Windows, Mac และ Linux

Ganache - Truffle Suite
Quickly fire up a personal Ethereum blockchain which you can use to run tests, execute commands, and inspect state while controlling how the chain operates.

Anvil (Foundry)

วิธีสุดท้าย เราจะใช้ Anvil ในการรัน วิธีการติดตั้ง anvil คือ ติดตั้งผ่าน Foundry

การติดตั้ง Foundry

curl -L https://foundry.paradigm.xyz | bash
ติดตั้งและลองใช้งาน Foundry
วันนี้ได้ลองหัดเล่น Foundry ซึ่งเคยติดตั้งไว้นานมากๆ แล้ว แต่ไม่ได้ใช้งานซักที ฮ่าๆ วันนี้ลองมานั่งอ่าน Foundry Book นั่งเล่นใหม่ พบว่ามันน่าสนใจมากๆ ตัว Foundry ให้คำนิยามตัวเองไว้ คือ Foundry is a blazing fast,

เมื่อติดตั้งเสร็จ เราสามารถใช้คำสั่ง anvil เพื่อรัน Local Node ได้

anvil

จะได้ผลลัพธ์ประมาณนี้



                             _   _
                            (_) | |
      __ _   _ __   __   __  _  | |
     / _` | | '_ \  \ \ / / | | | |
    | (_| | | | | |  \ V /  | | | |
     \__,_| |_| |_|   \_/   |_| |_|

    0.1.0 (f2d0d83 2023-03-25T00:11:19.130243Z)
    https://github.com/foundry-rs/foundry

Available Accounts
==================

(0) "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" (10000 ETH)
(1) "0x70997970C51812dc3A010C7d01b50e0d17dc79C8" (10000 ETH)
(2) "0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC" (10000 ETH)
(3) "0x90F79bf6EB2c4f870365E785982E1f101E93b906" (10000 ETH)
(4) "0x15d34AAf54267DB7D7c367839AAf71A00a2C6A65" (10000 ETH)
(5) "0x9965507D1a55bcC2695C58ba16FB37d819B0A4dc" (10000 ETH)
(6) "0x976EA74026E726554dB657fA54763abd0C3a0aa9" (10000 ETH)
(7) "0x14dC79964da2C08b23698B3D3cc7Ca32193d9955" (10000 ETH)
(8) "0x23618e81E3f5cdF7f54C3d65f7FBc0aBf5B21E8f" (10000 ETH)
(9) "0xa0Ee7A142d267C1f36714E4a8F75612F20a79720" (10000 ETH)

Private Keys
==================

(0) 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80
(1) 0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d
(2) 0x5de4111afa1a4b94908f83103eb1f1706367c2e68ca870fc3fb9a804cdab365a
(3) 0x7c852118294e51e653712a81e05800f419141751be58f605c371e15141b007a6
(4) 0x47e179ec197488593b187f80a00eb0da91f1b9d0b13f8733639f19c30a34926a
(5) 0x8b3a350cf5c34c9194ca85829a2df0ec3153be0318b5e2d3348e872092edffba
(6) 0x92db14e403b83dfe3df233f83dfa3a0d7096f21ca9b0d6d6b8d88b2b4ec1564e
(7) 0x4bbbf85ce3377467afe5d46f804f221813b2bb87f24d81f60f1fcdbf7cbf4356
(8) 0xdbda1821b80551c9d65939329250298aa3472ba22feea921c0cf5d620ea67b97
(9) 0x2a871d0798f97d79848a013d4936a73bf4cc922c825d33c1cf7073dff6d409c6

Wallet
==================
Mnemonic:          test test test test test test test test test test test junk
Derivation path:   m/44'/60'/0'/0/


Base Fee
==================

1000000000

Gas Limit
==================

30000000

Genesis Timestamp
==================

1681059982

Listening on 127.0.0.1:8545

การต่อ RPC

สุดท้าย การต่อ RPC เราก็แค่กำหนด RPC เป็น localhost:8545

Ethers

import { ethers } from 'ethers'

let provider = new ethers.providers.JsonRpcProvider('http://127.0.0.1:8545')

const blockNumber = await provider.getBlockNumber() 

Viem

import { createPublicClient, http } from 'viem'
import { localhost } from 'viem/chains'

const client = createPublicClient({
  chain: localhost,
  transport: http()
})

const blockNumber = await client.getBlockNumber() 

References

Hardhat Network | Ethereum development environment for professionals by Nomic Foundation
Hardhat Network
Ganache - Truffle Suite
Quickly fire up a personal Ethereum blockchain which you can use to run tests, execute commands, and inspect state while controlling how the chain operates.
Foundry Book
A book on all things Foundry

Tags

Chai Phonbopit

เป็น Web Dev ทำงานมา 10 ปีหน่อยๆ ด้วยภาษา JavaScript, Node.js, React, Vue และปัจจุบันกำลังสนใจ Web3, Crypto และ Blockchain เขียนบล็อกที่ https://devahoy.com