evm-swiss-knife

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

EVM Swiss Knife

EVM 瑞士军刀

This skill enables interaction with EVM-compatible blockchains through Foundry's
cast
command-line tool. It covers common blockchain operations like balance queries, contract calls, transaction sending, and network inspection.
本技能支持通过Foundry的
cast
命令行工具与兼容EVM的区块链进行交互,涵盖余额查询、合约调用、交易发送和网络查看等常见区块链操作。

Installation

安装

To use this skill, you need Foundry installed, which provides the
cast
command.
Follow the official installation guide: https://getfoundry.sh/introduction/installation
要使用本技能,你需要安装Foundry,它会提供
cast
命令。

Quick Install (Linux/Mac)

快速安装(Linux/Mac)

  1. Install Foundryup:
    bash
    curl -L https://foundry.paradigm.xyz | bash
  2. Source your shell configuration or start a new terminal:
    bash
    source ~/.bashrc  # or ~/.zshrc
  3. Install Foundry:
    bash
    foundryup
  4. Verify installation:
    bash
    cast --version
For other platforms or detailed instructions, see the full guide at https://getfoundry.sh/introduction/installation
  1. 安装Foundryup:
    bash
    curl -L https://foundry.paradigm.xyz | bash
  2. 加载Shell配置文件或重启终端:
    bash
    source ~/.bashrc  # 或 ~/.zshrc
  3. 安装Foundry:
    bash
    foundryup
  4. 验证安装:
    bash
    cast --version
其他平台或详细说明请查看完整指南:https://getfoundry.sh/introduction/installation

Prerequisites

前置条件

  • Foundry installed (
    cast
    command available)
  • RPC endpoint configured (via
    --rpc-url
    or environment variables)
  • Private key for transaction operations (if sending txs)
  • 已安装Foundry(可使用
    cast
    命令)
  • 已配置RPC端点(通过
    --rpc-url
    或环境变量)
  • 交易操作所需的私钥(若要发送交易)

RPC URL Selection

RPC端点选择

To interact with different EVM networks, you need reliable RPC endpoints. Use ChainList's API at https://chainlist.org/rpcs.json to get a comprehensive JSON list of RPC URLs for all supported chains.
要与不同的EVM网络交互,你需要可靠的RPC端点。可使用ChainList的API:https://chainlist.org/rpcs.json 获取所有支持链的RPC端点完整JSON列表。

How to Select RPC URLs:

如何选择RPC端点:

  1. Fetch the RPC data:
    bash
    curl -s https://chainlist.org/rpcs.json | jq '.[] | select(.chainId == 1)'  # For Ethereum (chain ID 1)
  2. Look for RPC URLs that are:
    • Public and free: No API keys required
    • No or limited tracking: Avoid URLs with "tracking": "yes"
    • Fast and reliable: Prefer URLs with low latency or high score
    • Open source: Prefer community-maintained endpoints
  3. Popular choices (extracted from ChainList):
  4. For production use, consider paid RPC services like Alchemy, Infura, or QuickNode for higher reliability and rate limits.
  5. Always verify the chain ID matches your intended network to avoid connecting to wrong chains.
  1. 获取RPC数据:
    bash
    curl -s https://chainlist.org/rpcs.json | jq '.[] | select(.chainId == 1)'  # 以太坊(链ID 1)
  2. 优先选择以下类型的RPC端点:
    • 公开免费:无需API密钥
    • 无跟踪或低跟踪:避免标记为"tracking": "yes"的URL
    • 快速可靠:优先选择延迟低或评分高的URL
    • 开源:优先选择社区维护的端点
  3. 热门选项(来自ChainList):
  4. 生产环境使用时,可考虑付费RPC服务如Alchemy、Infura或QuickNode,以获得更高的可靠性和速率限制。
  5. 始终验证链ID是否与目标网络匹配,避免连接到错误的链。

Common Operations

常见操作

Query Account Balance

查询账户余额

Get ETH balance of an address:
bash
cast balance <address> --rpc-url <rpc_url>
Get ERC20 token balance:
bash
cast call <token_contract> "balanceOf(address)(uint256)" <address> --rpc-url <rpc_url>
获取地址的ETH余额:
bash
cast balance <address> --rpc-url <rpc_url>
获取ERC20代币余额:
bash
cast call <token_contract> "balanceOf(address)(uint256)" <address> --rpc-url <rpc_url>

Call Contract Functions (Read-Only)

调用合约函数(只读)

Call a view/pure function:
bash
cast call <contract_address> "<function_signature>" [args...] --rpc-url <rpc_url>
Example - get token total supply:
bash
cast call 0xA0b86a33E6441e88C5F2712C3E9b74B6F3f5a8b8 "totalSupply()(uint256)" --rpc-url <rpc_url>
调用view/pure函数:
bash
cast call <contract_address> "<function_signature>" [args...] --rpc-url <rpc_url>
示例 - 获取代币总供应量:
bash
cast call 0xA0b86a33E6441e88C5F2712C3E9b74B6F3f5a8b8 "totalSupply()(uint256)" --rpc-url <rpc_url>

Send Transactions

发送交易

Send ETH:
bash
cast send --private-key <pk> --rpc-url <rpc_url> <to_address> --value <amount_in_wei>
Call contract function (write):
bash
cast send --private-key <pk> --rpc-url <rpc_url> <contract_address> "<function_signature>" [args...]
发送ETH:
bash
cast send --private-key <pk> --rpc-url <rpc_url> <to_address> --value <amount_in_wei>
调用合约函数(写入操作):
bash
cast send --private-key <pk> --rpc-url <rpc_url> <contract_address> "<function_signature>" [args...]

Blockchain Inspection

区块链查看

Get latest block number:
bash
cast block-number --rpc-url <rpc_url>
Get block details:
bash
cast block <block_number> --rpc-url <rpc_url>
Get transaction details:
bash
cast tx <tx_hash> --rpc-url <rpc_url>
获取最新区块号:
bash
cast block-number --rpc-url <rpc_url>
获取区块详情:
bash
cast block <block_number> --rpc-url <rpc_url>
获取交易详情:
bash
cast tx <tx_hash> --rpc-url <rpc_url>

Contract Deployment

合约部署

Deploy a contract:
bash
cast send --private-key <pk> --rpc-url <rpc_url> --create <bytecode> [constructor_args...]
部署合约:
bash
cast send --private-key <pk> --rpc-url <rpc_url> --create <bytecode> [constructor_args...]

Decoding Data

数据解码

Decode hex data:
bash
cast --to-ascii <hex_string>
cast --to-dec <hex_string>
解码十六进制数据:
bash
cast --to-ascii <hex_string>
cast --to-dec <hex_string>

Network-Specific Examples

特定网络示例

Ethereum Mainnet

以太坊主网

bash
export ETH_RPC_URL=https://eth-mainnet.alchemyapi.io/v2/YOUR_API_KEY
cast balance 0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045
bash
export ETH_RPC_URL=https://eth-mainnet.alchemyapi.io/v2/YOUR_API_KEY
cast balance 0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045

Polygon

Polygon

bash
cast balance <address> --rpc-url https://polygon-rpc.com/
bash
cast balance <address> --rpc-url https://polygon-rpc.com/

Local Development (Anvil)

本地开发(Anvil)

bash
cast balance <address> --rpc-url http://127.0.0.1:8545
bash
cast balance <address> --rpc-url http://127.0.0.1:8545

Error Handling

错误处理

  • RPC errors: Check endpoint URL and network connectivity
  • Insufficient funds: Ensure account has enough ETH for gas + value
  • Contract errors: Verify function signatures and parameter types
  • Private key errors: Ensure correct format (0x prefixed hex)
  • RPC错误:检查端点URL和网络连接
  • 余额不足:确保账户有足够的ETH用于燃气费+转账金额
  • 合约错误:验证函数签名和参数类型
  • 私钥错误:确保格式正确(以0x开头的十六进制字符串)

Advanced Usage

进阶用法

Batch Operations

批量操作

Use shell loops for multiple queries:
bash
for addr in addr1 addr2 addr3; do
  echo "Balance of $addr: $(cast balance $addr --rpc-url $RPC_URL)"
done
使用Shell循环进行多查询:
bash
for addr in addr1 addr2 addr3; do
  echo "Balance of $addr: $(cast balance $addr --rpc-url $RPC_URL)"
done

Gas Estimation

燃气费估算

Estimate gas for a transaction:
bash
cast estimate --rpc-url <rpc_url> <to_address> --value <amount>
估算交易的燃气费:
bash
cast estimate --rpc-url <rpc_url> <to_address> --value <amount>

ABI Interactions

ABI交互

For complex contracts, save ABI to file and use:
bash
cast call --abi <abi_file> <contract> <function> [args...]
对于复杂合约,将ABI保存到文件后使用:
bash
cast call --abi <abi_file> <contract> <function> [args...]

Security Notes

安全注意事项

  • Never expose private keys in scripts or logs
  • Use environment variables for sensitive data
  • Test on testnets before mainnet operations
  • Verify contract addresses and function signatures
  • Monitor gas prices for transaction timing
  • 切勿在脚本或日志中暴露私钥
  • 使用环境变量存储敏感数据
  • 在主网操作前先在测试网测试
  • 验证合约地址和函数签名
  • 监控燃气价格以选择合适的交易时机