关于 ethereum-development
ethereum-development 面向 Ethereum 开发者,涵盖 EVM 行为、Gas 优化、交易机制和节点交互。它结合概念说明、Solidity、viem 与 Foundry 示例,以及故障排查命令。
主要功能
- 01讲解 EVM 执行与存储
- 02介绍 Gas 优化技巧
- 03说明交易类型与费用
- 04展示 Ethereum RPC 和状态查询
- 05包含 Foundry 与 cast 命令
适用场景
- 01优化 Solidity 合约 Gas 用量
- 02检查存储槽和合约状态
- 03估算或排查交易费用
- 04追踪耗尽 Gas 或 nonce 错误
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ pluginagentmarketplace/ custom-plugin-blockchain --skill ethereum-development安装后在 Claude Code 中输入/ethereum-development确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
assets/config.yamlschema.jsonreferences/GUIDE.mdPATTERNS.mdscripts/validate.py详细介绍
Ethereum 开发技能
精通 Ethereum 开发,包括 EVM 内部机制、Gas 优化、交易机制和客户端交互。
快速入门
# Invoke this skill for Ethereum development
Skill("ethereum-development", topic="gas", network="mainnet")
涵盖主题
1. EVM(Ethereum 虚拟机)
了解执行环境:
- 栈式机器:256 位字,深度为 1024
- 内存:线性字节数组,扩展需要消耗 Gas
- 存储:持久化键值存储,32 字节槽位
- 操作码:成本、效果、Gas 消耗
2. Gas 优化
降低交易成本:
- 存储打包:在一个槽位中容纳多个值
- Calldata 与 Memory:高效选择
- 循环优化:缓存存储读取
- 自定义错误:减少回滚时的 Gas 消耗
3. 交易机制
掌握交易生命周期:
- 类型:Legacy(0)、Access List(1)、EIP-1559(2)
- 费用估算:基础费用、优先费用、最高费用
- Nonce 管理:顺序排列
- 收据:状态、日志、已用 Gas
4. 客户端交互
与 Ethereum 节点交互:
- RPC 方法:eth_、debug_、trace_
- 状态查询:存储槽位、代码、余额
- 事件订阅:筛选日志和主题
代码示例
高 Gas 效率存储
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
contract Optimized {
// Pack into single slot (32 bytes)
struct User {
uint128 balance; // 16 bytes
uint64 lastUpdate; // 8 bytes
uint32 nonce; // 4 bytes
bool active; // 1 byte
// 3 bytes padding
}
mapping(address => User) public users;
}
读取存储槽位
import { createPublicClient, http, keccak256, encodePacked, pad } from 'viem';
import { mainnet } from 'viem/chains';
const client = createPublicClient({ chain: mainnet, transport: http() });
// Read mapping value: balances[address]
async function getBalance(contract: `0x${string}`, user: `0x${string}`) {
const slot = keccak256(encodePacked(['address', 'uint256'], [user, 0n]));
return await client.getStorageAt({ address: contract, slot });
}
EIP-1559 交易
import { createWalletClient, http, parseEther } from 'viem';
const client = createWalletClient({ transport: http() });
const hash = await client.sendTransaction({
to: '0x...',
value: parseEther('0.1'),
type: 'eip1559',
maxFeePerGas: parseGwei('30'),
maxPriorityFeePerGas: parseGwei('2'),
});
Gas 优化速查表
| 技术 | 节省量 | 示例 |
|---|---|---|
| 存储打包 | ~20k/槽位 | 一个槽位中的 uint128 + uint128 |
| Calldata 与 memory | ~3/字节 | 对只读数据使用 calldata |
| 不检查的数学运算 | ~80/次操作 | unchecked { i++; } |
| 自定义错误 | ~200+ | error Unauthorized() |
| 短路求值 | 不定 | 先进行成本较低的检查 |
常见陷阱
| 陷阱 | 问题 | 解决方案 |
|---|---|---|
| 在循环中读取存储 | 读取成本高 | 先缓存到内存中 |
| 字符串存储 | 占用多个槽位 | 尽可能使用 bytes32 |
| 存储零值 | 无法获得全额退款 | 不要依赖 SSTORE 退款 |
故障排除
“交易费用过低”
# Check current gas prices
cast gas-price --rpc-url $RPC
cast basefee --rpc-url $RPC
将 maxFeePerGas 设置为至少当前基础费用的 2 倍。
“Gas 不足”
# Trace transaction to find issue
cast run --trace $TX_HASH --rpc-url $RPC
“Nonce 过低”
# Get current nonce
cast nonce $ADDRESS --rpc-url $RPC
CLI 命令
# Foundry essentials
forge build --sizes # Contract sizes
forge test --gas-report # Gas consumption
forge snapshot # Gas snapshots
cast storage $ADDR $SLOT # Read storage
cast call $ADDR "fn()" # Simulate call
测试模板
contract GasTest is Test {
function test_GasOptimization() public {
uint256 gasBefore = gasleft();
target.optimizedFunction();
uint256 gasUsed = gasBefore - gasleft();
assertLt(gasUsed, 50000, "Too much gas used");
}
}
交叉引用
- 绑定 Agent:
02-ethereum-development - 相关技能:
solidity-development、web3-frontend
版本历史
| 版本 | 日期 | 变更 |
|---|---|---|
| 2.0.0 | 2025-01 | 面向生产环境,采用 viem 并优化 Gas |
| 1.0.0 | 2024-12 | 初始版本 |
评论
支持 Markdown