Get the FREE Ultimate OpenClaw Setup Guide →

cli-cast

Scanned
npx machina-cli add skill PaulRBerg/agent-skills/cli-cast --openclaw
Files (1)
SKILL.md
7.6 KB

Foundry Cast CLI

Overview

Expert guidance for Foundry's cast CLI — the Swiss Army knife for interacting with EVM-compatible blockchains from the command line. Use this skill for signing transactions, sending them to chain RPCs, reading on-chain state, encoding/decoding ABI data, and managing wallets.

Key capabilities:

  • Send transactions and call contracts via RPC
  • Sign messages and typed data
  • Encode and decode ABI calldata
  • Query balances, transaction receipts, and block data
  • Resolve ENS names and addresses
  • Manage keystores and wallet operations

RPC Configuration

All on-chain commands require an RPC endpoint. Use RouteMesh as the default RPC provider.

URL pattern:

https://lb.routeme.sh/{CHAIN_ID}/{ROUTEMESH_API_KEY}

Construct the RPC URL by looking up the chain ID from references/chains.md and reading the ROUTEMESH_API_KEY environment variable.

Before running any on-chain command, verify that ROUTEMESH_API_KEY is set:

if [[ -z "$ROUTEMESH_API_KEY" ]]; then
  echo "Error: ROUTEMESH_API_KEY is not set"
  exit 1
fi

Example usage with a chain ID:

# Ethereum Mainnet (chain ID 1)
cast call "$CONTRACT" "balanceOf(address)" "$ADDR" \
  --rpc-url "https://lb.routeme.sh/1/$ROUTEMESH_API_KEY"

# Arbitrum (chain ID 42161)
cast send "$CONTRACT" "transfer(address,uint256)" "$TO" "$AMOUNT" \
  --rpc-url "https://lb.routeme.sh/42161/$ROUTEMESH_API_KEY" \
  --private-key "$ETH_PRIVATE_KEY"

Signing & Key Management

Cast supports multiple signing methods. Choose based on the security context.

Default private key: Read ETH_PRIVATE_KEY from the environment. If the variable is unset and the task requires signing (e.g., cast send, cast mktx, cast wallet sign), stop and inform the user that no private key was found, then ask them to either:

  1. Export ETH_PRIVATE_KEY in their shell, or
  2. Provide a private key or keystore account for this session

Private Key (dev/testing only)

cast send "$CONTRACT" "approve(address,uint256)" "$SPENDER" "$AMOUNT" \
  --rpc-url "$RPC_URL" \
  --private-key "$ETH_PRIVATE_KEY"

Keystore Account (recommended for persistent keys)

# Import a private key into a keystore
cast wallet import my-account --interactive

# Use the keystore account
cast send "$CONTRACT" "transfer(address,uint256)" "$TO" "$AMOUNT" \
  --rpc-url "$RPC_URL" \
  --account my-account

Hardware Wallet

# Ledger
cast send "$CONTRACT" "transfer(address,uint256)" "$TO" "$AMOUNT" \
  --rpc-url "$RPC_URL" \
  --ledger

Core Commands

Send Transactions

Use cast send to submit state-changing transactions on-chain.

# Send ETH
cast send "$TO" --value 1ether \
  --rpc-url "$RPC_URL" \
  --private-key "$ETH_PRIVATE_KEY"

# Call a contract function
cast send "$CONTRACT" "approve(address,uint256)" "$SPENDER" "$AMOUNT" \
  --rpc-url "$RPC_URL" \
  --private-key "$ETH_PRIVATE_KEY"

# With gas parameters
cast send "$CONTRACT" "mint(uint256)" 100 \
  --rpc-url "$RPC_URL" \
  --private-key "$ETH_PRIVATE_KEY" \
  --gas-limit 200000 \
  --gas-price 20gwei

Read Contract State

Use cast call for read-only calls that do not submit transactions.

# Read a single value
cast call "$CONTRACT" "totalSupply()(uint256)" --rpc-url "$RPC_URL"

# Read with arguments
cast call "$CONTRACT" "balanceOf(address)(uint256)" "$ADDR" --rpc-url "$RPC_URL"

# Read multiple return values
cast call "$CONTRACT" "getReserves()(uint112,uint112,uint32)" --rpc-url "$RPC_URL"

Build Raw Transactions

Use cast mktx to create a signed raw transaction without broadcasting it.

cast mktx "$CONTRACT" "transfer(address,uint256)" "$TO" "$AMOUNT" \
  --rpc-url "$RPC_URL" \
  --private-key "$ETH_PRIVATE_KEY"

Inspect Transactions

# View transaction details
cast tx "$TX_HASH" --rpc-url "$RPC_URL"

# View transaction receipt
cast receipt "$TX_HASH" --rpc-url "$RPC_URL"

# Get specific receipt fields
cast receipt "$TX_HASH" status --rpc-url "$RPC_URL"
cast receipt "$TX_HASH" gasUsed --rpc-url "$RPC_URL"

ABI Utilities

Encode Calldata

# Encode a function call
cast calldata "transfer(address,uint256)" "$TO" "$AMOUNT"

# ABI-encode arguments (without function selector)
cast abi-encode "transfer(address,uint256)" "$TO" "$AMOUNT"

Decode Calldata

# Decode calldata with a known signature
cast decode-calldata "transfer(address,uint256)" "$CALLDATA"

# Decode ABI-encoded data (without selector)
cast abi-decode "balanceOf(address)(uint256)" "$DATA"

Function Signatures

# Get the 4-byte selector for a function
cast sig "transfer(address,uint256)"

# Get the event topic hash
cast sig-event "Transfer(address,address,uint256)"

Wallet & ENS

Wallet Operations

# Generate a new wallet
cast wallet new

# Get address from private key
cast wallet address --private-key "$ETH_PRIVATE_KEY"

# List keystore accounts
cast wallet list

# Sign a message
cast wallet sign "Hello, world!" --private-key "$ETH_PRIVATE_KEY"

ENS Resolution

# Resolve ENS name to address
cast resolve-name "vitalik.eth" --rpc-url "$RPC_URL"

# Reverse lookup: address to ENS name
cast lookup-address "$ADDR" --rpc-url "$RPC_URL"

Balance Queries

# Get ETH balance
cast balance "$ADDR" --rpc-url "$RPC_URL"

# Get balance in ether (human-readable)
cast balance "$ADDR" --ether --rpc-url "$RPC_URL"

Chain Resolution

When the user specifies a chain by name, resolve the chain ID using these steps:

  1. Check references/chains.md first — it contains the 25 most commonly used chains
  2. If the chain is not listed, web search for the correct chain ID on chainlist.org
  3. Construct the RPC URL using the resolved chain ID and RouteMesh pattern

Quick Reference

OperationCommandKey Flags
Send txcast send--rpc-url, --private-key, --value
Read statecast call--rpc-url, --block
View txcast tx--rpc-url, --json
View receiptcast receipt--rpc-url, --json
Build txcast mktx--rpc-url, --private-key
Encode callcast calldata(function sig + args)
Decode callcast decode-calldata(function sig + data)
ABI encodecast abi-encode(function sig + args)
ABI decodecast abi-decode(function sig + data)
Function sigcast sig(function signature string)
Balancecast balance--rpc-url, --ether
ENS resolvecast resolve-name--rpc-url
New walletcast wallet new
Sign messagecast wallet sign--private-key, --account

Additional Resources

Source

git clone https://github.com/PaulRBerg/agent-skills/blob/main/skills/cli-cast/SKILL.mdView on GitHub

Overview

This skill provides expert guidance for Foundry's cast CLI, enabling you to send transactions, read on-chain state, encode/decode ABI data, sign messages, and manage wallets. It emphasizes using RPC endpoints (like ROUTEMESH), secure signing methods, and common workflows to interact with EVM-compatible chains.

How This Skill Works

Cast commands execute against a configured RPC URL. You select a signing method—default private key from ETH_PRIVATE_KEY, a keystore account, or a hardware wallet—and then use commands such as cast send for state-changing transactions and cast call for read-only queries. Additional capabilities include encoding/decoding ABI data, resolving ENS names, and managing keystores or wallets for signing.

When to Use It

  • You need to send a transaction or call a contract function via an RPC endpoint.
  • You want to read contract state without submitting a transaction.
  • You need to encode or decode ABI calldata for function calls or receipts.
  • You must sign messages or typed data for verification or authentication.
  • You want to manage signing keys with a private key, keystore, or hardware wallet.

Quick Start

  1. Step 1: Ensure an RPC endpoint is available and ROUTEMESH_API_KEY is set in your environment.
  2. Step 2: Choose a signing method (private key, keystore, or hardware) and prepare credentials.
  3. Step 3: Run a basic command, e.g., cast call or cast send, with --rpc-url and appropriate arguments.

Best Practices

  • Set ROUTEMESH_API_KEY and the correct RPC URL before executing any on-chain commands.
  • Prefer keystore or hardware-wallet signing over exposing private keys in environment variables.
  • Double-check contract addresses, function signatures, and gas parameters before sending a transaction.
  • Test with small amounts or on a testnet before large transfers; verify receipts and logs.
  • Monitor gas estimates and adjust gas limits/prices to avoid failed or stuck transactions.

Example Use Cases

  • Read totalSupply from a contract using cast call to verify token metrics.
  • Check an account's balance with cast call balanceOf(address) to confirm funds.
  • Send ETH or tokens with cast send and a private key to perform a transfer.
  • Call a contract function like approve or mint with cast send and proper arguments.
  • Import a keystore and perform a transfer via a keystore account for persistent keys.

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers