AgentSkillsCN

midnight-indexer:indexer-service

在通过 Midnight 索引器查询区块链数据、获取账户余额、列出交易记录、读取合约状态,或构建数据驱动型 DApp 后端时使用。

SKILL.md
--- frontmatter
name: midnight-indexer:indexer-service
description: Use when querying the Midnight indexer for blockchain data, fetching account balances, listing transactions, reading contract state, or building data-driven DApp backends.

Indexer Service

Connect to and query the Midnight Network indexer for blockchain data including balances, transactions, and contract state.

When to Use

  • Fetching account balances and UTXO data
  • Listing transaction history for an address
  • Reading contract state from the blockchain
  • Querying shielded vs unshielded transaction data
  • Building data-driven DApp backends
  • Implementing pagination for large result sets

Key Concepts

Indexer Architecture

The Midnight indexer provides a GraphQL API for querying blockchain data. It indexes all on-chain activity and exposes it through structured queries.

ComponentPurpose
GraphQL APIQuery interface for blockchain data
WebSocketReal-time subscriptions (see event-subscriptions skill)
REST HealthService health checks

API Versions

The indexer API has evolved through several versions. Ensure your queries are compatible with your target version.

VersionKey FeaturesBreaking Changes
2.0.0Base GraphQL schemaInitial release
2.1.0Enhanced filters, paginationQuery parameter changes
2.1.4Performance optimizationsNone from 2.1.0

UTXO Model

Midnight uses a UTXO (Unspent Transaction Output) model rather than an account model. Balance queries return the set of unspent outputs owned by an address.

Shielded Transactions

Midnight supports both transparent and shielded transactions. The indexer provides different access patterns:

  • Transparent: Full transaction details visible
  • Shielded: Only commitment hashes visible, details encrypted

References

DocumentDescription
api-versions.mdVersion differences and migration guide
query-patterns.mdCommon query patterns and optimization

Examples

ExampleDescription
balance-query/Query account balance and UTXOs
transaction-list/List transaction history with pagination
contract-state/Read deployed contract state

Quick Start

1. Configure Indexer Client

typescript
import { createIndexerClient } from '@midnight-ntwrk/midnight-js-indexer';

const indexer = createIndexerClient({
  uri: 'https://indexer.testnet.midnight.network/api/v1/graphql',
  wsUri: 'wss://indexer.testnet.midnight.network/api/v1/graphql',
});

2. Query Balance

typescript
const balance = await indexer.query({
  query: `
    query GetBalance($address: String!) {
      balance(address: $address) {
        total
        utxos {
          txHash
          outputIndex
          amount
        }
      }
    }
  `,
  variables: { address: 'addr_test1...' },
});

console.log('Total balance:', balance.data.balance.total);

3. List Transactions

typescript
const transactions = await indexer.query({
  query: `
    query GetTransactions($address: String!, $limit: Int!) {
      transactions(address: $address, first: $limit) {
        edges {
          node {
            hash
            blockNumber
            timestamp
            inputs { address amount }
            outputs { address amount }
          }
        }
        pageInfo {
          hasNextPage
          endCursor
        }
      }
    }
  `,
  variables: { address: 'addr_test1...', limit: 20 },
});

Common Patterns

Environment-Based Configuration

typescript
interface IndexerConfig {
  uri: string;
  wsUri: string;
}

function getIndexerConfig(): IndexerConfig {
  const network = process.env.MIDNIGHT_NETWORK || 'testnet';

  const configs: Record<string, IndexerConfig> = {
    testnet: {
      uri: 'https://indexer.testnet.midnight.network/api/v1/graphql',
      wsUri: 'wss://indexer.testnet.midnight.network/api/v1/graphql',
    },
    mainnet: {
      uri: 'https://indexer.midnight.network/api/v1/graphql',
      wsUri: 'wss://indexer.midnight.network/api/v1/graphql',
    },
  };

  return configs[network] ?? configs.testnet;
}

Pagination with Cursors

typescript
async function fetchAllTransactions(
  indexer: IndexerClient,
  address: string
): Promise<Transaction[]> {
  const allTransactions: Transaction[] = [];
  let cursor: string | null = null;
  let hasMore = true;

  while (hasMore) {
    const result = await indexer.query({
      query: TRANSACTIONS_QUERY,
      variables: {
        address,
        first: 100,
        after: cursor,
      },
    });

    const { edges, pageInfo } = result.data.transactions;
    allTransactions.push(...edges.map(e => e.node));

    hasMore = pageInfo.hasNextPage;
    cursor = pageInfo.endCursor;
  }

  return allTransactions;
}

Health Check

typescript
async function checkIndexerHealth(baseUrl: string): Promise<boolean> {
  try {
    const response = await fetch(`${baseUrl}/health`);
    return response.ok;
  } catch {
    return false;
  }
}

Retry with Backoff

typescript
async function queryWithRetry<T>(
  indexer: IndexerClient,
  query: string,
  variables: Record<string, unknown>,
  maxRetries = 3
): Promise<T> {
  let lastError: Error | null = null;

  for (let attempt = 1; attempt <= maxRetries; attempt++) {
    try {
      return await indexer.query({ query, variables });
    } catch (error) {
      lastError = error as Error;
      if (attempt < maxRetries) {
        await new Promise(r => setTimeout(r, 1000 * attempt));
      }
    }
  }

  throw lastError;
}

Related Skills

  • event-subscriptions - Real-time WebSocket event streams
  • midnight-tooling:contract-calling - Invoking contracts that change state
  • midnight-dapp:state-management - Frontend state synchronization with indexer

Related Commands

  • /midnight-tooling:check - Verify indexer connectivity