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

# Tranching SDK

> Get started with the Exponent Tranching SDK

Set up the Exponent SDK and walk through the main tranching lifecycle - loading a market, depositing into Senior or Junior, withdrawing, and reading market state.

## Installation

<CodeGroup>
  ```bash yarn theme={null}
  yarn add @exponent-labs/exponent-sdk
  ```

  ```bash npm theme={null}
  npm install @exponent-labs/exponent-sdk
  ```

  ```bash pnpm theme={null}
  pnpm add @exponent-labs/exponent-sdk
  ```
</CodeGroup>

## Setup

```typescript theme={null}
import {
  LOCAL_ENV,
  TranchingMarket,
} from "@exponent-labs/exponent-sdk";
import { TrancheSide } from "@exponent-labs/exponent-sdk/client/tranching";
import {
  Connection,
  Keypair,
  PublicKey,
  Transaction,
  sendAndConfirmTransaction,
} from "@solana/web3.js";

const connection = new Connection("https://api.mainnet-beta.solana.com");
const wallet = Keypair.fromSecretKey(/* your keypair */);
const marketAddress = new PublicKey("...");

// Loads onchain market state, return model storage, SY linkage, and tranche LP mints.
const market = await TranchingMarket.load(connection, marketAddress, LOCAL_ENV);
```

<Note>
  Amount inputs use raw token units. `amountBase` uses the base token's smallest units, `amountIn` uses SY token units, and LP amounts use tranche LP token units. APY inputs use decimal rates, so `0.10` means 10%.
</Note>

<Steps>
  <Step title="Choose a tranche">
    Use `TrancheSide.Senior` for protected exposure and `TrancheSide.Junior` for first-loss exposure with higher expected return.

    ```typescript theme={null}
    const trancheSide = TrancheSide.Senior;
    ```
  </Step>

  <Step title="Deposit base assets">
    Use `ixWrapperDeposit` to deposit the market's base asset. The wrapper mints SY first, deposits SY into the selected tranche, and mints Senior or Junior LP shares.

    ```typescript theme={null}
    const { ixs, setupIxs } = await market.ixWrapperDeposit({
      user: wallet.publicKey,
      trancheSide: TrancheSide.Senior,
      amountBase: 1_000_000_000n, // raw base-token units; for 9 decimals, this is 1 token
      minLpOut: 0n,               // raw Senior LP units; set above 0 for output protection
    });

    const tx = new Transaction().add(...setupIxs, ...ixs);
    await sendAndConfirmTransaction(connection, tx, [wallet]);
    ```

    <Tip>
      Set `minLpOut` to protect against stale exchange-rate or NAV assumptions. The transaction reverts if LP output is below that value.
    </Tip>
  </Step>

  <Step title="Deposit into Junior">
    Junior deposits use the same wrapper with `TrancheSide.Junior`.

    ```typescript theme={null}
    const { ixs, setupIxs } = await market.ixWrapperDeposit({
      user: wallet.publicKey,
      trancheSide: TrancheSide.Junior,
      amountBase: 1_000_000_000n, // raw base-token units
      minLpOut: 0n,               // raw Junior LP units
    });

    const tx = new Transaction().add(...setupIxs, ...ixs);
    await sendAndConfirmTransaction(connection, tx, [wallet]);
    ```
  </Step>

  <Step title="Withdraw to base assets">
    Use `ixWrapperWithdraw` to burn tranche LP shares, withdraw SY from the market, and redeem SY back to the base asset.

    ```typescript theme={null}
    const { ixs, setupIxs } = await market.ixWrapperWithdraw({
      user: wallet.publicKey,
      trancheSide: TrancheSide.Senior,
      lpAmountIn: 500_000_000n, // raw Senior LP units to burn
      minBaseOut: 0n,           // raw base-token units; set above 0 for output protection
    });

    const tx = new Transaction().add(...setupIxs, ...ixs);
    await sendAndConfirmTransaction(connection, tx, [wallet]);
    ```

    <Warning>
      Senior withdrawals are only allowed while the market is `Active`. During `FixedTermRecovery`, Senior withdrawals are paused.
    </Warning>
  </Step>

  <Step title="Read market state">
    Use read helpers to inspect current NAV, LP prices, capacity, APY, and Senior protection before sending transactions.

    ```typescript theme={null}
    console.log("Market size:", market.getMarketSize());
    console.log("Effective market size:", market.getEffectiveMarketSize());
    console.log("Senior LP price:", market.getSrLpPriceNetAsset());
    console.log("Junior LP price:", market.getJrLpPriceNetAsset());
    console.log("Target coverage:", market.getTargetCoverageRatio());

    const seniorApy = market.calculateSeniorApy(0.10); // 0.10 = 10% underlying APY
    const juniorApy = market.calculateJuniorApy(0.10);

    console.log("Senior APY:", seniorApy);
    console.log("Junior APY:", juniorApy);
    ```
  </Step>
</Steps>

## SY-only instructions

If you already hold SY, use `ixDeposit` and `ixWithdraw` instead of the wrappers. These methods skip base asset minting and redemption.

```typescript theme={null}
const ix = market.ixDeposit({
  user: wallet.publicKey,
  trancheSide: TrancheSide.Senior,
  amountIn: 1_000_000_000n, // raw SY units
  minLpOut: 0n,             // raw Senior LP units
  tokenSrc: userSyTokenAccount,
  tokenLpDst: userSeniorLpTokenAccount,
});
```

## Next Steps

<CardGroup cols={2}>
  <Card href="/developer-tranching/concepts" title="Technical Concepts">
    Understand coverage, utilization, return curves, recovery periods, and LP pricing.
  </Card>

  <Card href="/developer-tranching/typescript/instructions/overview" title="TypeScript Instructions">
    Explore high-level deposit and withdraw builders.
  </Card>

  <Card href="/developer-tranching/typescript/read-functions/overview" title="Read Functions">
    Query NAV, capacity, APY, and Senior protection without submitting transactions.
  </Card>

  <Card href="/developer-tranching/account-references/exponent-tranching-market" title="Account References">
    Inspect the market account, return model storage, risk config, and fee config.
  </Card>
</CardGroup>
