nexus-sdk-swap-flows
Swap Flows
Call swapWithExactIn(input, options?)
- Use when input amount is fixed and sources are known.
- Signature:
sdk.swapWithExactIn(input, { onEvent? })
- Params (
ExactInSwapInput):from: { chainId: number; amount: bigint; tokenAddress: Hex }[]toChainId: numbertoTokenAddress: Hex
- Notes:
- Ensure
fromamounts are in smallest units. - Use chain-specific token addresses (see
TOKEN_CONTRACT_ADDRESSES).
- Ensure
- Result (
SwapResult):{ success: true; result: SuccessfulSwapResult }
Call swapWithExactOut(input, options?)
- Use when desired output amount is fixed.
- Signature:
sdk.swapWithExactOut(input, { onEvent? })
- Params (
ExactOutSwapInput):toChainId: numbertoTokenAddress: HextoAmount: biginttoNativeAmount?: bigint(optional native token output)fromSources?: { chainId: number; tokenAddress: Hex }[](optional)
- Notes:
- If
fromSourcesis omitted, SDK auto-selects sources.
- If
Call swapAndExecute(input, options?)
- Use to perform swap (if needed) and then execute a contract call.
- Signature:
sdk.swapAndExecute(input, { onEvent? })
- Params (
SwapAndExecuteParams):toChainId: numbertoTokenAddress: HextoAmount: bigintfromSources?: { chainId: number; tokenAddress: Hex }[]execute: SwapExecuteParams
SwapExecuteParams:to: Hexdata?: Hexvalue?: bigintgas: bigintgasPrice?: 'low' | 'medium' | 'high'tokenApproval?: { token: Hex; amount: bigint; spender: Hex }
- Result (
SwapAndExecuteResult):swapResult: SuccessfulSwapResult | null(null if swap skipped)
Use token addresses and chains
- Use SDK constants for addresses and chain IDs:
SUPPORTED_CHAINSTOKEN_CONTRACT_ADDRESSES
Attach swap intent hook
- Set
sdk.setOnSwapIntentHook(...)and callallow()to proceed. - If the hook is not set, the SDK auto-approves.
Stream swap events
- Listen for
NEXUS_EVENTS.SWAP_STEP_COMPLETE. - Handle special cases like swap skipped (e.g., already on destination token).
Convert amounts to bigint
- Use
sdk.utils.parseUnits(value, decimals)or sdk.convertTokenReadableAmountToBigInt(value, tokenSymbol, chainId)when decimals vary by chain.
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