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
swap. Reverts AdapterNotUsable(address adapter) when adapter_ is zero or has no code. The adapter cannot change later.
swap
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:
- Reverts
CallerNotAdapter()whenmsg.senderis notadapter. - Reverts
SwapReentered()when a swap runs already. The lock covers the router call and each token callback. - Records two balances: the
tokenInbalance before the adapter funded the executor, which is the current balance minusamountIn, and the currenttokenOutbalance. - Approves
spenderfor exactlyamountInoftokenIn, with OpenZeppelinforceApprove. - Calls
routerwithrouterDataand no value. A revert of the router revertsRouterCallFailed(bytes revertData). - Sets the approval of
spenderback to zero. - Computes
amountOutas the growth of thetokenOutbalance in this call. - Computes
amountInUnspentas the part ofamountInthat is still in the executor, at mostamountIn. - Sends
amountOutoftokenOutandamountInUnspentoftokenIntorecipient. 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 thanswap. A token that a route leaves in the executor stays there:
- the dust of an intermediate token of a route,
tokenInthat a router returns above the amount of the leg.
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
adaptercan callswap, and the adapter never changes. - After
swap, the approval ofspenderfortokenInis zero. amountInUnspentis never more thanamountIn.- The executor sends tokens only to
recipient.