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));
async function doNymWithdraw(assetSymbol) {
// Initialize the SatoshiClient with protocol and wallet configurations
const satoshiClient = new SatoshiClient(protocolConfig, walletClient);
// Retrieve asset details based on the asset symbol
const asset = (await satoshiClient.NexusYieldModule.getAssetList()).find((t) => t.symbol === assetSymbol);
if (!asset) {
throw new Error(`Asset ${assetSymbol} not found.`);
}
// Fetch the list of pending withdrawal information for the specified asset
const pendingInfos = await satoshiClient.NexusYieldModule.getNymPendingWithdrawInfos([asset]);
if (!pendingInfos || pendingInfos.length === 0) {
console.log('No pending withdrawals found.');
return;
}
// Iterate through each pending withdrawal info
for (const pendingInfo of pendingInfos) {
const { scheduledWithdrawalAmount, withdrawalTime, asset } = pendingInfo;
// Skip if there's no scheduled amount or withdrawal time
if (!(scheduledWithdrawalAmount > 0n) || !withdrawalTime) continue;
// Get the current time in seconds
const currentTime = Math.floor(Date.now() / 1000);
// Check if the current time is greater than the withdrawal time
if (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 withdrawal
const assetBalanceBefore = await getBalanceOf(asset);
// Execute the withdrawal
const receipt = await satoshiClient.NexusYieldModule.doNymWithdraw(asset);
// Confirm that the withdrawal was successful
if (receipt.status !== 'success') {
throw new Error('Withdrawal transaction failed.');
}
// Fetch the asset balance after withdrawal to validate the change
const assetBalanceAfter = await getBalanceOf(asset);
// Validate that the asset balance increased by the scheduled withdrawal amount
if (assetBalanceAfter - assetBalanceBefore !== scheduledWithdrawalAmount) {
throw new Error(
`Balance mismatch: expected an increase of ${scheduledWithdrawalAmount}, but got ${
assetBalanceAfter - assetBalanceBefore
}`
);
}
console.log('Withdrawal was successful:', receipt);
}
}