# Operation - Swap Out

The **Swap-out Operation** within the Nexus Yield Module (NYM) of the Satoshi Protocol allows users to convert satUSD back into other stablecoins or supported assets like USDT or USDC.

{% hint style="info" %}
Please note that the Swap-out process includes a pending period before the assets can be withdrawn.
{% endhint %}

### Key Steps:

1. **Define** satUSD **Amount**:
   * The amount of satUSD to be swapped out is defined and converted into the correct units using `parseUnits`.
2. **Preview the Swap-out**:
   * The `getPreviewSwapOut` function simulates the swap-out operation, providing details about the expected asset amount to be received and the associated fee.
3. **Execute the Swap-out**:
   * The `doNymSwapOut` method is called to execute the swap-out transaction, converting the specified amount of $satUSD back into the target asset.

### Example Code

```typescript
import { parseUnits } from 'viem';
import { walletClient, publicClient, protocolConfig, collateral, DEBT_TOKEN_DECIMALS } from 'satoshi-sdk';

// Example usage:
doSwapIn('USDT', 1)
  .then((receipt) => console.log('Swap-out completed:', receipt))
  .catch((error) => console.error('Error during swap-out:', error));

async function doSwapOut(assetSymbol: string, amount: number) {
  // Initialize the SatoshiClient with protocol and wallet configurations
  const satoshiClient = new SatoshiClient(protocolConfig, walletClient);
  const asset = (satoshiClient.NexusYieldModule.getAssetList()).find(t => t.symbol === assetSymbol)!;

  // Fetch the current SAT balance before the swap
  const satBalanceBefore = await getBalanceOf(debtAddress);

  // Define the amount of SAT to swap out, converting to the appropriate units
  const satAmount = parseUnits(amount.toString(), DEBT_TOKEN_DECIMALS);

  // Preview the swap-out to get information on the expected asset amount and fees
  const previewSwapOut = await satoshiClient.NexusYieldModule.getPreviewSwapOut(asset.address, satAmount);
  const expectedReceiveAssetAmt = Number(previewSwapOut?.assetAmount); // Asset amount expected to be received

  // Execute the swap-out operation
  const receipt = await satoshiClient.NexusYieldModule.doNymSwapOut(asset.address, satAmount);

  // Fetch the SAT balance after the swap to validate the amount swapped out
  const satBalanceAfter = await getBalanceOf(debtAddress);

  // Confirm that the swap was successful
  if (receipt.status !== 'success') {
    throw new Error('Swap-out transaction failed.');
  }

  // Validate that the SAT balance decreased by the amount swapped out
  const swappedAmount = satBalanceBefore - satBalanceAfter;
  if (swappedAmount !== satAmount) {
    throw new Error(`Balance mismatch: expected to swap out ${satAmount}, but got ${swappedAmount}`);
  }

  console.log('Swap-out was successful:', receipt);
  return receipt;
}


```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.river.inc/satoshi-protocol-v1/sdk/nexus-yield-module/operation-swap-out.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
