> ## Documentation Index
> Fetch the complete documentation index at: https://docs.nilemarkets.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Query Examples

> Common GraphQL query patterns for the FX Forward subgraph — open positions by account, position history with PnL, pool state, LP vault events, oracle price rounds, daily analytics, and the unified activity feed

All queries target the subgraph's GraphQL endpoint. See [Overview](/build/subgraph-overview) for endpoint URLs.

## Position Queries

### Open Positions for an Account

```graphql theme={null}
query OpenPositions($account: ID!) {
  positions(
    where: { account: $account, status: "OPEN" }
    orderBy: openTimestamp
    orderDirection: desc
    first: 25
  ) {
    id
    pairId
    side
    tenor
    notional
    entryStrike
    fixingTimestamp
    imLocked
    mmThreshold
    openTimestamp
  }
}
```

### Position History with PnL

```graphql theme={null}
query ClosedPositions($account: ID!, $first: Int!, $skip: Int!) {
  positions(
    where: { account: $account, status_not: "OPEN" }
    orderBy: closeTimestamp
    orderDirection: desc
    first: $first
    skip: $skip
  ) {
    id
    side
    tenor
    notional
    entryStrike
    status
    closeReason
    closedPrice
    realizedPnl
    marketPnl
    openTimestamp
    closeTimestamp
  }
}
```

### Single Position by ID

```graphql theme={null}
query Position($id: ID!) {
  position(id: $id) {
    id
    account { id }
    pairId
    side
    tenor
    notional
    entryStrike
    fixingTimestamp
    imLocked
    mmThreshold
    snapshotImBps
    snapshotMmBps
    status
    closeReason
    closedPrice
    realizedPnl
    marketPnl
    openTxHash
    closeTxHash
    openTimestamp
    closeTimestamp
  }
}
```

## Account Queries

### Account Overview

```graphql theme={null}
query Account($id: ID!) {
  account(id: $id) {
    collateralBalance
    imLockedTotal
    availableBalance
    positionCount
    openPositionCount
    totalRealizedPnl
    lpShares
    lpAssetsDeposited
  }
}
```

### Top Accounts by Realized PnL

```graphql theme={null}
query TopTraders {
  accounts(
    orderBy: totalRealizedPnl
    orderDirection: desc
    first: 10
    where: { positionCount_gt: 0 }
  ) {
    id
    totalRealizedPnl
    positionCount
    openPositionCount
  }
}
```

## Pool State

### Current Pool Snapshot

```graphql theme={null}
query PoolState {
  poolState(id: "pool") {
    totalAssets
    totalShares
    netExposure
    grossNotional
    sharePrice
    totalPnlApplied
    totalFeesCollected
    tradingFeesCollected
    liquidationPenaltiesCollected
    earlyTerminationFeesCollected
    maturityFeesCollected
    oracleFeesCollected
    depositCount
    withdrawCount
  }
}
```

## Vault Events

### LP Deposit/Withdraw History

```graphql theme={null}
query VaultHistory($first: Int!, $skip: Int!) {
  vaultEvents(
    orderBy: timestamp
    orderDirection: desc
    first: $first
    skip: $skip
  ) {
    id
    type
    sender
    owner
    assets
    shares
    timestamp
    txHash
  }
}
```

### Deposits Only

```graphql theme={null}
query Deposits($first: Int!) {
  vaultEvents(
    where: { type: "DEPOSIT" }
    orderBy: timestamp
    orderDirection: desc
    first: $first
  ) {
    sender
    assets
    shares
    timestamp
  }
}
```

## Oracle Queries

### Latest Forward Rounds

`OracleRound` is append-only, so there is no `isLatest` flag. Fetch a bounded recent window ordered by `publishTime` desc and dedupe by `fixingTimestamp` client-side — the first hit per key is the most recent round for that fixing.

```graphql theme={null}
query LatestForwardRounds {
  oracleRounds(
    orderBy: publishTime
    orderDirection: desc
    first: 200
  ) {
    id
    fixingTimestamp
    forwardPrice
    roundId
    publishTime
  }
}
```

### Forward Price History for a Fixing Timestamp

```graphql theme={null}
query ForwardHistory($fixingTs: BigInt!) {
  oracleRounds(
    where: { fixingTimestamp: $fixingTs }
    orderBy: roundId
    orderDirection: asc
    first: 100
  ) {
    roundId
    forwardPrice
    publishTime
  }
}
```

### Fixing Prices

```graphql theme={null}
query FixingPrices($first: Int!) {
  fixingPrices(
    orderBy: fixingTimestamp
    orderDirection: desc
    first: $first
  ) {
    pairId
    fixingTimestamp
    price
    timestamp
    txHash
  }
}
```

## Activity Feed

### Unified Pool Transactions

The `PoolTransaction` entity provides a single timeline of all position-related activity.

```graphql theme={null}
query ActivityFeed($first: Int!, $skip: Int!) {
  poolTransactions(
    orderBy: timestamp
    orderDirection: desc
    first: $first
    skip: $skip
  ) {
    id
    type
    positionId
    account
    notional
    pnl
    tradingFee
    oracleFee
    timestamp
    txHash
  }
}
```

### Activity by Account

```graphql theme={null}
query AccountActivity($account: Bytes!, $first: Int!) {
  poolTransactions(
    where: { account: $account }
    orderBy: timestamp
    orderDirection: desc
    first: $first
  ) {
    type
    positionId
    notional
    pnl
    tradingFee
    timestamp
    txHash
  }
}
```

## Analytics

### Daily Stats

```graphql theme={null}
query DailyStats($first: Int!) {
  dailyStats_collection(
    orderBy: id
    orderDirection: desc
    first: $first
  ) {
    id
    date
    positionsOpened
    positionsClosed
    totalVolume
    totalPnl
    totalFees
    forwardRounds
  }
}
```

## Protocol State

### Current Mode and Transition History

```graphql theme={null}
query ProtocolState {
  protocolState(id: "protocol") {
    currentMode
    modeTransitionCount
    oracleInvalidatedCount
    lastModeTransition {
      fromMode
      toMode
      reasonCode
      actor
      timestamp
    }
  }
}
```

```graphql theme={null}
query ModeHistory($first: Int!) {
  modeTransitions(
    orderBy: timestamp
    orderDirection: desc
    first: $first
  ) {
    fromMode
    toMode
    reasonCode
    actor
    timestamp
    txHash
  }
}
```

## Position Reductions

```graphql theme={null}
query Reductions($positionId: ID!, $first: Int!) {
  positionReductions(
    where: { position: $positionId }
    orderBy: timestamp
    orderDirection: desc
    first: $first
  ) {
    reductionNotional
    remainingNotional
    settledPnl
    fee
    timestamp
    txHash
  }
}
```

## Related Pages

<Columns cols={3}>
  <Card title="Pagination" icon="arrow-right" href="/build/subgraph-pagination">
    Cursor-based pagination and advanced filtering.
  </Card>

  <Card title="Schema Reference" icon="database" href="/build/subgraph-schema">
    Complete entity and field definitions.
  </Card>

  <Card title="Data Source Guide" icon="diagram-project" href="/build/subgraph-data-source">
    When to query the subgraph vs RPC.
  </Card>
</Columns>
