Operation - Swap In
Key Steps:
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-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