The Withdraw Operation in the Nexus Yield Module (NYM) of the Satoshi Protocol allows users to withdraw previously scheduled withdrawals of assets, such as USDT, USDC, or other supported tokens.
Key Steps:
Fetch Pending Withdrawals:
Retrieve the list of pending withdrawals using the getNymPendingWithdrawInfos method, which returns information on assets that are scheduled for withdrawal.
Check Conditions:
Iterate through each pending withdrawal to check if the scheduled amount is greater than zero and if the withdrawal time is set.
Execute Withdrawal:
Call the doNymWithdraw method to execute the withdrawal for the specified asset.
Example
import { walletClient, publicClient, protocolConfig } from'satoshi-sdk';// Example usage:doNymWithdraw('USDT').then(() =>console.log('Withdraw process completed successfully')).catch((error) =>console.error('Error during withdrawal:', error));asyncfunctiondoNymWithdraw(assetSymbol) {// Initialize the SatoshiClient with protocol and wallet configurationsconstsatoshiClient=newSatoshiClient(protocolConfig, walletClient);// Retrieve asset details based on the asset symbolconstasset= (awaitsatoshiClient.NexusYieldModule.getAssetList()).find((t) =>t.symbol === assetSymbol);if (!asset) {thrownewError(`Asset ${assetSymbol} not found.`); }// Fetch the list of pending withdrawal information for the specified assetconstpendingInfos=awaitsatoshiClient.NexusYieldModule.getNymPendingWithdrawInfos([asset]);if (!pendingInfos ||pendingInfos.length===0) {console.log('No pending withdrawals found.');return; }// Iterate through each pending withdrawal infofor (constpendingInfoof pendingInfos) {const { scheduledWithdrawalAmount,withdrawalTime,asset } = pendingInfo;// Skip if there's no scheduled amount or withdrawal timeif (!(scheduledWithdrawalAmount >0n) ||!withdrawalTime) continue;// Get the current time in secondsconstcurrentTime=Math.floor(Date.now() /1000);// Check if the current time is greater than the withdrawal timeif (currentTime < withdrawalTime) {console.log( `Withdrawal for ${asset.symbol} is scheduled for a future time. Current time: ${currentTime}, Withdrawal time: ${withdrawalTime}. Skipping...`
);continue; }// Fetch the asset balance before withdrawalconstassetBalanceBefore=awaitgetBalanceOf(asset);// Execute the withdrawalconstreceipt=awaitsatoshiClient.NexusYieldModule.doNymWithdraw(asset);// Confirm that the withdrawal was successfulif (receipt.status !=='success') {thrownewError('Withdrawal transaction failed.'); }// Fetch the asset balance after withdrawal to validate the changeconstassetBalanceAfter=awaitgetBalanceOf(asset);// Validate that the asset balance increased by the scheduled withdrawal amountif (assetBalanceAfter - assetBalanceBefore !== scheduledWithdrawalAmount) {thrownewError(`Balance mismatch: expected an increase of ${scheduledWithdrawalAmount}, but got ${ assetBalanceAfter - assetBalanceBefore}` ); }console.log('Withdrawal was successful:', receipt); }}