Here we can see how to withdraw or unlock an unlockable amount. If the unlockable amount is a deposit it will be withdrawn. If it is a withdraw and it is available for unlock (the unlockable amount type has a boolean, unlockable, which indicates it) it will be unlocked and available with the compensation added.
import { ConnectionService, Environments, WalletService, Logger } from"../src";constckbUrl="http://localhost:8117/rpc";constindexerUrl="http://localhost:8117/indexer";constmnemonic="private pond zero popular fashion omit february obscure pattern city camp pistol";constmain=async () => {try {constconnectionService=newConnectionService(ckbUrl, indexerUrl,Environments.Testnet);constwallet=newWalletService(connectionService, mnemonic);// We will try to unlock first unlockable amountconstunlockableAmounts=awaitwallet.getDAOUnlockableAmounts();if (unlockableAmounts.length===0) {thrownewError("No unlockable or withdrawable amount. Deposit before trying to withdraw"); }Logger.info("Withdrawing or unlocking the following unlockable amount:");Logger.info(unlockableAmounts[0]);constunlockHash=awaitwallet.withdrawOrUnlock(unlockableAmounts[0], mnemonic);Logger.info(unlockHash);// You can view DAO balance withconstdaoBalance=awaitwallet.getDAOBalance();Logger.info(daoBalance); } catch (error) {Logger.error(`${error.name}: ${error.message}`);Logger.error(`${error.stack}`); }};main();
Remember to withdraw a deposit at the end of the cycle but be careful not to do it too late! We have the fields remainingEpochs and remainingCycleMinutes to control the cycle status.