Skip to main content
After you read this page, you can run a plain address as an Anafi account: check that the leg serves it, commit a rule document with the right approvals, set caps, block the adapter, and revoke. An ERC20Allow account (kind erc20_allow) is a plain address: a key with no code, or an address with an EIP-7702 pointer. The account grants its permission with ERC-20 allowances to AnafiDelegationAdapterERC20Allow. It can also set a block switch and caps on the adapter. See the ERC20Allow adapter and the ERC20Allow account flow.

Rules of this leg

  • Every write is a plain transaction from the account. The account sends it and pays the gas. The client never asks for a relay and never reads the sponsorship for this leg.
  • One transaction at a time. Each transaction waits for its receipt (up to 180 seconds) before the next prompt. A reverted receipt rejects with Error("owner transaction <hash> reverted"). The transactions that landed before stay on chain.
  • The adapter must be the configured row. Each method throws Error("<adapter> is not this chain's configured ERC20Allow adapter") when adapter is not delegationAdapters.erc20_allow.
  • Only ERC-20 tokens. The adapter cannot pull the native coin. A document of this leg cannot send, sell or buy native.
  • A fee token is required. A document of this leg must set global.fees_method. See fees.
  • The client copies your input. The stage and the commit read their parameters one time, when you call them. A later change to your object, also from a callback, changes nothing.

tokens(D)

tokens(D) is the list of tokens that the engine can pull for a document D. The client computes it from the text:
  1. each token that a send_one or a send_batch row spends, and each token that an exchange sells, in document order, each one time;
  2. then the fee token of global.fees_method, when no action spends it.
The token that an exchange buys is not in tokens(D), unless an action also spends it or it is the fee token. The account must approve each token of tokens(D) to the adapter.

classifyErc20AllowAccount

Classifies account for this leg. Every read is at one block: options.block, or the uncached tip. With no erc20_allow row in the config, it answers leg_not_live and reads nothing.

readErc20AllowDelegation

Reads the whole permission of one account at one block: adapter.isBlocked(account), and for each token token.allowance(account, adapter) and adapter.capOf(account, token). An answer of the wrong type throws.
The package also exports the functions behind it:

readErc20AllowSwapSupport

Reads swapExecutor() of the configured ERC20Allow adapter at the latest block. Any other failure throws Erc20AllowSwapSupportUnreadError. A failure never reads as not_served. served does not promise that a swap can run. The adapter can permit no router pair yet. The engine also refuses every exchange action on polygon-amoy, where no swap aggregator is available. The function behind it:

stageErc20AllowDsl

Sends the document to the engine for a stage. Nothing prompts the wallet.

Checks, in order

The method stops at the first check that fails. An allowance of 0 does not stop the stage. The token goes to approvals, and the commit approves it.

Return value: StagedErc20AllowDsl

commitStagedErc20AllowDsl

Commits a staged document from the account with plain transactions.
  1. Checks that staged.adapter is the configured row.
  2. Throws Error("commitStagedErc20AllowDsl: the engine's attestation over this staged document has lapsed; stage it again") when the deadline has passed.
  3. Checks that staged.commitment is dslCommitment(staged.salt, staged.yaml), and that staged.tokens is tokens(D) of staged.yaml in order. Else it throws Erc20AllowStagedDocumentMismatchError.
  4. Applies to staged.yaml the token checks 2 to 9 of the stage, with a new read of swapExecutor() for a document with a swap.
  5. Reads the allowance of each token of staged.tokens again, at one tip block. The owner can have revoked or approved in the meantime, so the list can differ from staged.approvals.
  6. Checks the deadline again.
  7. For each token with allowance 0, in order: onStep({ kind: "approve", token }), then the prompt for token.approve(adapter, 2^256 - 1), then the receipt, then onConfirmed(block).
  8. Checks the deadline again.
  9. onStep({ kind: "commit" }), then the prompt for AnafiDslRegistry.commitDsl(commitment, validUntil, engineSig, adapter) from the account. onSigned() runs when the wallet returns the hash. Then the receipt and onConfirmed(block).
It returns { txHash, kind: "committed", confirmedBlock }. A reverted receipt rejects. Prompts: one per token with allowance 0, then one. The commit does not check the block switch or the caps again. The engine reports a later problem of the permission, such as a block or a full cap, in the tenant field of recover.

setErc20AllowBlocked

Sends adapter.setBlocked(blocked) from the account. With true, the adapter refuses each pull of every token of this account. The allowances stay. With false, the pulls work again. Prompts: 1. This is the fastest stop of this leg: one transaction pauses all payments, and one transaction resumes them.

setErc20AllowCaps

Sends one adapter.setCaps(entries) from the account. Each entry lets the adapter pull at most limit base units of token in each window of periodSeconds. Prompts: 1. Throws Error("setCaps needs at least one cap") for an empty list. Throws an Error when a window is under ERC20_ALLOW_MIN_CAP_PERIOD_SECONDS (3600 seconds), because setCaps reverts on it. When the window length stays the same, the adapter keeps the amount spent in the window. A new cap, or a new window length, starts with nothing spent. erc20AllowCapAfterSetCaps computes this.

clearErc20AllowCaps

Sends one adapter.clearCaps(tokens) from the account. Throws Error("clearCaps needs at least one token") for an empty list. Prompts: 1.

approveErc20AllowTokens

Reads the allowance of each token at one tip block, then raises each allowance to the unlimited amount, in order. onStep(token) runs before each prompt. onConfirmed gets the block of each receipt, also when a later transaction fails. With no token to change, it sends nothing and returns { txHashes: [], confirmedBlock: null }.

revokeErc20Allowances

Revokes the permission. It reads the allowance of the account to the adapter on each token of erc20s, at one tip block. Then it sends approve(adapter, 0) to each token whose allowance is above 0, in table order. onStep(token) runs before each prompt. It needs no Anafi API. Prompts: one per token with an allowance. A token that is not in your erc20s table is not revoked. Revoke such a token yourself with buildErc20ApproveCalldata(adapter, 0n).

Helpers of this leg

Allowance and cap helpers

Live check

Answers why the permission cannot serve tokens, or null when it can. The block switch comes first. Then, for each token in order: an allowance of 0 (or no row) answers allowance_zero, and a cap with spentInPeriod >= limit answers cap_exhausted.

Document refusal

Answers why the tokens or the swaps of a document stop it on this leg, or null. It throws nothing. Use it to show a refusal in an editor before any request. The causes are weighed in this order, and the first that holds answers: findErc20AllowDocumentRefusalFromActions takes the result of documentActionTokens, so you read the text one time.

tokens(D) readers

erc20AllowDocumentTokenSymbols returns the spent tokens, then the fee token when it is new. readErc20AllowDocumentTokens returns tokens(D) with their contracts. It throws the errors of checks 2 to 8 of the stage. It does not read the swap support.

Erc20AllowOwnerTransaction and the calldata builders

A plain transaction with no value that the account sends. buildErc20AllowCommitTransaction returns one. The calldata builders of this leg are on low-level helpers.

Errors of this leg

See errors for each class.