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:- each token of a
send_onevalue and of eachsend_batchrow, and eachintoken of anexchange, in document order, each one time; - the token of
global.fees_method, when it is not in the list yet.
"<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 withtransferFrom. Three facts of the account decide what it can pull.
Rules of the cap:
periodSecondsmust be at least 3600 (MIN_CAP_PERIOD_SECONDS). A shorter window revertsCapPeriodTooShort, and the SDK refuses it first.- The windows are fixed: window number
block.timestamp / periodSeconds. A window does not start when you callsetCaps. - A new
setCapswith the sameperiodSeconds, 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 ofmaxChargethat 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.
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:- refuses an adapter that is not the
erc20_allowrow of the config; - applies the document refusals of the table above, and reads
swapExecutor()when the document swaps; - 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_liveand an account class that is not served. It then counts each allowance of 0 as approved (the commit approves it), and the cap ofplannedCap.tokenas the cap after thatsetCaps. It throwsErc20AllowNotLiveErrorwhen the owner is blocked (reason.kind: "blocked") or when a cap has no room left ("cap_exhausted"); - seals the document and sends
POST /api/wallets/{account}/dsl/commit/stage; - 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.
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 One transaction prompt:
plannedCap to the stage, send that cap before the commit:setCaps(entries) on the adapter. You can also set a cap at any later time.3
Commit
Call
commitStagedErc20AllowDsl({ staged, onStep, onSigned, onConfirmed }).- The SDK refuses an adapter that is not the
erc20_allowrow of the config, and a lapsed attestation. - It refuses a staged value whose
commitmentis notkeccak256(salt ‖ yaml)of its own text, or whosetokensare not tokens(D) of that text (Erc20AllowStagedDocumentMismatchError). It applies the document refusals again, and readsswapExecutor()again for a document that swaps. - It reads the allowance of each token of
staged.tokensagain, at one tip block. A token that the owner revoked after the stage needs an approval thatstaged.approvalsdoes not list. - For each token with allowance 0, in order:
onStep({ kind: "approve", token }), then one transaction prompttoken.approve(adapter, 2^256 - 1). The SDK waits for each receipt before the next prompt.onConfirmed(block)runs after each receipt. - It checks the attestation deadline again.
onStep({ kind: "commit" }), then one transaction prompt:AnafiDslRegistry.commitDsl(commitment, validUntil, engineSig, adapter)from the account.onSignedruns when the wallet returns the hash. The SDK waits for the receipt.
{ txHash, kind: "committed", confirmedBlock }. A reverted receipt throws owner transaction ... reverted.