0Pricing
Web3 & DApp Development Fundamentals · บทเรียน

cast และ anvil

CLI และโหนดภายในเครื่อง

cast และ anvil เป็นบทเรียน Web3 & DApp Development Fundamentals ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Web3 & DApp Development Fundamentals และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Web3 & DApp Development Fundamentals มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Two CLI Workhorses

Beyond forge, Foundry ships cast and anvil. cast is a Swiss-army knife for interacting with chains and contracts from the terminal. anvil is a fast local Ethereum node for development.

Together they let you deploy, call, and inspect contracts without writing any application code.

Starting Anvil

Running anvil spins up a local chain at http://127.0.0.1:8545. It prints ten pre-funded accounts with their private keys, ready for testing.

By default it mines a block for every transaction instantly, so there is no waiting.

$ anvil
Listening on 127.0.0.1:8545
Available Accounts
(0) 0xf39F...2266 (10000 ETH)
Private Keys
(0) 0xac09...ff80

Anvil Options

Anvil is highly configurable:

  • --fork-url — fork a live network's state
  • --block-time — mine on a fixed interval instead of per-transaction
  • --accounts / --balance — control test accounts
  • --chain-id — set the chain id
$ anvil --fork-url https://eth.example.com --block-time 5

Reading Chain Data with Cast

cast queries chain state directly. Common read commands:

  • cast block-number — current block
  • cast balance <addr> — ETH balance
  • cast nonce <addr> — account nonce
$ cast block-number --rpc-url http://127.0.0.1:8545
$ cast balance 0xf39F...2266

Calling View Functions

cast call performs a read-only call to a contract function, returning the result without sending a transaction or spending gas.

$ cast call 0xToken "balanceOf(address)(uint256)" 0xUser \
    --rpc-url http://127.0.0.1:8545

Sending Transactions

cast send signs and broadcasts a state-changing transaction. You supply the contract address, the function signature, arguments, and a private key.

$ cast send 0xToken "transfer(address,uint256)" 0xBob 100 \
    --private-key $PK --rpc-url http://127.0.0.1:8545

Encoding and Decoding

Cast handles ABI and data conversions:

  • cast abi-encode / cast calldata — build calldata
  • cast 4byte — look up a function selector
  • cast --to-dec / --to-hex — number base conversion
  • cast keccak — hash a value
$ cast calldata "transfer(address,uint256)" 0xBob 100
$ cast keccak "Transfer(address,address,uint256)"

Inspecting Transactions

After sending, inspect results:

  • cast tx <hash> — transaction details
  • cast receipt <hash> — receipt including gasUsed and logs
  • cast run <hash> — replay with a full trace

cast run is invaluable for debugging why a transaction reverted.

$ cast receipt 0xabc...123
$ cast run 0xabc...123 --rpc-url http://127.0.0.1:8545

Cheats on Anvil

Anvil exposes special RPC methods for testing, mirroring Foundry cheatcodes:

  • anvil_setBalance — set any account's balance
  • anvil_impersonateAccount — send as any address
  • anvil_mine — mine blocks on demand
  • evm_increaseTime — advance the clock

Call them through cast rpc.

$ cast rpc anvil_setBalance 0xUser 0xDE0B6B3A7640000
$ cast rpc evm_increaseTime 3600

A Local Workflow

A typical local loop:

  • Start anvil in one terminal
  • Deploy with forge script or forge create against it
  • Use cast send and cast call to exercise the contract
  • Debug failures with cast run

All without leaving the command line.

$ forge create src/Counter.sol:Counter \
    --private-key $PK --rpc-url http://127.0.0.1:8545

Forking for Realistic Tests

Combining anvil --fork-url with cast lets you experiment against a snapshot of mainnet locally: impersonate a whale, call a live protocol, and see real behavior without spending real funds.

This is the safest way to prototype interactions with deployed contracts.

Quick Check

Which command performs a read-only call to a contract function without sending a transaction or spending gas?

Recap

You learned the cast and anvil tools:

  • anvil runs a fast local node with pre-funded accounts and forking
  • cast call reads state; cast send writes it
  • Cast encodes/decodes calldata, hashes, and inspects receipts and traces
  • cast run replays transactions for debugging
  • Anvil RPC cheats manipulate balances, time, and mining

This completes the Foundry Toolkit course.

คำถามที่พบบ่อย

บทเรียน “cast และ anvil” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “cast และ anvil” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Web3 & DApp Development Fundamentals ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Web3 & DApp Development Fundamentals มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “cast และ anvil”

CLI และโหนดภายในเครื่อง คุณปฏิบัติ Web3 & DApp Development Fundamentals ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Web3 & DApp Development Fundamentals หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Web3 & DApp Development Fundamentals บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “cast และ anvil” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Web3 & DApp Development Fundamentals นี้ได้ไหม

ได้ บทเรียน Web3 & DApp Development Fundamentals ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. Foundry เทียบกับ Hardhat
  2. การทดสอบด้วย forge
  3. การทำ fuzz และอินวาเรียนต์
  4. cast และ anvil
← กลับไปที่ Web3 & DApp Development Fundamentals