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

# Long Positions

> Check liquidity, open single or multi-leg Longs, and close Long positions back to the pool.

A Long buys option contracts from an `OptionPool` and can sell them back to the pool before expiration. All Long trade builders require a fresh shard-0 Pyth price feed account as `priceUpdateAccount`. See [Pricing and Oracles](https://docs.epicentral.markets/sos-sdk/pricing-and-oracles.md) for how to resolve it.

## Opening a Long

**1. Check Pool Liquidity**

Call `preflightBuyFromPoolMarketOrder` before building the transaction. It checks that both `OptionPool.totalAvailable` and active Writers' Unsold Size can fill your quantity.

```ts theme={"dark"}
import {
  contractsToBaseUnits,
  OptionType,
  preflightBuyFromPoolMarketOrder,
} from "@epicentral/sos-sdk";
import { rpc, UNDERLYING_MINT } from "./config";

const quantity = contractsToBaseUnits(1);

const check = await preflightBuyFromPoolMarketOrder({
  underlyingAsset: UNDERLYING_MINT,
  optionType: OptionType.Call,
  strikePrice: 150,
  expirationDate: 1_767_225_600n,
  quantity,
  rpc,
});

if (!check.canBuy) {
  throw new Error(check.reason ?? "Insufficient pool liquidity.");
}
```

**2. Build and Send the Market Order**

Use `buildBuyFromPoolMarketOrderTransactionWithDerivation` to derive all accounts, attach active Writer remaining accounts, and enforce your `quotedPremiumTotal` plus slippage buffer. Send the result as a version 1 transaction.

```ts theme={"dark"}
import {
  buildBuyFromPoolMarketOrderTransactionWithDerivation,
  OptionType,
  sendBuiltTransaction,
} from "@epicentral/sos-sdk";
import { rpc, rpcSubscriptions, UNDERLYING_MINT } from "./config";

const built = await buildBuyFromPoolMarketOrderTransactionWithDerivation({
  underlyingAsset: UNDERLYING_MINT,
  optionType: OptionType.Call,
  strikePrice: 150,
  expirationDate: 1_767_225_600n,
  buyer: buyerSigner.address,
  quantity,
  quotedPremiumTotal, // payment-mint base units
  slippageBufferBaseUnits: 500_000n,
  priceUpdateAccount,
  rpc,
});

const signature = await sendBuiltTransaction({
  ...built,
  rpc,
  rpcSubscriptions,
  feePayer: buyerSigner,
  version: 1,
});
```

### Multi-Leg Long Buy

Use `buildBuyFromPoolMultiFromIdentities` to open up to 4 Long legs in one instruction when all legs share the same `underlyingAsset` and payment mint:

```ts theme={"dark"}
import {
  buildBuyFromPoolMultiFromIdentities,
  contractsToBaseUnits,
  OptionType,
  sendBuiltTransaction,
} from "@epicentral/sos-sdk";
import { rpc, rpcSubscriptions, UNDERLYING_MINT } from "./config";

const built = await buildBuyFromPoolMultiFromIdentities({
  legs: [
    {
      underlyingAsset: UNDERLYING_MINT,
      optionType: OptionType.Call,
      strikePrice: 150,
      expirationDate: 1_767_225_600n,
      quantity: contractsToBaseUnits(1),
      quotedPremiumTotal: legOnePremiumBaseUnits,
    },
    {
      underlyingAsset: UNDERLYING_MINT,
      optionType: OptionType.Call,
      strikePrice: 160,
      expirationDate: 1_767_225_600n,
      quantity: contractsToBaseUnits(1),
      quotedPremiumTotal: legTwoPremiumBaseUnits,
    },
  ],
  buyer: buyerSigner.address,
  priceUpdateAccount,
  rpc,
});

const signature = await sendBuiltTransaction({
  ...built,
  rpc,
  rpcSubscriptions,
  feePayer: buyerSigner,
  version: 1,
});
```

## Closing a Long

Check `preflightCloseLongToPool` before closing to verify that active Writers' sold quantity matches the pool.

> [tab] Single Leg
>
> ```ts theme={"dark"}
> import {
>   buildCloseLongToPoolTransactionWithDerivation,
>   contractsToBaseUnits,
>   OptionType,
>   preflightCloseLongToPool,
>   sendBuiltTransaction,
> } from "@epicentral/sos-sdk";
> import { rpc, rpcSubscriptions, UNDERLYING_MINT } from "./config";
>
> const quantity = contractsToBaseUnits(1);
>
> const check = await preflightCloseLongToPool({
>   underlyingAsset: UNDERLYING_MINT,
>   optionType: OptionType.Call,
>   strikePrice: 150,
>   expirationDate: 1_767_225_600n,
>   quantity,
>   rpc,
> });
>
> if (!check.canClose) {
>   throw new Error(check.reason ?? "Cannot close position.");
> }
>
> const built = await buildCloseLongToPoolTransactionWithDerivation({
>   underlyingAsset: UNDERLYING_MINT,
>   optionType: OptionType.Call,
>   strikePrice: 150,
>   expirationDate: 1_767_225_600n,
>   buyer: buyerSigner.address,
>   buyerLongAccount,
>   quantity,
>   minPayoutAmount, // collateral-mint base units
>   priceUpdateAccount,
>   closeLongTokenAccount: true,
>   rpc,
> });
>
> const signature = await sendBuiltTransaction({
>   ...built,
>   rpc,
>   rpcSubscriptions,
>   feePayer: buyerSigner,
>   version: 1,
> });
> ```

> [tab] Multi-Leg (Up to 4)
>
> All legs must share the same `underlyingAsset` and Collateral Mint. Unlike the single-leg builder, `buildCloseLongToPoolMultiFromIdentities` does not close the buyer's LONG token accounts automatically.
>
> ```ts theme={"dark"}
> import {
>   buildCloseLongToPoolMultiFromIdentities,
>   contractsToBaseUnits,
>   OptionType,
>   sendBuiltTransaction,
> } from "@epicentral/sos-sdk";
> import { rpc, rpcSubscriptions, UNDERLYING_MINT } from "./config";
>
> const built = await buildCloseLongToPoolMultiFromIdentities({
>   legs: [
>     {
>       underlyingAsset: UNDERLYING_MINT,
>       optionType: OptionType.Call,
>       strikePrice: 150,
>       expirationDate: 1_767_225_600n,
>       quantity: contractsToBaseUnits(1),
>       minPayoutAmount: legOneMinPayout,
>     },
>     {
>       underlyingAsset: UNDERLYING_MINT,
>       optionType: OptionType.Call,
>       strikePrice: 160,
>       expirationDate: 1_767_225_600n,
>       quantity: contractsToBaseUnits(1),
>       minPayoutAmount: legTwoMinPayout,
>     },
>   ],
>   buyer: buyerSigner.address,
>   priceUpdateAccount,
>   rpc,
> });
>
> const signature = await sendBuiltTransaction({
>   ...built,
>   rpc,
>   rpcSubscriptions,
>   feePayer: buyerSigner,
>   version: 1,
> });
> ```

> [!WARNING]
>
> `buildCloseLongToPoolTransactionWithDerivation` appends a `CloseAccount` instruction by default to reclaim the buyer's LONG token account rent. On a **partial close**, always pass `closeLongTokenAccount: false` so the account stays open for the remaining contracts.


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