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

# Short Positions

> Initialize Option Series, mint Shorts with or without OMLP leverage, manage loans, and unwind Unsold Size.

A Writer opens a Short by ensuring the Option Series exists and minting contracts into the `OptionPool` against collateral in the Collateral Mint.

## Opening a Short

**1. Check Which Strikes Need Initialization**

Derive per-strike addresses with `deriveOptionSeriesLegAddresses`, then pass them to `findLegsNeedingSeriesInit`. It returns strikes that are missing an `OptionAccount` or a `WriterPosition` for the maker.

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

const strikes = [150, 160];
const legAddresses = await Promise.all(
  strikes.map((strikePrice) =>
    deriveOptionSeriesLegAddresses({
      underlyingAsset: UNDERLYING_MINT,
      optionType: OptionType.Call,
      strikePrice,
      expirationDate: 1_767_225_600n,
      collateralMint: COLLATERAL_MINT,
      maker: writerSigner.address,
    })
  )
);

const needingInit = await findLegsNeedingSeriesInit(rpc, legAddresses);

for (
  let i = 0;
  i < needingInit.length;
  i += MAX_SERIES_INITS_PER_TRANSACTION
) {
  const batch = needingInit.slice(i, i + MAX_SERIES_INITS_PER_TRANSACTION);
  const instructions = await Promise.all(
    batch.map((leg) =>
      buildInitOptionSeriesInstruction({
        underlyingAsset: UNDERLYING_MINT,
        underlyingMint: UNDERLYING_MINT,
        collateralMint: COLLATERAL_MINT,
        optionType: OptionType.Call,
        strikePrice: leg.strikePrice,
        expirationDate: 1_767_225_600n,
        maker: writerSigner.address,
        payer: writerSigner, // may differ from maker
      })
    )
  );

  await sendBuiltTransaction({
    instructions,
    rpc,
    rpcSubscriptions,
    feePayer: writerSigner,
    version: 1,
  });
}
```

**2. Mint Contracts**

Call `buildOptionMintMultiTransactionWithDerivation` to mint up to `MAX_MINT_LEGS` (4) legs per transaction. Send each chunk sequentially with `version: 1`.

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

const result = await buildOptionMintMultiTransactionWithDerivation({
  underlyingAsset: UNDERLYING_MINT,
  underlyingMint: UNDERLYING_MINT,
  collateralMint: COLLATERAL_MINT,
  optionType: OptionType.Call,
  expirationDate: 1_767_225_600n,
  maker: writerSigner,
  priceUpdateAccount,
  rpc,
  legs: [
    {
      strikePrice: 150,
      quantity: contractsToBaseUnits(1),
      makerCollateralAmount: collateralBaseUnits,
      borrowedAmount: 0n,
      maxRequiredCollateralAmount: collateralBaseUnits,
    },
  ],
});

for (const chunk of result.chunks) {
  await sendBuiltTransaction({
    ...chunk,
    rpc,
    rpcSubscriptions,
    feePayer: writerSigner,
    version: 1,
  });
}
```

> [!TIP]
>
> If a 4-leg mint hits the compute limit during simulation, retry with a smaller `maxLegsPerTransaction` (`3`, `2`, or `1`) before prompting the user to sign again.

## Leveraged Minting and OMLP Loans

Writers can borrow from the Collateral Mint's OMLP vault to collateralize a Short:

* **Fused borrow and mint**: `buildLeveragedOptionMintMultiWithBorrowTransaction` borrows from OMLP and mints in one instruction, up to `MAX_LEVERAGED_MINT_LEGS` (4) legs per transaction.
* **Standalone borrow**: `buildBorrowFromPoolInstruction` borrows OMLP liquidity into a `WriterPosition` and transfers the maker's own collateral into the shared escrow in the same instruction. `PoolLoan` (`version = 3`, 229 bytes onchain) tracks `escrowedPrincipal` and `escrowedCollateral` explicitly per loan.
* **Opening margin requirement**: Every minted leg must pass the program's conservative post-fill opening margin check (`OpeningMarginNotMet`, `6171`). Call `computeMinimumOpeningOwnCollateralBaseUnits` before submitting a leveraged mint to size `makerCollateralAmount` and `borrowedAmount`.
* **Repaying a loan**: Use `buildRepayPoolLoanFromWalletInstruction` to repay from the maker's wallet, `buildRepayPoolLoanInstruction` to repay from escrow, or `buildRepayPoolLoanFromCollateralInstruction` to repay from the series collateral vault.

> [!NOTE]
>
> Multi-leg net margin (`init_strategy_margin`) is refused on the current program with `StrategyMarginDisabled` (`6168`). Post collateral per leg.

## Unwinding Unsold Size

A Writer can unwind only **Unsold Size**. Sold contracts cannot be unwound until Longs close them back to the pool.

Always run `preflightUnwindWriterUnsold` first to compute `effectiveUnwindQty` and inspect active OMLP loans. Then build the unwind with automatic loan repayment:

> [tab] Single Leg
>
> ```ts theme={"dark"}
> import {
>   buildUnwindWriterUnsoldWithLoanRepayment,
>   contractsToBaseUnits,
>   OptionType,
>   preflightUnwindWriterUnsold,
>   sendBuiltTransaction,
> } from "@epicentral/sos-sdk";
> import { rpc, rpcSubscriptions, UNDERLYING_MINT } from "./config";
>
> const preflight = await preflightUnwindWriterUnsold({
>   underlyingAsset: UNDERLYING_MINT,
>   optionType: OptionType.Call,
>   strikePrice: 150,
>   expirationDate: 1_767_225_600n,
>   writer: writerSigner.address,
>   unwindQty: contractsToBaseUnits(1),
>   rpc,
> });
>
> if (!preflight.canUnwind || preflight.effectiveUnwindQty === 0n) {
>   throw new Error(preflight.reason ?? "No unwindable size available.");
> }
>
> const built = await buildUnwindWriterUnsoldWithLoanRepayment({
>   underlyingAsset: UNDERLYING_MINT,
>   optionType: OptionType.Call,
>   strikePrice: 150,
>   expirationDate: 1_767_225_600n,
>   writer: writerSigner.address,
>   unwindQty: preflight.effectiveUnwindQty,
>   rpc,
> });
>
> const signature = await sendBuiltTransaction({
>   ...built,
>   rpc,
>   rpcSubscriptions,
>   feePayer: writerSigner,
>   version: 1,
> });
> ```

> [tab] Multi-Leg (Up to 4)
>
> Use `buildUnwindWriterUnsoldMultiWithLoanRepayment` when unwinding up to 4 legs that share the same underlying asset and Collateral Mint:
>
> ```ts theme={"dark"}
> import {
>   buildUnwindWriterUnsoldMultiWithLoanRepayment,
>   contractsToBaseUnits,
>   OptionType,
>   sendBuiltTransaction,
> } from "@epicentral/sos-sdk";
> import { rpc, rpcSubscriptions, UNDERLYING_MINT } from "./config";
>
> const built = await buildUnwindWriterUnsoldMultiWithLoanRepayment({
>   legs: [
>     {
>       underlyingAsset: UNDERLYING_MINT,
>       optionType: OptionType.Call,
>       strikePrice: 150,
>       expirationDate: 1_767_225_600n,
>       unwindQty: contractsToBaseUnits(1),
>     },
>     {
>       underlyingAsset: UNDERLYING_MINT,
>       optionType: OptionType.Call,
>       strikePrice: 160,
>       expirationDate: 1_767_225_600n,
>       unwindQty: contractsToBaseUnits(1),
>     },
>   ],
>   writer: writerSigner,
>   rpc,
> });
>
> const signature = await sendBuiltTransaction({
>   ...built,
>   rpc,
>   rpcSubscriptions,
>   feePayer: writerSigner,
>   version: 1,
> });
> ```


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