---
name: nexus-sdk-hooks-events
description: Configure Nexus SDK intent/allowance/swap intent hooks and event streaming. Use when integrating approval flows, intent previews, or real-time progress events (NEXUS_EVENTS).
title: nexus-sdk-hooks-events
canonical_url: https://skilld.dev/gh/availproject/widgets/nexus-sdk-hooks-events
last_updated: 2026-09-29T07:05:50.000Z
---

> Index of every page on this site: [llms.txt](https://skilld.dev/llms.txt)

> **Skill from skilld.dev.** Follow the user's requested action.
> If the user asked to fork this Skill, follow the fork workflow below. Do not execute the Skill instructions while copying it.
> Otherwise, follow the instructions below for this session. You do not need to install anything.
>
> If the user asked to install this Skill, run `npx skilld install availproject/widgets/nexus-sdk-hooks-events`. Install writes the Skill files into the project, so every session loads them.
>
> ## Fork workflow
>
> A fork creates an editable local Skill with its original author and licence. The request authorizes copying and local installation.
> 1. Check `./skills/nexus-sdk-hooks-events`, the project lockfile, and selected Agent targets together. If the local directory or installed Skill exists, stop. Never overwrite an existing directory or Agent target.
> 2. Read [source metadata](https://skilld.dev/api/v1/skills/availproject/widgets/nexus-sdk-hooks-events) once. Use sourceUrl, sourceCommit, skillPath, sourceGone, and license. If the source is gone or its path is missing, stop. If license is null, read licence files at the source commit.
> 3. Fetch only the source commit into a temporary Git repository. Do not clone full history. Derive repository_url from sourceUrl, including repository renames. If sourceCommit is absent, resolve the sourceUrl ref once. Set source_commit to that actual commit. Run these commands in one shell call:
>
> ```sh
> git init --quiet "$temporary_dir"
> git -C "$temporary_dir" fetch --quiet --depth=1 "$repository_url" "$source_commit"
> git -C "$temporary_dir" checkout --quiet --detach FETCH_HEAD
> ```
>
> Read applicable licence declarations and notices at that commit. If copying is not permitted, report the restriction and stop.
> 4. Inspect source entries together, then copy the directory containing skillPath into `./skills/nexus-sdk-hooks-events`. Use the user's path if selected. Keep the original SKILL.md, relative links, scripts, binary assets, and executable modes. Exclude .git metadata. Reject symlinks and paths outside the Skill directory. After checking entries, use cp -a where available. A regular source directory needs no custom copy script. Do not save this page wrapper as SKILL.md.
> Preserve author credit, notices, and applicable licence files from repository or parent directories. Add PROVENANCE.md with the Skill page, source URL, actual commit, original path, and licence. Retain any existing PROVENANCE.md and record new provenance separately. Batch source inspection, copying, and provenance work where practical.
> 5. In the project root, run `skilld install ./skills/nexus-sdk-hooks-events --mode copy --plain`. If skilld is unavailable, use `npx skilld install ./skills/nexus-sdk-hooks-events --mode copy --plain`. This known command needs no help lookup. Install does not support --json. Use detected Agent targets, or add --agent for the targets the user selected. Install the local path, never the upstream selector. If installation fails, preserve the local copy and report the exact failure.
> 6. Confirm the local lockfile source and installed Agent copies once. Report the local path, actual commit, and Agent targets. After edits, reinstall the same local path. Upstream updates must not replace it. Do not publish or push unless the user asks.

# Hooks and Events

## Set up hooks once after SDK init
- Call `sdk.setOnIntentHook(callback)`.
- Call `sdk.setOnAllowanceHook(callback)`.
- Call `sdk.setOnSwapIntentHook(callback)`.
- If a hook is not set, the SDK auto-approves and continues.
- If a hook is set, always call `allow(...)` or `deny()` or the flow will stall.
- Optionally auto-deny after a timeout to avoid hanging UX.

## Handle intent hook (bridge / bridgeAndTransfer / bridgeAndExecute)

### Signature
```ts
sdk.setOnIntentHook((data) => {
  // data: OnIntentHookData
});
```

### OnIntentHookData
- `allow(): void` — continue the flow
- `deny(): void` — reject the flow
- `intent: ReadableIntent` — readable intent info for UI
- `refresh(selectedSources?: number[]): Promise<ReadableIntent>` — refresh the intent

### How to use
- Store `data` in a ref so UI can call `allow()`/`deny()` later.
- Call `refresh()` on an interval (e.g., every 15s) if you show intent details.
- If the user closes the modal or cancels, call `deny()` and clear the ref.

## Handle allowance hook (approval control)

### Signature
```ts
sdk.setOnAllowanceHook((data) => {
  // data: OnAllowanceHookData
});
```

### OnAllowanceHookData
- `allow(decisions): void`
  - `decisions` must match the number of `sources`
  - each entry can be `'min'`, `'max'`, `bigint`, or numeric string
- `deny(): void`
- `sources: AllowanceHookSources`
  - includes chain/token info and current vs required allowance

### How to use
- Present each source to the user (chain + token + required allowance).
- Build the `decisions` array in the same order as `sources`.
- Call `allow(decisions)` to continue, or `deny()` to cancel.
- Ensure `decisions.length === sources.length` or the SDK will reject with `INVALID_VALUES_ALLOWANCE_HOOK`.

## Handle swap intent hook (swap flows)

### Signature
```ts
sdk.setOnSwapIntentHook((data) => {
  // data: OnSwapIntentHookData
});
```

### OnSwapIntentHookData
- `allow(): void`
- `deny(): void`
- `intent: SwapIntent`
- `refresh(): Promise<SwapIntent>`

### How to use
- Store `data` and show a UI summary.
- Call `allow()` to proceed or `deny()` to cancel.

## Stream events for progress UI

### Attach listeners
- All main operations accept `{ onEvent }` in options.

### Event names and payloads
- `NEXUS_EVENTS.STEPS_LIST` → `BridgeStepType[]`
- `NEXUS_EVENTS.STEP_COMPLETE` → `BridgeStepType`
- `NEXUS_EVENTS.SWAP_STEP_COMPLETE` → `SwapStepType`

### Typical usage
- Use `STEPS_LIST` to seed step trackers.
- Use `STEP_COMPLETE` and `SWAP_STEP_COMPLETE` to update progress and explorer links.

## Handle errors and cancellation
- If an operation throws, clear intent/allowance refs to avoid stale state.
- On user cancel, call `deny()` for the active hook and reset UI state.
- If a hook is set but no `allow()`/`deny()` is called, the intent remains pending.

## Expand error-handling patterns

### Normalize errors
- The SDK throws `NexusError` for known failures.
- Import from SDK:
  - `import { NexusError, ERROR_CODES } from '@avail-project/nexus-core'`

### Map common error codes to UX responses
- `USER_DENIED_INTENT` / `USER_DENIED_ALLOWANCE` / `USER_DENIED_INTENT_SIGNATURE`:
  - Treat as user cancel; show a neutral message and reset UI.
- `INVALID_VALUES_ALLOWANCE_HOOK`:
  - Fix `decisions` length/type and resubmit.
- `SDK_NOT_INITIALIZED` / `WALLET_NOT_CONNECTED` / `CONNECT_ACCOUNT_FAILED`:
  - Prompt user to reconnect; re-run `sdk.initialize(...)`.
- `INVALID_INPUT` / `INVALID_ADDRESS_LENGTH`:
  - Validate inputs; block submission until fixed.
- `INSUFFICIENT_BALANCE` / `NO_BALANCE_FOR_ADDRESS`:
  - Prompt user to reduce amount or fund source chain.
- `TOKEN_NOT_SUPPORTED` / `CHAIN_NOT_FOUND`:
  - Block submission and update selectors.
- `QUOTE_FAILED` / `SWAP_FAILED` / `RATES_CHANGED_BEYOND_TOLERANCE` / `SLIPPAGE_EXCEEDED_ALLOWANCE`:
  - Re-run quote or suggest a smaller amount.
- `RFF_FEE_EXPIRED`:
  - Re-simulate and re-submit the transaction.
- `TRANSACTION_TIMEOUT` / `LIQUIDITY_TIMEOUT`:
  - Show “pending” state and allow retry or check intent history.
- `TRANSACTION_REVERTED`:
  - Display failure with explorer link if available.
- `COSMOS_ERROR` / `INTERNAL_ERROR`:
  - Show a generic error and log `err.data?.details` for support.

### Use a helper pattern
```ts
import { NexusError, ERROR_CODES } from '@avail-project/nexus-core';

export function getReadableNexusError(err: unknown): string {
  if (err instanceof NexusError) {
    switch (err.code) {
      case ERROR_CODES.USER_DENIED_INTENT:
      case ERROR_CODES.USER_DENIED_ALLOWANCE:
      case ERROR_CODES.USER_DENIED_INTENT_SIGNATURE:
        return 'Transaction cancelled by user';
      case ERROR_CODES.INSUFFICIENT_BALANCE:
        return 'Insufficient balance';
      case ERROR_CODES.SLIPPAGE_EXCEEDED_ALLOWANCE:
      case ERROR_CODES.RATES_CHANGED_BEYOND_TOLERANCE:
        return 'Price changed too much. Please try again.';
      case ERROR_CODES.TRANSACTION_TIMEOUT:
        return 'Timed Out';
      default:
        return err.message;
    }
  }
  if (err instanceof Error) return err.message;
  return 'Unknown error';
}
```

### Clean up on error
- Clear intent/allowance refs and reset progress state:
  - `intentRef.current = null; allowanceRef.current = null; swapIntentRef.current = null;`
  - reset step UI and pending flags
