cli-cast

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Foundry Cast CLI

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
Foundry的
cast
CLI工具专家指南——这是一款从命令行与EVM兼容区块链交互的“瑞士军刀”。使用本技能进行交易签署、向链RPC发送交易、读取链上状态、编码/解码ABI数据以及钱包管理。
核心功能:
  • 通过RPC发送交易和调用合约
  • 签署消息和类型化数据
  • 编码和解码ABI calldata
  • 查询余额、交易收据和区块数据
  • 解析ENS名称和地址
  • 管理密钥库和钱包操作

RPC Configuration

RPC配置

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:
bash
if [[ -z "$ROUTEMESH_API_KEY" ]]; then
  echo "Error: ROUTEMESH_API_KEY is not set"
  exit 1
fi
Example usage with a chain ID:
bash
undefined
所有链上命令都需要RPC端点。默认使用RouteMesh作为RPC服务提供商。
URL格式:
https://lb.routeme.sh/{CHAIN_ID}/{ROUTEMESH_API_KEY}
构建RPC URL:从
references/chains.md
中查找链ID,并读取
ROUTEMESH_API_KEY
环境变量。
在运行任何链上命令之前,请确认
ROUTEMESH_API_KEY
已设置:
bash
if [[ -z "$ROUTEMESH_API_KEY" ]]; then
  echo "Error: ROUTEMESH_API_KEY is not set"
  exit 1
fi
带链ID的使用示例:
bash
undefined

Ethereum Mainnet (chain ID 1)

以太坊主网(链ID 1)

cast call "$CONTRACT" "balanceOf(address)" "$ADDR"
--rpc-url "https://lb.routeme.sh/1/$ROUTEMESH_API_KEY"
cast call "$CONTRACT" "balanceOf(address)" "$ADDR"
--rpc-url "https://lb.routeme.sh/1/$ROUTEMESH_API_KEY"

Arbitrum (chain ID 42161)

Arbitrum(链ID 42161)

cast send "$CONTRACT" "transfer(address,uint256)" "$TO" "$AMOUNT"
--rpc-url "https://lb.routeme.sh/42161/$ROUTEMESH_API_KEY"
--private-key "$PRIVATE_KEY"
undefined
cast send "$CONTRACT" "transfer(address,uint256)" "$TO" "$AMOUNT"
--rpc-url "https://lb.routeme.sh/42161/$ROUTEMESH_API_KEY"
--private-key "$PRIVATE_KEY"
undefined

Signing & Key Management

签署与密钥管理

Cast supports multiple signing methods. Choose based on the security context.
Cast支持多种签署方式,请根据安全场景选择。

Private Key (dev/testing only)

私钥(仅用于开发/测试)

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

Keystore Account (recommended for persistent keys)

密钥库账户(推荐用于持久化密钥)

bash
undefined
bash
undefined

Import a private key into a keystore

将私钥导入密钥库

cast wallet import my-account --interactive
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
undefined
cast send "$CONTRACT" "transfer(address,uint256)" "$TO" "$AMOUNT"
--rpc-url "$RPC_URL"
--account my-account
undefined

Hardware Wallet

硬件钱包

bash
undefined
bash
undefined

Ledger

Ledger

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

Core Commands

核心命令

Send Transactions

发送交易

Use
cast send
to submit state-changing transactions on-chain.
bash
undefined
使用
cast send
提交会改变链上状态的交易。
bash
undefined

Send ETH

发送ETH

cast send "$TO" --value 1ether
--rpc-url "$RPC_URL"
--private-key "$PRIVATE_KEY"
cast send "$TO" --value 1ether
--rpc-url "$RPC_URL"
--private-key "$PRIVATE_KEY"

Call a contract function

调用合约函数

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

With gas parameters

带Gas参数

cast send "$CONTRACT" "mint(uint256)" 100
--rpc-url "$RPC_URL"
--private-key "$PRIVATE_KEY"
--gas-limit 200000
--gas-price 20gwei
undefined
cast send "$CONTRACT" "mint(uint256)" 100
--rpc-url "$RPC_URL"
--private-key "$PRIVATE_KEY"
--gas-limit 200000
--gas-price 20gwei
undefined

Read Contract State

读取合约状态

Use
cast call
for read-only calls that do not submit transactions.
bash
undefined
使用
cast call
进行无需提交交易的只读调用。
bash
undefined

Read a single value

读取单个值

cast call "$CONTRACT" "totalSupply()(uint256)" --rpc-url "$RPC_URL"
cast call "$CONTRACT" "totalSupply()(uint256)" --rpc-url "$RPC_URL"

Read with arguments

带参数读取

cast call "$CONTRACT" "balanceOf(address)(uint256)" "$ADDR" --rpc-url "$RPC_URL"
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"
undefined
cast call "$CONTRACT" "getReserves()(uint112,uint112,uint32)" --rpc-url "$RPC_URL"
undefined

Build Raw Transactions

构建原始交易

Use
cast mktx
to create a signed raw transaction without broadcasting it.
bash
cast mktx "$CONTRACT" "transfer(address,uint256)" "$TO" "$AMOUNT" \
  --rpc-url "$RPC_URL" \
  --private-key "$PRIVATE_KEY"
使用
cast mktx
创建已签名的原始交易但不广播。
bash
cast mktx "$CONTRACT" "transfer(address,uint256)" "$TO" "$AMOUNT" \
  --rpc-url "$RPC_URL" \
  --private-key "$PRIVATE_KEY"

Inspect Transactions

检查交易

bash
undefined
bash
undefined

View transaction details

查看交易详情

cast tx "$TX_HASH" --rpc-url "$RPC_URL"
cast tx "$TX_HASH" --rpc-url "$RPC_URL"

View transaction receipt

查看交易收据

cast receipt "$TX_HASH" --rpc-url "$RPC_URL"
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"
undefined
cast receipt "$TX_HASH" status --rpc-url "$RPC_URL" cast receipt "$TX_HASH" gasUsed --rpc-url "$RPC_URL"
undefined

ABI Utilities

ABI工具

Encode Calldata

编码Calldata

bash
undefined
bash
undefined

Encode a function call

编码函数调用

cast calldata "transfer(address,uint256)" "$TO" "$AMOUNT"
cast calldata "transfer(address,uint256)" "$TO" "$AMOUNT"

ABI-encode arguments (without function selector)

ABI编码参数(不含函数选择器)

cast abi-encode "transfer(address,uint256)" "$TO" "$AMOUNT"
undefined
cast abi-encode "transfer(address,uint256)" "$TO" "$AMOUNT"
undefined

Decode Calldata

解码Calldata

bash
undefined
bash
undefined

Decode calldata with a known signature

使用已知签名解码calldata

cast decode-calldata "transfer(address,uint256)" "$CALLDATA"
cast decode-calldata "transfer(address,uint256)" "$CALLDATA"

Decode ABI-encoded data (without selector)

解码ABI编码数据(不含选择器)

cast abi-decode "balanceOf(address)(uint256)" "$DATA"
undefined
cast abi-decode "balanceOf(address)(uint256)" "$DATA"
undefined

Function Signatures

函数签名

bash
undefined
bash
undefined

Get the 4-byte selector for a function

获取函数的4字节选择器

cast sig "transfer(address,uint256)"
cast sig "transfer(address,uint256)"

Get the event topic hash

获取事件主题哈希

cast sig-event "Transfer(address,address,uint256)"
undefined
cast sig-event "Transfer(address,address,uint256)"
undefined

Wallet & ENS

钱包与ENS

Wallet Operations

钱包操作

bash
undefined
bash
undefined

Generate a new wallet

生成新钱包

cast wallet new
cast wallet new

Get address from private key

从私钥获取地址

cast wallet address --private-key "$PRIVATE_KEY"
cast wallet address --private-key "$PRIVATE_KEY"

List keystore accounts

列出密钥库账户

cast wallet list
cast wallet list

Sign a message

签署消息

cast wallet sign "Hello, world!" --private-key "$PRIVATE_KEY"
undefined
cast wallet sign "Hello, world!" --private-key "$PRIVATE_KEY"
undefined

ENS Resolution

ENS解析

bash
undefined
bash
undefined

Resolve ENS name to address

将ENS名称解析为地址

cast resolve-name "vitalik.eth" --rpc-url "$RPC_URL"
cast resolve-name "vitalik.eth" --rpc-url "$RPC_URL"

Reverse lookup: address to ENS name

反向查询:地址转ENS名称

cast lookup-address "$ADDR" --rpc-url "$RPC_URL"
undefined
cast lookup-address "$ADDR" --rpc-url "$RPC_URL"
undefined

Balance Queries

余额查询

bash
undefined
bash
undefined

Get ETH balance

获取ETH余额

cast balance "$ADDR" --rpc-url "$RPC_URL"
cast balance "$ADDR" --rpc-url "$RPC_URL"

Get balance in ether (human-readable)

以ether为单位获取余额(易读格式)

cast balance "$ADDR" --ether --rpc-url "$RPC_URL"
undefined
cast balance "$ADDR" --ether --rpc-url "$RPC_URL"
undefined

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
当用户按名称指定链时,请按照以下步骤解析链ID:
  1. 首先查看
    references/chains.md
    ——其中包含25个最常用的链
  2. 如果链未列出,在chainlist.org上搜索正确的链ID
  3. 使用解析后的链ID和RouteMesh格式构建RPC URL

Quick Reference

快速参考

OperationCommandKey Flags
Send tx
cast send
--rpc-url
,
--private-key
,
--value
Read state
cast call
--rpc-url
,
--block
View tx
cast tx
--rpc-url
,
--json
View receipt
cast receipt
--rpc-url
,
--json
Build tx
cast mktx
--rpc-url
,
--private-key
Encode call
cast calldata
(function sig + args)
Decode call
cast decode-calldata
(function sig + data)
ABI encode
cast abi-encode
(function sig + args)
ABI decode
cast abi-decode
(function sig + data)
Function sig
cast sig
(function signature string)
Balance
cast balance
--rpc-url
,
--ether
ENS resolve
cast resolve-name
--rpc-url
New wallet
cast wallet new
Sign message
cast wallet sign
--private-key
,
--account
操作命令关键参数
发送交易
cast send
--rpc-url
,
--private-key
,
--value
读取状态
cast call
--rpc-url
,
--block
查看交易
cast tx
--rpc-url
,
--json
查看收据
cast receipt
--rpc-url
,
--json
构建交易
cast mktx
--rpc-url
,
--private-key
编码调用
cast calldata
(函数签名 + 参数)
解码调用
cast decode-calldata
(函数签名 + 数据)
ABI编码
cast abi-encode
(函数签名 + 参数)
ABI解码
cast abi-decode
(函数签名 + 数据)
函数签名
cast sig
(函数签名字符串)
余额查询
cast balance
--rpc-url
,
--ether
ENS解析
cast resolve-name
--rpc-url
新建钱包
cast wallet new
签署消息
cast wallet sign
--private-key
,
--account

Additional Resources

额外资源