mcpField
E
ethereum-development

ethereum-development

github.com/pluginagentmarketplace开发151 次安装43 周前更新
代码仓库

关于 ethereum-development

ethereum-development 面向 Ethereum 开发者,涵盖 EVM 行为、Gas 优化、交易机制和节点交互。它结合概念说明、Solidity、viem 与 Foundry 示例,以及故障排查命令。

主要功能

  1. 01讲解 EVM 执行与存储
  2. 02介绍 Gas 优化技巧
  3. 03说明交易类型与费用
  4. 04展示 Ethereum RPC 和状态查询
  5. 05包含 Foundry 与 cast 命令

适用场景

  1. 01优化 Solidity 合约 Gas 用量
  2. 02检查存储槽和合约状态
  3. 03估算或排查交易费用
  4. 04追踪耗尽 Gas 或 nonce 错误

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/pluginagentmarketplace/custom-plugin-blockchain --skill ethereum-development

安装后在 Claude Code 中输入/ethereum-development确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

assets/
config.yaml
schema.json
references/
GUIDE.md
PATTERNS.md
scripts/
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.02025-01面向生产环境,采用 viem 并优化 Gas
1.0.02024-12初始版本

评论

支持 Markdown