@cashu/cashu-ts
    Preparing search index...

    Class WalletEvents

    Index

    Constructors

    Methods

    • Register a callback that fires whenever deterministic counters are reserved.

      Timing: the callback is invoked synchronously after a successful reservation and before the enclosing wallet method returns. The wallet does not await your callback, it is fire-and-forget.

      Responsibility for async work is on the consumer. If your handler calls an async function (e.g. persisting start + count to storage), make sure to handle errors inside it to avoid unhandled rejections.

      Typical use: persist start + count for the keysetId so counters survive restarts.

      Parameters

      Returns SubscriptionCanceller

      A function that unsubscribes the handler.

      wallet.on.countersReserved(({ keysetId, start, count, next }) => {
      saveNextToDb(keysetId, start + count); // handle async errors inside saveNextToDb
      });
    • Create a composite canceller that can collect many subscriptions and dispose them all in one call.

      Accepts both a SubscriptionCanceller and a Promise<SubscriptionCanceller>. When the composite canceller is called, all collected cancellations are invoked. Errors from individual cancellers are caught and ignored.

      The returned function also has an .add() method to register more cancellers, and a .cancelled boolean property for debugging.

      Returns SubscriptionCanceller & {
          add: (c: CancellerLike) => CancellerLike;
          cancelled: boolean;
      }

      Composite canceller function with .add() and .cancelled members.

      const cancelAll = wallet.on.group();
      cancelAll.add(wallet.on.mintQuotes(ids, onUpdate, onErr));
      cancelAll.add(asyncSubscribeElsewhere());

      // later
      cancelAll(); // disposes everything
    • Resolve when ANY of several mint quotes is PAID, cancelling the rest.

      Subscribes to all distinct ids, resolves with { id, quote } for the first PAID, and cancels all remaining subscriptions.

      Errors from individual subscriptions are ignored by default so a single noisy stream does not abort the whole race. Set failOnError: true to reject on the first error instead. If all subscriptions error and none paid, the promise rejects with the last seen error.

      Parameters

      • ids: string[]

        Array of mint quote ids (duplicates are ignored).

      • Optionalopts: { failOnError?: boolean; signal?: AbortSignal; timeoutMs?: number }

        Optional controls.

        • OptionalfailOnError?: boolean

          When true, reject on first error. Default false.

        • Optionalsignal?: AbortSignal

          AbortSignal to cancel the wait early.

        • OptionaltimeoutMs?: number

          Milliseconds to wait before rejecting with a timeout error.

      Returns Promise<{ id: string; quote: MintQuoteResponse }>

      A promise resolving to the id that won and its MintQuoteResponse.

      // Race multiple quotes obtained from splitting a large top up
      const { id, quote } = await wallet.on.onceAnyMintPaid(batchQuoteIds, {
      timeoutMs: 120_000,
      });
      console.log('First top up paid', id, quote.preimage?.length);
    • Resolve once a melt quote transitions to PAID, with automatic unsubscription, optional abort signal, and optional timeout.

      Mirrors onceMintPaid, but for melts.

      Parameters

      • id: string

        Melt quote id to watch.

      • Optionalopts: { signal?: AbortSignal; timeoutMs?: number }

        Optional controls.

        • Optionalsignal?: AbortSignal

          AbortSignal to cancel the wait early.

        • OptionaltimeoutMs?: number

          Milliseconds to wait before rejecting with a timeout error.

      Returns Promise<MeltQuoteResponse>

      A promise that resolves with the MeltQuoteResponse once PAID.

      try {
      const paid = await wallet.on.onceMeltPaid(meltId, { timeoutMs: 45_000 });
      console.log('Invoice paid by mint, paid msat', paid.paid ?? 0);
      } catch (e) {
      console.error('Payment did not complete in time', e);
      }
    • Resolve once a mint quote transitions to PAID, with automatic unsubscription, optional abort signal, and optional timeout.

      The underlying subscription is always cancelled after resolution or rejection, including on timeout or abort.

      Parameters

      • id: string

        Mint quote id to watch.

      • Optionalopts: { signal?: AbortSignal; timeoutMs?: number }

        Optional controls.

        • Optionalsignal?: AbortSignal

          AbortSignal to cancel the wait early.

        • OptionaltimeoutMs?: number

          Milliseconds to wait before rejecting with a timeout error.

      Returns Promise<MintQuoteResponse>

      A promise that resolves with the latest MintQuoteResponse once PAID.

      const ac = new AbortController();
      // Cancel if the user navigates away
      window.addEventListener('beforeunload', () => ac.abort(), { once: true });

      try {
      const paid = await wallet.on.onceMintPaid(quoteId, {
      signal: ac.signal,
      timeoutMs: 60_000,
      });
      console.log('Mint paid, amount', paid.amount);
      } catch (e) {
      if ((e as Error).name === 'AbortError') {
      console.log('User aborted');
      } else {
      console.error('Mint not paid', e);
      }
      }
    • Async iterable that yields proof state updates for the provided proofs.

      Adds a bounded buffer option:

      • If maxBuffer is set and the queue is full when a new payload arrives, either drop the oldest queued payload (drop: 'oldest', default) or the incoming payload (drop: 'newest'). In both cases onDrop is invoked with the dropped payload.

      The stream ends and cleans up on abort or on the wallet error callback. Errors from the wallet are treated as a graceful end for this iterator.

      Type Parameters

      • T = unknown

      Parameters

      • proofs: Proof[]

        The proofs to subscribe to. Only secret is required.

      • Optionalopts: {
            drop?: "oldest" | "newest";
            maxBuffer?: number;
            onDrop?: (payload: T) => void;
            signal?: AbortSignal;
        }

        Optional controls.

        • Optionaldrop?: "oldest" | "newest"

          Overflow strategy when maxBuffer is reached, 'oldest' | 'newest'. Default 'oldest'.

        • OptionalmaxBuffer?: number

          Maximum number of queued items before applying the drop strategy.

        • OptionalonDrop?: (payload: T) => void

          Callback invoked with the payload that was dropped.

        • Optionalsignal?: AbortSignal

          AbortSignal that stops the stream when aborted.

      Returns AsyncIterable<T>

      An async iterable of update payloads.

      const ac = new AbortController();
      try {
      for await (const update of wallet.on.proofStatesStream(myProofs)) {
      if (update.state === CheckStateEnum.SPENT) {
      console.warn('Spent proof', update.proof.id);
      }
      }
      } catch (e) {
      if ((e as Error).name !== 'AbortError') {
      console.error('Stream error', e);
      }
      }