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.
Please note that the Swap-out process includes a pending period before the assets can be withdrawn.
Key Steps:
Define satUSD Amount:
The amount of $satUSD to be swapped out is defined and converted into the correct units using parseUnits.
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.
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
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));asyncfunctiondoSwapOut(assetSymbol:string, amount:number) {// Initialize the SatoshiClient with protocol and wallet configurationsconstsatoshiClient=newSatoshiClient(protocolConfig, walletClient);constasset= (satoshiClient.NexusYieldModule.getAssetList()).find(t =>t.symbol === assetSymbol)!;// Fetch the current SAT balance before the swapconstsatBalanceBefore=awaitgetBalanceOf(debtAddress);// Define the amount of SAT to swap out, converting to the appropriate unitsconstsatAmount=parseUnits(amount.toString(),DEBT_TOKEN_DECIMALS);// Preview the swap-out to get information on the expected asset amount and feesconstpreviewSwapOut=awaitsatoshiClient.NexusYieldModule.getPreviewSwapOut(asset.address, satAmount);constexpectedReceiveAssetAmt=Number(previewSwapOut?.assetAmount); // Asset amount expected to be received// Execute the swap-out operationconstreceipt=awaitsatoshiClient.NexusYieldModule.doNymSwapOut(asset.address, satAmount);// Fetch the SAT balance after the swap to validate the amount swapped outconstsatBalanceAfter=awaitgetBalanceOf(debtAddress);// Confirm that the swap was successfulif (receipt.status !=='success') {thrownewError('Swap-out transaction failed.'); }// Validate that the SAT balance decreased by the amount swapped outconstswappedAmount= satBalanceBefore - satBalanceAfter;if (swappedAmount !== satAmount) {thrownewError(`Balance mismatch: expected to swap out ${satAmount}, but got ${swappedAmount}`); }console.log('Swap-out was successful:', receipt);return receipt;}