Withdraw
Request a withdrawal from a Volo vault by amount, by shares or in full, with a minimum payout.
A Volo withdrawal is also a request, one for each receipt it draws from. The shares keep earning until the operator settles the request; then the vault converts them at the current share price, deducts any withdrawal fee and sends the asset to the owner.
import { Transaction } from '@mysten/sui/transactions';
import { getPositions, getVault, isVaultSdkError, withdrawPTB } from '@naviprotocol/vault';
export async function buildWithdrawAll(owner: string, vaultId: string, slippage = 0.01) {
const vault = await getVault(vaultId);
const [position] = await getPositions(owner, { vaults: [vault.id] });
if (!position?.shares || position.tokenBalance === null) throw new Error('No position in this vault');
// Floor: the position's estimated value minus `slippage`, in raw base units
const { decimals } = vault.assets.baseCoin;
const estimate = BigInt(Math.floor(position.tokenBalance * 10 ** decimals));
const minAmountOut = (estimate * BigInt(Math.round((1 - slippage) * 10_000))) / 10_000n;
const tx = new Transaction();
tx.setSender(owner);
try {
// Request exactly the shares the floor was computed for. `{ kind: 'all' }` would
// skip locked receipts and leave the floor too high for what is withdrawn.
const target = { kind: 'shares', shares: position.shares } as const;
const { requestIds } = await withdrawPTB(tx, vault, owner, target, { minAmountOut });
return { tx, requests: requestIds?.length ?? 0 };
} catch (error) {
if (isVaultSdkError(error) && error.code === 'INSUFFICIENT_BALANCE') {
// Some receipts are locked after a recent deposit or already have a pending withdrawal
console.log('Not withdrawable yet:', error.details?.excludedReceipts);
}
throw error;
}
}Choose a target
| Target | Use it when |
|---|---|
{ kind: 'amount', amount: '1.5' } | The user enters an amount in coin units. For Volo vaults it is converted to shares from an API snapshot, so it is an estimate. |
{ kind: 'shares', shares } | You need an exact burn. Use the raw count from VaultPosition.shares. |
{ kind: 'all' } | The user exits everything that is currently eligible. Skipped receipts stay in the position, so do not pair it with a floor based on the whole position. |
Set a payout floor
minAmountOut is the least the user accepts, in raw base units. The SDK splits it across the requests, and settlement fails rather than pay out less, leaving the request pending until it is cancelled. The example derives the floor from the position’s estimated value minus 1%, so it requests exactly the position’s shares with { kind: 'shares' }.
Do not combine a floor computed from the whole position with { kind: 'all' }. That target skips ineligible receipts, so it can withdraw fewer shares than the floor assumes, and every request would then fail at settlement.
Eligible receipts
A receipt is skipped while it already has a pending withdrawal, or while it is inside the withdrawal lock after its last deposit. If the eligible receipts do not hold enough shares, withdrawPTB throws INSUFFICIENT_BALANCE with the skipped receipts in details.excludedReceipts, each with a reason.