npm package: @silo-finance/indexer
Description: GraphQL client for Silo Finance indexer - fetch markets, positions, and historical data
Latest version: 0.0.1

# @silo-finance/indexer

[![npm version](https://img.shields.io/npm/v/@silo-finance/indexer.svg)](https://www.npmjs.com/package/@silo-finance/indexer)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

GraphQL client for Silo Finance indexer. Fetch markets, positions, vaults, and historical data.

> **Note**: This package fetches data from the Silo indexer (GraphQL). For direct blockchain access, use [`@silo-finance/adapter-viem`](../adapter-viem/).

## Installation

```bash
pnpm add @silo-finance/indexer
```

## Quick Start

```typescript
import { createIndexerClient } from "@silo-finance/indexer";

const client = createIndexerClient({
  url: "https://your-indexer-url.com",
});

// Fetch all markets on Arbitrum
const markets = await client.getMarkets({ chainId: 42161 });

// Fetch user positions
const positions = await client.getAccountPositions(userAddress, 42161);

// Fetch historical data
const snapshots = await client.getMarketSnapshots({
  marketId: "42161-0x...",
  from: BigInt(Date.now() / 1000 - 86400 * 7), // Last 7 days
});
```

## API

### `createIndexerClient(config)`

Creates an indexer client.

```typescript
const client = createIndexerClient({
  url: "https://indexer.example.com",
  headers: { Authorization: "Bearer token" }, // optional
});
```

### Methods

| Method | Description |
|--------|-------------|
| `isAvailable()` | Check if indexer is reachable |
| `getSilos(options?)` | Get all silos (market pairs) |
| `getSilo(configAddress, chainId)` | Get a specific silo |
| `getMarkets(options?)` | Get all markets |
| `getMarket(marketAddress, chainId)` | Get a specific market |
| `getPositions(options?)` | Get positions with filters |
| `getAccountPositions(address, chainId)` | Get all positions for an account |
| `getToken(tokenAddress, chainId)` | Get token metadata |
| `getVaults(options?)` | Get all vaults |
| `getVault(vaultAddress, chainId)` | Get a specific vault |
| `getIncentivesPrograms(marketAddress, chainId)` | Get incentive programs |
| `getMarketSnapshots(options)` | Get historical market data |

### Query Options

```typescript
// Pagination
interface PaginationOptions {
  limit?: number;  // default: 100
  offset?: number; // default: 0
}

// Market filters
interface MarketQueryOptions extends PaginationOptions {
  chainId?: number;
  siloId?: string;
}

// Position filters
interface PositionQueryOptions extends PaginationOptions {
  chainId?: number;
  account?: Address;
  marketId?: string;
  openOnly?: boolean;
}

// Historical data
interface SnapshotQueryOptions extends PaginationOptions {
  marketId: string;
  from?: bigint; // timestamp
  to?: bigint;   // timestamp
}
```

## Data Types

All data uses branded types from `@silo-finance/primitives`:

```typescript
import type { MarketData, PositionData, TokenData } from "@silo-finance/indexer";

// MarketData includes:
// - address, chainId, siloId
// - inputToken, sToken, spToken, dToken (TokenData)
// - maxLtv, liquidationThreshold, liquidationFee (Percent)
// - totalSupply, totalBorrowed, liquidity (Amount)
// - utilization, borrowRate, depositRate (Percent)

// PositionData includes:
// - accountAddress, marketId, chainId
// - collateralBalance, protectedBalance, debtBalance (Amount)
// - isOpen, lastUpdatedTimestamp
```

## Using with SDK Calculations

Combine indexer data with `@silo-finance/core` for calculations:

```typescript
import { createIndexerClient } from "@silo-finance/indexer";
import { calculateLtv } from "@silo-finance/core";

const client = createIndexerClient({ url: "..." });

const market = await client.getMarket(marketAddress, chainId);
const positions = await client.getPositions({ account: userAddress, chainId });

// Use data with SDK calculations
const ltv = calculateLtv({
  collateralSilo: {
    tokenAddress: market.inputToken.address,
    decimals: market.inputToken.decimals,
    collateralBalance: positions[0].collateralBalance,
    // ...
  },
  debtSilo: { /* ... */ },
});
```

## Type-Safe GraphQL

This package uses [gql.tada](https://gql-tada.0no.co/) for type-safe GraphQL queries. Types are automatically inferred from the indexer schema.

```bash
# Regenerate types after schema changes
pnpm gql:generate
```

## License

MIT