Skip to Content
HooksuseSbtcWithdraw

useSbtcWithdraw

Withdraw sBTC → BTC. Builds an initiate-withdrawal-request contract call to the sBTC withdrawal contract, signs it (via the connect adapter or your override), broadcasts to the Stacks node, then polls Emily for the BTC release (~60 min).

import { useSbtcWithdraw } from '@baoku26/sbtc-sdk'; const { withdraw, status, stacksTxid, btcTxid, estimatedConfirmationMinutes, error, reset } = useSbtcWithdraw(config);

Signature

function useSbtcWithdraw(config: UseSbtcWithdrawConfig): UseSbtcWithdrawResult;

Config

FieldTypeDescription
stacksAddressstringAddress initiating the withdrawal (sender + fee payer).
stacksPublicKeystringSender’s Stacks public key (hex) — builds the unsigned contract call.
maxFee?numberMax BTC fee (sats) the signers may take for the sweep. Default 80000.
fee?number | bigintStacks transaction fee in µSTX. Default 10000.
signTx?(tx: Uint8Array) => Promise<Uint8Array>Override the signer. Defaults to adapter.connect.signStacksTx.
pollIntervalMs?numberEmily poll interval while pending. Default 15000.
onSuccess?(bitcoinTxid: string) => voidCalled when BTC is released.
onError?(error: SbtcError) => voidCalled on failure.

Returns

FieldTypeDescription
withdraw(amountSats, btcAddress)(number | bigint, string) => Promise<void>Build → sign → broadcast the request, then poll Emily.
statusWithdrawalStatusidle | building | signing | broadcasting | pending | confirmed | failed.
stacksTxidstring | nullStacks txid of the withdrawal request, once broadcast.
btcTxidstring | nullBitcoin txid of the released funds, once Emily confirms.
estimatedConfirmationMinutesnumberStatic estimate to show users (~60, ≈6 BTC confirmations).
errorSbtcError | nullFailure detail.
reset()() => voidReturn to idle and stop polling.

Example

import { useStacksWallet, useSbtcWithdraw, WithdrawalStatus } from '@baoku26/sbtc-sdk'; function Withdraw() { const { address, publicKey } = useStacksWallet(); const { withdraw, status, stacksTxid, btcTxid, estimatedConfirmationMinutes, error } = useSbtcWithdraw({ stacksAddress: address!, stacksPublicKey: publicKey! }); return ( <> <button disabled={status !== WithdrawalStatus.IDLE} onClick={() => withdraw(50_000, 'tb1q...recipient')} > Withdraw 50k sats </button> <p>Status: {status} (~{estimatedConfirmationMinutes} min)</p> {stacksTxid && <p>STX txid: {stacksTxid}</p>} {btcTxid && <p>BTC txid: {btcTxid}</p>} {error && <p>{error.code}: {error.message}</p>} </> ); }

Notes & errors

  • The recipient BTC address is validated; a malformed one fails with INVALID_BTC_ADDRESS.
  • Signing failures surface as TX_SIGNING_FAILED; pipeline/Emily failures as EMILY_API_ERROR.
  • The status machine stays failed until you call reset().
  • The withdrawal contract id comes from apiConfig.sbtcWithdrawalContract — override it for testnet (see Provider).
Last updated on