# Reader

> The Reader contract provides convenience functions for retrieving information such as markets, positions, and pricing data.

# Reader

The Reader contract provides convenience functions for retrieving information such as markets, positions, and pricing data.

This page focuses on the most commonly used read helpers. For the full public surface, see [`Reader.sol`](https://github.com/gmx-io/gmx-synthetics/blob/updates/contracts/reader/Reader.sol).

Examples below assume you already have contract instances such as `reader`, `dataStore`, and `referralStorage`, plus any required market or price structs.

## Market list

Use `Reader.getMarkets` to retrieve a paginated list of all markets. Each market is represented by a `Market.Props` struct.

### Reader.getMarkets

```solidity
function getMarkets(DataStore dataStore, uint256 start, uint256 end)
    external view returns (Market.Props[] memory)
```

**Example (TypeScript / ethers):**

```typescript
const markets = await reader.getMarkets(dataStore.address, 0, 20);
```

**Parameters:**

| Parameter   | Type      | Description                       |
| ----------- | --------- | --------------------------------- |
| `dataStore` | `address` | Address of the DataStore contract |
| `start`     | `uint256` | Start index for pagination        |
| `end`       | `uint256` | End index for pagination          |

**Returns:** Array of `Market.Props`.

### Market.Props

| Field         | Type      | Description                                                          |
| ------------- | --------- | -------------------------------------------------------------------- |
| `marketToken` | `address` | Address of the GM token for this market                              |
| `indexToken`  | `address` | Address of the index token (for example, ETH for the ETH/USD market) |
| `longToken`   | `address` | Address of the long collateral token                                 |
| `shortToken`  | `address` | Address of the short collateral token                                |

## Detailed market list

Use `Reader.getMarketInfoList` to retrieve markets with additional runtime data including borrowing rates, funding rates, and whether the market is disabled.

### Reader.getMarketInfoList

```solidity
function getMarketInfoList(
    DataStore dataStore,
    MarketUtils.MarketPrices[] memory marketPricesList,
    uint256 start,
    uint256 end
) external view returns (ReaderUtils.MarketInfo[] memory)
```

**Example (TypeScript / ethers):**

```typescript
const marketInfoList = await reader.getMarketInfoList(dataStore.address, marketPricesList, 0, 20);
```

**Parameters:**

| Parameter          | Type                         | Description                                                                    |
| ------------------ | ---------------------------- | ------------------------------------------------------------------------------ |
| `dataStore`        | `address`                    | Address of the DataStore contract                                              |
| `marketPricesList` | `MarketUtils.MarketPrices[]` | Current prices for each market, in the same order as the markets being queried |
| `start`            | `uint256`                    | Start index for pagination                                                     |
| `end`              | `uint256`                    | End index for pagination                                                       |

**Returns:** Array of `ReaderUtils.MarketInfo`, which includes the market props, borrowing rates, funding rates, and a flag indicating whether the market is disabled.

### MarketUtils.MarketPrices

| Field             | Type          | Description                             |
| ----------------- | ------------- | --------------------------------------- |
| `indexTokenPrice` | `Price.Props` | Current price range for the index token |
| `longTokenPrice`  | `Price.Props` | Current price range for the long token  |
| `shortTokenPrice` | `Price.Props` | Current price range for the short token |

### Price.Props

| Field | Type      | Description         |
| ----- | --------- | ------------------- |
| `min` | `uint256` | Minimum (bid) price |
| `max` | `uint256` | Maximum (ask) price |

### Price and amount encoding

Reader pricing helpers (for example `getMarketTokenPrice`, `getSwapAmountOut`, `getDepositAmountOut`, `getWithdrawalAmountOut`) share two encoding conventions for the values you pass in.

**Token amounts** use each token's **native** decimals — the same decimals the ERC-20 reports on-chain. Use each deposit or swap token's own decimals; do not substitute the index token's decimals.

**Prices** stored in `Price.Props.min` / `Price.Props.max` are encoded as:

```
priceValue = usdPrice * 10^(30 - tokenDecimals)
```

This is the form produced by the oracle providers and consumed directly by the Reader. Common examples for a `$1` price:

| Token              | Decimals | Encoded price for $1 |
| ------------------ | -------- | -------------------- |
| USDC, USDT         | 6        | `1 * 10^24`          |
| WBTC, WBTC.e       | 8        | `1 * 10^22`          |
| TAO                | 9        | `1 * 10^21`          |
| WETH, most ERC-20s | 18       | `1 * 10^12`          |

The invariant is `tokenAmount * priceValue = usdValue * 10^30`, so the raw token amount never needs a unit adjustment when multiplied by the encoded price.

:::danger Common mistake
Do **not** pass prices as `usdPrice * 10^30` (pure 30-decimal USD). That form is only correct for a hypothetical 0-decimal token — it is not the encoding used anywhere in the protocol.

Anti-example — for USDC (6 decimals) at $1:

| Form                     | Value          | Correct?                         |
| ------------------------ | -------------- | -------------------------------- |
| `usdPrice * 10^30`       | `1 * 10^30`    | ❌ 10^6× too large                |
| `usdPrice * 10^(30 − 6)` | `1 * 10^24`    | ✅                                |

Passing pure 30-decimal USD inflates each token's internal USD value by `10^tokenDecimals`. Since the Reader also uses the same prices to compute the pool value, `getDepositAmountOut` returns a ratio of two wrong numbers — typically thousands of times larger than the expected GM amount.
:::

:::warning
Synthetic index markets can mix decimals across the index, long, and short tokens. The `TAO/USD` GM market, for example, is backed by WBTC.e (8 decimals) as the long token and USDC (6 decimals) as the short token, while the index token TAO itself has 9 decimals and is never deposited. Always apply each token's own decimals when encoding its price and scaling its amount — reusing the index token's decimals for a deposit amount is a common source of off-by-10^n errors (for the TAO/USD market, off by a factor of `10^3` against USDC).
:::

**Worked example — USDC-only deposit into the `TAO/USD` market** (TAO at `$420`, WBTC.e at `$60,000`, USDC at `$1`):

```typescript
const prices = {
  // TAO index token — 9 decimals
  indexTokenPrice: {
    min: 420n * 10n ** 21n,
    max: 420n * 10n ** 21n,
  },
  // WBTC.e long token — 8 decimals
  longTokenPrice: {
    min: 60_000n * 10n ** 22n,
    max: 60_000n * 10n ** 22n,
  },
  // USDC short token — 6 decimals
  shortTokenPrice: {
    min: 1n * 10n ** 24n,
    max: 1n * 10n ** 24n,
  },
};

const longTokenAmount = 0n;           // not depositing the long token
const shortTokenAmount = 1_070_000n;  // 1.07 USDC (6 decimals) — NOT 1.07 * 10^9
```

The return value from `getDepositAmountOut` (and any other helper that returns a GM amount) is a raw `uint256` at 18 decimals. Divide by `10^18` for human-readable display.

## Getting GM token price

Use `Reader.getMarketTokenPrice` to retrieve a GM token price for the supplied market, token prices, PnL factor type, and min/max direction. The call also returns detailed pool information: pool value, PnL, token amounts, borrowing fees, and the impact pool.

Use this method when you need direct GMX Reader valuation for a specific block and supplied price context, or when a Chainlink Data Feed is not available. For external protocol integrations that only need a standard oracle interface, prefer Chainlink Data Feeds where available. For the integration decision tree, see [Getting GM and GLV token prices](../gm-glv-prices.md).

### Reader.getMarketTokenPrice

```solidity
function getMarketTokenPrice(
    DataStore dataStore,
    Market.Props memory market,
    Price.Props memory indexTokenPrice,
    Price.Props memory longTokenPrice,
    Price.Props memory shortTokenPrice,
    bytes32 pnlFactorType,
    bool maximize
) external view returns (int256, MarketPoolValueInfo.Props memory)
```

**Example (TypeScript / ethers):**

```typescript
const [gmPrice, poolInfo] = await reader.getMarketTokenPrice(
  dataStore.address,
  market,
  indexTokenPrice,
  longTokenPrice,
  shortTokenPrice,
  pnlFactorType,
  true
);
```

**Parameters:**

| Parameter         | Type           | Description                                                                                                                                                                                                               |
| ----------------- | -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `dataStore`       | `address`      | Address of the DataStore contract                                                                                                                                                                                         |
| `market`          | `Market.Props` | Market data struct (see [Market list](#market-list))                                                                                                                                                                      |
| `indexTokenPrice` | `Price.Props`  | Current price range for the index token                                                                                                                                                                                   |
| `longTokenPrice`  | `Price.Props`  | Current price range for the long token                                                                                                                                                                                    |
| `shortTokenPrice` | `Price.Props`  | Current price range for the short token                                                                                                                                                                                   |
| `pnlFactorType`   | `bytes32`      | Which PnL factor cap to apply. Use `Keys.MAX_PNL_FACTOR_FOR_TRADERS` for trading contexts, `Keys.MAX_PNL_FACTOR_FOR_DEPOSITS` for deposit simulation, or `Keys.MAX_PNL_FACTOR_FOR_WITHDRAWALS` for withdrawal simulation. |
| `maximize`        | `bool`         | Pass `true` to use maximum prices (gives the higher GM token price), `false` to use minimum prices                                                                                                                        |

**Returns:** A tuple of `(int256 price, MarketPoolValueInfo.Props poolInfo)` where `price` is the GM token price and `poolInfo` contains pool value, PnL, token amounts, borrowing fees, and the impact pool balance.

## Position list

Use `Reader.getAccountPositions` to retrieve all open positions for a given account. The function returns raw position data without fee or pricing calculations.

### Reader.getAccountPositions

```solidity
function getAccountPositions(
    DataStore dataStore,
    address account,
    uint256 start,
    uint256 end
) external view returns (Position.Props[] memory)
```

**Example (TypeScript / ethers):**

```typescript
const positions = await reader.getAccountPositions(dataStore.address, account.address, 0, 20);
```

**Parameters:**

| Parameter   | Type      | Description                                   |
| ----------- | --------- | --------------------------------------------- |
| `dataStore` | `address` | Address of the DataStore contract             |
| `account`   | `address` | The account address to retrieve positions for |
| `start`     | `uint256` | Start index for pagination                    |
| `end`       | `uint256` | End index for pagination                      |

**Returns:** Array of `Position.Props`, each containing the raw position state (size, collateral, entry price, and so on).

## Detailed position list

Use `Reader.getAccountPositionInfoList` to retrieve all open positions for a given account with full fee and pricing context. This function uses the account address and market list directly rather than position keys.

:::note
`Reader.getAccountPositionInfoList` uses an account plus market list rather than explicit `positionKeys`. If you need to query specific keys directly, use `Reader.getPositionInfoList`.
:::

### Reader.getAccountPositionInfoList

```solidity
function getAccountPositionInfoList(
    DataStore dataStore,
    IReferralStorage referralStorage,
    address account,
    address[] memory markets,
    MarketUtils.MarketPrices[] memory marketPrices,
    address uiFeeReceiver,
    uint256 start,
    uint256 end
) external view returns (ReaderPositionUtils.PositionInfo[] memory)
```

**Example (TypeScript / ethers):**

```typescript
const positionInfoList = await reader.getAccountPositionInfoList(
  dataStore.address,
  referralStorage.address,
  account.address,
  markets,
  marketPrices,
  ethers.constants.AddressZero,
  0,
  20
);
```

**Parameters:**

| Parameter         | Type                         | Description                                                                                                                                 |
| ----------------- | ---------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- |
| `dataStore`       | `address`                    | Address of the DataStore contract                                                                                                           |
| `referralStorage` | `address`                    | Address of the ReferralStorage contract                                                                                                     |
| `account`         | `address`                    | The account address to retrieve positions for                                                                                               |
| `markets`         | `address[]`                  | Array of perp market addresses covering every position you expect to retrieve. Must contain only perpetual markets (not swap-only markets). |
| `marketPrices`    | `MarketUtils.MarketPrices[]` | Current prices for each market in the same order as `markets`. If a returned position's market is missing from this list, the call reverts. |
| `uiFeeReceiver`   | `address`                    | Address of the UI fee receiver used in fee calculations. Pass the zero address if not applicable.                                           |
| `start`           | `uint256`                    | Start index for pagination                                                                                                                  |
| `end`             | `uint256`                    | End index for pagination                                                                                                                    |

**Returns:** Array of `ReaderPositionUtils.PositionInfo`, each including the position, fees, execution price, and PnL.

## Position information

Use `Reader.getPositionInfo` to retrieve full details for a single position, including fees, execution price, and PnL.

### Reader.getPositionInfo

```solidity
function getPositionInfo(
    DataStore dataStore,
    IReferralStorage referralStorage,
    bytes32 positionKey,
    MarketUtils.MarketPrices memory prices,
    uint256 sizeDeltaUsd,
    address uiFeeReceiver,
    bool usePositionSizeAsSizeDeltaUsd
) public view returns (ReaderPositionUtils.PositionInfo memory)
```

**Example (TypeScript / ethers):**

```typescript
const positionInfo = await reader.getPositionInfo(
  dataStore.address,
  referralStorage.address,
  positionKey,
  prices,
  0,
  ethers.constants.AddressZero,
  false
);
```

**Parameters:**

| Parameter                       | Type                       | Description                                                                                                               |
| ------------------------------- | -------------------------- | ------------------------------------------------------------------------------------------------------------------------- |
| `dataStore`                     | `address`                  | Address of the DataStore contract                                                                                         |
| `referralStorage`               | `address`                  | Address of the ReferralStorage contract                                                                                   |
| `positionKey`                   | `bytes32`                  | Key identifying the position                                                                                              |
| `prices`                        | `MarketUtils.MarketPrices` | Current prices for the index, long, and short tokens                                                                      |
| `sizeDeltaUsd`                  | `uint256`                  | Used to calculate fees and execution price when simulating a position decrease. Pass `0` if not simulating a size change. |
| `uiFeeReceiver`                 | `address`                  | Address of the UI fee receiver for fee calculations. Pass the zero address if not applicable.                             |
| `usePositionSizeAsSizeDeltaUsd` | `bool`                     | Pass `true` to use the full position size as `sizeDeltaUsd` (for example, when simulating a full close)                   |

**Returns:** `ReaderPositionUtils.PositionInfo` containing the position data, fees, execution price, and PnL.

Two aggregate fields are useful when presenting position state:

| Field | Composition |
| --- | --- |
| `pnlAfterPriceImpactUsd` | `basePnlUsd + executionPriceResult.priceImpactUsd` |
| `positionValueInUsd` | Collateral valued at its minimum price, plus claimable long-token and short-token funding valued at their minimum prices, plus `basePnlUsd` and `executionPriceResult.totalImpactUsd`, minus total costs valued at the collateral token's maximum price |

Both values are signed USD amounts. `positionValueInUsd` uses conservative price bounds for assets and costs, and can be negative.

## Detailed position list by key

Use `Reader.getPositionInfoList` to retrieve detailed position data when you already have an array of position keys.

### Reader.getPositionInfoList

```solidity
function getPositionInfoList(
    DataStore dataStore,
    IReferralStorage referralStorage,
    bytes32[] memory positionKeys,
    MarketUtils.MarketPrices[] memory prices,
    address uiFeeReceiver
) external view returns (ReaderPositionUtils.PositionInfo[] memory)
```

**Example (TypeScript / ethers):**

```typescript
const positionInfoList = await reader.getPositionInfoList(
  dataStore.address,
  referralStorage.address,
  positionKeys,
  prices,
  ethers.constants.AddressZero
);
```

**Parameters:**

| Parameter         | Type                         | Description                                                                                       |
| ----------------- | ---------------------------- | ------------------------------------------------------------------------------------------------- |
| `dataStore`       | `address`                    | Address of the DataStore contract                                                                 |
| `referralStorage` | `address`                    | Address of the ReferralStorage contract                                                           |
| `positionKeys`    | `bytes32[]`                  | Position keys to query                                                                            |
| `prices`          | `MarketUtils.MarketPrices[]` | Current prices for the markets of the requested positions, in the same order as `positionKeys`    |
| `uiFeeReceiver`   | `address`                    | Address of the UI fee receiver used in fee calculations. Pass the zero address if not applicable. |

**Returns:** Array of `ReaderPositionUtils.PositionInfo` values in the same order as `positionKeys`.

## Execution price for increasing or decreasing a position

Use `Reader.getExecutionPrice` to estimate the execution price for increasing or decreasing a position, accounting for price impact.

### Reader.getExecutionPrice

```solidity
function getExecutionPrice(
    DataStore dataStore,
    address marketKey,
    MarketUtils.MarketPrices memory prices,
    uint256 positionSizeInUsd,
    uint256 positionSizeInTokens,
    int256 sizeDeltaUsd,
    int256 pendingImpactAmount,
    bool isLong
) external view returns (ReaderPricingUtils.ExecutionPriceResult memory)
```

**Example (TypeScript / ethers):**

```typescript
const executionPriceResult = await reader.getExecutionPrice(
  dataStore.address,
  marketKey,
  prices,
  positionSizeInUsd,
  positionSizeInTokens,
  sizeDeltaUsd,
  pendingImpactAmount,
  true
);
```

**Parameters:**

| Parameter              | Type                       | Description                                                                                   |
| ---------------------- | -------------------------- | --------------------------------------------------------------------------------------------- |
| `dataStore`            | `address`                  | Address of the DataStore contract                                                             |
| `marketKey`            | `address`                  | Address of the market                                                                         |
| `prices`               | `MarketUtils.MarketPrices` | Current prices for the index, long, and short tokens                                          |
| `positionSizeInUsd`    | `uint256`                  | Current size of the open position in USD. Pass `0` if there is no existing position.          |
| `positionSizeInTokens` | `uint256`                  | Current size of the open position in index tokens. Pass `0` if there is no existing position. |
| `sizeDeltaUsd`         | `int256`                   | Size change in USD. Positive to increase, negative to decrease.                               |
| `pendingImpactAmount`  | `int256`                   | Pending price impact amount already accumulated for this position                             |
| `isLong`               | `bool`                     | Pass `true` for a long position, `false` for short                                            |

**Returns:** `ReaderPricingUtils.ExecutionPriceResult` containing the execution price and price impact amounts.

## Output amount for swaps

Use `Reader.getSwapAmountOut` to estimate how many tokens you receive for a swap, along with the price impact and swap fees.

### Reader.getSwapAmountOut

```solidity
function getSwapAmountOut(
    DataStore dataStore,
    Market.Props memory market,
    MarketUtils.MarketPrices memory prices,
    address tokenIn,
    uint256 amountIn,
    address uiFeeReceiver
) external view returns (uint256, int256, SwapPricingUtils.SwapFees memory fees)
```

**Example (TypeScript / ethers):**

```typescript
const [amountOut, impactAmount, fees] = await reader.getSwapAmountOut(
  dataStore.address,
  market,
  prices,
  tokenIn,
  amountIn,
  ethers.constants.AddressZero
);
```

**Parameters:**

| Parameter       | Type                       | Description                                                              |
| --------------- | -------------------------- | ------------------------------------------------------------------------ |
| `dataStore`     | `address`                  | Address of the DataStore contract                                        |
| `market`        | `Market.Props`             | Market to route the swap through                                         |
| `prices`        | `MarketUtils.MarketPrices` | Current prices for the index, long, and short tokens                     |
| `tokenIn`       | `address`                  | Address of the input token                                               |
| `amountIn`      | `uint256`                  | Amount of the input token                                                |
| `uiFeeReceiver` | `address`                  | Address of the UI fee receiver. Pass the zero address if not applicable. |

**Returns:** A tuple of `(uint256 amountOut, int256 impactAmount, SwapPricingUtils.SwapFees fees)` — the output token amount, price impact amount, and fee breakdown.

## Output amount for deposits

Use `Reader.getDepositAmountOut` to estimate how many GM tokens you receive for a deposit.

### Reader.getDepositAmountOut

```solidity
function getDepositAmountOut(
    DataStore dataStore,
    Market.Props memory market,
    MarketUtils.MarketPrices memory prices,
    uint256 longTokenAmount,
    uint256 shortTokenAmount,
    address uiFeeReceiver,
    ISwapPricingUtils.SwapPricingType swapPricingType,
    bool includeVirtualInventoryImpact
) external view returns (uint256)
```

**Example (TypeScript / ethers):**

```typescript
const marketTokensOut = await reader.getDepositAmountOut(
  dataStore.address,
  market,
  prices,
  longTokenAmount,
  shortTokenAmount,
  ethers.constants.AddressZero,
  swapPricingType,
  true
);
```

**Parameters:**

| Parameter                       | Type                                | Description                                                                     |
| ------------------------------- | ----------------------------------- | ------------------------------------------------------------------------------- |
| `dataStore`                     | `address`                           | Address of the DataStore contract                                               |
| `market`                        | `Market.Props`                      | The market to deposit into                                                      |
| `prices`                        | `MarketUtils.MarketPrices`          | Current prices for the index, long, and short tokens                            |
| `longTokenAmount`               | `uint256`                           | Amount of the long token to deposit                                             |
| `shortTokenAmount`              | `uint256`                           | Amount of the short token to deposit                                            |
| `uiFeeReceiver`                 | `address`                           | Address of the UI fee receiver. Pass the zero address if not applicable.        |
| `swapPricingType`               | `ISwapPricingUtils.SwapPricingType` | Pricing method to use for the internal swap component of the deposit            |
| `includeVirtualInventoryImpact` | `bool`                              | Pass `true` to include virtual inventory impact in the price impact calculation |

**Returns:** `uint256` — the estimated number of GM tokens minted, at 18 decimals.

:::note
Pass `longTokenAmount` and `shortTokenAmount` in each collateral token's **native** decimals (not the index token's), and encode the entries in `prices` as `usdPrice * 10^(30 - tokenDecimals)`. See [Price and amount encoding](#price-and-amount-encoding) for the full convention and a worked example. The returned GM amount is at 18 decimals — divide by `10^18` for display.
:::

## Output amount for withdrawals

Use `Reader.getWithdrawalAmountOut` to estimate how many long and short tokens you receive when withdrawing GM tokens.

### Reader.getWithdrawalAmountOut

```solidity
function getWithdrawalAmountOut(
    DataStore dataStore,
    Market.Props memory market,
    MarketUtils.MarketPrices memory prices,
    uint256 marketTokenAmount,
    address uiFeeReceiver,
    ISwapPricingUtils.SwapPricingType swapPricingType
) external view returns (uint256, uint256)
```

**Example (TypeScript / ethers):**

```typescript
const [longTokenAmountOut, shortTokenAmountOut] = await reader.getWithdrawalAmountOut(
  dataStore.address,
  market,
  prices,
  marketTokenAmount,
  ethers.constants.AddressZero,
  swapPricingType
);
```

**Parameters:**

| Parameter           | Type                                | Description                                                              |
| ------------------- | ----------------------------------- | ------------------------------------------------------------------------ |
| `dataStore`         | `address`                           | Address of the DataStore contract                                        |
| `market`            | `Market.Props`                      | The market to withdraw from                                              |
| `prices`            | `MarketUtils.MarketPrices`          | Current prices for the index, long, and short tokens                     |
| `marketTokenAmount` | `uint256`                           | Amount of GM tokens to burn                                              |
| `uiFeeReceiver`     | `address`                           | Address of the UI fee receiver. Pass the zero address if not applicable. |
| `swapPricingType`   | `ISwapPricingUtils.SwapPricingType` | Pricing method to use for the internal swap component of the withdrawal  |

**Returns:** A tuple of `(uint256 longTokenAmount, uint256 shortTokenAmount)` — the estimated amounts of long and short tokens returned.

## Direct lookups

Use these methods when you already have a key, salt, or account and want the raw stored value without additional pricing or fee calculations.

### Reader.getMarket

```solidity
function getMarket(DataStore dataStore, address key)
    external view returns (Market.Props memory)
```

**Parameters:**

| Parameter   | Type      | Description                                 |
| ----------- | --------- | ------------------------------------------- |
| `dataStore` | `address` | Address of the DataStore contract           |
| `key`       | `address` | Market token address used as the market key |

**Returns:** `Market.Props` for the requested market.

### Reader.getMarketBySalt

```solidity
function getMarketBySalt(DataStore dataStore, bytes32 salt)
    external view returns (Market.Props memory)
```

**Parameters:**

| Parameter   | Type      | Description                          |
| ----------- | --------- | ------------------------------------ |
| `dataStore` | `address` | Address of the DataStore contract    |
| `salt`      | `bytes32` | Deterministic market deployment salt |

**Returns:** `Market.Props` for the requested market.

### Reader.getDeposit

```solidity
function getDeposit(DataStore dataStore, bytes32 key)
    external view returns (Deposit.Props memory)
```

**Parameters:**

| Parameter   | Type      | Description                       |
| ----------- | --------- | --------------------------------- |
| `dataStore` | `address` | Address of the DataStore contract |
| `key`       | `bytes32` | Deposit request key               |

**Returns:** Raw `Deposit.Props` for the requested deposit.

### Reader.getWithdrawal

```solidity
function getWithdrawal(DataStore dataStore, bytes32 key)
    external view returns (Withdrawal.Props memory)
```

**Parameters:**

| Parameter   | Type      | Description                       |
| ----------- | --------- | --------------------------------- |
| `dataStore` | `address` | Address of the DataStore contract |
| `key`       | `bytes32` | Withdrawal request key            |

**Returns:** Raw `Withdrawal.Props` for the requested withdrawal.

### Reader.getShift

```solidity
function getShift(DataStore dataStore, bytes32 key)
    external view returns (Shift.Props memory)
```

**Parameters:**

| Parameter   | Type      | Description                       |
| ----------- | --------- | --------------------------------- |
| `dataStore` | `address` | Address of the DataStore contract |
| `key`       | `bytes32` | Shift request key                 |

**Returns:** Raw `Shift.Props` for the requested shift.

### Reader.getPosition

```solidity
function getPosition(DataStore dataStore, bytes32 key)
    external view returns (Position.Props memory)
```

**Parameters:**

| Parameter   | Type      | Description                       |
| ----------- | --------- | --------------------------------- |
| `dataStore` | `address` | Address of the DataStore contract |
| `key`       | `bytes32` | Position key                      |

**Returns:** Raw `Position.Props` for the requested position.

### Reader.getOrder

```solidity
function getOrder(DataStore dataStore, bytes32 key)
    external view returns (Order.Props memory)
```

**Parameters:**

| Parameter   | Type      | Description                       |
| ----------- | --------- | --------------------------------- |
| `dataStore` | `address` | Address of the DataStore contract |
| `key`       | `bytes32` | Order key                         |

**Returns:** Raw `Order.Props` for the requested order.

## Account and position state helpers

Use these methods when you need raw account orders, PnL diagnostics, or liquidation checks rather than the higher-level position info helpers above.

### Reader.getPositionPnlUsd

```solidity
function getPositionPnlUsd(
    DataStore dataStore,
    Market.Props memory market,
    MarketUtils.MarketPrices memory prices,
    bytes32 positionKey,
    uint256 sizeDeltaUsd
) external view returns (int256, int256, uint256)
```

**Parameters:**

| Parameter      | Type                       | Description                                        |
| -------------- | -------------------------- | -------------------------------------------------- |
| `dataStore`    | `address`                  | Address of the DataStore contract                  |
| `market`       | `Market.Props`             | Market for the position                            |
| `prices`       | `MarketUtils.MarketPrices` | Current prices for the market                      |
| `positionKey`  | `bytes32`                  | Position key                                       |
| `sizeDeltaUsd` | `uint256`                  | Position size delta to evaluate when computing PnL |

**Returns:** A tuple of `(positionPnlUsd, cappedPositionPnlUsd, sizeDeltaInTokens)`.

### Reader.isPositionLiquidatable

```solidity
function isPositionLiquidatable(
    DataStore dataStore,
    IReferralStorage referralStorage,
    bytes32 positionKey,
    Market.Props memory market,
    MarketUtils.MarketPrices memory prices,
    bool shouldValidateMinCollateralUsd,
    bool forLiquidation
) public view returns (bool, string memory, PositionUtils.IsPositionLiquidatableInfo memory)
```

**Parameters:**

| Parameter                        | Type                       | Description                                                                |
| -------------------------------- | -------------------------- | -------------------------------------------------------------------------- |
| `dataStore`                      | `address`                  | Address of the DataStore contract                                          |
| `referralStorage`                | `address`                  | Address of the ReferralStorage contract                                    |
| `positionKey`                    | `bytes32`                  | Position key                                                               |
| `market`                         | `Market.Props`             | Market for the position                                                    |
| `prices`                         | `MarketUtils.MarketPrices` | Current prices for the market                                              |
| `shouldValidateMinCollateralUsd` | `bool`                     | Whether to enforce the minimum collateral USD checks                       |
| `forLiquidation`                 | `bool`                     | Pass `true` when checking liquidation conditions, `false` for a soft check |

**Returns:** A tuple of `(isLiquidatable, reason, info)` with the liquidation result, reason string, and detailed diagnostics.

### Reader.getAccountOrders

```solidity
function getAccountOrders(
    DataStore dataStore,
    address account,
    uint256 start,
    uint256 end
) external view returns (ReaderUtils.OrderInfo[] memory)
```

**Parameters:**

| Parameter   | Type      | Description                       |
| ----------- | --------- | --------------------------------- |
| `dataStore` | `address` | Address of the DataStore contract |
| `account`   | `address` | Account to retrieve orders for    |
| `start`     | `uint256` | Start index for pagination        |
| `end`       | `uint256` | End index for pagination          |

**Returns:** Array of `ReaderUtils.OrderInfo` values for the account.

## Market state and PnL helpers

Use these methods when you need one-market diagnostics without going through the paginated market list helpers.

### Reader.getMarketInfo

```solidity
function getMarketInfo(
    DataStore dataStore,
    MarketUtils.MarketPrices memory prices,
    address marketKey
) public view returns (ReaderUtils.MarketInfo memory)
```

**Parameters:**

| Parameter   | Type                       | Description                       |
| ----------- | -------------------------- | --------------------------------- |
| `dataStore` | `address`                  | Address of the DataStore contract |
| `prices`    | `MarketUtils.MarketPrices` | Current prices for the market     |
| `marketKey` | `address`                  | Market token address              |

**Returns:** `ReaderUtils.MarketInfo` for the requested market.

### Reader.getPendingPositionImpactPoolDistributionAmount

```solidity
function getPendingPositionImpactPoolDistributionAmount(
    DataStore dataStore,
    address market
) external view returns (uint256, uint256)
```

**Parameters:**

| Parameter   | Type      | Description                       |
| ----------- | --------- | --------------------------------- |
| `dataStore` | `address` | Address of the DataStore contract |
| `market`    | `address` | Market token address              |

**Returns:** A tuple of pending long-token and short-token impact pool distribution amounts.

### Reader.getNetPnl

```solidity
function getNetPnl(
    DataStore dataStore,
    Market.Props memory market,
    Price.Props memory indexTokenPrice,
    bool maximize
) external view returns (int256)
```

**Parameters:**

| Parameter         | Type           | Description                              |
| ----------------- | -------------- | ---------------------------------------- |
| `dataStore`       | `address`      | Address of the DataStore contract        |
| `market`          | `Market.Props` | Market to evaluate                       |
| `indexTokenPrice` | `Price.Props`  | Index token price range                  |
| `maximize`        | `bool`         | Whether to use the maximizing price path |

**Returns:** Net market PnL as an `int256`.

### Reader.getPnl

```solidity
function getPnl(
    DataStore dataStore,
    Market.Props memory market,
    Price.Props memory indexTokenPrice,
    bool isLong,
    bool maximize
) external view returns (int256)
```

**Parameters:**

| Parameter         | Type           | Description                              |
| ----------------- | -------------- | ---------------------------------------- |
| `dataStore`       | `address`      | Address of the DataStore contract        |
| `market`          | `Market.Props` | Market to evaluate                       |
| `indexTokenPrice` | `Price.Props`  | Index token price range                  |
| `isLong`          | `bool`         | Pass `true` for long-side PnL            |
| `maximize`        | `bool`         | Whether to use the maximizing price path |

**Returns:** Long-side or short-side PnL as an `int256`.

### Reader.getOpenInterestWithPnl

```solidity
function getOpenInterestWithPnl(
    DataStore dataStore,
    Market.Props memory market,
    Price.Props memory indexTokenPrice,
    bool isLong,
    bool maximize
) external view returns (int256)
```

**Parameters:**

| Parameter         | Type           | Description                              |
| ----------------- | -------------- | ---------------------------------------- |
| `dataStore`       | `address`      | Address of the DataStore contract        |
| `market`          | `Market.Props` | Market to evaluate                       |
| `indexTokenPrice` | `Price.Props`  | Index token price range                  |
| `isLong`          | `bool`         | Pass `true` for long-side open interest  |
| `maximize`        | `bool`         | Whether to use the maximizing price path |

**Returns:** Open interest with PnL included, as an `int256`.

### Reader.getPnlToPoolFactor

```solidity
function getPnlToPoolFactor(
    DataStore dataStore,
    address marketAddress,
    MarketUtils.MarketPrices memory prices,
    bool isLong,
    bool maximize
) external view returns (int256)
```

**Parameters:**

| Parameter       | Type                       | Description                              |
| --------------- | -------------------------- | ---------------------------------------- |
| `dataStore`     | `address`                  | Address of the DataStore contract        |
| `marketAddress` | `address`                  | Market token address                     |
| `prices`        | `MarketUtils.MarketPrices` | Current prices for the market            |
| `isLong`        | `bool`                     | Pass `true` for the long side            |
| `maximize`      | `bool`                     | Whether to use the maximizing price path |

**Returns:** Current PnL-to-pool factor as an `int256`.

## Pricing and risk helpers

Use these methods when you need direct swap-impact or ADL diagnostics without calling the larger quote helpers.

### Reader.getSwapPriceImpact

```solidity
function getSwapPriceImpact(
    DataStore dataStore,
    address marketKey,
    address tokenIn,
    address tokenOut,
    uint256 amountIn,
    Price.Props memory tokenInPrice,
    Price.Props memory tokenOutPrice
) external view returns (int256, int256, int256)
```

**Parameters:**

| Parameter       | Type          | Description                       |
| --------------- | ------------- | --------------------------------- |
| `dataStore`     | `address`     | Address of the DataStore contract |
| `marketKey`     | `address`     | Market token address              |
| `tokenIn`       | `address`     | Input token address               |
| `tokenOut`      | `address`     | Output token address              |
| `amountIn`      | `uint256`     | Input token amount                |
| `tokenInPrice`  | `Price.Props` | Input token price range           |
| `tokenOutPrice` | `Price.Props` | Output token price range          |

**Returns:** A tuple of swap price impact values as signed integers.

### Reader.getAdlState

```solidity
function getAdlState(
    DataStore dataStore,
    address market,
    bool isLong,
    MarketUtils.MarketPrices memory prices
) external view returns (uint256, bool, int256, uint256)
```

**Parameters:**

| Parameter   | Type                       | Description                       |
| ----------- | -------------------------- | --------------------------------- |
| `dataStore` | `address`                  | Address of the DataStore contract |
| `market`    | `address`                  | Market token address              |
| `isLong`    | `bool`                     | Pass `true` for the long side     |
| `prices`    | `MarketUtils.MarketPrices` | Current prices for the market     |

**Returns:** A tuple describing the ADL state, including the ADL phase, whether ADL is enabled, the PnL factor, and the minimum PnL factor for ADL.
