> ## Documentation Index
> Fetch the complete documentation index at: https://docs.dynamic.xyz/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# executeSwapTransaction

# 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.

<Note>
  When using Flow, [`submitFlowTransaction`](/javascript/reference/client/submit-flow-transaction) calls this function internally. Use `executeSwapTransaction` directly only when building a standalone swap flow or a custom signing flow.
</Note>

## Usage

```javascript theme={"system"}
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

| Parameter        | Type                                                     | Description                                                                                                                                                                     |
| ---------------- | -------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `walletAccount`  | `WalletAccount`                                          | The connected wallet account to execute the swap with. Must be on a supported chain.                                                                                            |
| `signingPayload` | `SwapTransactionSigningPayload`                          | The signing payload from a quote response (via [`getSwapQuote`](/javascript/reference/client/get-swap-quote) or [`getFlowQuote`](/javascript/reference/client/get-flow-quote)). |
| `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

| Step          | Description                                                                                                                        |
| ------------- | ---------------------------------------------------------------------------------------------------------------------------------- |
| `approval`    | Signing a token approval transaction. Only fires for ERC-20 tokens when the current allowance is insufficient for the swap amount. |
| `transaction` | Signing the main swap transaction.                                                                                                 |

## Examples

### Standalone swap

```javascript theme={"system"}
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

```javascript theme={"system"}
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>
  );
};
```

<Note>
  This example uses React; the JavaScript SDK is framework-agnostic and can be used with any frontend or in Node.
</Note>

## Supported Chains

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

On Solana, the signing payload is a serialized versioned transaction and there are no token approvals — only the `'transaction'` step fires (`'approval'` is EVM-only). To quote a Solana swap, pass `chain: 'SOL'`, `networkId: '101'`, and a token mint (or `11111111111111111111111111111111` for native SOL) to [`getSwapQuote`](/javascript/reference/client/get-swap-quote); `executeSwapTransaction` then signs and broadcasts it with the connected Solana wallet.

## React

```tsx theme={"system"}
import { useExecuteSwapTransaction } from '@dynamic-labs-sdk/react-hooks';
import { useState } from 'react';

function SwapButton({ walletAccount, signingPayload, onComplete }) {
  const { mutate: executeSwap, isPending } = useExecuteSwapTransaction();
  const [step, setStep] = useState(null);

  return (
    <button
      onClick={() =>
        executeSwap(
          { walletAccount, signingPayload, onStepChange: setStep },
          { onSuccess: ({ transactionHash }) => onComplete(transactionHash) },
        )
      }
      disabled={isPending}
    >
      {step === 'approval' && 'Approving token...'}
      {step === 'transaction' && 'Signing swap...'}
      {!step && 'Swap'}
    </button>
  );
}
```

## Related

* [`getSwapQuote`](/javascript/reference/client/get-swap-quote) - Get a swap quote with signing payload
* [`getSwapStatus`](/javascript/reference/client/get-swap-status) - Check swap execution status
* [`submitFlowTransaction`](/javascript/reference/client/submit-flow-transaction) - Full flow (uses this internally)
