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

# Selling Shares

> Selling outcome shares back to the market.

Sell your shares back to the pool to realize profits or cut losses.

***

## Basic Sell

> [tab] TypeScript
>
> ```typescript theme={"dark"}
> import { BN } from "@coral-xyz/anchor";
> import { Transaction } from "@solana/web3.js";
>
> const sharesToSell = new BN(5000); // Number of shares
> const outcomeIndex = 0;
>
> const instructions = await sdk.orica.buildSell(
>   wallet.publicKey,
>   userUsdcAccount,
>   marketAddress,
>   sharesToSell,
>   outcomeIndex
> );
>
> const tx = new Transaction().add(...instructions);
> const sig = await sendAndConfirmTransaction(connection, tx, [wallet]);
> console.log("Sell transaction:", sig);
> ```

> [tab] Python
>
> ```python theme={"dark"}
> from solana.transaction import Transaction
>
> shares_to_sell = 5000  # Number of shares
> outcome_index = 0
>
> instructions = await sdk.orica.build_sell(
>     user=wallet.pubkey(),
>     user_usdc=user_usdc_account,
>     market_address=market_address,
>     shares=shares_to_sell,
>     outcome_index=outcome_index,
> )
>
> tx = Transaction()
> tx.add(*instructions)
> sig = await connection.send_transaction(tx, wallet)
> print(f"Sell transaction: {sig}")
> ```

***

## Check Available Shares First

> [tab] TypeScript
>
> ```typescript theme={"dark"}
> const position = await sdk.orica.fetchPosition(marketAddress, wallet.publicKey);
>
> if (!position) {
>   console.log("No position in this market");
>   return;
> }
>
> const availableShares = position.outcomeAmounts[outcomeIndex];
> console.log("Available to sell:", availableShares.toString());
>
> if (sharesToSell.gt(availableShares)) {
>   console.error("Insufficient shares");
>   return;
> }
> ```

> [tab] Python
>
> ```python theme={"dark"}
> position = await sdk.orica.fetch_position(market_address, wallet.pubkey())
>
> if not position:
>     print("No position in this market")
>     return
>
> available_shares = position.outcome_amounts[outcome_index]
> print(f"Available to sell: {available_shares}")
>
> if shares_to_sell > available_shares:
>     print("Insufficient shares")
>     return
> ```

***

## Sell All Shares

> [tab] TypeScript
>
> ```typescript theme={"dark"}
> const position = await sdk.orica.fetchPosition(marketAddress, wallet.publicKey);
>
> if (position) {
>   const allShares = position.outcomeAmounts[outcomeIndex];
>
>   const instructions = await sdk.orica.buildSell(
>     wallet.publicKey,
>     userUsdcAccount,
>     marketAddress,
>     allShares,
>     outcomeIndex
>   );
>
>   const tx = new Transaction().add(...instructions);
>   await sendAndConfirmTransaction(connection, tx, [wallet]);
> }
> ```

> [tab] Python
>
> ```python theme={"dark"}
> position = await sdk.orica.fetch_position(market_address, wallet.pubkey())
>
> if position:
>     all_shares = position.outcome_amounts[outcome_index]
>
>     instructions = await sdk.orica.build_sell(
>         user=wallet.pubkey(),
>         user_usdc=user_usdc_account,
>         market_address=market_address,
>         shares=all_shares,
>         outcome_index=outcome_index,
>     )
>
>     tx = Transaction()
>     tx.add(*instructions)
>     await connection.send_transaction(tx, wallet)
> ```

***

## Understanding the Flow

**1. Specify Shares**

You specify how many shares to sell.

**2. Price Calculation**

The bonding curve determines the USDC payout.

**3. Fee Deduction**

Trading fees (1.70%) are deducted from the payout.

**4. Share Burning**

Shares are removed from your position.

**5. USDC Transfer**

You receive USDC in your token account.

***

## Price Impact

> [!WARNING]
>
> Selling large amounts will move the price down. The more you sell, the lower the average price per share.

> [tab] TypeScript
>
> ```typescript theme={"dark"}
> // Check price impact before selling
> const market = await sdk.orica.fetchMarket(marketAddress);
> const prices = sdk.orica.calculatePrices(market);
> const currentPrice = prices[outcomeIndex];
>
> console.log(`Current price: $${currentPrice.toFixed(4)}`);
> console.log(`Selling ${sharesToSell.toString()} shares will decrease the price`);
> ```

> [tab] Python
>
> ```python theme={"dark"}
> # Check price impact before selling
> market = await sdk.orica.fetch_market(market_address)
> prices = sdk.orica.calculate_prices(market)
> current_price = prices[outcome_index]
>
> print(f"Current price: ${current_price:.4f}")
> print(f"Selling {shares_to_sell} shares will decrease the price")
> ```

***

## Error Handling

> [tab] TypeScript
>
> ```typescript theme={"dark"}
> try {
>   const tx = new Transaction().add(...instructions);
>   await sendAndConfirmTransaction(connection, tx, [wallet]);
> } catch (error) {
>   if (error.message.includes("InsufficientShares")) {
>     console.error("Not enough shares to sell");
>   } else if (error.message.includes("MarketClosed")) {
>     console.error("Market is no longer accepting trades");
>   } else {
>     throw error;
>   }
> }
> ```

> [tab] Python
>
> ```python theme={"dark"}
> try:
>     tx = Transaction()
>     tx.add(*instructions)
>     await connection.send_transaction(tx, wallet)
> except Exception as error:
>     if "InsufficientShares" in str(error):
>         print("Not enough shares to sell")
>     elif "MarketClosed" in str(error):
>         print("Market is no longer accepting trades")
>     else:
>         raise
> ```


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