Swaps
Let users swap assets across chains with either an exact-in (fixed input amount) or exact-out (fixed output amount).
MAIN POINTS TO NOTE:
- Setting
exactIntotrue(default) renders the Exact-In flow which callsswapWithExactIn()under the hood;falserenders the Exact-Out flow which callsswapWithExactOut(). - Prefill objects (
exactInprefill,exactOutprefill) become read-only in the UI. - The widget streams
NEXUS_EVENTS.SWAP_STEP_COMPLETEupdates to keep the transaction progress UI in sync and exposesonStart,onError,onCompletecallbacks. - You can find a quickstart example for using
nexus-elementsin Nexus Elements Quickstart.
Install
You can find the source code for the Swaps Widget here:
To install the Swaps Widget into your project, you can use the following command:
pnpm
Terminal
pnpm dlx shadcn@latest add https://elements.nexus.availproject.org/r/swaps.jsonProps
TypeScript
interface SwapsProps {
exactIn?: boolean;
onComplete?: (amount?: string) => void;
onStart?: () => void;
onError?: (message: string) => void;
exactInprefill?: {
fromChainID?: number;
fromToken?: string;
fromAmount?: string;
toChainID?: number;
toToken?: string;
};
exactOutprefill?: {
toAmount?: string;
toChainID?: number;
toToken?: string;
};
}exactIn: Whether to use the exact-in flow. Set totrueby default.onComplete: Callback function called when the swap is complete.onStart: Callback function called when the swap begins.onError: Callback function called when the swap fails.exactInprefill: Optional prefill object for the exact-in flow.exactOutprefill: Optional prefill object for the exact-out flow.
Usage
Exact In
tsx
<Swaps
exactIn
onComplete={completeCurrent}
onError={handleError}
onStart={handleStart}
exactInprefill={{
fromToken: "USDC",
fromChainID: SUPPORTED_CHAINS.BASE,
fromAmount: "100",
toToken: "USDT",
toChainID: SUPPORTED_CHAINS.ARBITRUM,
}}
/>Exact Out
tsx
<Swaps
exactIn={false}
onComplete={completeCurrent}
onError={handleError}
onStart={handleStart}
exactOutprefill={{
toToken: "USDT",
toChainID: SUPPORTED_CHAINS.ARBITRUM,
toAmount: "100",
}}
/>Last updated on