Token导航 LogoToken导航TokenDH.com
运维和基础设施只读github未标认证来源可访问clear审计通过

solidity-gas-optimization固体气体优化

Agent Skill

solidity-gas-optimization 用于处理 GitHub 仓库、Issue、Pull Request 和代码协作信息,适合在 Codex、Claude、Cursor、Gemini CLI 中需要围绕仓库状态、代码变更或协作事项进行整理时使用。可结合来源仓库、安装命令和原始 README 继续核验具体用法。安装前建议确认权限范围、维护状态,以及是否会触发联网、命令执行或文件读写。

总安装

2,521

周安装

103

GitHub Stars

4

下载量

808
CodexClaudeCursorGemini CLI

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

GitHub

来源数

3

许可证

MIT

最后核验

2026-05-01

来源状态

来源可访问

安装方式

通过对话安装

复制提示词发给支持本地命令或 Skills 的 AI 助手,先确认命令和权限,再让它执行。

请帮我安装这个 Agent Skill:solidity-gas-optimization(固体气体优化)
来源仓库:https://github.com/pseudoyu/agent-skills
仓库路径:skills/solidity-gas-optimization
安装命令:
npx skills add https://github.com/pseudoyu/agent-skills --skill solidity-gas-optimization
安装前请先检查当前环境是否支持对应 CLI,并向我确认将要执行的命令、安装目录、联网范围和文件读写权限;确认后再执行。

命令行安装

复制命令到本机终端执行。不同来源提供的安装方式可能略有差异;本站展示可直接复制的安装命令,安装前请核对来源页面。

skills.shnpx skills
npx skills add https://github.com/pseudoyu/agent-skills --skill solidity-gas-optimization

简介

solidity-gas-optimization 用于处理 GitHub 仓库、Issue、Pull Request 和代码协作信息。

  • 适用于围绕仓库状态、代码变更或协作事项进行整理和分析的场景。
  • 通过 npx skills add 命令从指定 GitHub 仓库安装,建议结合原始 README 验证功能细节。
  • 安装前应确认权限范围和维护状态,避免触发不必要的联网或文件操作。
  • 适用宿主包括 Codex、Claude、Cursor、Gemini CLI,接入前应确认版本、权限和运行环境要求。

SKILL.md

Solidity Gas Optimization

Overview

Comprehensive gas optimization guide for Solidity smart contracts, containing 80+ techniques across 8 categories. Based on the RareSkills Book of Gas Optimization. Rules are prioritized by impact and safety.

When to Apply

Reference these guidelines when:

  • Writing new Solidity smart contracts
  • Reviewing or auditing existing contracts
  • Optimizing gas costs for deployment or execution
  • Refactoring contract storage layouts
  • Implementing cross-contract interactions
  • Choosing between design patterns (ERC721 vs ERC1155, etc.)

Priority-Ordered Categories

PriorityCategoryImpactRisk
1Storage OptimizationCRITICALLOW
2Deployment OptimizationHIGHLOW
3Calldata OptimizationHIGHLOW
4Design PatternsHIGHMEDIUM
5Cross-Contract CallsMEDIUM-HIGHMEDIUM
6Compiler OptimizationsMEDIUMLOW
7Assembly TricksMEDIUMHIGH
8Dangerous TechniquesLOWCRITICAL

Quick Reference

Critical: Storage Optimization (Apply First)

Zero-to-One Writes:

  • Avoid zero-to-one storage writes (costs 22,100 gas)
  • Use 1/2 instead of 0/1 for boolean-like values
  • Keep minimum balances in ERC20 contracts

Variable Packing:

// Bad: 3 slots
struct Unpacked {
    uint64 time;      // slot 1
    uint256 amount;   // slot 2
    address user;     // slot 3
}

// Good: 2 slots
struct Packed {
    uint64 time;      // slot 1 (with address)
    address user;     // slot 1
    uint256 amount;   // slot 2
}

Caching:

// Bad: reads storage twice
function increment() public {
    require(count < 10);
    count = count + 1;
}

// Good: reads storage once
function increment() public {
    uint256 _count = count;
    require(_count < 10);
    count = _count + 1;
}

Constants & Immutables:

uint256 constant MAX = 100;        // No storage slot
address immutable owner;           // Set in constructor, no storage

High: Deployment Optimization

Custom Errors:

// Bad: ~64+ bytes
require(amount <= limit, "Amount exceeds limit");

// Good: ~4 bytes
error ExceedsLimit();
if (amount > limit) revert ExceedsLimit();

Payable Constructors:

// Saves ~200 gas on deployment
constructor() payable {}

Clone Patterns:

  • Use EIP-1167 minimal proxies for repeated deployments
  • Use UUPS over Transparent Proxy for upgradeable contracts

High: Calldata Optimization

Calldata vs Memory:

// Bad: copies to memory
function process(bytes memory data) external {}

// Good: reads directly from calldata
function process(bytes calldata data) external {}

Avoid Signed Integers:

  • Small negative numbers are expensive (e.g., -1 = 0xffff...)
  • Use unsigned integers in function parameters

High: Design Patterns

Token Standards:

  • Prefer ERC1155 over ERC721 for NFTs (no balanceOf overhead)
  • Consider consolidating multiple ERC20s into one ERC1155

Signature vs Merkle:

  • Prefer ECDSA signatures over Merkle trees for allowlists
  • Implement ERC20Permit for approve + transfer in one tx

Alternative Libraries:

  • Consider Solmate/Solady over OpenZeppelin for gas efficiency

Medium-High: Cross-Contract Calls

Reduce Interactions:

  • Use ERC1363 transferAndCall instead of approve + transferFrom
  • Implement multicall for batching operations
  • Cache external call results (e.g., Chainlink oracles)

Access Lists:

  • Use ERC2930 access list transactions to pre-warm storage

Medium: Compiler Optimizations

Loop Patterns:

// Good: unchecked increment, cached length
uint256 len = arr.length;
for (uint256 i; i < len; ) {
    // logic
    unchecked { ++i; }
}

Named Returns:

// More efficient bytecode
function calc(uint256 x) pure returns (uint256 result) {
    result = x * 2;
}

Bitshifting:

// Cheaper: 3 gas
x << 1   // x * 2
x >> 2   // x / 4

// Expensive: 5 gas
x * 2
x / 4

Short-Circuit Booleans:

  • Place likely-to-fail conditions first in &&
  • Place likely-to-succeed conditions first in ||

Medium: Assembly (Use Carefully)

Efficient Checks:

// Check address(0) with assembly
assembly {
    if iszero(caller()) { revert(0, 0) }
}

// Even/odd check
x & 1  // instead of x % 2

Memory Reuse:

  • Reuse scratch space (0x00-0x40) for small operations
  • Avoid memory expansion in loops

Avoid: Dangerous Techniques

These are unsafe for production:

  • Making all functions payable
  • Ignoring send() return values
  • Using gasleft() for branching
  • Manipulating block.number in tests

Outdated Patterns

These no longer apply in modern Solidity:

  • "external is cheaper than public" - No longer true
  • "!= 0 is cheaper than > 0" - Changed around 0.8.12

References

Full documentation with code examples:

  • references/solidity-gas-guidelines.md - Complete guide
  • references/rules/ - Individual patterns by category

To look up specific patterns:

grep -l "storage" references/rules/
grep -l "assembly" references/rules/
grep -l "struct" references/rules/

Rule Categories in references/rules/

  • storage-* - Storage optimization patterns
  • deploy-* - Deployment gas savings
  • calldata-* - Calldata optimization
  • design-* - Design pattern choices
  • crosscall-* - Cross-contract call optimization
  • compiler-* - Compiler optimization patterns
  • assembly-* - Low-level assembly tricks

Key Principles

  1. Always Benchmark - Compiler behavior varies by context and version
  2. Balance Readability - Not all optimizations are worth code complexity
  3. Test Both Approaches - Counterintuitive optimizations sometimes increase costs
  4. Consider --via-ir - Modern compiler option may obsolete some tricks
  5. Use Alternative Libraries - Solmate/Solady often beat OpenZeppelin on gas

适合场景

01

用户想查找某类 Agent Skill 时

02

需要根据任务场景推荐可安装能力包时

03

需要对比不同来源的安装命令和来源信息时

04

需要参考平台分布和安装热度时

能力概览

能力 1

按任务关键词查找相关 Skills

能力 2

展示可复制的安装命令

能力 3

保留来源站点、仓库和原始说明,方便继续核验

能力 4

补充不同宿主或平台的使用分布数据

能力 5

展示第三方安全扫描或审计结果

安装后应在对应宿主中按原始 README 的触发条件使用;具体调用方式请以来源页面和 README 为准。

平台分布

Claude Code

31.84%
按下载量换算257

Codex

22.12%
按下载量换算179

OpenCode

19.79%
按下载量换算160

Gemini CLI

12.47%
按下载量换算101

Antigravity

7.32%
按下载量换算59

Cursor

3.19%
按下载量换算26

安全审计

Gen Agent Trust Hub

通过

Socket

通过

Snyk

通过

权限和风险

只读

该 Skill 主要提供规则、说明或参考内容,本身偏只读;真正读写文件、联网或执行命令仍取决于宿主 Agent 的任务。

安装前确认

本站仅展示第三方公开信息,不托管安装包,不提供自动安装或运行环境。安装前应自行审查源码、依赖和命令行为。

来源信息

继续浏览同类 Skills