> ## 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.

# Code Examples

> TypeScript, Rust, and GraphQL code examples covering SDK setup, contract reads (pool state, positions, oracle), contract writes (open position, deposit margin), subgraph queries, and CLI usage patterns

This page provides reference implementations and example code for integrating with the Open Nile Protocol. Each section describes the available SDKs and tools with usage examples.

<Tip>
  Explore the full source code on the [GitHub](https://github.com/nil3labs) for the latest implementations and usage patterns.
</Tip>

***

## Smart Contract Integration

### TypeScript SDK

Auto-generated TypeScript bindings from Foundry contract artifacts via the wagmi CLI. Install `@nile-markets/sdk` to access pure ABI objects and constants with zero runtime dependencies on React or wagmi.

**Exports include:**

* Contract ABIs (`positionManagerAbi`, `configAbi`, `poolVaultAbi`, etc.)
* Type-safe enum constants (`Side`, `Tenor`, `Mode`, `CloseReason`, `PositionStatus`)
* Pair identifiers (`PAIR_IDS`)

```typescript theme={null}
import {
  positionManagerAbi,
  Side,
  Tenor,
  PAIR_IDS
} from "@nile-markets/sdk";
```

<Note>
  Contract addresses are loaded from deployment JSON files via the address config module, not hardcoded in the SDK.
</Note>

### Rust SDK

Auto-generated Rust bindings from Foundry artifacts. The `fx-contracts` crate provides type-safe contract interfaces that are regenerated automatically when contract artifacts change.

**Exports include:**

* Contract interfaces (`OracleModule`, `PositionManager`, `PoolVault`, etc.)
* Constants (`EUR_USD_PAIR_ID` computed at build time via Keccak256)
* Type-safe enums (`Side`, `Tenor`, `Mode`, `PositionStatus`, `CloseReason`) with `#[repr(u8)]`

```rust theme={null}
use fx_contracts::generated::oracle_module::OracleModule;
use fx_contracts::{EUR_USD_PAIR_ID, Tenor, Side};

let contract = OracleModule::new(address, &provider);
let result = contract
    .publishRound(
        EUR_USD_PAIR_ID,
        vec![pyth_vaa_bytes],
        vec![fixing_timestamp],
        vec![forward_price],
        vec![round_id],
    )
    .value(pyth_fee)
    .send()
    .await?;
```

<Info>
  Enable the `test-contracts` feature to access MockOracle, MockPyth, and MockUSDC bindings for local development and testing.
</Info>

***

## Data Indexing

### Subgraph

A Graph Protocol subgraph that indexes all protocol events. Provides GraphQL access to:

* Position lifecycle events (open, increase, close, settle, liquidate)
* LP vault events (deposits, withdrawals, share price changes)
* Oracle price updates and forward round publications
* Daily statistics and fee accumulation
* Protocol mode transitions

<Note>
  See the [Subgraph API](/subgraph-api) documentation for the full GraphQL schema and query examples.
</Note>

***

## Agent Integrations

<Info>
  Agent integration tooling is under active development for M2. The following components provide programmatic access to the protocol for AI agents and automated systems.
</Info>

### MCP Server

Model Context Protocol server enabling AI agents to interact with the protocol. Provides structured tools for reading protocol state, querying positions, and executing trades.

### x402 API

Pay-per-request REST API using the x402 payment protocol. Allows agents to access protocol data and execute operations with per-call USDC micropayments.

### CLI Tool

Command-line interface for protocol interaction. Supports position management, LP operations, oracle queries, and administrative functions from the terminal.

<Tip>
  See the [Agent Integrations](/ai-agents) section for detailed integration guides and API references.
</Tip>
