nexus-sdk-hooks-events
Original:🇺🇸 English
Translated
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).
2installs
Sourceavailproject/nexus-sdk
Added on
NPX Install
npx skill4agent add availproject/nexus-sdk nexus-sdk-hooks-eventsTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →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 or
allow(...)or the flow will stall.deny() - Optionally auto-deny after a timeout to avoid hanging UX.
Handle intent hook (bridge / bridgeAndTransfer / bridgeAndExecute)
Signature
ts
sdk.setOnIntentHook((data) => {
// data: OnIntentHookData
});OnIntentHookData
- — continue the flow
allow(): void - — reject the flow
deny(): void - — readable intent info for UI
intent: ReadableIntent - — refresh the intent
refresh(selectedSources?: number[]): Promise<ReadableIntent>
How to use
- Store in a ref so UI can call
data/allow()later.deny() - Call on an interval (e.g., every 15s) if you show intent details.
refresh() - If the user closes the modal or cancels, call and clear the ref.
deny()
Handle allowance hook (approval control)
Signature
ts
sdk.setOnAllowanceHook((data) => {
// data: OnAllowanceHookData
});OnAllowanceHookData
allow(decisions): void- must match the number of
decisionssources - each entry can be ,
'min','max', or numeric stringbigint
deny(): voidsources: 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 array in the same order as
decisions.sources - Call to continue, or
allow(decisions)to cancel.deny() - Ensure or the SDK will reject with
decisions.length === sources.length.INVALID_VALUES_ALLOWANCE_HOOK
Handle swap intent hook (swap flows)
Signature
ts
sdk.setOnSwapIntentHook((data) => {
// data: OnSwapIntentHookData
});OnSwapIntentHookData
allow(): voiddeny(): voidintent: SwapIntentrefresh(): Promise<SwapIntent>
How to use
- Store and show a UI summary.
data - Call to proceed or
allow()to cancel.deny()
Stream events for progress UI
Attach listeners
- All main operations accept in options.
{ onEvent }
Event names and payloads
- →
NEXUS_EVENTS.STEPS_LISTBridgeStepType[] - →
NEXUS_EVENTS.STEP_COMPLETEBridgeStepType - →
NEXUS_EVENTS.SWAP_STEP_COMPLETESwapStepType
Typical usage
- Use to seed step trackers.
STEPS_LIST - Use and
STEP_COMPLETEto update progress and explorer links.SWAP_STEP_COMPLETE
Handle errors and cancellation
- If an operation throws, clear intent/allowance refs to avoid stale state.
- On user cancel, call for the active hook and reset UI state.
deny() - If a hook is set but no /
allow()is called, the intent remains pending.deny()
Expand error-handling patterns
Normalize errors
- The SDK throws for known failures.
NexusError - 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 length/type and resubmit.
decisions
- Fix
- /
SDK_NOT_INITIALIZED/WALLET_NOT_CONNECTED:CONNECT_ACCOUNT_FAILED- Prompt user to reconnect; re-run .
sdk.initialize(...)
- Prompt user to reconnect; re-run
- /
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 for support.
err.data?.details
- Show a generic error and log
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 'Transaction timed out. Please retry.';
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