> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ana.fi/llms.txt
> Use this file to discover all available pages before exploring further.

# Contracts overview

> The Anafi contract set, who can call each part, the admin powers, and how an owner leaves.

After you read this page, you know which Anafi contract does what, which party can call which function, what the contract admin can change, and how each account kind stops Anafi with one transaction. The pages after it give each contract function by function.

All Anafi contracts are on public chains. Each network has its own copy of the set. The addresses are on [Deployments](https://docs.ana.fi/contracts/deployments.md).

## The contract set

| Contract | Kind of deployment | Purpose |
| - | - | - |
| [`AnafiRegistry`](https://docs.ana.fi/contracts/registry) | ERC-1967 proxy, UUPS | Publishes the engine keys, the compose hash and the attestation evidence. Records the engine heartbeat. Holds the list of live adapters. Its `owner()` is the admin of the whole set. |
| [`AnafiDslRegistry`](https://docs.ana.fi/contracts/dsl-registry) | ERC-1967 proxy, UUPS | Stores the latest commit of each account: the salted hash of the rule document and a version counter. Each commit names the adapter. |
| [`AnafiDelegationAdapterSafe`](https://docs.ana.fi/contracts/safe-adapter) | ERC-1967 proxy, UUPS | The adapter of the `safe_module` account kind. A Safe enables it as a module. It executes engine-signed calls as the Safe and charges the Safe the gas. |
| [`AnafiDelegationAdapterERC20Allow`](https://docs.ana.fi/contracts/erc20-allow-adapter) | ERC-1967 proxy, UUPS | The adapter of the `erc20_allow` account kind. An address approves it for each token. It pulls engine-signed transfers and swaps, and charges the gas in a token. The owner can set caps and a block switch. |
| [`AnafiSwapExecutor`](https://docs.ana.fi/contracts/swap-executor) | Plain contract, no proxy, no owner | Calls the swap router for a swap leg of the ERC20Allow adapter. Only that adapter can call it. |
| [`AnafiOffRampFallbackHandler`](https://docs.ana.fi/contracts/off-ramp-handler) | Plain contract, no proxy, no owner | A Safe fallback handler. It answers ERC-1271 signature checks for an engine-signed bank payout order under a policy that the Safe sets. |

Two more source units have no deployment of their own:

* `IAnafiDelegationAdapter` is the interface that each adapter implements. It has one function, `anafiRegistry()`, which returns the registry that the adapter reads.
* `AdapterGasSettlement` is an internal library. Both adapters use it to compute the redeem digest and the gas charge. [Redeem](https://docs.ana.fi/contracts/redeem.md) gives its formulas.

The Safe adapter also calls one function of the Safe, which the source declares as `IAvatar`:

```solidity theme={null}
interface IAvatar {
    function execTransactionFromModule(address to, uint256 value, bytes calldata data, uint8 operation)
        external
        returns (bool success);
}
```

A Safe executes the call only when the caller is an enabled module. The Safe returns `false` for a failed inner call and does not revert. The adapter always sends `operation = 0`, a plain call. It never sends a delegatecall.

## How the contracts relate

```mermaid theme={null}
flowchart LR
    Owner["Owner wallet"]
    Submitter["Any sender"]
    Admin["Registry owner"]
    Registry["AnafiRegistry"]
    Dsl["AnafiDslRegistry"]
    SafeAd["AnafiDelegationAdapterSafe"]
    ErcAd["AnafiDelegationAdapterERC20Allow"]
    Swap["AnafiSwapExecutor"]
    Router["Allowed swap router"]
    Safe["Safe account"]
    Token["ERC-20 token"]
    Handler["AnafiOffRampFallbackHandler"]

    Owner -- "enableModule, disableModule" --> Safe
    Owner -- "approve" --> Token
    Owner -- "setCaps, setBlocked" --> ErcAd
    Owner -- "commitDsl" --> Dsl
    Submitter -- "redeemAsEngine" --> SafeAd
    Submitter -- "redeemAsEngine" --> ErcAd
    Submitter -- "attestHeartbeat" --> Registry
    SafeAd -- "execTransactionFromModule" --> Safe
    ErcAd -- "transferFrom" --> Token
    ErcAd -- "swap" --> Swap
    Swap -- "router call" --> Router
    Safe -- "isValidSignature" --> Handler
    Dsl -- "engineSigKey, owner" --> Registry
    SafeAd -- "isDelegationAdapter, engineSigKey, owner" --> Registry
    ErcAd -- "isDelegationAdapter, engineSigKey, owner" --> Registry
    Handler -- "engineSigKey" --> Registry
    Admin -- "admin calls, upgrades" --> Registry
```

The registry is the root. Each other upgradeable contract stores the registry address once, at initialization, and reads three things from it:

* `engineSigKey()`: the address that must sign each redeem and each attested commit.
* `isDelegationAdapter(address)`: an adapter checks its own entry first on each redeem.
* `owner()`: the only address that can call the admin functions and upgrade the contract.

The off-ramp handler reads `engineSigKey()` from the registry that each Safe names in its own policy.

## Who can call what

The contracts know four parties. The engine never needs to send a transaction itself: a signature of the engine key is the authorization, and any address can submit it.

| Party | What it is | What it can do on chain |
| - | - | - |
| Owner | The key that controls the account. For a Safe, the Safe owners act through a Safe transaction. For an ERC20Allow account, the address sends its own transactions. | Grant and revoke the permission. Commit a rule document with `commitDsl` or `commitDslDirect`. On ERC20Allow: `setCaps`, `clearCaps`, `setBlocked`. On the off-ramp handler: `setOffRampPolicy`, `setApprovedMessageHash`. Claim its own gas credit with `claimGasCredit`. |
| Engine key | The address that `AnafiRegistry.engineSigKey()` returns. | Sign redeem digests, commit attestations, heartbeats and off-ramp orders. A signature has effect only inside a call that the contract checks. |
| Anyone | Any address. | Submit `redeemAsEngine` with a valid engine signature. Submit `attestHeartbeat` with a valid engine signature. Claim its own gas credit. Read every view function. |
| Registry owner | The address that `AnafiRegistry.owner()` returns. It is the admin of the contract set. | All admin functions and all upgrades. The next section lists them. |

## Admin powers and upgrades

The registry uses OpenZeppelin `Ownable2Step`. Ownership moves only in two steps: the owner calls `transferOwnership(newOwner)`, and then `newOwner` calls `acceptOwnership()`. `renounceOwnership()` always reverts with `OwnershipNotRenounceable()`. So the set always has an admin.

The DSL registry and both adapters have no owner of their own. Each one returns `AnafiRegistry.owner()` from its own `owner()` and reads it at the time of each call. A handover at the registry moves the admin of all four contracts in the same transaction.

The registry owner can do these things:

| Power | Function | Contract |
| - | - | - |
| Replace the code of the registry, the DSL registry or an adapter | `upgradeToAndCall(address,bytes)` | `AnafiRegistry`, `AnafiDslRegistry`, both adapters |
| Set the engine keys, the compose hash and the attestation evidence | `setEngineIdentity` | `AnafiRegistry` |
| List or delist an adapter | `addDelegationAdapter`, `removeDelegationAdapter` | `AnafiRegistry` |
| Admit or withdraw a Safe proxy code hash or a Safe singleton | `setSafeProxyCodehashAdmitted`, `setSafeSingletonAdmitted` | Safe adapter |
| Set the gas overhead of a gas token, which also admits or stops the token for gas charges | `setGasOverhead` | both adapters |
| Set the swap executor one time, and add or remove router and spender pairs | `initializeSwaps`, `setSwapRouterAllowed` | ERC20Allow adapter |
| Hand the admin role to a new address | `transferOwnership` | `AnafiRegistry` |

The admin functions above do not include these things:

* An upgrade of `AnafiSwapExecutor` or `AnafiOffRampFallbackHandler`. Neither has a proxy or an owner. A fix is a new contract.
* A change of the swap executor after `initializeSwaps`, other than through an upgrade of the ERC20Allow adapter.
* A call to a Safe, or a change of the allowance that an owner gave to a token.
* A renounce of the admin role.

A redeem leg of the Safe adapter is a plain call from the Safe to any target, the Safe itself included. See [Safe adapter](https://docs.ana.fi/contracts/safe-adapter.md#the-permission).

> [!NOTE]
>
> The Safe adapter executes each leg as a plain call from the Safe, with the target, the value and the call data of the leg. The ERC20Allow adapter pulls tokens only through the allowances that the account gave it. [Redeem](https://docs.ana.fi/contracts/redeem.md) lists the checks of each adapter.

Each upgradeable contract emits the OpenZeppelin event `Upgraded(address indexed implementation)` on an upgrade. The registry emits `EngineIdentitySet` on each key change, and `DelegationAdapterAdded` and `DelegationAdapterRemoved` on each list change. [Verification](https://docs.ana.fi/contracts/verification.md) shows how to watch these events.

## The exit of each account kind

The owner can stop Anafi without the Anafi engine, the Anafi API or the registry owner. The exit is one owner transaction for a Safe, and one transaction for each approved token for an ERC20Allow account. It takes effect in the block where it lands.

| Account kind | Exit call | Sent by | What it stops |
| - | - | - | - |
| `safe_module` | `disableModule(prevModule, safeAdapter)` on the Safe | The Safe, in a Safe transaction that the owners sign | Every leg of every redeem. The Safe refuses each module call from the adapter. |
| `erc20_allow` | `approve(erc20AllowAdapter, 0)` on each token that the owner approved | The account | Every transfer leg, swap leg and gas charge in that token. |

Use these facts when you build an exit:

* `prevModule` is the module before the adapter in the linked module list of the Safe. Read it with `getModulesPaginated` on the Safe. Do not guess it.
* An exit does not cancel a redeem that the engine signed before. The adapter keeps no record of the grant. If the owner grants the permission again, an unexpired signed redeem can land. Only its `validUntil` bounds it.
* For an ERC20Allow account, `setBlocked(true)` stops all pulls in one transaction. The block switch is state of the adapter. The allowance is state of the token, and only the account can set it.
* When the registry owner delists an adapter, its redeems stop, but no grant changes. The owner exit is still the only call that removes the grant.
* An exit does not clear a gas credit. The account claims a credit with `claimGasCredit(token, to)` at any time, with no grant.
* A Safe that installed the off-ramp handler stops off-ramp orders with `setOffRampPolicy` and a zero `authority`, or with a new fallback handler. See [Off-ramp handler](https://docs.ana.fi/contracts/off-ramp-handler.md).

The SDK builds these calls. See [Update, pause and revoke](https://docs.ana.fi/flows/update-pause-revoke.md).

## Read more

> [card] [Redeem](https://docs.ana.fi/contracts/redeem.md)
>
> How one payment is authorized and executed on chain.

> [card] [Verification](https://docs.ana.fi/contracts/verification.md)
>
> What the chain publishes about the engine, and how to check it.

> [card] [Deployments](https://docs.ana.fi/contracts/deployments.md)
>
> The addresses on each network.

> [card] [Security](https://docs.ana.fi/concepts/security.md)
>
> The trust model in plain words.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.