Skip to main content
After you read this page, you know what an 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

The payload of a redeem is abi.encode(address account, Leg[] legs). Any other kind makes the legs fail with UnsupportedLegKind(uint8 kind).

Functions for the account

setBlocked

Sets the block switch of 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

Sets one cap for each entry, in order, for msg.sender. It emits CapSet(msg.sender, token, limit, periodSeconds) for each entry.
  • An entry with periodSeconds below MIN_CAP_PERIOD_SECONDS (3600) reverts the whole call with CapPeriodTooShort(uint32 periodSeconds).
  • The window of a cap is block.timestamp / periodSeconds. Windows are fixed. They start at multiples of periodSeconds after 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 limit of zero lets only zero-amount pulls pass.

clearCaps

Removes the cap of each token for msg.sender and emits CapCleared(msg.sender, token) for each token, also for a token that had no cap.

claimGasCredit

Pays the whole credit of 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

Executes the transfer and swap legs of an engine-signed payload, and charges the account the gas of the whole call in an ERC-20 token. Any address can submit it. Redeem gives the digest, the order of checks, the gas formula, and the full list of revert and failure causes.

redeemRefusal and redeemRefusalWithGas

Returns the first reason why the adapter or a token refuses 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:
  1. The block switch. A blocked account returns (OwnerBlocked, 0).
  2. Only redeemRefusalWithGas with maxCharge above zero: the gas token. A zero gasToken, or one with no gas overhead, reverts GasTokenNotSupported(gasToken). Then the reservation of maxCharge counts against the cap and the allowance of gasToken. A refusal of the reservation returns the index legs.length.
  3. 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.
  4. The first swap leg whose router and spender pair is not allowed: SwapRouterNotAllowed.
  5. The balance of each pulled token. A swap leg adds its minAmountOut to the balance room of tokenOut for the later legs. A refusal of the reservation returns the index legs.length.
A sum of amounts that does not fit in 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:
  1. swapExecutor() must be set. Else: SwapExecutorNotSet().
  2. The pair (router, spender) must be allowed. Else: SwapRouterNotAllowed(router, spender).
  3. tokenIn must differ from tokenOut, and amountIn and minAmountOut must be above zero. Else: SwapLegNotValid().
  4. amountIn counts against the cap of tokenIn. Else: CapExceeded(owner, token).
  5. The adapter reads the tokenOut balance of the account.
  6. The adapter calls tokenIn.transferFrom(account, swapExecutor, amountIn). The executor balance must grow by exactly amountIn. Else: SwapInputNotReceived(tokenIn, amountReceived, amountIn). Then the account balance must fall by exactly amountIn. Else: SwapInputDebitNotExact(tokenIn, amountDebited, amountIn).
  7. 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.
  8. The tokenOut balance of the account must have grown by at least minAmountOut. Else: SwapOutputBelowMinimum(tokenOut, amountOut, minAmountOut).
Limits of the swap leg:
  • 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 reverts CallerNotRegistryOwner() 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

The inner call of the legs. redeemAsEngine calls it on the adapter itself with exactly legsGasLimit gas. Any other caller gets CallerNotSelf().

Views and constants

Events

In 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 routerData that 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 redeemAsEngine does not revert.
  • The charge is never more than maxCharge. The account gets back maxCharge - 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. Only validUntil bounds 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.