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

# AnafiSwapExecutor

> Reference of the contract that calls the swap router for a swap leg of the ERC20Allow adapter.

After you read this page, you know how a swap leg reaches the router, what the executor pays to the account, and what the executor keeps.

`AnafiSwapExecutor` calls one swap router for one swap leg of the [ERC20Allow adapter](https://docs.ana.fi/contracts/erc20-allow-adapter.md). It is a plain contract. It has no proxy, no owner and no admin function. Only its adapter can call it.

The executor holds no allowance of any account. The adapter pulls the sold amount from the account into the executor first, and then calls `swap`.

## Constructor

```solidity theme={null}
constructor(address adapter_)
```

Records the adapter proxy that alone can call `swap`. Reverts `AdapterNotUsable(address adapter)` when `adapter_` is zero or has no code. The adapter cannot change later.

## swap

```solidity theme={null}
function swap(
    address tokenIn,
    uint256 amountIn,
    address tokenOut,
    address router,
    address spender,
    bytes calldata routerData,
    address recipient
) external returns (uint256 amountOut, uint256 amountInUnspent)
```

Access: only `adapter`. The ERC20Allow adapter calls it with `recipient` set to the account of the payload, and only with a router and spender pair from its allowed list.

The steps:

1. Reverts `CallerNotAdapter()` when `msg.sender` is not `adapter`.
2. Reverts `SwapReentered()` when a swap runs already. The lock covers the router call and each token callback.
3. Records two balances: the `tokenIn` balance before the adapter funded the executor, which is the current balance minus `amountIn`, and the current `tokenOut` balance.
4. Approves `spender` for exactly `amountIn` of `tokenIn`, with OpenZeppelin `forceApprove`.
5. Calls `router` with `routerData` and no value. A revert of the router reverts `RouterCallFailed(bytes revertData)`.
6. Sets the approval of `spender` back to zero.
7. Computes `amountOut` as the growth of the `tokenOut` balance in this call.
8. Computes `amountInUnspent` as the part of `amountIn` that is still in the executor, at most `amountIn`.
9. Sends `amountOut` of `tokenOut` and `amountInUnspent` of `tokenIn` to `recipient`. A zero amount is not sent.

| Return | Description |
| - | - |
| `amountOut` | The `tokenOut` that this call received and sent to `recipient`. |
| `amountInUnspent` | The `tokenIn` that the router did not take and that went back to `recipient`. |

The adapter does not use these return values. It measures the `tokenOut` growth at the account itself and compares it with `minAmountOut`.

A failed `transfer` of the output or the refund reverts with the OpenZeppelin error `SafeERC20FailedOperation(address token)`, or with the error of the token.

## View

| Function | Returns | Description |
| - | - | - |
| `adapter()` | `address` | The ERC20Allow adapter proxy that alone can call `swap`. |

The ERC20Allow adapter accepts an executor in `initializeSwaps` only when `adapter()` returns that adapter.

## Errors

| Error | Raised by | Cause |
| - | - | - |
| `AdapterNotUsable(address adapter)` | constructor | The adapter is zero or has no code. |
| `CallerNotAdapter()` | `swap` | The caller is not the adapter. |
| `SwapReentered()` | `swap` | A router or token callback started a second swap. |
| `RouterCallFailed(bytes revertData)` | `swap` | The router call reverted. `revertData` is the revert data of the router. |
| `SafeERC20FailedOperation(address token)` | `swap` | A token `transfer` returned `false`. |

Each of these errors inside a swap leg makes the legs of the redeem fail. The redeem still spends its nonce and pays its gas charge. See [Redeem](https://docs.ana.fi/contracts/redeem.md).

The executor has no events.

## What the executor keeps

The executor has no function that moves a token out, other than `swap`. A token that a route leaves in the executor stays there:

* the dust of an intermediate token of a route,
* `tokenIn` that a router returns above the amount of the leg.

No later swap can use such a balance. The executor approves only `amountIn` of its own leg, and pays only the balance changes of its own call. The value is lost to the owner whose swap left it.

## Invariants

* Only `adapter` can call `swap`, and the adapter never changes.
* After `swap`, the approval of `spender` for `tokenIn` is zero.
* `amountInUnspent` is never more than `amountIn`.
* The executor sends tokens only to `recipient`.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.