cli-cast
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseFoundry Cast CLI
Foundry Cast CLI
Overview
概述
Expert guidance for Foundry's 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.
castKey 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的 CLI工具专家指南——这是一款从命令行与EVM兼容区块链交互的“瑞士军刀”。使用本技能进行交易签署、向链RPC发送交易、读取链上状态、编码/解码ABI数据以及钱包管理。
cast核心功能:
- 通过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 and reading the environment variable.
references/chains.mdROUTEMESH_API_KEYBefore running any on-chain command, verify that is set:
ROUTEMESH_API_KEYbash
if [[ -z "$ROUTEMESH_API_KEY" ]]; then
echo "Error: ROUTEMESH_API_KEY is not set"
exit 1
fiExample usage with a chain ID:
bash
undefined所有链上命令都需要RPC端点。默认使用RouteMesh作为RPC服务提供商。
URL格式:
https://lb.routeme.sh/{CHAIN_ID}/{ROUTEMESH_API_KEY}构建RPC URL:从中查找链ID,并读取环境变量。
references/chains.mdROUTEMESH_API_KEY在运行任何链上命令之前,请确认已设置:
ROUTEMESH_API_KEYbash
if [[ -z "$ROUTEMESH_API_KEY" ]]; then
echo "Error: ROUTEMESH_API_KEY is not set"
exit 1
fi带链ID的使用示例:
bash
undefinedEthereum Mainnet (chain ID 1)
以太坊主网(链ID 1)
cast call "$CONTRACT" "balanceOf(address)" "$ADDR"
--rpc-url "https://lb.routeme.sh/1/$ROUTEMESH_API_KEY"
--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"
--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"
--rpc-url "https://lb.routeme.sh/42161/$ROUTEMESH_API_KEY"
--private-key "$PRIVATE_KEY"
undefinedcast send "$CONTRACT" "transfer(address,uint256)" "$TO" "$AMOUNT"
--rpc-url "https://lb.routeme.sh/42161/$ROUTEMESH_API_KEY"
--private-key "$PRIVATE_KEY"
--rpc-url "https://lb.routeme.sh/42161/$ROUTEMESH_API_KEY"
--private-key "$PRIVATE_KEY"
undefinedSigning & 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
undefinedbash
undefinedImport 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
--rpc-url "$RPC_URL"
--account my-account
undefinedcast send "$CONTRACT" "transfer(address,uint256)" "$TO" "$AMOUNT"
--rpc-url "$RPC_URL"
--account my-account
--rpc-url "$RPC_URL"
--account my-account
undefinedHardware Wallet
硬件钱包
bash
undefinedbash
undefinedLedger
Ledger
cast send "$CONTRACT" "transfer(address,uint256)" "$TO" "$AMOUNT"
--rpc-url "$RPC_URL"
--ledger
--rpc-url "$RPC_URL"
--ledger
undefinedcast send "$CONTRACT" "transfer(address,uint256)" "$TO" "$AMOUNT"
--rpc-url "$RPC_URL"
--ledger
--rpc-url "$RPC_URL"
--ledger
undefinedCore Commands
核心命令
Send Transactions
发送交易
Use to submit state-changing transactions on-chain.
cast sendbash
undefined使用提交会改变链上状态的交易。
cast sendbash
undefinedSend ETH
发送ETH
cast send "$TO" --value 1ether
--rpc-url "$RPC_URL"
--private-key "$PRIVATE_KEY"
--rpc-url "$RPC_URL"
--private-key "$PRIVATE_KEY"
cast send "$TO" --value 1ether
--rpc-url "$RPC_URL"
--private-key "$PRIVATE_KEY"
--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"
--rpc-url "$RPC_URL"
--private-key "$PRIVATE_KEY"
cast send "$CONTRACT" "approve(address,uint256)" "$SPENDER" "$AMOUNT"
--rpc-url "$RPC_URL"
--private-key "$PRIVATE_KEY"
--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
--rpc-url "$RPC_URL"
--private-key "$PRIVATE_KEY"
--gas-limit 200000
--gas-price 20gwei
undefinedcast send "$CONTRACT" "mint(uint256)" 100
--rpc-url "$RPC_URL"
--private-key "$PRIVATE_KEY"
--gas-limit 200000
--gas-price 20gwei
--rpc-url "$RPC_URL"
--private-key "$PRIVATE_KEY"
--gas-limit 200000
--gas-price 20gwei
undefinedRead Contract State
读取合约状态
Use for read-only calls that do not submit transactions.
cast callbash
undefined使用进行无需提交交易的只读调用。
cast callbash
undefinedRead 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"
undefinedcast call "$CONTRACT" "getReserves()(uint112,uint112,uint32)" --rpc-url "$RPC_URL"
undefinedBuild Raw Transactions
构建原始交易
Use to create a signed raw transaction without broadcasting it.
cast mktxbash
cast mktx "$CONTRACT" "transfer(address,uint256)" "$TO" "$AMOUNT" \
--rpc-url "$RPC_URL" \
--private-key "$PRIVATE_KEY"使用创建已签名的原始交易但不广播。
cast mktxbash
cast mktx "$CONTRACT" "transfer(address,uint256)" "$TO" "$AMOUNT" \
--rpc-url "$RPC_URL" \
--private-key "$PRIVATE_KEY"Inspect Transactions
检查交易
bash
undefinedbash
undefinedView 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"
undefinedcast receipt "$TX_HASH" status --rpc-url "$RPC_URL"
cast receipt "$TX_HASH" gasUsed --rpc-url "$RPC_URL"
undefinedABI Utilities
ABI工具
Encode Calldata
编码Calldata
bash
undefinedbash
undefinedEncode 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"
undefinedcast abi-encode "transfer(address,uint256)" "$TO" "$AMOUNT"
undefinedDecode Calldata
解码Calldata
bash
undefinedbash
undefinedDecode 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"
undefinedcast abi-decode "balanceOf(address)(uint256)" "$DATA"
undefinedFunction Signatures
函数签名
bash
undefinedbash
undefinedGet 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)"
undefinedcast sig-event "Transfer(address,address,uint256)"
undefinedWallet & ENS
钱包与ENS
Wallet Operations
钱包操作
bash
undefinedbash
undefinedGenerate 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"
undefinedcast wallet sign "Hello, world!" --private-key "$PRIVATE_KEY"
undefinedENS Resolution
ENS解析
bash
undefinedbash
undefinedResolve 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"
undefinedcast lookup-address "$ADDR" --rpc-url "$RPC_URL"
undefinedBalance Queries
余额查询
bash
undefinedbash
undefinedGet 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"
undefinedcast balance "$ADDR" --ether --rpc-url "$RPC_URL"
undefinedChain Resolution
链解析
When the user specifies a chain by name, resolve the chain ID using these steps:
- Check first — it contains the 25 most commonly used chains
references/chains.md - If the chain is not listed, web search for the correct chain ID on chainlist.org
- Construct the RPC URL using the resolved chain ID and RouteMesh pattern
当用户按名称指定链时,请按照以下步骤解析链ID:
- 首先查看——其中包含25个最常用的链
references/chains.md - 如果链未列出,在chainlist.org上搜索正确的链ID
- 使用解析后的链ID和RouteMesh格式构建RPC URL
Quick Reference
快速参考
| Operation | Command | Key Flags |
|---|---|---|
| Send tx | | |
| Read state | | |
| View tx | | |
| View receipt | | |
| Build tx | | |
| Encode call | | (function sig + args) |
| Decode call | | (function sig + data) |
| ABI encode | | (function sig + args) |
| ABI decode | | (function sig + data) |
| Function sig | | (function signature string) |
| Balance | | |
| ENS resolve | | |
| New wallet | | — |
| Sign message | | |
| 操作 | 命令 | 关键参数 |
|---|---|---|
| 发送交易 | | |
| 读取状态 | | |
| 查看交易 | | |
| 查看收据 | | |
| 构建交易 | | |
| 编码调用 | | (函数签名 + 参数) |
| 解码调用 | | (函数签名 + 数据) |
| ABI编码 | | (函数签名 + 参数) |
| ABI解码 | | (函数签名 + 数据) |
| 函数签名 | | (函数签名字符串) |
| 余额查询 | | |
| ENS解析 | | |
| 新建钱包 | | — |
| 签署消息 | | |
Additional Resources
额外资源
- Chain Reference — Chain names and IDs for RouteMesh RPC URL construction
- Foundry Book: https://book.getfoundry.sh/reference/cast/
- 链参考 — 用于构建RouteMesh RPC URL的链名称和ID
- Foundry手册: https://book.getfoundry.sh/reference/cast/