# Developer Guide

### 1. Environment Setup

Prerequisites:

* Docker: For containerized testing.
* Node.js & Yarn: For the Flux SDK.
* Solana CLI: For network interaction.
* Rust/Anchor: Familiarity with Solana program structures is recommended.

Initialize Project:

```
codeBash
npx create-flux-app my-protocol-index
cd my-protocol-index
code .
```

### 2. Project Structure

Flux projects follow a rigid directory structure to ensure deterministic compilation.

```
codeText
├── flux.toml               # Main Manifest
├── schema.graphql          # Data Schema
├── src
│   ├── mappings            # Logic Handlers
│   │   └── raydium.ts
│   └── idls                # Anchor IDLs (JSON)
│       └── raydium_amm.json
└── package.json
```

### 3. Creating a Manifest

We will index the Raydium Liquidity Pool (AMM) on Solana Mainnet.

```
codeToml
[package]
name = "raydium-amm-index"
version = "1.0.0"

[network]
chain = "solana-mainnet"
transport = "geyser" # Flux uses Geyser plugins for low-latency

[sources]
  [sources.raydium]
  program_id = "675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8"
  start_slot = 100_000_000
  idl = "./src/idls/raydium_amm.json"
```

### 4. Defining the Schema

Define the entities you wish to query. Flux uses GraphQL schemas to generate the underlying database tables.

```
codeGraphql
type User @entity {
  id: ID! # User Wallet Address (Base58)
  positions: [LiquidityPosition!]! @derivedFrom(field: "user")
}

type LiquidityPool @entity {
  id: ID! # Pool Address (PDA)
  tokenA: String! # Mint Address
  tokenB: String! # Mint Address
  reserveA: BigInt!
  reserveB: BigInt!
}

type LiquidityPosition @entity {
  id: ID! # Composite: Wallet + Pool Address
  user: User!
  pool: LiquidityPool!
  lpTokenBalance: BigInt!
  updatedAtSlot: BigInt!
}
```

### 5. Implementing Logic

Flux SDK allows you to map Solana Instructions (decoded via Anchor IDL) to your Schema.

```
codeTypeScript
import { InitializePool, Deposit } from '../generated/raydium';
import { LiquidityPool, LiquidityPosition, User } from '../generated/schema';
import { Flux, Solana } from '@flux-network/sdk';

// Handle Pool Creation
export function handleInitializePool(ix: InitializePool, ctx: Solana.Context): void {
  let pool = new LiquidityPool(ctx.accounts.poolAddress.toBase58());
  
  pool.tokenA = ctx.accounts.tokenAMint.toBase58();
  pool.tokenB = ctx.accounts.tokenBMint.toBase58();
  pool.reserveA = BigInt(0);
  pool.reserveB = BigInt(0);
  
  pool.save();
}

// Handle Liquidity Deposit
export function handleDeposit(ix: Deposit, ctx: Solana.Context): void {
  const userAddr = ctx.accounts.userSourceOwner.toBase58();
  const poolAddr = ctx.accounts.pool.toBase58();
  const positionId = `${userAddr}-${poolAddr}`;

  // Ensure User Entity Exists
  let user = User.load(userAddr);
  if (user == null) {
    user = new User(userAddr);
    user.save();
  }

  // Update Position
  let position = LiquidityPosition.load(positionId);
  if (position == null) {
    position = new LiquidityPosition(positionId);
    position.user = userAddr;
    position.pool = poolAddr;
    position.lpTokenBalance = BigInt(0);
  }

  // Add LP tokens minted to user
  position.lpTokenBalance = position.lpTokenBalance.plus(ix.data.lpAmount);
  position.updatedAtSlot = ctx.slot;
  
  position.save();
}
```

### 6. Build & Test

Flux provides a local Dockerized sandbox that simulates the network environment.

```
codeBash
# Compile the Projection
yarn build

# Spin up local Flux Engine (Indexer + Gateway)
docker compose up -d

# Deploy to local sandbox
yarn deploy:local
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.fluxtoken.vip/developer-guide/developer-guide.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
