> ## 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.

# 索引器

> Geode 的持久数据源——索引链上事件并提供代币、池和结算数据。

## 什么是索引器？

Geode 索引器是一个轻量级服务，扫描以太坊上的 Geode 协议事件并存储在持久数据库中。它是以下数据的**唯一数据源**：

* **已发行代币** — 每个通过 [Geocurve](/cn/geocurve) 部署的代币
* **批量结算** — 出清价格、匹配量和结算者费用

<Info>
  索引器替代了结算者的易失性内存代币注册表。代币在重启后持久保存，[结算者机器人](/cn/settler)每 30 秒自动从索引器同步池列表。
</Info>

## 架构

```
┌─────────────┐                       ┌──────────────────────┐
│  前端        │ ◄── GET /tokens ────► │                      │
│  (发射台)    │ ──► POST /tokens ───► │    Geode 索引器       │
└─────────────┘                       │    (Express + SQLite)  │
                                      │                      │
┌─────────────┐   每 5 秒轮询         │  ┌────────────────┐  │
│  以太坊      │ ◄────────────────────►│  │ SQLite (WAL)   │  │
│  (RPC)      │   TokenLaunched       │  │ • tokens       │  │
└─────────────┘   BatchSettled        │  │ • settlements  │  │
                                      │  │ • indexer_state │  │
┌─────────────┐                       │  └────────────────┘  │
│  结算者      │ ◄── GET /tokens ────► │                      │
│  机器人      │   (每 30 秒同步)       └──────────────────────┘
└─────────────┘
```

## 托管实例

Geode 运行一个公共索引器：

```
https://indexer.geode.ag
```

## 自托管

<Steps>
  <Step title="克隆并安装">
    ```bash theme={null}
    git clone https://github.com/Geode-vAMM/geodex.git
    cd geodex/indexer
    npm install
    ```
  </Step>

  <Step title="配置">
    ```bash theme={null}
    cp .env.example .env
    ```

    至少设置：

    * `FACTORY_ADDRESS` — GeodeFactory 合约地址
    * `HOOK_ADDRESS` — GeodeHook 合约地址
    * `RPC_URL` — 您的以太坊 JSON-RPC 端点
  </Step>

  <Step title="启动">
    ```bash theme={null}
    npm start
    ```

    索引器将开始轮询事件并在端口 3004 上提供数据。
  </Step>
</Steps>

## 配置

| 变量                 | 必需 | 默认值                                   | 描述                |
| ------------------ | -- | ------------------------------------- | ----------------- |
| `RPC_URL`          | 否  | `https://ethereum-rpc.publicnode.com` | 以太坊 JSON-RPC 端点   |
| `FACTORY_ADDRESS`  | ⚠️ | —                                     | GeodeFactory 合约地址 |
| `HOOK_ADDRESS`     | ⚠️ | —                                     | GeodeHook 合约地址    |
| `START_BLOCK`      | 否  | 当前                                    | 开始索引的区块号          |
| `POLL_INTERVAL_MS` | 否  | `5000`                                | 扫描新区块的频率（毫秒）      |
| `PORT`             | 否  | `3004`                                | HTTP API 端口       |

## API 参考

### 列出所有代币

```
GET /tokens
```

### 按地址获取代币

```
GET /tokens/:address
```

### 注册代币

```
POST /tokens
Content-Type: application/json
```

### 列出结算

```
GET /settlements?limit=50&poolId=0x...
```

### 统计信息

```
GET /stats
```

### 健康检查

```
GET /health
```

## 索引的事件

<CardGroup cols={2}>
  <Card title="TokenLaunched" icon="rocket">
    GeodeFactory 部署新 Geocurve 代币时发出。捕获名称、符号、部署者、储备和版税配置。
  </Card>

  <Card title="GeodeBatchSettled" icon="check-double">
    GeodeHook 每次成功结算后发出。记录出清价格、匹配量、费用和结算者地址。
  </Card>

  <Card title="GeodeCurveSwap" icon="chart-line">
    每次直接联合曲线交换时发出。追踪输入/输出金额、费用和更新的 ETH 储备。
  </Card>

  <Card title="GeodeLaunched" icon="gem">
    来自 GeodeHook 的备份事件。确保即使 Factory 事件被遗漏，代币也能被捕获。
  </Card>
</CardGroup>

## 持久化

索引器使用 **SQLite**（WAL 模式）作为数据库。关键表：

| 表               | 内容                              |
| --------------- | ------------------------------- |
| `tokens`        | 所有已发行代币及其元数据                    |
| `settlements`   | 每次批量结算及出清价格、费用、结算者              |
| `indexer_state` | 追踪 `last_indexed_block` 用于重启后恢复 |

## 结算者集成

[结算者机器人](/cn/settler)自动从索引器同步池列表。每 30 秒，结算者从索引器获取 `GET /tokens` 并注册任何新池。这意味着**在 Geode 上发行的每个代币都自动可结算**——无需手动池注册。
