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:
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.
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.
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));asyncfunctiondoSwapIn(assetSymbol:string, amount:number) {// Initialize the SatoshiClient with protocol and wallet configurationsconstsatoshiClient=newSatoshiClient(protocolConfig, walletClient);constasset= (satoshiClient.NexusYieldModule.getAssetList()).find(t =>t.symbol === assetSymbol)!;// Define the amount of the asset to swap in, converting to the appropriate unitsconstassetAmount=parseUnits(amount.toString(),asset.decimals);// Fetch the current SAT balance before the swapconstsatBalanceBefore=awaitgetBalanceOf(debtAddress);// Preview the swap to get information on the expected SAT amount and feesconstsatAmountInfo=awaitsatoshiClient.NexusYieldModule.getPreviewSwapIn(asset.address, assetAmount);constexpectedSatBalanceReceived= satAmountInfo!.debtTokenToMintAmt; // SAT amount to be received// Execute the swap-in operationconstreceipt=awaitsatoshiClient.NexusYieldModule.doNymSwapIn(asset.address, assetAmount);// Confirm that the swap was successfulif (receipt.status !=='success') {thrownewError('Swap-in transaction failed.'); }// Fetch the SAT balance after the swap to validate the amount receivedconstsatBalanceAfter=awaitgetErc20Balance( { publicClient, tokenAddr: debtAddress, },walletClient.account.address );// Validate that the SAT balance increased by the expected amount received from the swapconstreceivedAmount= satBalanceAfter - satBalanceBefore;if (receivedAmount !== expectedSatBalanceReceived) {thrownewError(`Balance mismatch: expected ${expectedSatBalanceReceived}, got ${receivedAmount}`); }console.log('Swap-in was successful:', receipt);return receipt;}