Initiates a withdrawal to move funds from the Spark network to an on-chain Bitcoin address via the SparkWallet.
Method Signature
interface WithdrawParams {
onchainAddress: string;
exitSpeed: ExitSpeed;
amountSats?: number;
feeQuoteId?: string; // ID from getWithdrawalFeeQuote
feeAmountSats?: number; // Fee amount based on exitSpeed
feeQuote?: CoopExitFeeQuote; // @deprecated - use feeQuoteId and feeAmountSats
deductFeeFromWithdrawalAmount?: boolean; // default: true
}
async withdraw(params: WithdrawParams): Promise<CoopExitRequest | null | undefined>
Parameters
The Bitcoin address where the funds should be sent
The desired speed of the exit (FAST, MEDIUM, SLOW)
The amount in satoshis to withdraw (if not specified, withdraws all available funds)
The ID from the fee quote returned by getWithdrawalFeeQuote
The fee amount in satoshis based on the chosen exitSpeed
Deprecated: Use feeQuoteId and feeAmountSats instead. The fee quote object returned by getWithdrawalFeeQuote
deductFeeFromWithdrawalAmount
When true, fees are deducted from withdrawal amount; when false, from remaining wallet balance (default: true)
Returns
request
CoopExitRequest | null | undefined
required
The withdrawal request details, or null/undefined if the request cannot be completed
Example
// 1) Fetch a fee quote
const feeQuote = await wallet.getWithdrawalFeeQuote({
amountSats: 17000,
withdrawalAddress: "bcrt1pf8hed85p94emupfpfhq2g0p5c40cgzqs4agvvfmeuy32nxeh549syu2lwf",
});
// 2) Calculate fee based on exit speed
const exitSpeed = ExitSpeed.MEDIUM;
let feeAmountSats: number;
switch (exitSpeed) {
case ExitSpeed.FAST:
feeAmountSats = (feeQuote.l1BroadcastFeeFast?.originalValue || 0) +
(feeQuote.userFeeFast?.originalValue || 0);
break;
case ExitSpeed.MEDIUM:
feeAmountSats = (feeQuote.l1BroadcastFeeMedium?.originalValue || 0) +
(feeQuote.userFeeMedium?.originalValue || 0);
break;
case ExitSpeed.SLOW:
feeAmountSats = (feeQuote.l1BroadcastFeeSlow?.originalValue || 0) +
(feeQuote.userFeeSlow?.originalValue || 0);
break;
}
// 3) Use the quote before it expires to create the withdrawal
const withdraw_result = await wallet.withdraw({
onchainAddress: "bcrt1pf8hed85p94emupfpfhq2g0p5c40cgzqs4agvvfmeuy32nxeh549syu2lwf",
amountSats: 17000,
exitSpeed,
feeQuoteId: feeQuote.id,
feeAmountSats,
deductFeeFromWithdrawalAmount: true,
});
console.log("Withdraw Result:", withdraw_result);