Skip to main content
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. 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

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

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

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

Errors

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