Skip to main content
Manually optimizes wallet leaf structure by consolidating or splitting leaves into power-of-2 denominations. This is an async generator that yields progress updates and can be interrupted between steps.
For background on leaf optimization concepts, multiplicity levels, and tradeoffs, see Leaf Optimization in the initialize docs.

When to Use

Use this method when you’ve configured manual mode (auto: false) in your initialization options. In manual mode, the SDK won’t optimize automatically—you control exactly when optimization runs.
In auto mode (default), the SDK handles optimization automatically. You typically don’t need to call this method directly.

Method Signature

Parameters

number
Optimization multiplicity (0-5). Defaults to the value set in optimizationOptions during initialization.

Yields

object
required
Progress object containing:
  • step: Current step number
  • total: Total number of steps
  • controller: AbortController to cancel optimization between steps

Interrupting Optimization

The abort controller lets you pause optimization between steps—useful when the user wants to send a payment mid-optimization.
Optimization runs multiple swap operations with the SSP. If you abort mid-optimization, the wallet remains in a valid state but may not be fully optimized. You can resume optimization later.

Example: Basic Usage

Example: With Progress UI