Crane code style

From cyotee/indexedex · View interactive version

Crane code style is an AI agent skill from the GitHub repository cyotee/indexedex. Crane Solidity conventions for naming, imports, storage slots, formatting, Solar compilation, and stack-depth fixes without viaIR.

How to use this skill

Skill file

Crane Code Style Guide

Crane follows strict code conventions for consistency and maintainability. This skill covers section headers, imports, naming, and critical compilation rules.

Section Headers

Use 78-character wide comment blocks for major sections:

/* -------------------------------------------------------------------------- */
/*                             Section Name                                   */
/* -------------------------------------------------------------------------- */

Shorter form for subsections:

/* ------ Feature Name ------ */

Import Organization

Group imports by source, with import aliases:

// External libraries
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {SafeERC20} from "@solady/utils/SafeERC20.sol";

// Crane interfaces
import {IFacet} from "@crane/contracts/interfaces/IFacet.sol";
import {IOperable} from "@crane/contracts/access/operable/interfaces/IOperable.sol";

// Crane contracts
import {OperableRepo} from "@crane/contracts/access/operable/OperableRepo.sol";
import {OperableTarget} from "@crane/contracts/access/operable/OperableTarget.sol";

// Test utilities (in test files)
import {Test} from "forge-std/Test.sol";
import {Vm, VM_ADDRESS} from "forge-std/Vm.sol";

Import Aliases

Defined in foundry.toml and remappings.txt:

| Alias | Path |

|-------|------|

| @crane/ | Crane framework contracts |

| @solady/ | Solady library |

| @openzeppelin/ | OpenZeppelin contracts |

| forge-std/ | Foundry test utilities |

Function Organization

Order functions by visibility:

  1. Constructor
  2. Receive
  3. Fallback
  4. External
  5. Public
  6. Internal
  7. Private

Naming Conventions

| Pattern | Usage | Example |

|---------|-------|---------|

| _layoutStruct() | Storage access | _layoutStruct(), _layoutStruct(bytes32 slot_) |

| _initialize() | Storage setup | _initialize(address owner_) |

| _functionName() | Internal Repo functions | _isOperator(), _setOperator() |

| _onlyXxx() | Guard functions in Repos | _onlyOwner(), _onlyOperator() |

| onlyXxx | Modifiers | onlyOwner, onlyOperator |

| layoutStruct | Storage parameter name | Storage storage layoutStruct |

| param_ | Function parameters | owner_, slot_, name_ |

Parameter Trailing Underscore

All function parameters end with underscore to avoid shadowing:

function transfer(address to_, uint256 amount_) external {
    // to_ and amount_ don't shadow state variables
}

Storage Slot Naming

Use hierarchical dot-notation for storage slot names:

| Category | Pattern | Example |

|----------|---------|---------|

| Crane core | crane.{domain}.{feature} | "crane.access.operable" |

| Protocol integrations | protocols.{category}.{protocol}.{version}.{feature} | "protocols.dexes.balancer.v3.vault.aware" |

| EIP implementations | eip.erc.{number} | "eip.erc.8023" |

bytes32 internal constant STORAGE_SLOT = keccak256(abi.encode("crane.access.operable"));

Compiler: Solar, never viaIR

Compile with Solar. Every forge subcommand that compiles includes --use solar:

forge build --use solar
forge test --use solar
forge script --use solar <script>

IndexedEx agent law § Compiler is the full rule. Crane compiles the same way. Do not omit the flag and do not fall back to solc.

NEVER enable via_ir or viaIR in foundry.toml, and never pass --via-ir. Solar is not solc's Yul via-IR pipeline. Solc via-IR stays forbidden: it is slow, memory-heavy, and is not the stack-too-deep fix.

Requires a Foundry build that accepts --use solar (foundryup --install nightly at or after 2026-11-03 / Forge 1.9.1). Forge 1.9.0 rejects the flag.

Resolving Stack Too Deep Errors

Compile with Solar first. Do not refactor a function only because solc would report stack too deep. Solar's pipeline is what removes that solc failure.

If Solar itself reports stack too deep, then refactor using structs:

// WRONG - too many local variables
function badFunction(
    address tokenA,
    address tokenB,
    uint256 amountA,
    uint256 amountB,
    address recipient,
    uint256 deadline
) external {
    uint256 reserveA = pair.reserveA();
    uint256 reserveB = pair.reserveB();
    uint256 totalSupply = pair.totalSupply();
    // ... stack too deep!
}

// CORRECT - group into structs
struct DepositParams {
    address tokenA;
    address tokenB;
    uint256 amountA;
    uint256 amountB;
    address recipient;
    uint256 deadline;
}

struct PoolState {
    uint256 reserveA;
    uint256 reserveB;
    uint256 totalSupply;
}

function goodFunction(DepositParams memory params) external {
    PoolState memory state = PoolState({
        reserveA: pair.reserveA(),
        reserveB: pair.reserveB(),
        totalSupply: pair.totalSupply()
    });
    // Struct members don't consume stack slots
}

Stack Too Deep Solutions

Use these only after Solar itself reports stack too deep:

  1. Group parameters into structs: Bundle related function parameters
  2. Group intermediate state: Use State or Context structs
  3. Use memory for computation: Struct fields don't consume stack
  4. Extract helper functions: Split complex functions
  5. Use block scoping: Limit variable lifetimes with { ... }

Do not enable via_ir as step 6.

Configuration

Standard Crane configuration:

Additional Resources

Reference Files

Key Files

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.