castとanvil
CLIとローカルノード
「castとanvil」はCoddyKit上の無料Web3 & DApp Development Fundamentalsレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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...ff80Anvil 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 5Reading Chain Data with Cast
cast queries chain state directly. Common read commands:
cast block-number— current blockcast balance <addr>— ETH balancecast nonce <addr>— account nonce
$ cast block-number --rpc-url http://127.0.0.1:8545
$ cast balance 0xf39F...2266Calling 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:8545Sending 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:8545Encoding and Decoding
Cast handles ABI and data conversions:
cast abi-encode/cast calldata— build calldatacast 4byte— look up a function selectorcast --to-dec/--to-hex— number base conversioncast 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 detailscast receipt <hash>— receipt including gasUsed and logscast 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:8545Cheats on Anvil
Anvil exposes special RPC methods for testing, mirroring Foundry cheatcodes:
anvil_setBalance— set any account's balanceanvil_impersonateAccount— send as any addressanvil_mine— mine blocks on demandevm_increaseTime— advance the clock
Call them through cast rpc.
$ cast rpc anvil_setBalance 0xUser 0xDE0B6B3A7640000
$ cast rpc evm_increaseTime 3600A Local Workflow
A typical local loop:
- Start
anvilin one terminal - Deploy with
forge scriptorforge createagainst it - Use
cast sendandcast callto 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:8545Forking 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:
anvilruns a fast local node with pre-funded accounts and forkingcast callreads state;cast sendwrites it- Cast encodes/decodes calldata, hashes, and inspects receipts and traces
cast runreplays transactions for debugging- Anvil RPC cheats manipulate balances, time, and mining
This completes the Foundry Toolkit course.
よくある質問
「castとanvil」レッスンは無料ですか?
はい。「castとanvil」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Web3 & DApp Development Fundamentalsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Web3 & DApp Development Fundamentalsコースには全4レッスンが含まれています。
「castとanvil」で何を学びますか?
CLIとローカルノード ブラウザで直接実行するハンズオンコードでWeb3 & DApp Development Fundamentalsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Web3 & DApp Development Fundamentalsを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのWeb3 & DApp Development Fundamentalsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「castとanvil」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このWeb3 & DApp Development Fundamentalsレッスンでコードを書いて実行できますか?
はい。すべてのWeb3 & DApp Development Fundamentalsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- FoundryとHardhatの比較
- forgeによるテスト
- ファジングと不変条件
- castとanvil