Skip to main content
After this page, you can take a plain address to a committed rule document that runs through AnafiDelegationAdapterERC20Allow. You know which tokens the address must approve, how caps and the block switch work, each prompt, each refusal, and how to resume.

When to use it

Use the ERC20Allow kind (erc20_allow) when the funds stay in the address of the owner, and the rules move only ERC-20 tokens. The account is the owner address itself. Each owner act is a plain transaction that the address sends and pays for. Anafi never relays it. Use the Safe account for the native coin, for off_ramp, or when the funds live in a Safe.

Preconditions

Today, the Anafi API hosts answer CORS only for https://app.ana.fi. Run the calls that use the Anafi API from a server, a script, or your own same-origin proxy. Approvals, caps, the block switch, the commit and the revoke are chain transactions, so this limit does not apply to them.

Classification

classifyErc20AllowAccount(account, options?) reads at one block and returns one value:

Tokens that the document spends

The SDK reads tokens(D), the list of tokens that the engine can pull for the document, in this order:
  1. each token of a send_one value and of each send_batch row, and each in token of an exchange, in document order, each one time;
  2. the token of global.fees_method, when it is not in the list yet.
A send value names its token in one of two forms: a literal "<amount> <TOKEN>", or trigger.value (with an optional * <n>%) under an incoming_tx or outgoing_tx trigger whose value range ends in a token. A bought token (out of an exchange) needs no approval, unless it is also a spent token or the fee token. Each token of tokens(D) must have a row in the erc20s table of the client config.

What the document may not contain

The SDK refuses these before any request, in this order. The first cause that holds decides. The engine also refuses an off_ramp for this kind, and a fee token that is not an ERC-20. These refusals come back as DslStageRefusedError. You can run the same check with no request: findErc20AllowDocumentRefusal(yaml, { native, erc20s, swapSupport }) returns the first cause or null. See ERC20Allow methods.

Swap support

readErc20AllowSwapSupport() reads swapExecutor() on the adapter and returns "served" (an executor is set) or "not_served" (zero address, or the call reverted). "served" does not prove that a swap can run. The adapter also checks the router of each swap (see Swap executor), and the engine must have a swap route on the network. On polygon-amoy, the engine refuses each exchange today. A swap leg pulls exactly amountIn of the sold token from the account to AnafiSwapExecutor. The executor calls an allowed router and pays the output and the unused input back to the account. When the output at the account is below the signed minAmountOut, the legs of the payment fail with SwapOutputBelowMinimum. See Swap executor.

Approvals, caps and the block switch

The adapter pulls tokens with transferFrom. Three facts of the account decide what it can pull. Rules of the cap:
  • periodSeconds must be at least 3600 (MIN_CAP_PERIOD_SECONDS). A shorter window reverts CapPeriodTooShort, and the SDK refuses it first.
  • The windows are fixed: window number block.timestamp / periodSeconds. A window does not start when you call setCaps.
  • A new setCaps with the same periodSeconds, in the same window, keeps the amount spent. Any other change starts the count at zero.
  • Each pull counts: each payment leg, each Anafi fee leg, each swap input, and the reserved gas charge (maxCharge) in its token. The part of maxCharge that the adapter returns is taken off the count again.
  • A pull above the room left fails with CapExceeded(owner, token). For the reserved gas charge, the whole redeem reverts. For a leg, the legs of the payment fail.
Read all three at one block with readErc20AllowDelegation({ account, adapter, tokens }). It returns { blocked, tokens: [{ token, allowance, cap }], block }. cap is null when no cap is set, else { limit, periodSeconds, spentInPeriod }. See ERC20Allow adapter for each function, event and error of the adapter.

Sequence

Steps

1

Stage the document

Call stageErc20AllowDsl({ account, adapter, yaml, salt, parent, plannedCap? }). Use a new random 32-byte salt. Set parent to await readDslCommitment(account).The SDK, in this order:
  1. refuses an adapter that is not the erc20_allow row of the config;
  2. applies the document refusals of the table above, and reads swapExecutor() when the document swaps;
  3. at one tip block, classifies the account and reads its block switch, and the allowance and cap of each token of tokens(D). It refuses leg_not_live and an account class that is not served. It then counts each allowance of 0 as approved (the commit approves it), and the cap of plannedCap.token as the cap after that setCaps. It throws Erc20AllowNotLiveError when the owner is blocked (reason.kind: "blocked") or when a cap has no room left ("cap_exhausted");
  4. seals the document and sends POST /api/wallets/{account}/dsl/commit/stage;
  5. checks the reply as for a Safe: the adapter echo, commitment = keccak256(salt ‖ yaml), and a deadline in the future and at most 1 hour away.
No wallet prompt. The result is a StagedErc20AllowDsl: account, adapter, yaml, salt, commitment, engineSig, attestationValidUntilUnixSeconds, warnings, tokens (tokens(D) with addresses) and approvals (the tokens whose allowance was 0 at the stage).Show staged.warnings and staged.approvals to the owner. The approvals tell the owner how many prompts follow.
2

Set a cap (optional)

When you passed plannedCap to the stage, send that cap before the commit:
One transaction prompt: setCaps(entries) on the adapter. You can also set a cap at any later time.
3

Commit

Call commitStagedErc20AllowDsl({ staged, onStep, onSigned, onConfirmed }).
  1. The SDK refuses an adapter that is not the erc20_allow row of the config, and a lapsed attestation.
  2. It refuses a staged value whose commitment is not keccak256(salt ‖ yaml) of its own text, or whose tokens are not tokens(D) of that text (Erc20AllowStagedDocumentMismatchError). It applies the document refusals again, and reads swapExecutor() again for a document that swaps.
  3. It reads the allowance of each token of staged.tokens again, at one tip block. A token that the owner revoked after the stage needs an approval that staged.approvals does not list.
  4. For each token with allowance 0, in order: onStep({ kind: "approve", token }), then one transaction prompt token.approve(adapter, 2^256 - 1). The SDK waits for each receipt before the next prompt. onConfirmed(block) runs after each receipt.
  5. It checks the attestation deadline again.
  6. onStep({ kind: "commit" }), then one transaction prompt: AnafiDslRegistry.commitDsl(commitment, validUntil, engineSig, adapter) from the account. onSigned runs when the wallet returns the hash. The SDK waits for the receipt.
The result is { txHash, kind: "committed", confirmedBlock }. A reverted receipt throws owner transaction ... reverted.

What can refuse

Resume after a failure or a reload

End state on chain

Example

This example uses the client of the Quickstart. It stages, sets a daily cap, and commits.