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

# Indexer

> Envio HyperIndex writes Stockline events to Postgres and serves GraphQL.

The indexer lives in the sibling `indexer` repo. It is not inside the contracts tree.

Testnet chain ID 46630 is indexed over RPC. Mainnet 4663 is stubbed until deploy.

## Entities

| Entity        | Meaning                                               |
| ------------- | ----------------------------------------------------- |
| `Account`     | User address                                          |
| `Position`    | Per-account per-book collateral and event-summed debt |
| `Book`        | Registry book plus optional symbol                    |
| `Tier`        | Shared LTV / LT / max discount                        |
| `Oracle`      | From `OracleCreated`                                  |
| `Liquidation` | From `LiquidationModule.Liquidated`                   |
| `TimelockOp`  | Schedule / execute / cancel                           |
| `Pool`        | USDG pool cash path and shares                        |
| `FaucetDrip`  | Testnet claims                                        |

`Position.debt` is the sum of borrow, repay, liquidate, and deleverage event amounts. It is not live index-accrued debt. AccrueInterest updates the book index only. Query `RiskLens` for the live owed amount.

## Example queries

Account positions:

```graphql theme={"system"}
query AccountPositions($account: String!) {
  Account(where: { id: { _eq: $account } }) {
    id
    positions {
      book {
        bookId
        symbol
        borrowingEnabled
        tier { ltv lt }
      }
      collateral
      debt
      updatedAt
    }
  }
}
```

Books:

```graphql theme={"system"}
query Books {
  Book(order_by: { bookId: asc }) {
    bookId
    symbol
    collateral
    oracle { id stockToken }
    issuerId
    borrowCap
    borrowingEnabled
    tier { tierId ltv lt maxLiqDiscount }
  }
}
```

Recent liquidations:

```graphql theme={"system"}
query RecentLiquidations {
  Liquidation(order_by: { timestamp: desc }, limit: 20) {
    account { id }
    book { bookId symbol }
    liquidator
    repay
    seizeToLiquidator
    seizeToProtocol
    discount
    txHash
    timestamp
  }
}
```

Timelock queue:

```graphql theme={"system"}
query TimelockQueue {
  TimelockOp(order_by: { scheduledAt: desc }) {
    id
    target
    eta
    status
    scheduledAt
    executedAt
    cancelledAt
    txHash
  }
}
```

## Notes

* Replay `BookCreated` and `BookBorrowingEnabled` from the start block. Testnet books already exist. The indexer currently sees borrowing on; `ARCHITECTURE.md` says the launch flag is off. Trust the chain.
* `Book.symbol` fills only when `collateral` matches a known testnet token.
* ABIs in the indexer come from `./scripts/export-abis.sh` against `../contracts`.

The indexer lives in the sibling indexer repo. It is Envio HyperIndex writing Stockline events to Postgres and serving GraphQL.

Testnet chain ID 46630 is indexed over RPC. Mainnet 4663 is stubbed until deploy.

Position.debt is the sum of borrow, repay, liquidate, and deleverage event amounts. It is not live index-accrued debt. AccrueInterest updates the book index only. Query RiskLens for the live owed amount.

Replay BookCreated and BookBorrowingEnabled from the start block. Testnet books already exist. The indexer currently may see borrowing on while ARCHITECTURE.md says the launch flag is off. Trust the chain.

Related: [Liquidators](/developers/liquidators), [Addresses](/resources/addresses).
