# Markets Data

> Markdown export of the Gearbox Protocol documentation page for agents and retrieval systems.

Canonical page: https://docs.gearbox.finance/developers/gm-markets-data
Source file: content/developers/gm-markets-data.mdx
Section router: https://docs.gearbox.finance/developers/llms.txt
Section full export: https://docs.gearbox.finance/developers/llms-full.txt

Read pool metrics, credit manager configuration, collateral tokens, and interest rates using the Gearbox SDK. All data is cached on SDK initialization and can be refreshed via compressors.

## Initialize the SDK

```typescript
import { GearboxSDK } from "@gearbox-protocol/sdk";
import { createPublicClient, http } from "viem";
import { mainnet } from "viem/chains";

const client = createPublicClient({
  chain: mainnet,
  transport: http(),
});

const sdk = await GearboxSDK.attach({
  client,
  marketConfigurators: [],
});
```

## Finding Markets

The `marketRegister` provides cached access to all Gearbox markets:

```typescript
// All markets
const markets = sdk.marketRegister.markets;

// Find by pool address
const market = sdk.marketRegister.findByPool(poolAddress);

// Find by credit manager address
const market = sdk.marketRegister.findByCreditManager(cmAddress);
```

## Filtering Markets

Query markets by underlying token, liquidity, or configurator:

```typescript
// All USDC markets
const usdcMarkets = sdk.marketRegister.markets.filter(
  m => m.pool.underlying.symbol === "USDC"
);

// Markets with > 1M available liquidity
const liquidMarkets = sdk.marketRegister.markets.filter(
  m => m.pool.availableLiquidity > 1_000_000n * 10n ** 6n
);

// Markets by configurator
const curatedMarkets = sdk.marketRegister.markets.filter(
  m => m.configurator === curatorAddress
);
```

## Pool State

Access core pool metrics through the market object:

```typescript
const pool = market.pool;

console.log(`Underlying: ${pool.underlying.symbol}`);
console.log(`Total assets: ${pool.totalAssets}`);
console.log(`Available liquidity: ${pool.availableLiquidity}`);
console.log(`Diesel rate (share price): ${pool.dieselRate}`);
```

### Interest Rates

Rates are stored in RAY precision (27 decimals):

```typescript
const RAY = 10n ** 27n;

const supplyAPY = Number(pool.supplyRate * 10000n / RAY) / 100;
const borrowAPR = Number(pool.baseInterestRate * 10000n / RAY) / 100;

console.log(`Supply APY: ${supplyAPY.toFixed(2)}%`);
console.log(`Borrow APR: ${borrowAPR.toFixed(2)}%`);
```

| Field | Type | Description |
|---|---|---|
| `supplyRate` | `bigint` | Annual supply rate for LPs (RAY) |
| `baseInterestRate` | `bigint` | Annual base borrow rate (RAY) |
| `dieselRate` | `bigint` | Current share price (RAY) |
| `totalAssets` | `bigint` | Total underlying in pool |
| `availableLiquidity` | `bigint` | Underlying available for borrowing |

## Credit Manager Data

Each market can have multiple credit managers with different risk profiles:

```typescript
for (const cm of market.creditManagers) {
  console.log(`Credit Manager: ${cm.address}`);
  console.log(`Credit Facade: ${cm.creditFacade}`);
  console.log(`Min debt: ${cm.minDebt}`);
  console.log(`Max debt: ${cm.maxDebt}`);

  // Allowed collateral tokens and their liquidation thresholds
  for (const token of cm.collateralTokens) {
    console.log(`  ${token.symbol}: LT ${token.liquidationThreshold}`);
  }
}
```

| Field | Type | Description |
|---|---|---|
| `minDebt` | `bigint` | Minimum borrowable amount |
| `maxDebt` | `bigint` | Maximum borrowable amount |
| `collateralTokens` | `array` | Allowed tokens with liquidation thresholds |
| `creditFacade` | `Address` | Entry point for user interactions |

## Price Oracle Data

Access token prices through the market:

```typescript
const priceOracle = market.priceOracle;

for (const token of market.tokens) {
  console.log(`${token.symbol}: ${token.price} USD`);
}
```

## Real-Time Data via Compressors

The SDK caches data on initialization. For fresh on-chain data, query compressors directly:

```typescript
import {
  GearboxSDK,
  marketCompressorAbi,
  AP_MARKET_COMPRESSOR,
  VERSION_RANGE_310,
} from "@gearbox-protocol/sdk";

// Get compressor address
const [compressor] = sdk.addressProvider.mustGetLatest(
  AP_MARKET_COMPRESSOR,
  VERSION_RANGE_310
);

// Fetch fresh market data
const freshData = await client.readContract({
  address: compressor,
  abi: marketCompressorAbi,
  functionName: "getMarketData",
  args: [poolAddress],
});

console.log(`Fresh available liquidity: ${freshData.pool.availableLiquidity}`);
```

## Complete Example

```typescript
import { GearboxSDK } from "@gearbox-protocol/sdk";
import { createPublicClient, http } from "viem";
import { mainnet } from "viem/chains";

async function getMarketOverview() {
  const client = createPublicClient({
    chain: mainnet,
    transport: http(),
  });

  const sdk = await GearboxSDK.attach({
    client,
    marketConfigurators: [],
  });

  const RAY = 10n ** 27n;

  for (const market of sdk.marketRegister.markets) {
    const pool = market.pool;

    console.log(`\n=== ${pool.underlying.symbol} Market ===`);
    console.log(`Pool: ${pool.address}`);
    console.log(`Total assets: ${pool.totalAssets}`);
    console.log(`Available: ${pool.availableLiquidity}`);

    const supplyAPY = Number(pool.supplyRate * 10000n / RAY) / 100;
    console.log(`Supply APY: ${supplyAPY.toFixed(2)}%`);
    console.log(`Credit Managers: ${market.creditManagers.length}`);
  }
}

getMarketOverview().catch(console.error);
```

## Next Steps

- [Pool Operations](https://docs.gearbox.finance/developers/gm-markets-pools) -- Deposit and withdraw liquidity
- [Interest Rates & Quotas](https://docs.gearbox.finance/developers/gm-markets-rates) -- Understand rate models and quota limits
- [Insurance Mechanism](https://docs.gearbox.finance/developers/gm-markets-insurance) -- How protocol revenue protects lenders
