# Flash SDK

### Introduction

Flash.trade SDK written in TypeScript provides the easiest way for developers, partners, and others to integrate with flash.trade. This page will walk you through setting up the Flash client, creating transactions, and executing them.

{% hint style="info" %}

#### **v15.16+ migration notes**

* Prices now come from the [Pyth Lazer](https://pyth.network/lazer) HTTP proxy. The old `@pythnetwork/client` dependency is no longer needed.
* Privilege accounts (Referral / Stake) are resolved automatically per-trade — see [Trader Interactions → Privilege accounts](/flash-trade/flash-trade-protocol/build-on-flash/flash-sdk/trader-interactions.md#privilege-accounts-referral-and-stake).
* A single `POOL_CONFIGS` array replaces hardcoded `Crypto.1` selection.
  {% endhint %}

### Install the Flash SDK

```bash
npm install flash-sdk

yarn add flash-sdk
```

### Setting up the Flash SDK

**Mainnet pools** (load every pool you might trade against — the SDK auto-routes to the right one per trade):

| Pool name      | a.k.a. | Notable markets             |
| -------------- | ------ | --------------------------- |
| `Crypto.1`     | flp.1  | BTC, ETH, SOL, ZEC, JitoSOL |
| `Virtual.1`    | flp.2  | Virtuals                    |
| `Governance.1` | flp.3  | HYPE, JUP, JTO              |
| `Community.1`  | flp.4  | BONK, PENGU, PUMP           |
| `Community.2`  | flp.5  | WIF                         |
| `Trump.1`      | flp.7  | FARTCOIN                    |
| `Ore.1`        | flp.8  | ORE                         |
| `Remora.1`     | flp.r  | Remora                      |
| `Equity.1`     | flp.x  | SPY and other equities      |

**Devnet pools:** `devnet.1` through `devnet.5`.

Pool definitions live at `node_modules/flash-sdk/dist/PoolConfig.json`.

#### Load every pool up front

All mainnet pools share the same `programId`, `perpComposibilityProgramId`, `fbNftRewardProgramId`, and `rewardDistributionProgram.programId`. One `PerpetualsClient` instance therefore serves every pool — the per-trade `poolConfig` argument is what scopes each instruction.

```ts
import { AnchorProvider, BN } from "@coral-xyz/anchor"
import { PerpetualsClient, PoolConfig } from 'flash-sdk'

const RPC_URL = process.env.RPC_URL

// Configure the AnchorProvider
const provider: AnchorProvider = AnchorProvider.local(RPC_URL, {
    commitment: 'processed',
    preflightCommitment: 'processed',
    skipPreflight: true,
})

// Load every mainnet pool — the SDK auto-routes per trade
export const MAINNET_POOL_NAMES = [
    'Crypto.1',     // flp.1
    'Virtual.1',    // flp.2
    'Governance.1', // flp.3
    'Community.1',  // flp.4
    'Community.2',  // flp.5
    'Trump.1',      // flp.7
    'Ore.1',        // flp.8
    'Remora.1',     // flp.r
    'Equity.1',     // equities (SPY, etc.)
] as const

export const POOL_CONFIGS: PoolConfig[] = MAINNET_POOL_NAMES.map((n) =>
    PoolConfig.fromIdsByName(n, 'mainnet-beta')
)

export const PROGRAM_ID = POOL_CONFIGS[0].programId

// Setup the Flash client — all mainnet pools share the same program IDs
export const flashClient = new PerpetualsClient(
    provider,
    POOL_CONFIGS[0].programId,
    POOL_CONFIGS[0].perpComposibilityProgramId,
    POOL_CONFIGS[0].fbNftRewardProgramId,
    POOL_CONFIGS[0].rewardDistributionProgram.programId,
    {
        prioritizationFee: 0, // can be set dynamically via flashClient.setPrioritizationFee
    }
)
```

#### Picking the right pool per trade

Don't hardcode a single pool — use these two helpers to look up the right one from a token + side, or from a position's market account.

```ts
import { isVariant, PoolConfig, Side } from 'flash-sdk'
import { PublicKey } from '@solana/web3.js'

/** Find the pool that has a market for `targetSymbol` on the given `side`. */
export const getRequiredPoolConfig = (targetSymbol: string, side: Side): PoolConfig => {
    for (const config of POOL_CONFIGS) {
        const targetToken = config.tokens.find((t) => t.symbol === targetSymbol)
        if (!targetToken) continue
        const market = config.markets.find(
            (m) =>
                m.targetMint.equals(targetToken.mintKey) &&
                isVariant(m.side, 'long') === isVariant(side, 'long')
        )
        if (market) return config
    }
    throw new Error(`No pool found with a ${isVariant(side, 'long') ? 'long' : 'short'} market for ${targetSymbol}`)
}

/** Find the pool that owns a given position's market account. Useful when closing or modifying positions. */
export const getPoolConfigForMarket = (marketPk: PublicKey): PoolConfig => {
    for (const config of POOL_CONFIGS) {
        if (
            config.markets.find((m) => m.marketAccount.equals(marketPk)) ||
            config.marketsDeprecated.find((m) => m.marketAccount.equals(marketPk))
        ) {
            return config
        }
    }
    throw new Error(`No pool found containing market ${marketPk.toBase58()}`)
}
```

SOL/BTC routes to Crypto.1, SPY to Equity.1, FARTCOIN to Trump.1, ORE to Ore.1, and so on. `getPoolConfigForMarket` also checks `marketsDeprecated`, so positions on retired markets still resolve cleanly.

#### Required environment variables

```env
RPC_URL=https://your-mainnet-rpc.example.com
ANCHOR_WALLET=./localPublicKey.json
# Optional — defaults to https://pyth-lazer-proxy-3.dourolabs.app
# LAZER_PROXY_URL=https://your-own-lazer-proxy.example.com
```

`ANCHOR_WALLET` points at a keypair JSON consumed by `AnchorProvider.local`. The wallet doesn't need to sign anything for read-only queries (its pubkey is used for view-function simulations), but it must hold some SOL to pay rent and fees if you intend to send transactions.

### Detailed Example

Checkout the public repo [here](https://github.com/flash-trade/flash-trade-sdk/tree/main/examples).


---

# 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.flash.trade/flash-trade/flash-trade-protocol/build-on-flash/flash-sdk.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.
