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

# Execute Swap

> Execute a swap transaction using a quote

## Overview

Execute a swap in one function call. The SDK handles building calls, approvals, and transaction execution automatically.

## SDK Method

```typescript theme={null}
executeSwap(
  params: InvokeSwapParams,
  options?: AvnuOptions
): Promise<InvokeTransactionResponse>
```

## Parameters

<ParamField path="params" type="InvokeSwapParams" required>
  <Expandable title="properties">
    <ParamField path="quote" type="Quote" required>
      The quote object returned by `getQuotes`
    </ParamField>

    <ParamField path="slippage" type="number" required>
      Maximum slippage as a decimal (0..1). E.g., 0.001 = 0.1%, 0.01 = 1%
    </ParamField>

    <ParamField path="provider" type="AccountInterface" required>
      The Starknet account executing the swap
    </ParamField>

    <ParamField path="executeApprove" type="boolean" default="true">
      Whether to automatically handle token approvals
    </ParamField>

    <ParamField path="paymaster" type="object">
      Optional paymaster configuration
    </ParamField>
  </Expandable>
</ParamField>

## Example

<CodeGroup>
  ```typescript Basic Swap theme={null}
  import { RpcProvider, Account } from 'starknet';
  import { getQuotes, executeSwap } from '@avnu/avnu-sdk';
  import { parseUnits } from 'ethers';

  // ... setup provider and account ...

  // 1. Get quotes
  const quotes = await getQuotes({
    sellTokenAddress: ETH_ADDRESS,
    buyTokenAddress: USDC_ADDRESS,
    sellAmount: parseUnits('1', 18),
    takerAddress: account.address,
  });

  // 2. Execute swap
  const result = await executeSwap({
    provider: account,
    quote: quotes[0],
    slippage: 0.005, // 0.5%
  });

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

  ```typescript With Paymaster theme={null}
  // Execute gasless swap
  const result = await executeSwap({
    provider: account,
    quote: quotes[0],
    slippage: 0.005, // 0.5%
    paymaster: {
      active: true,
      provider: paymasterProvider, // Your Paymaster instance
      params: {} // Paymaster params
    }
  });
  ```

  ```typescript Manual Approval theme={null}
  // If token is already approved
  const result = await executeSwap({
    provider: account,
    quote: quotes[0],
    slippage: 0.005, // 0.5%
    executeApprove: false,
  });
  ```
</CodeGroup>

## Response

```typescript theme={null}
{
  transactionHash: '0x...'
}
```

## Slippage Protection

Slippage is defined as a **decimal** (0..1 range):

* `0.001` = 0.1%
* `0.005` = 0.5%
* `0.01` = 1%

The SDK automatically calculates the minimum received amount:
`minAmount = buyAmount * (1 - slippage)`

## Token Approval

By default (`executeApprove: true`), the SDK:

1. Checks the user's current allowance for the avnu router.
2. If insufficient, adds an `approve` call to the transaction.
3. Executes `approve` and `swap` in a single multicall (atomic).

## Best Practices

<AccordionGroup>
  <Accordion title="Always use fresh quotes" icon="clock">
    With Starknet's block time of \~2 seconds, quotes become stale after just 1 block. Refresh quotes every block before execution.
  </Accordion>

  <Accordion title="Set appropriate slippage" icon="percentage">
    * Stable pairs: 0.001 - 0.003 (0.1% - 0.3%)
    * Regular pairs: 0.005 - 0.01 (0.5% - 1%)
    * Volatile pairs: 0.01 - 0.03 (1% - 3%)
  </Accordion>

  <Accordion title="Handle failures gracefully" icon="shield">
    Implement retry logic with exponential backoff for network issues.
  </Accordion>
</AccordionGroup>
