> ## Documentation Index
> Fetch the complete documentation index at: https://docs.cloak.ag/llms.txt
> Use this file to discover all available pages before exploring further.

# Events and callbacks

> Progress callbacks, account events and scan status, with the values each one emits

The SDK reports progress through callbacks you pass per call, and through events on the `Cloak`
object. Progress text is written for people: show it, but do not branch on it. Branch on results and
on error codes.

## Per-call `onProgress`

`shield`, `send`, `withdraw`, `createPaymentLink`, `claimPaymentLink` and `payRequest` take
`onProgress: (step: string) => void`.

```ts theme={null}
const receipt = await cloak.send({
  to: "cloak_…",
  token: "USDC",
  amount: "3",
  onProgress: (step) => setStatus(step),
});
```

Typical steps on Solana, in order:

| Phase | Example text |
| - | - |
| Validation | `Validating transaction parameters...` |
| First proof of the page | `Downloading circuit files (12 of 23 MB)...` |
| Merkle data | `Fetching Merkle proofs...` |
| Proving | `Generating ZK proof...`, `Generating zero-knowledge proof...` |
| A slow proof | `Proof is taking longer than expected, retrying on a single thread...` |
| A shield | `Waiting for wallet signature...`, `Sending transaction...`, `Confirming transaction...` |
| A send or withdrawal | `Computing commitments...`, `Verifying settlement on chain (attempt n)...` |
| A stale root | `Root not found (stale), will regenerate proof with fresh tree...` |

Cross-chain calls add their own steps, for example `Sending 6 USDC from base...` on a shield from
Base, and `Reserving the payout...`, `Withdrawing...`, `Sending...` on a payout.

## Events on the `Cloak` object

`cloak.on(event, handler)` returns a function that unsubscribes.

| Event | Payload | When |
| - | - | - |
| `finished` | `ResumedItem`: `{ kind, status, token, amount, txs }` | A deposit or payout left open by an earlier run was completed |
| `warning` | `string` | A non-fatal problem: a wallet that could not be linked, exposed notes not moved yet, a resume pass that failed |
| `secured` | `{ moved: number }` | Notes an older client left exposed were moved into the account |

```ts theme={null}
const off = cloak.on("finished", (item) => console.log("finished from an earlier run:", item));
cloak.on("warning", (message) => console.warn(message));
// later
off();
```

> [!NOTE]
>
> The `CloakEvent` type also lists `"progress"`, but `createCloak` does not emit it. Use each call's
> `onProgress`.

`createCloak({ onWarning })` receives the same messages as the `warning` event.

## Low-level callbacks

The low-level functions take these on `TransactOptions`:

| Callback | Signature | Emits |
| - | - | - |
| `onProgress` | `(status: string) => void` | The step texts above, plus retries and fallbacks |
| `onProofProgress` | `(percent: number) => void` | Exactly `10`, `30` and `100` |
| `onWarning` | `(message: string) => void` | A partner code that was dropped as invalid |

`transactBatch` prefixes each item's status with `[batch i/N]`. A wallet prompt is announced before it
opens, for example `Approve the request in your wallet. This signs the request for the relay, not a
transaction, and it must be approved within a few minutes to stay valid.` Use that moment to bring
your UI's attention to the wallet.

## Scanning callbacks

`scanTransactions` takes:

| Callback | Signature | Emits |
| - | - | - |
| `onProgress` | `(processed: number, total: number) => void` | Transactions scanned so far |
| `onStatus` | `(status: string) => void` | `Fetching transaction signatures...`, `Scanning N transactions...`, `Scanned i/N transactions...` |

`recoverNotesFromIndex` reports `{ pagesDone, pagesTotal, itemsChecked, notesFound }` through its
`onProgress`.

## Showing progress well

* Warm the circuits on mount (`verifyAllCircuits(getCircuitsPath())`) so the first action does not
  start with a 23 MB download.
* Show a determinate bar only for the circuit download and `onProofProgress`. Everything else is a
  status line.
* On a wallet prompt announcement, show "Check your wallet". The request expires if left unapproved.

## Related

* [Error codes](https://docs.cloak.ag/reference/errors.md)
* [Configuration options](https://docs.cloak.ag/reference/configuration.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.