Cast Commands
Cast is Foundry's command-line tool for interacting with EVM-compatible blockchains.
When to Use
- Querying contract state (balances, storage, call results)
- Sending transactions
- Calling contract functions
- Converting between data formats
- Debugging transactions
- ENS resolution
- ABI encoding/decoding
Reading Contract State
call - Read-Only Function Calls
# Call a view function
cast call 0xContract "balanceOf(address)(uint256)" 0xAddress
# Call with RPC URL
cast call 0xContract "totalSupply()(uint256)" --rpc-url mainnet
# Call function with multiple return values
cast call 0xContract "getReserves()(uint112,uint112,uint32)"
balance - Get ETH Balance
# Get balance in wei
cast balance 0xAddress --rpc-url mainnet
# Get balance in ether
cast balance 0xAddress --rpc-url mainnet -e
storage - Read Storage Slots
# Read slot 0
cast storage 0xContract 0 --rpc-url mainnet
# Read specific slot
cast storage 0xContract 0x2 --rpc-url mainnet
code - Get Contract Bytecode
# Get deployed bytecode
cast code 0xContract --rpc-url mainnet
Sending Transactions
send - Write Transactions
# Send transaction
cast send 0xContract "transfer(address,uint256)" \
0xRecipient 1000000000000000000 \
--rpc-url mainnet \
--private-key $PRIVATE_KEY
# Send with value (ETH)
cast send 0xContract "deposit()" \
--value 1ether \
--rpc-url mainnet \
--private-key $PRIVATE_KEY
# Send raw ETH transfer
cast send 0xRecipient --value 1ether \
--rpc-url mainnet \
--private-key $PRIVATE_KEY
Gas Configuration
# Specify gas limit
cast send 0xContract "mint()" --gas 500000
# Specify gas price (legacy)
cast send 0xContract "mint()" --gas-price 50gwei
# Specify EIP-1559 fees
cast send 0xContract "mint()" \
--priority-gas-price 2gwei \
--gas-price 100gwei
ABI Encoding/Decoding
abi-encode - Encode Function Arguments
# Encode function call data
cast abi-encode "transfer(address,uint256)" 0xRecipient 1000
# Encode constructor arguments
cast abi-encode "constructor(string,string,uint8)" "Token" "TKN" 18
abi-decode - Decode Data
# Decode function output
cast abi-decode "balanceOf(address)(uint256)" 0x000...0001
# Decode with multiple outputs
cast abi-decode "getReserves()(uint112,uint112,uint32)" 0x...
calldata - Encode Full Calldata
# Encode function selector + arguments
cast calldata "transfer(address,uint256)" 0xRecipient 1000
# Output: 0xa9059cbb...
# Decode calldata
cast calldata-decode "transfer(address,uint256)" 0xa9059cbb...
sig - Get Function Selector
# Get 4-byte selector
cast sig "transfer(address,uint256)"
# Output: 0xa9059cbb
# Get event signature
cast sig-event "Transfer(address,address,uint256)"
Data Conversion
Numbers
# Hex to decimal
cast to-dec 0xff
# Output: 255
# Decimal to hex
cast to-hex 255
# Output: 0xff
# Convert to wei
cast to-wei 1.5 ether
# Output: 1500000000000000000
# Convert from wei
cast from-wei 1500000000000000000
# Output: 1.5
Units
# Parse units
cast to-unit 1000000000000000000 ether
# Output: 1
# Format units
cast to-wei 100 gwei
# Output: 100000000000
Strings and Bytes
# String to bytes32
cast format-bytes32-string "hello"
# Bytes32 to string
cast parse-bytes32-string 0x68656c6c6f...
# ASCII to hex
cast from-utf8 "hello"
# Hex to ASCII
cast to-ascii 0x68656c6c6f
Addresses
# Checksum address
cast to-checksum-address 0xaddress
# Compute CREATE address
cast compute-address 0xDeployer --nonce 5
# Compute CREATE2 address
cast create2 --starts-with 0000 \
--deployer 0xDeployer \
--init-code-hash 0x...
Block & Transaction Info
block - Get Block Data
# Get latest block
cast block latest --rpc-url mainnet
# Get specific block
cast block 18000000 --rpc-url mainnet
# Get block field
cast block latest --field timestamp --rpc-url mainnet
tx - Get Transaction Data
# Get transaction by hash
cast tx 0xTxHash --rpc-url mainnet
# Get specific field
cast tx 0xTxHash --field gasPrice --rpc-url mainnet
receipt - Get Transaction Receipt
# Get receipt
cast receipt 0xTxHash --rpc-url mainnet
# Get logs
cast receipt 0xTxHash --field logs --rpc-url mainnet
logs - Get Event Logs
# Get logs by event signature
cast logs "Transfer(address,address,uint256)" \
--from-block 18000000 \
--to-block latest \
--address 0xTokenContract \
--rpc-url mainnet
Chain Info
# Get chain ID
cast chain-id --rpc-url mainnet
# Get gas price
cast gas-price --rpc-url mainnet
# Get base fee
cast base-fee --rpc-url mainnet
# Get nonce
cast nonce 0xAddress --rpc-url mainnet
ENS
# Resolve ENS name
cast resolve-name vitalik.eth --rpc-url mainnet
# Lookup address
cast lookup-address 0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045 --rpc-url mainnet
Wallet Operations
wallet - Manage Keys
# Generate new wallet
cast wallet new
# Get address from private key
cast wallet address --private-key $PRIVATE_KEY
# Sign message
cast wallet sign "message" --private-key $PRIVATE_KEY
# Verify signature
cast wallet verify --address 0xAddress "message" 0xSignature
Debugging
run - Trace Transaction
# Run transaction trace
cast run 0xTxHash --rpc-url mainnet
# Quick trace
cast run 0xTxHash --quick --rpc-url mainnet
4byte - Decode Function Signature
# Lookup function signature
cast 4byte 0xa9059cbb
# Output: transfer(address,uint256)
# Lookup event signature
cast 4byte-event 0xddf252ad...
Utility Commands
keccak - Hash Data
# Keccak256 hash
cast keccak "hello"
# Hash function signature (for selector)
cast keccak "transfer(address,uint256)" | cut -c1-10
concat-hex - Concatenate Hex
cast concat-hex 0x1234 0x5678
# Output: 0x12345678
access-list - Generate Access List
# Generate access list for transaction
cast access-list 0xContract "swap(uint256)" 1000 \
--rpc-url mainnet
RPC Configuration
Using foundry.toml
[rpc_endpoints]
mainnet = "${MAINNET_RPC_URL}"
sepolia = "${SEPOLIA_RPC_URL}"
# Use named endpoint
cast call 0xContract "totalSupply()(uint256)" --rpc-url mainnet
Environment Variable
export ETH_RPC_URL=https://eth-mainnet.g.alchemy.com/v2/...
cast call 0xContract "totalSupply()(uint256)"
Common Patterns
See patterns.md for more examples.
Check Token Balance
cast call $TOKEN "balanceOf(address)(uint256)" $ADDRESS --rpc-url mainnet | cast to-dec
Approve and Transfer
# Approve
cast send $TOKEN "approve(address,uint256)" $SPENDER $AMOUNT \
--rpc-url mainnet --private-key $PK
# Transfer
cast send $TOKEN "transferFrom(address,address,uint256)" $FROM $TO $AMOUNT \
--rpc-url mainnet --private-key $PK
Interact with Uniswap
# Get reserves
cast call $PAIR "getReserves()(uint112,uint112,uint32)" --rpc-url mainnet
# Get price
cast call $ROUTER "getAmountsOut(uint256,address[])(uint256[])" \
1000000000000000000 "[$WETH,$USDC]" --rpc-url mainnet
