nexus-elements-swaps
Nexus Elements - Swaps
Install
- Install widget:
npx shadcn@latest add @nexus-elements/swaps
- Ensure
NexusProvideris installed and initialized before rendering.
Required setup before rendering
- Ensure
useNexus().nexusSDKis initialized. - Ensure
swapBalanceis fetched (fetchSwapBalanceis called by hook if missing).
Initialize SDK (required once per app)
- On wallet connect, resolve an EIP-1193 provider and call
useNexus().handleInit(provider). - Wait for
useNexus().nexusSDKbefore swap simulation starts. - Re-run init after reconnect if wallet session resets.
Render widget
"use client";
import SwapWidget from "@/components/swaps/swap-widget";
export function SwapPanel() {
return (
<SwapWidget
onStart={() => {
// swap started
}}
onComplete={(amount) => {
console.log("Destination amount:", amount);
}}
onError={() => {
console.error("Swap failed");
}}
/>
);
}
Live prop contract
onStart?(): voidonComplete?(amount?: string): voidonError?(): void(component signature currently ignores message argument)
SDK flow details (under the hood)
- Exact-in mode:
- build
ExactInSwapInput - call
sdk.swapWithExactIn(input, { onEvent })
- build
- Exact-out mode:
- build
ExactOutSwapInput - optional
fromSourcescomes from selected source options - call
sdk.swapWithExactOut(input, { onEvent })
- build
- Intent confirmation:
- uses
swapIntent.currentfromsetOnSwapIntentHook - final execution happens after
swapIntent.current.allow()incontinueSwap()
- uses
- Event mapping:
- listens for
NEXUS_EVENTS.SWAP_STEP_COMPLETE - captures source/destination explorer URLs from step payloads
- listens for
Understand mode behavior
- Widget auto-simulates when inputs are valid (debounced).
exactIn: requires source chain/token/amount and destination chain/token.exactOut: requires destination chain/token/amount; sources are auto-selected unless overridden.- Exact-out source selection:
- users can toggle source options.
- if selection changes, widget re-simulates before allowing continue.
E2E verification
- Enter exact-in inputs and confirm simulation starts automatically.
- Confirm swap intent appears and progress steps render.
- Switch to exact-out, set destination amount, and verify source options load.
- Change exact-out source selection and confirm re-simulation occurs.
- Continue swap and confirm explorer links for source/destination swaps.
- Confirm balances refresh after success.
Common failure cases
- No valid inputs for current mode:
- ensure required fields for active mode are populated.
- Stale intent after input changes:
- widget calls
deny()and resets; re-enter or wait for re-simulation.
- widget calls
- Simulation loops:
- clear errors and verify destination/source token-chain combinations are supported.
More from availproject/nexus-elements
nexus-elements-deposit
Integrate the Deposit element for swap-plus-execute deposit flows in React/TypeScript apps. Use when installing or debugging destination-fixed deposits, execute call builders, swap-intent confirmation UX, and `sdk.swapAndExecute` progress from quote to completion.
26nexus-elements-overview
End-to-end integration guide for Nexus Elements in any TypeScript/React codebase. Use when setting up Nexus Elements from scratch, choosing which widget to install, wiring NexusProvider + wallet initialization, or validating bridge/transfer/swap/deposit/history behavior in production-like flows.
24nexus-elements-common
Use shared Nexus Elements hooks, transaction-step helpers, and constants to build custom Nexus UX. Use when extending widgets or implementing custom bridge/transfer/swap/deposit flows that need debouncing, polling, step orchestration, or Nexus error normalization.
23nexus-elements-nexus-provider
Install and configure NexusProvider for Nexus Elements with full SDK lifecycle wiring. Use when setting up or debugging SDK initialization, wallet/provider connection, hook attachment (intent/allowance/swapIntent), balance preloads, exchange-rate support, and provider context consumption in React/TypeScript apps.
23nexus-elements-bridge-deposit
Integrate the Bridge Deposit element for bridge-plus-execute deposit flows in React/TypeScript apps. Use when installing or debugging source-chain constrained deposits, bridge+execute simulation, intent/allowance approvals, and `sdk.bridgeAndExecute` transaction execution.
22nexus-elements-fast-bridge
Integrate the FastBridge element for intent-based cross-chain bridge UX in React/TypeScript apps. Use when installing or debugging self-bridge flows, source-chain selection, allowance gating, step progress events, and `sdk.bridge` execution end-to-end.
21