DIM · Dagon Inference Marketplace

Paying with vouchers

The SDK pays sellers by signing vouchers: after each verified receipt it waits briefly, then signs one voucher covering every receipt queued on that channel, up to 32, and posts it to the seller. You do not have to call anything for this to happen. Call sdk.flushVouchers() when you need to know that every verified receipt has been acknowledged, for example before you close a channel or exit.

The code on this page assumes the sdk and wallet from the buyer quickstart.

Automatic batching

Client option Default Effect
batchSize 32 (max 256) Most receipts in one voucher.
batchDelayMs 10 How long the SDK waits after a receipt before posting, to gather more.
pendingPerChannel 256 Unpaid attempts plus queued receipts per channel. Above it, new requests on the channel fail with wallet until vouchers catch up.
settlementTimeoutMs 5,000 Timeout for each voucher POST and receipt lookup.

Larger batches mean fewer POSTs; vouchering promptly keeps the seller's unvouchered exposure low, which keeps it admitting your requests. If a channel is busy enough that the seller answers 402 insufficient_channel_funds, the SDK flushes that channel and retries the request once the voucher is acknowledged.

Waiting for acknowledgment

await sdk.flushVouchers();                       // every channel
for (const channel of wallet.channels()) {
  await sdk.flushVouchers(channel.address);      // or one channel
}

flushVouchers resolves when every receipt queued at the time of the call has been acknowledged with the exact expected settled_total. If the seller rejects a voucher, it throws a MarketplaceError with code settlement, the HTTP status, and voucherCode, one of:

voucherCode Meaning
malformed_voucher The body or voucher bytes could not be decoded.
bad_signature The signature does not verify under the channel's session key.
wrong_channel The voucher names a channel this seller does not serve.
over_budget settled_total exceeds the channel budget.
channel_closed The channel is finalized or past its close deadline.
wrong_delta The total went backwards, or the delta is not the sum of the receipts' charges.
invalid_receipt A receipt was not issued by this seller, or was already acknowledged.
receipt_order Receipts are duplicated or not in canonical order.
wrong_commitment The commitment does not continue the acknowledged chain over these receipts.

A rejection keeps the exact signed batch; the SDK retries the same body rather than signing a different one. Persistent rejections need resolving with the seller before you finalize the channel.

What a voucher commits to

Each voucher carries the new cumulative settled_total and a hash chain over its receipts, ordered by created_at, then request_id, continuing from the last acknowledged commitment. The protocol package computes it:

import { emptyCommitment, encodeVoucher, orderedReceiptsCommitment, type Voucher } from "@im/protocol";

declare const domain: Uint8Array;           // genesis hash ‖ program id, 64 bytes
declare const channel: Uint8Array;          // channel address, 32 bytes
declare const receiptBytes: Uint8Array[];   // the 382-byte receipts this voucher covers

const commitment = orderedReceiptsCommitment(emptyCommitment(), receiptBytes); // first voucher on the channel
const voucher: Voucher = { domain, channel, channelEpoch: 1n, seq: 1n, settledTotal: 1_183n, receiptsCommitment: commitment };
const bytes = encodeVoucher(voucher); // 168 bytes; sign them with the channel's session key
console.log(bytes.length);

You rarely need this: the SDK builds, signs, journals and posts vouchers itself, and never keeps more than one unacknowledged batch per channel.

You choose what you pay for

A voucher may cover any batch of receipts you accept, as long as they are unacknowledged and in canonical order. The SDK vouchers the receipts it verified and never vouchers a receipt it rejected. A receipt you never voucher stays the seller's exposure, which is why sellers stop admitting holds when that exposure gets too large.

Acknowledged is not yet paid

An acknowledgment means the seller stored your voucher. The seller collects the money later by redeeming the newest voucher on-chain, which it may batch. Until the channel's close deadline passes, it can always do so. See settlement and redemption.