moonproof-indexer-client 0.2.2

Rust client for the Midnight indexer's GraphQL API
Documentation

moonproof-indexer-client

Rust client for the Midnight indexer's GraphQL API. Drop-in equivalent of @midnight-ntwrk/midnight-js-indexer-public-data-provider.

The indexer is read-only — transaction submission goes to the Midnight node directly via moonproof-node-client (crates/node-client).

Usage

use moonproof_indexer_client::{BlockOffset, HttpIndexerClient, PublicDataProvider};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let client = HttpIndexerClient::new("http://localhost:8088")?;

    // Latest block
    let tip = client.block(None).await?;
    println!("tip = {tip:?}");

    // Block by height
    let b = client.block(Some(BlockOffset::Height(42))).await?;
    println!("block 42 = {b:?}");

    Ok(())
}

HttpIndexerClient::new(base) appends /api/v4/graphql. Use HttpIndexerClient::with_endpoint(url) to point at a non-standard path.

Design

Consumers program against the PublicDataProvider trait, not the HTTP client directly. This keeps tests mockable and gives us room to add alternate transports (e.g. an in-process provider that talks to an embedded indexer) without breaking callers.

┌──────────────────┐
│  consumer code   │
└────────┬─────────┘
         │  trait PublicDataProvider
         ▼
┌──────────────────┐        ┌────────────────────────┐
│ HttpIndexerClient│───────▶│ graphql_client codegen │
└──────────────────┘  POST  │ (from .graphql files)  │
                             └────────────────────────┘

Public domain types in types.rs are deliberately distinct from the graphql_client-generated types so the public API stays stable across schema revisions. The client layer does the conversion.

Crate layout

crates/indexer-client/
├── graphql/
│   ├── schema.graphql          # vendored from midnight-indexer
│   └── queries/                # one .graphql file per operation
│       └── block.graphql
└── src/
    ├── lib.rs                  # public re-exports
    ├── error.rs                # IndexerError
    ├── types.rs                # Block, BlockOffset, BlockRef, ...
    ├── provider.rs             # PublicDataProvider trait
    ├── client.rs               # HttpIndexerClient (generic execute<Q>)
    └── queries/
        ├── mod.rs
        └── block.rs            # #[derive(GraphQLQuery)] wrapper

Syncing the schema

The schema is vendored from midnightntwrk/midnight-indexer at indexer-api/graphql/schema-v4.graphql. To pull the latest:

bun run schema:indexer              # from main
bun run schema:indexer ledger-8     # from a specific ref
# or directly:
bash scripts/sync-indexer-schema.sh [ref]

The script validates that the fetched file looks like a real GraphQL schema before overwriting the vendored copy. After syncing, run cargo check -p moonproof-indexer-client to catch codegen breakage early.

Commit the updated schema.graphql alongside any query-side adjustments.

Adding a new query

  1. Write the operation in graphql/queries/<name>.graphql.

  2. Add a wrapper in src/queries/<name>.rs:

    use graphql_client::GraphQLQuery;
    
    pub type HexEncoded = String; // add other scalar aliases as needed
    
    #[derive(GraphQLQuery)]
    #[graphql(
        schema_path = "graphql/schema.graphql",
        query_path = "graphql/queries/<name>.graphql",
        response_derives = "Debug, Clone",
        variables_derives = "Debug, Clone"
    )]
    pub struct MyQuery;
    
  3. Register the module in src/queries/mod.rs.

  4. Add a method on PublicDataProvider and implement it on HttpIndexerClient using self.execute::<MyQuery>(variables). Map codegen response types to stable types::* shapes so the trait surface stays decoupled from generated code.

Status

All 28 queries + 2 mutations from the v4 schema are ported.

Chain reads:

  • block(offset) — latest / by-hash / by-height
  • transactions(offset) — regular / system, discriminated via TransactionKind
  • contractAction(address, offset) — deploy / call / update, discriminated via ContractActionKind
  • zswapMerkleTreeCollapsedUpdate(startIndex, endIndex)

Dust:

  • dustGenerationStatus(cardanoRewardAddresses)
  • dustGenerations(cardanoRewardAddresses)
  • dustCommitmentMerkleTreeUpdate(startIndex, endIndex)
  • dustGenerationMerkleTreeUpdate(startIndex, endIndex)

Governance history:

  • dParameterHistory
  • termsAndConditionsHistory

SPO identity / metadata:

  • spoIdentities(limit, offset), spoIdentityByPoolId(poolIdHex), spoCount
  • poolMetadata(poolIdHex), poolMetadataList(limit, offset, withNameOnly)
  • spoByPoolId(poolIdHex), spoList(limit, offset, search)
  • spoCompositeByPoolId(poolIdHex)
  • stakePoolOperators(limit)

Performance & epoch:

  • spoPerformanceLatest(limit, offset)
  • spoPerformanceBySpoSk(spoSkHex, limit, offset)
  • epochPerformance(epoch, limit, offset)
  • currentEpochInfo, epochUtilization(epoch), committee(epoch)

Registration stats:

  • registeredTotalsSeries(fromEpoch, toEpoch)
  • registeredSpoSeries(fromEpoch, toEpoch)
  • registeredPresence(fromEpoch, toEpoch)
  • registeredFirstValidEpochs(uptoEpoch)

Stake distribution:

  • stakeDistribution(limit, offset, search, orderByStakeDesc)

Wallet session mutations:

  • connect(viewingKey, options) → session ID
  • disconnect(sessionId)

WebSocket subscriptions (src/subscription.rs) run over graphql-transport-ws (with the +deflate variant when offered) and cover all 9 streams: blocks, contractActions, dustGenerations, dustLedgerEvents, dustNullifierTransactions, shieldedNullifierTransactions, shieldedTransactions, unshieldedTransactions, and zswapLedgerEvents.