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

# Markets

> Market discovery and orderbook data with the Rust SDK

## Overview

The `MarketFetcher` handles market discovery, individual market lookups, user-order lookup, and orderbook retrieval. It also caches venue contract addresses used by the order-signing flow.

## Setup

```rust theme={null}
use limitless_exchange_rust_sdk::Client;

let client = Client::new()?;

// Access through the root client
let markets = client.markets.clone();
```

## Fetching active markets

Use `get_active_markets()` to retrieve a paginated list of active markets:

```rust theme={null}
use limitless_exchange_rust_sdk::{ActiveMarketsParams, ActiveMarketsSortBy, Client};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Client::new()?;

    let result = client
        .markets
        .get_active_markets(Some(&ActiveMarketsParams {
            page: Some(1),
            limit: Some(10),
            sort_by: Some(ActiveMarketsSortBy::Newest),
        }))
        .await?;

    for market in result.data {
        println!("{} {}", market.title, market.slug);
    }

    Ok(())
}
```

| Parameter | Type | Default | Description |
| - | - | - | - |
| `page` | `Option<u32>` | `None` | Page number for pagination |
| `limit` | `Option<u32>` | `None` (server default 25) | Markets per page, 1 to 25. Larger values return HTTP 400. |
| `sort_by` | `Option<ActiveMarketsSortBy>` | `None` | Sort order |

### Sort options

| Variant | Description |
| - | - |
| `ActiveMarketsSortBy::LpRewards` | Sort by LP rewards |
| `ActiveMarketsSortBy::EndingSoon` | Sort by markets ending soonest |
| `ActiveMarketsSortBy::Newest` | Sort by newest markets |
| `ActiveMarketsSortBy::HighValue` | Sort by highest value |

The API also accepts `trending`, which crate 1.1.0 does not expose. There is no `liquidity` sort; the server rejects it with HTTP 400.

## Fetching a single market

Use `get_market()` to retrieve full details for a specific market:

```rust theme={null}
let market = client
    .markets
    .get_market("btc-above-100k-march-2025")
    .await?;

println!("{}", market.title);

if let Some(tokens) = market.tokens.as_ref() {
    println!("YES token: {}", tokens.yes);
    println!("NO token: {}", tokens.no);
}

if let Some(venue) = market.venue.as_ref() {
    println!("Exchange: {}", venue.exchange);
}
```

> [!NOTE]
>
> `market.tokens` and `market.venue` are optional in the Rust models, so unwrap them only after checking they are present.

## Fetching the orderbook

Use `get_order_book()` to retrieve bids and asks for a market:

```rust theme={null}
let orderbook = client
    .markets
    .get_order_book("btc-above-100k-march-2025")
    .await?;

for bid in &orderbook.bids {
    println!("bid {} {} shares", bid.price, bid.size / 1e6);
}

for ask in &orderbook.asks {
    println!("ask {} {} shares", ask.price, ask.size / 1e6);
}

println!("midpoint {}", orderbook.adjusted_midpoint);
```

> [!NOTE]
>
> Level `size` and `min_size` are in raw 6-decimal units (1 share = 1,000,000); `min_size` is a decimal string. `price` is not scaled.

## Fetching user orders

Use `get_user_orders()` to retrieve your orders on a market. This requires authentication:

```rust theme={null}
let orders = client
    .markets
    .get_user_orders("btc-above-100k-march-2025")
    .await?;

for order in orders {
    println!("{} {:?} {:?}", order.id, order.status, order.price);
}
```

> [!WARNING]
>
> Crate 1.1.0 types `UserOrder` as the signed-order shape, but the endpoint returns matching-engine rows (`ownerId`, `marketId` as text, decimal-string `price` and `remainingSize`), so any non-empty response fails with `LimitlessError::Decode`. On 1.1.0, read the endpoint raw with `client.http.get_raw(&format!("/markets/{slug}/user-orders"), RequestOptions::default()).await?` and parse `raw.body` yourself.

You can also call it from a fetched `Market` instance:

```rust theme={null}
let market = client.markets.get_market("btc-above-100k-march-2025").await?;
let orders = market.get_user_orders().await?;
```

## Venue caching

Every call to `get_market()` caches the market venue (exchange and adapter addresses). The `OrderClient` reads from this cache when resolving the correct EIP-712 verifying contract.

**1. Fetch the market**

Calling `get_market()` retrieves and caches the venue:

```rust theme={null}
let market = client.markets.get_market("btc-above-100k-march-2025").await?;
```

**2. Check the cache**

Access the cached venue directly:

```rust theme={null}
if let Some(venue) = client.markets.get_venue("btc-above-100k-march-2025") {
    println!("exchange {}", venue.exchange);
}
```

**3. Place an order**

The `OrderClient` will use the cached venue for signing:

```rust theme={null}
let private_key = std::env::var("PRIVATE_KEY")?;
let order_client = client.new_order_client(&private_key, None)?;

let market = client.markets.get_market("btc-above-100k-march-2025").await?;
let tokens = market.tokens.as_ref().expect("market tokens");

let response = order_client
    .create_order(limitless_exchange_rust_sdk::CreateOrderParams {
        order_type: limitless_exchange_rust_sdk::OrderType::Gtc,
        market_slug: market.slug.clone(),
        args: limitless_exchange_rust_sdk::GtcOrderArgs {
            token_id: tokens.yes.clone(),
            side: limitless_exchange_rust_sdk::Side::Buy,
            price: 0.65,
            size: 10.0,
            expiration: None,
            nonce: None,
            taker: None,
            post_only: false,
        }
        .into(),
    })
    .await?;

println!("{}", response.order.id);
```

> [!WARNING]
>
> Calling `get_market()` before `create_order()` is still the recommended pattern. The Rust SDK will fetch venue data on demand if the cache is empty, but prefetching avoids an extra API roundtrip.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.