Skip to main content

executeSwapTransaction

Executes a swap transaction on-chain using the connected wallet. This handles token approvals (when needed) and the main swap transaction signing, delegating to the wallet provider.
When using the checkout flow, submitCheckoutTransaction calls this function internally. Use executeSwapTransaction directly only when building a standalone swap flow or a custom checkout signing flow.

Usage

import { executeSwapTransaction } from '@dynamic-labs-sdk/client';

const { transactionHash } = await executeSwapTransaction({
  walletAccount: connectedWallet,
  signingPayload: quoteResponse.signingPayload,
  onStepChange: (step) => {
    console.log('Step:', step); // 'approval' or 'transaction'
  },
});

console.log('Transaction hash:', transactionHash);

Parameters

ParameterTypeDescription
walletAccountWalletAccountThe connected wallet account to execute the swap with. Must be on a supported chain.
signingPayloadSwapTransactionSigningPayloadThe signing payload from a quote response (via getSwapQuote or getCheckoutTransactionQuote).
onStepChange(step: 'approval' | 'transaction') => void (optional)Callback invoked when the execution step changes. 'approval' fires for ERC-20 token allowance approval; 'transaction' fires for the main swap.

Returns

Promise<{ transactionHash: string }> - The on-chain transaction hash.

Step Lifecycle

StepDescription
approvalSigning a token approval transaction. Only fires for ERC-20 tokens when the current allowance is insufficient for the swap amount.
transactionSigning the main swap transaction.

Examples

Standalone swap

import {
  getSwapQuote,
  executeSwapTransaction,
  getWalletAccounts,
} from '@dynamic-labs-sdk/client';

const walletAccounts = getWalletAccounts();
const wallet = walletAccounts[0];

// Get a swap quote
const quote = await getSwapQuote({
  from: {
    address: wallet.address,
    chain: 'EVM',
    networkId: '1',
    tokenAddress: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', // USDC
    amount: '1000000', // 1 USDC (6 decimals)
  },
  to: {
    address: wallet.address,
    chain: 'EVM',
    networkId: '1',
    tokenAddress: '0x0000000000000000000000000000000000000000', // ETH
  },
  slippage: 0.005,
});

// Execute the swap
const { transactionHash } = await executeSwapTransaction({
  walletAccount: wallet,
  signingPayload: quote.signingPayload,
  onStepChange: (step) => console.log('Step:', step),
});

console.log('Swap executed:', transactionHash);

With progress UI

import { useState } from 'react';
import { executeSwapTransaction } from '@dynamic-labs-sdk/client';

const SwapButton = ({ walletAccount, signingPayload, onComplete }) => {
  const [step, setStep] = useState(null);

  const handleSwap = async () => {
    const { transactionHash } = await executeSwapTransaction({
      walletAccount,
      signingPayload,
      onStepChange: setStep,
    });

    onComplete(transactionHash);
  };

  return (
    <button onClick={handleSwap} disabled={!!step}>
      {step === 'approval' && 'Approving token...'}
      {step === 'transaction' && 'Signing swap...'}
      {!step && 'Swap'}
    </button>
  );
};
This example uses React; the JavaScript SDK is framework-agnostic and can be used with any frontend or in Node.

Supported Chains

executeSwapTransaction supports EVM and Solana chains. Attempting to use an unsupported chain throws an error.