nexus-elements-nexus-provider
Nexus Elements - NexusProvider
Install and wire provider
- Install:
npx shadcn@latest add @nexus-elements/nexus-provider
- Ensure dependencies exist:
@avail-project/nexus-corewagmi
Wrap your app
"use client";
import NexusProvider from "@/components/nexus/NexusProvider";
export function AppNexusProvider({ children }: { children: React.ReactNode }) {
return (
<NexusProvider config={{ network: "mainnet", debug: false }}>
{children}
</NexusProvider>
);
}
Initialize SDK on wallet connect
- Resolve an EIP-1193 provider from your wallet stack.
- Call
handleInit(provider)after wallet status becomes connected. - Pass only providers with
request().
"use client";
import { useEffect } from "react";
import { useAccount, useConnectorClient } from "wagmi";
import type { EthereumProvider } from "@avail-project/nexus-core";
import { useNexus } from "@/components/nexus/NexusProvider";
export function InitNexusOnConnect() {
const { status, connector } = useAccount();
const { data: walletClient } = useConnectorClient();
const { handleInit } = useNexus();
useEffect(() => {
if (status !== "connected") return;
void (async () => {
const mobileProvider = walletClient
? ({ request: (args: unknown) => walletClient.request(args as never) } as EthereumProvider)
: undefined;
const desktopProvider = await connector?.getProvider();
const provider = mobileProvider ?? (desktopProvider as EthereumProvider | undefined);
if (!provider || typeof provider.request !== "function") return;
await handleInit(provider);
})();
}, [status, connector, walletClient, handleInit]);
return null;
}
Understand provider lifecycle (current implementation)
handleInit(provider):- guard: skips if already initialized or loading.
- validate provider shape.
- call
initializeNexus(provider). - call setup preload (
supported chains/tokens, bridge balances, Coinbase rates). - attach hooks (
intent,allowance,swapIntent).
initializeNexus(provider):- calls
sdk.initialize(provider)once. - sets
nexusSDKstate.
- calls
deinitializeNexus():- calls
sdk.deinit(). - clears balances, rates, supported chains/tokens, and hook refs.
- calls
- Auto-disconnect behavior:
- provider uses
useAccountEffectfrom wagmi to deinit on disconnect.
- provider uses
SDK functions NexusProvider relies on
- Init/lifecycle:
sdk.initialize(provider)sdk.deinit()sdk.isInitialized()
- Metadata and balances:
sdk.utils.getSupportedChains(...)sdk.utils.getSwapSupportedChainsAndTokens()sdk.getBalancesForBridge()sdk.getBalancesForSwap()sdk.utils.getCoinbaseRates()
- Hook wiring:
sdk.setOnIntentHook(...)sdk.setOnAllowanceHook(...)sdk.setOnSwapIntentHook(...)
Understand hook contract
- Any active hook payload includes
allow()anddeny(). - If a widget sets hook-driven confirmation UI, always call one of them.
- Not calling
allow()/deny()leaves a flow pending.
useNexus() API surface
- Core:
nexusSDK,loading,network
- Lifecycle:
handleInit,initializeNexus,deinitializeNexus,attachEventHooks
- Hook refs:
intent,allowance,swapIntent
- Data:
bridgableBalance,swapBalance,exchangeRatesupportedChainsAndTokens,swapSupportedChainsAndTokens
- Helpers:
fetchBridgableBalance,fetchSwapBalance,getFiatValue(amount, token)
E2E validation
- Connect wallet and assert
nexusSDKbecomes non-null. - Assert supported chains/tokens are populated.
- Assert bridge and swap balances are fetched.
- Trigger a widget flow and confirm corresponding hook ref is populated.
- Disconnect wallet and confirm state clears.
Troubleshoot
Invalid EIP-1193 provider:- use
connector.getProvider()or a wallet client wrapper exposingrequest().
- use
- Init succeeds but widgets fail:
- ensure
handleInitruns before mounting interactive flows.
- ensure
- Missing fiat values:
- check
exchangeRate; fallback is token amount *1if rate missing.
- check
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-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.
21nexus-elements-unified-balance
Integrate the UnifiedBalance element for cross-chain token balance views in React/TypeScript apps. Use when installing or debugging bridgeable/swappable balance aggregation, per-chain breakdown rendering, and formatting powered by Nexus SDK balance APIs.
21