# Operation - Swap In

The **Swap-in Operation** within the Nexus Yield Module (NYM) of the Satoshi Protocol allows users to swap stablecoins or other supported assets into satUSD, the protocol’s native stablecoin. This process is crucial for maintaining liquidity and stability within the Satoshi Protocol ecosystem.

### Key Steps:

1. **Define Asset Amount**:
   * The amount of the asset to be swapped in is defined using the `parseUnits` function to convert it into the appropriate decimals format required by the protocol.
2. **Preview the Swap-in**:
   * The `getPreviewSwapIn` function is called to simulate the swap, returning important details such as the expected satUSD amount to be minted and the fee involved.
3. **Execute the Swap-in**:
   * The `doNymSwapIn` method is called to execute the actual swap operation. This will mint satUSD based on the asset amount provided.

### Example Code

Here's an example code snippet demonstrating the Swap-in operation using NYM:

```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-in completed:', receipt))
  .catch((error) => console.error('Error during swap-in:', error));

async function doSwapIn(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)!;

  // Define the amount of the asset to swap in, converting to the appropriate units
  const assetAmount = parseUnits(amount.toString(), asset.decimals);

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

  // Preview the swap to get information on the expected SAT amount and fees
  const satAmountInfo = await satoshiClient.NexusYieldModule.getPreviewSwapIn(asset.address, assetAmount);
  const expectedSatBalanceReceived = satAmountInfo!.debtTokenToMintAmt; // SAT amount to be received

  // Execute the swap-in operation
  const receipt = await satoshiClient.NexusYieldModule.doNymSwapIn(asset.address, assetAmount);

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

  // Fetch the SAT balance after the swap to validate the amount received
  const satBalanceAfter = await getErc20Balance(
    {
      publicClient,
      tokenAddr: debtAddress,
    },
    walletClient.account.address
  );

  // Validate that the SAT balance increased by the expected amount received from the swap
  const receivedAmount = satBalanceAfter - satBalanceBefore;
  if (receivedAmount !== expectedSatBalanceReceived) {
    throw new Error(`Balance mismatch: expected ${expectedSatBalanceReceived}, got ${receivedAmount}`);
  }

  console.log('Swap-in 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-in.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.
