DIM · Dagon Inference Marketplace

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

  1. 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.
  2. Begin close. begin_close sets the close deadline D = min(T, now) + G once, where T is the service end and G the 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.
  3. Grace. Until D, the seller can redeem the newest voucher. Redemption pays settled_total − redeemed.
  4. Finalize. From D on, anyone may call finalize. It moves budget − redeemed back 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.finalize refuses to run before the deadline.

Timing examples

  • You open a channel with service end in 24 hours and a one-hour grace. You call beginClose after 3 hours: D is 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 at T, 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.