For the complete documentation index, see llms.txt. This page is also available as Markdown.

Trader Interactions

Note

Set up flashClient, poolConfig, and walletKeypair as shown on the Flash SDK v2 landing page. Every snippet on this page assumes all three are in scope. Trading instructions are sent with sendAndConfirmErTransaction, signed by your walletKeypair (or a session key if you've set one up for a UI).

One-time setup

Run these once per wallet. The init/activate steps are idempotent — if the account already exists the SDK returns an empty instruction set, so they're safe to re-run; depositDirect always transfers funds.

import { BN } from '@coral-xyz/anchor'

const usdcMint = poolConfig.getTokenFromSymbol('USDC').mintKey

// 1) Deposit ledger (tracks your collateral).
await flashClient.sendAndConfirmTransaction(
  (await flashClient.initializeUserDepositLedger()).instructions,
)

// 2) Basket (holds your positions + orders).
await flashClient.sendAndConfirmTransaction(
  (await flashClient.initializeBasket()).instructions,
)

// 3) Trade vault for each collateral mint (once per mint, globally).
await flashClient.sendAndConfirmTransaction(
  (await flashClient.initTradeVault(usdcMint)).instructions,
)

// 4) Fund the deposit ledger (amount in base units).
await flashClient.sendAndConfirmTransaction(
  (await flashClient.depositDirect(usdcMint, new BN(25_000_000))).instructions, // 25 USDC
)

// 5) Activate the basket for trading.
await flashClient.sendAndConfirmTransaction(
  (await flashClient.delegateBasket(flashClient.wallet)).instructions,
)

depositDirect(tokenMint, amount, tokenProgramId?, depositor?) wraps native SOL (NATIVE_MINT) automatically and does idempotent ATA creation for SPL tokens. Pass TOKEN_2022_PROGRAM_ID as the third arg for Token-2022 mints.

Resolving the market

A position is scoped to a market — identified by a target asset, a collateral token, and a side. For a given target + side, the collateral is fixed by the market, so you look it up rather than choosing it:

Prices & slippage

Read the current oracle price and convert it to a slippage-bounded ContractOraclePrice with getPriceAfterSlippage:

Privilege accounts (Referral & Stake)

Position operations (openPosition, closePosition, increasePositionSize, decreasePositionSize) accept a trailing (privilege, referralAccount, tokenStakeAccount) triplet that determines your fee tier.

Privilege

When

Pass

Privilege.None (default)

no FAF stake, no referral

nothing — omit both accounts, trade at full fee

Privilege.Stake

trader holds an active FAF stake

trader's own tokenStakeAccount

Privilege.Referral

trader was referred

trader's own referralAccount and the referrer's tokenStakeAccount

A missing or invalid privilege account never reverts the trade — the program just charges full fee. If you don't care about discounts, omit the trailing args entirely (they default to Privilege.None).

Open a position

The 2nd and 3rd args are the market's collateral symbol — you pay collateral in the market's own token.

openPosition only opens a new position. To add to an existing position in the same market, use increasePositionSize.

Close a position

Pass an optional receivingSymbol (6th arg) to receive proceeds as a different token; omit it to receive the market's collateral.

Increase / decrease position size

Add / remove collateral

Limit orders

A limit order carries its own take-profit and stop-loss inline. Build the three prices as ContractOraclePrice from the live oracle exponent.

Edit and cancel by orderId (the order's index in your basket):

Trigger orders (Take Profit / Stop Loss)

A trigger order attaches a TP or SL to an open position. isStopLoss selects which: true = stop-loss, false = take-profit. deltaSizeAmount is how much of the position to close when it fires.

TP/SL price rules:

  • Stop Loss — above liq price & below current price for LONG; below liq price & above current price for SHORT.

  • Take Profit — above current price for LONG; below current price for SHORT.

  • Virtual tokens — take profit must be below the max-profit price for LONG.

Quotes (views)

Quote before you trade. The *Er variants read the basket-held position (pass owner for position-bound quotes). All take the poolConfig and an arg object.

Views are read-only simulations (no signing, no transaction). Poll getPositionDataEr for a live PnL / leverage / liquidation-price ticker.

Reading positions & orders

Your open positions and orders are embedded in the basket:

To stream updates, subscribe to the basket PDA with onAccountChange — see Reading accounts on the landing page.

Last updated

Was this helpful?