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:

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;
}

Last updated