Closing channels and getting leftovers back
To close a channel and get the unspent money back, flush your vouchers, call beginClose to start the grace period, and call finalize once the close deadline has passed. Finalizing returns budget − redeemed to your vault's free balance, deletes the channel account and refunds its rent to whoever opened it. From the free balance you can open new channels or withdraw.
The code on this page assumes the sdk and wallet from the buyer quickstart.
The steps
for (const channel of wallet.channels()) {
await sdk.flushVouchers(channel.address); // 1. pay for everything you received
const deadline = await wallet.beginClose(channel.address); // 2. start the grace period
console.log(channel.address, "can be finalized at", new Date(deadline * 1000).toISOString());
}
// 3. Later, once the deadline has passed (a scheduled job is fine):
const now = Math.floor(Date.now() / 1000);
for (const channel of wallet.channels()) {
const deadline = await wallet.beginClose(channel.address); // returns the stored deadline; no second transaction
if (now >= deadline) await wallet.finalize(channel.address);
}
What each step does
- Flush. Make sure the seller has a voucher for every receipt you accepted. After the close deadline the seller can no longer redeem, so pay for what you received before you start closing.
- Begin close.
begin_closesets the close deadlineD = min(T, now) + Gonce, whereTis the service end andGthe grace period. From this moment the reference gateway refuses new holds on the channel (402 channel_closed), and the SDK stops using it. The owner, the Mandate's agent or the seller may call it. - Grace. Until
D, the seller can redeem the newest voucher. Redemption payssettled_total − redeemed. - Finalize. From
Don, anyone may callfinalize. It movesbudget − redeemedback to your free balance, releases the Mandate reservation, closes the channel and sends its rent, about 2,644,800 lamports, back to the account that opened it.wallet.finalizerefuses to run before the deadline.
Timing examples
- You open a channel with service end in 24 hours and a one-hour grace. You call
beginCloseafter 3 hours:Dis 3 hours + 1 hour from opening, so you finalize one hour later. - You never call
beginClose, and the service end passes. The channel stops serving atT, but stays open on-chain. When you do call it,D = T + G; if that time has already passed, you can finalize immediately.
After a revoked or expired Mandate
When a Mandate in the channel's chain is revoked or has expired, anyone can call close_on_revoked, with the same effect as begin_close. In the SDK, use wallet.closeOnRevoked(address). Once all the Mandate's channels are finalized, wallet.closeMandate(address) closes the Mandate and refunds its rent. Close child Mandates before their parents.
Getting the money out of the vault
Finalized funds land in your vault's free balance. Withdraw them with the owner-signed withdraw_free(amount) instruction. The SDK does not build it; use the tracked IDL at programs/idl/im_channels.json, as for deposit. The vault itself cannot be closed.