An introduction to Foundry and using the cast
function, replacing complex scripts that may otherwise require multiple rpc calls and other functions in between with a single command.
forge
, cast
, anvil
, and chisel
—to streamline everything from project setup to live-chain interactions.
Among these, cast
serves as your “Swiss-army knife” for JSON-RPC calls, letting you execute tedious day-to-day tasks (like sending raw transactions, querying balances, fetching blocks, and looking up chain metadata) with simple, consistent commands instead of verbose curl
scripts.
This guide walks you through installing Foundry, configuring your environment, and using the most common cast
commands to accelerate your development workflow.
foundryup
, the Foundry toolchain installer. This will give you the four main binaries.
cast 0.2.0
).
--rpc-url
flag with every command, you can set defaults in a foundry.toml
file. This file can be placed in your project’s root directory for project-specific settings, or you can create a global configuration at ~/.foundry/foundry.toml
for settings you use across all projects.
Here is an example of a global configuration for connecting to a local Cosmos EVM node:
cast
cast
replaces the need for custom shell scripts and verbose curl
commands by offering first-class RPC commands.
reth/v1.0.0
).
cast
Command ReferenceCommand | Purpose |
---|---|
cast chain | Show the symbolic name of the current chain. |
cast chain-id | Fetch the numeric chain ID. |
cast client | Get the JSON-RPC client version. |
Command | Purpose |
---|---|
cast send | Sign and publish a transaction. |
cast publish | Publish a raw, signed transaction. |
cast receipt | Fetch the receipt for a transaction hash. |
cast tx | Query transaction details (status, logs, etc.). |
cast rpc | Invoke any raw JSON-RPC method. |
Command | Purpose |
---|---|
cast balance | Get an account’s balance (supports ENS and units). |
cast block | Fetch block information by number or hash. |
cast block-number | Get the latest block number. |
cast logs | Query event logs. |
Command | Purpose |
---|---|
cast estimate | Estimate gas for a call or deployment. |
cast find-block | Find a block by its timestamp. |
cast compute-address | Calculate a CREATE2 address. |
cast from-bin | Decode binary data to hex. |
cast from-wei | Convert wei to a more readable unit (e.g., ether). |
cast to-wei | Convert a unit (e.g., ether) to wei. |
cast abi-encode | Encode function arguments. |
cast keccak | Hash data with Keccak-256. |
~/.bashrc
, ~/.zshrc
), you can alias cast
with your default RPC URL
(or multiple on various chains) to save time.