> ## Documentation Index
> Fetch the complete documentation index at: https://docs.geode.ag/llms.txt
> Use this file to discover all available pages before exploring further.

# GeodeHook.sol

> 核心 Uniswap v4 Hook 合约——批量结算、直接曲线交换和费用管理。

## 概述

`GeodeHook.sol` 是整个 Geode 协议的单一合约（约 1,190 行）。它实现了 Uniswap v4 `IHooks` 接口和 `IUnlockCallback`，通过 `beforeSwap` 拦截交换，并通过 `geodeSettleBatch()` 提供批量结算。

**Hook 标志**：`BEFORE_SWAP | BEFORE_SWAP_RETURNS_DELTA`

## 不可变量

```solidity theme={null}
IPoolManager public immutable poolManager;    // Uniswap v4 PoolManager
ISignatureTransfer public immutable permit2;  // 规范的 Permit2 合约
address public immutable protocolTreasury;    // 接收约 1/3 的直接交换费用
address public immutable factory;             // GeodeFactory — 被授权注册发行
```

## 常量

```solidity theme={null}
uint256 public constant CURVE_RESERVE_FEE_SHARE_BPS = 3334; // 约 1/3 费用 → 曲线 ETH 储备
uint256 public constant PROTOCOL_FEE_SHARE_BPS = 3333;      // 约 1/3 费用 → 金库
// 余额（约 1/3）→ 盈余池
```

## 状态变量

```solidity theme={null}
mapping(PoolId => PoolConfig) internal _poolConfigs;      // 每池配置
mapping(PoolId => bool) public poolInitialized;           // 配置标志
mapping(PoolId => uint256) public lastSettledBlock;       // 批次间隔强制
mapping(PoolId => BatchState) internal _currentBatch;     // 当前批次追踪
mapping(PoolId => uint256) public surplusCurrency0;       // 累积盈余 (token0)
mapping(PoolId => uint256) public surplusCurrency1;       // 累积盈余 (token1)
mapping(PoolId => LaunchState) internal _launchStates;    // 永久曲线状态
mapping(PoolId => uint256) public launchTokenReserve;     // 曲线分发的代币储备
mapping(PoolId => uint256) public launchEthReserve;       // 来自曲线销售的 ETH 储备
```

## 外部入口

### `geodeSettleBatch()`

主结算函数。无许可——任何人都可以调用并赚取奖励。

```solidity theme={null}
function geodeSettleBatch(
    PoolKey calldata key,
    GeodeIntent[] calldata buys,
    GeodeIntent[] calldata sells,
    bytes[] calldata buySignatures,
    bytes[] calldata sellSignatures
) external
```

### `beforeSwap()`

Hook 回调，拦截配置池上的每次交换。

```solidity theme={null}
function beforeSwap(
    address sender,
    PoolKey calldata key,
    SwapParams calldata params,
    bytes calldata
) external onlyPoolManager returns (bytes4, BeforeSwapDelta, uint24)
```

**行为**：

* `sender == address(this)`（Hook 路由剩余）：**无费用**，直通
* 活跃曲线池：**`_curveDirectSwap()`** — Hook 计算联合曲线输出，收取 0.3% 三方分配费用，返回完整 delta
* 标准池：收取 `directSwapFeeBps`，约 1/3 → 金库，约 2/3 → 盈余

### `geodeInitializePool()`

一次性池配置。曲线池（设置了 deployer）需要工厂作为调用者。

### `registerLaunch()`

由工厂调用以注册永久曲线状态。

## 视图函数

```solidity theme={null}
function getBatchState(PoolId) external view returns (BatchState memory);
function getPoolConfig(PoolId) external view returns (PoolConfig memory);
function getSurplusCurrency0(PoolId) external view returns (uint256);
function getSurplusCurrency1(PoolId) external view returns (uint256);
function canSettle(PoolId) external view returns (bool);
function getLaunchState(PoolId) external view returns (LaunchState memory);
function getLaunchMetrics(PoolId) external view returns (
    uint256 currentPriceQ96, uint256 marketCap, uint256 fdv,
    uint256 ethRaised, uint256 circulatingSupply
);
```

## 事件

| 事件                    | 触发时机          |
| --------------------- | ------------- |
| `GeodeBatchSettled`   | 批量结算完成        |
| `GeodeIntentFilled`   | 每个已成交的意图      |
| `SettlerPaid`         | 结算者收到付款       |
| `GeodeCurveSwap`      | 直接联合曲线交换执行    |
| `GeodeDirectSwap`     | 直接交换费用收取（标准池） |
| `GeodePoolConfigured` | 池配置为批量处理      |
| `GeodeLaunched`       | 发行代币注册        |

## 源代码

<Card title="GeodeHook.sol" icon="github" href="https://github.com/Geode-vAMM/geodex/blob/main/contracts/src/GeodeHook.sol">
  在 GitHub 上查看完整源代码（约 1,190 行）。
</Card>
