Trigger a swap

1. Import quote and swap execution functions from useWallet hook

export function Page() {
     const {
         computeQuote,
         executeSwap,
         executeHeadlessSwap
     } = useWallet();
}

2. Compute quote for a token swap

const quoteResult: any = await calculateQuote({
    fromToken: {
      amount: ethers
        .parseUnits(fromAmount, fromToken.decimals)
        .toString(),
      chainId: fromChain.chainId,
      tokenAddress: fromChain.address,
      
      // Optional
      metadata: {
        tokenName: fromToken.name,
        tokenSymbol: fromToken.symbol,
        decimals: fromToken.decimals,
        logoURI: fromToken.logoURI,
        chainIds: fromToken.chainIds,
      },
    },
    toToken: {
      chainId: toChain.chainId,
      tokenAddress: toChain.address,
      
      // Optional
      metadata: {
        tokenName: toToken.name,
        tokenSymbol: toToken.symbol,
        decimals: toToken.decimals,
        logoURI: toToken.logoURI,
        chainIds: toToken.chainIds,
      },
    },
});

3. Execute the swap

3.a. Normal swap

Trigger the swap modal built into the embedded wallet

Calling swap() triggers the swap modal where user's can execute their trade

3.b. Headless swap

A headless swap is a swap that's executed directly from the context of the parent application without triggering the embedded wallet swap popup. This alternate method can be used when the app wants complete end-to-end control over the swap UX without relying on the embedded wallet's swap interface.

Quote Selection

For headless swaps, since user's don't go through the embedded wallet interface, the quote response provides the app developer the option of choosing between the quote with best price execution and the quote that executes the swap the fastest. The developer can pass this decision on to the user or choose on behalf of the user.

Quote results have 2 types: quote.bestQuote which returns the highest amount out for the given input token amount and swap pair and quote.fastestQuote which executes the swap in the lowest possible time. When quote.duplicate is true then both the quotes (the one with the best rate and one with the fastest execution time) are from the same provider and there is no choice to be made. If not, the app developer can choose on behalf of the user or

Last updated