erc20_allow account grants, how caps and the block switch work, how a swap leg moves funds, and every function, event and error of the contract.
AnafiDelegationAdapterERC20Allow is the adapter of the erc20_allow account kind. It runs behind an ERC-1967 proxy with UUPS upgrades. It has no owner of its own. Its admin is AnafiRegistry.owner().
The permission
The account grants the permission token by token, with the ERC-20 allowance of the token. The adapter keeps no grant record.
See ERC-20 account flow and ERC20Allow for the SDK calls.
The adapter cannot move the native coin. It has no native leg, and it refuses the native coin as a gas token. It does not check the code at the account address.
The allowance is state of the token. The block switch and the caps are state of the adapter. Only the account can set its own allowance with
approve.Types
abi.encode(address account, Leg[] legs).
Any other
kind makes the legs fail with UnsupportedLegKind(uint8 kind).
Functions for the account
setBlocked
msg.sender and emits BlockedSet(msg.sender, blocked). While the switch is on, redeemAsEngine reverts OwnerBlocked(account) before it collects a gas charge, so the redeem spends no nonce.
The adapter reads the switch at the start of a redeem and again at the start of the legs. A switch that a token callback sets during the legs stops the next redeem, not the later legs of the current one.
setCaps
msg.sender. It emits CapSet(msg.sender, token, limit, periodSeconds) for each entry.
- An entry with
periodSecondsbelowMIN_CAP_PERIOD_SECONDS(3600) reverts the whole call withCapPeriodTooShort(uint32 periodSeconds). - The window of a cap is
block.timestamp / periodSeconds. Windows are fixed. They start at multiples ofperiodSecondsafter the Unix epoch, not at the time of the call. - An entry for a cap that is set, with the same
periodSeconds, in the current window, keeps the amount spent in that window. Each other entry starts the counter at zero. - A
limitof zero lets only zero-amount pulls pass.
clearCaps
msg.sender and emits CapCleared(msg.sender, token) for each token, also for a token that had no cap.
claimGasCredit
msg.sender in token to to. A credit comes from a failed payment of a charge or a refund. The call needs no allowance and works while the switch is on. The adapter clears the credit before it pays. A failed payment reverts GasCreditClaimFailed() and keeps the credit. A zero credit reverts NoGasCredit(). On success, it emits GasCreditClaimed(msg.sender, token, to, amount).
How a cap counts
A cap counts each pull of its token from the account:
A pull fails with
CapExceeded(owner, token) when the spent amount is more than the limit, or when the pull is more than limit - spent. The counter starts at zero when a new window starts. When clearCaps removes the cap during the legs, or setCaps starts a new counter during the legs, the adapter does not subtract the unused reservation, so the new cap keeps the spend. A setCaps with the same periodSeconds in the same window keeps the counter, and the adapter subtracts the unused reservation from it.
Functions for anyone
redeemAsEngine
Redeem gives the digest, the order of checks, the gas formula, and the full list of revert and failure causes.
redeemRefusal and redeemRefusalWithGas
legs now, or None. Use it to show the owner what to fix, for example an allowance that is too low.
The checks run in this order:
- The block switch. A blocked account returns
(OwnerBlocked, 0). - Only
redeemRefusalWithGaswithmaxChargeabove zero: the gas token. A zerogasToken, or one with no gas overhead, revertsGasTokenNotSupported(gasToken). Then the reservation ofmaxChargecounts against the cap and the allowance ofgasToken. A refusal of the reservation returns the indexlegs.length. - Each leg in order: the leg kind, then the cap of the pulled token, then the allowance of the pulled token. Each leg uses the room that the earlier pulls of the same token left.
- The first swap leg whose router and spender pair is not allowed:
SwapRouterNotAllowed. - The balance of each pulled token. A swap leg adds its
minAmountOutto the balance room oftokenOutfor the later legs. A refusal of the reservation returns the indexlegs.length.
uint256 gives a refusal, not a revert. The views do not check the nonce, the deadline, the signature, the registry list, the swap executor, the shape of a swap leg, the exact debit of a swap leg, or the swap output.
Swap legs
A swap leg runs these steps, in this order, inside the legs of the redeem:swapExecutor()must be set. Else:SwapExecutorNotSet().- The pair
(router, spender)must be allowed. Else:SwapRouterNotAllowed(router, spender). tokenInmust differ fromtokenOut, andamountInandminAmountOutmust be above zero. Else:SwapLegNotValid().amountIncounts against the cap oftokenIn. Else:CapExceeded(owner, token).- The adapter reads the
tokenOutbalance of the account. - The adapter calls
tokenIn.transferFrom(account, swapExecutor, amountIn). The executor balance must grow by exactlyamountIn. Else:SwapInputNotReceived(tokenIn, amountReceived, amountIn). Then the account balance must fall by exactlyamountIn. Else:SwapInputDebitNotExact(tokenIn, amountDebited, amountIn). - The adapter calls
swapExecutor.swap(tokenIn, amountIn, tokenOut, router, spender, routerData, account). The executor pays the output and the unused input to the account. See AnafiSwapExecutor. - The
tokenOutbalance of the account must have grown by at leastminAmountOut. Else:SwapOutputBelowMinimum(tokenOut, amountOut, minAmountOut).
- It sells and buys ERC-20 tokens only. It moves no native coin.
- A token with a transfer tax cannot be the sold token, because step 6 requires an exact amount.
- The adapter checks the exact debit for a swap leg only. A transfer leg of a token that takes a tax from the sender can debit the account more than the leg amount.
- The registry owner controls the pair list. A pair that the registry owner removes stops each swap leg that names it, also a leg that the engine signed before the removal.
Functions of the registry owner
Each function revertsCallerNotRegistryOwner() when the caller is not AnafiRegistry.owner().
When
swapExecutor() returns a non-zero address, initializeSwaps ran already and the executor cannot change.
initialize(address anafiRegistry_) ran once at deployment. It reverts RegistryNotUsable(address registry) when the registry has no code, or when its owner() reverts, returns other than one address word, or returns zero.
Functions that only the adapter calls
redeemAsEngine calls it on the adapter itself with exactly legsGasLimit gas. Any other caller gets CallerNotSelf().
Views and constants
Events
BlockedSet, CapSet and CapCleared, the field owner is the account that called the function.
Errors
A payload that does not decode as
(address, Leg[]) reverts the whole call, so no nonce is spent.
Invariants
- The adapter executes no call data from the payload, except the
routerDatathat the swap executor sends to an allowed router. - Each pull comes from the account of the payload. A transfer leg pays
to. A swap leg pays its output and its unused input to the account. - A nonce is spent at most once on this adapter. It is spent exactly when
redeemAsEnginedoes not revert. - The charge is never more than
maxCharge. The account gets backmaxCharge - charge, or a credit of that amount. - Outside a redeem, the adapter holds only gas credits and funds that someone sent to it directly.
- A zero allowance stops every pull of that token until the account approves the adapter again.
- Revocation does not cancel a signed redeem. A new approval,
setBlocked(false), a cleared or raised cap, a changed period, or the next window can admit an unexpired redeem that was signed before. OnlyvalidUntilbounds it. A redeem that the adapter refused before its legs left its nonce unspent.
Storage
All state is in one ERC-7201 namespace,anafi.storage.AnafiDelegationAdapterERC20Allow, at slot 0x48349d960e8d331c8299c530b4e2a7d3a045c0e55fb8c6b6beb7c663738c7300.