> ## 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.

# Fees

> The Anafi fee and the network gas of each payment, the live fee terms of GET /api/fees, the fee token, sponsorship, and worked examples.

After you read this page, you can read the live fee terms of a network, compute the Anafi fee of a payment before it fires, find the fee and the gas charge of a payment in the activity feed, and tell a user who pays what.

A payment can cost the account 2 amounts:

* **The Anafi fee**, a fee transfer inside the payment, to the `treasury` address.
* **The gas charge**, the network gas of the payment, which the adapter takes back from the account.

Both depend on 2 facts: whether fees are on for the network, and whether the account has a sponsor.

| Case | Anafi fee | Gas charge of each payment | Gas of Safe owner transactions |
| - | - | - | - |
| Fees are off | None | None. Anafi pays the gas. | Anafi relays and pays. |
| Fees are on, and the account has a sponsor | None | None. Anafi pays the gas. | Anafi relays and pays. |
| Fees are on, and the account has no sponsor | Yes | Yes, in the fee token. | The owner address pays. |

An `erc20_allow` account always pays the gas of its own owner transactions, in all 3 cases. See [Accounts](https://docs.ana.fi/concepts/accounts.md).

Fees are on today on `polygon`, `xlayer` and `polygon-amoy`.

## The fee terms: `GET /api/fees`

`GET /api/fees` on the API host of a network returns the live fee terms of that network. In the SDK, call `fetchFeeTerms()`. The route needs no authentication.

```sh theme={null}
curl -s https://api.polygon.ana.fi/api/fees
```

| Field | Type on the wire | Meaning |
| - | - | - |
| `feesOn` | boolean | `true` if the engine adds a fee transfer to each payment of an account with no sponsor. |
| `treasury` | address or `null` | The address that receives each fee and each gas charge. `null` when fees are off. |
| `bps` | integer or `null` | The fee rate, in basis points of each moved amount, before the floor. 1 basis point is 0.01%. `null` when fees are off. |
| `floorUsdCents` | integer or `null` | The smallest fee for each moved amount, in US cents. `null` when fees are off. |
| `floors` | array | For each token of the network: `token`, `tokenAddress`, and `amountRaw`, the floor in base units of that token as a decimal string. The native coin has the zero address. Empty when fees are off. |
| `feeTokens` | array of symbols | The tokens that a document can choose to pay fees in. Present when fees are off too. |
| `observedAt` | integer or `null` | The Unix second of the price observation behind `floors`. |
| `gas` | object, or absent | The bounds of the gas charge. See [The gas charge](#the-gas-charge). |

The `gas` object:

| Field | Type on the wire | Meaning |
| - | - | - |
| `transferCostBoundBaseWei` | decimal string | The fixed part of the gas bound of a transfer payment, in wei of the native coin. |
| `transferCostBoundPerLegWei` | decimal string | What each transfer of the payment adds to the bound, in wei. The fee transfer counts as one transfer. |
| `swapCostBoundWei` | decimal string | The gas bound of an `exchange` payment, in wei. |
| `maxSendBatchRecipients` | integer | The most recipients in one `send_batch`. |
| `rates` | array | For each ERC-20 fee token with a price: `token`, `tokenAddress`, and `tokenUnitsPerNativeCoin`, the base units of the token that 1 native coin costs, rounded up, as a decimal string. |

The SDK returns the `gas` values as `bigint`, and the `floors` amounts as decimal strings. A field of the wrong shape makes `fetchFeeTerms()` throw.

The values of today, from the code of each network:

| Network | `bps` | `floorUsdCents` | `feeTokens` |
| - | - | - | - |
| `polygon` | 20 | 20 | `POL`, `USDC`, `USDC.e`, `USDT0` |
| `xlayer` | 20 | 20 | `OKB`, `USDG`, `USDT0`, `USDC` |
| `polygon-amoy` | 20 | 20 | `POL`, `USDC` |

So the fee is 0.2% of each moved amount, and at least USD 0.20. [ana.fi/pricing](https://ana.fi/pricing/) states the same rate and minimum for the dApp. Read `GET /api/fees` at run time and do not hard-code these values. The API answer is the price that applies.

## The Anafi fee

### The fee of one payment

The engine computes the fee in US dollars for each moved amount:

* each recipient of a `send_one` or a `send_batch`,
* the sold amount of an `exchange`.

For each moved amount, the fee is the larger of 2 values: `amount_usd × bps / 10000`, and the floor of `floorUsdCents`. The engine converts each fee into the fee token, rounds it up to a whole base unit, and adds the results. The payment then carries one fee transfer of that sum to `treasury`.

A dollar stablecoin of the fee-token list counts as exactly USD 1 for each whole token. Other tokens use the price of the engine at the fire.

### The floor refuses small amounts

The engine refuses a moved amount that is at or below the floor of its token, `floors[].amountRaw`. In a `send_batch`, one such recipient refuses the whole batch, and no recipient is paid. Nothing is sent.

Check each amount in your form against the floor of its token. For a dollar stablecoin with 6 decimals and a floor of USD 0.20, the floor is `200000`, so the smallest amount that the engine accepts is `0.200001`.

### The fee token: `global.fees_method`

The document chooses its fee token with `global.fees_method`:

```yaml theme={null}
global:
  fees_method:
    type: fixed_token
    token: USDC
```

`fixed_token` is the only `type`. The rules:

* The token must be in `feeTokens` of the network.
* If the document sets `global.token_allowlist`, the list must hold the fee token.
* A Safe document without `fees_method` pays its fees in the native coin of the network.
* An `erc20_allow` document must set `fees_method`, to an ERC-20. The native coin is not a valid fee token for this kind.
* An `erc20_allow` account must approve the fee token to the adapter, as for any token that it spends.

The engine checks these rules at the stage step. The SDK functions `readFeesMethod(yaml)` and `documentWithFeesMethod(yaml, method)` read and set the field.

### How the fee appears

| Where | What you see |
| - | - |
| On chain | One transfer of the fee token from the account to `treasury`, inside the same transaction as the payment. |
| Activity feed, `fee` field | On a `landed` row: a list of fee legs, each with `token`, `tokenAddress` and `amountRaw`. An empty list means that the payment paid no fee. `null` on rows in every other status. |
| Activity feed, `kind` field | `off_ramp_fee` marks a row that pays only the Anafi fee of one bank payout. The bank payout itself has no `payment` row. |

## The gas charge

Anafi submits each payment and pays its network gas. If fees are on and the account has no sponsor, the adapter takes the gas back from the account in the same transaction:

1. Before the transfers, the adapter collects a maximum charge, `maxCharge`, from the account, in the fee token.
2. After the transfers, it computes the real charge: the gas that the transaction used, plus fixed parts, times the gas price of the transaction. A token charge converts the cost with `tokenUnitsPerNativeCoin` and rounds up.
3. It pays the real charge, never more than `maxCharge`, to `treasury`, and pays the rest back to the account.
4. It emits `GasCharged(nonce, digest, token, charge)`.

A Safe adapter can charge the native coin or an ERC-20. The ERC20Allow adapter charges an ERC-20 only. For an `erc20_allow` account, `maxCharge` also counts against the cap of the fee token.

If the transfers of a payment fail after the checks pass, the payment still pays its gas charge. If a check fails before the transfers, the whole transaction reverts and the account pays nothing.

### The bound before a rule fires

`GET /api/fees` gives a bound that is at or above the most a payment can charge:

* A transfer payment with `n` transfers, the fee transfer included: `transferCostBoundBaseWei + n × transferCostBoundPerLegWei` wei.
* An `exchange` payment: `swapCostBoundWei` wei.
* In an ERC-20 fee token: `ceil(wei × tokenUnitsPerNativeCoin / 10^18)` base units.

The SDK functions `transferCostBoundWei(gas, legs)` and `tokenAmountOfWei(costWei, tokenUnitsPerNativeCoin)` do this arithmetic. Show the bound as "at most" in your form. The real charge is usually lower.

### Where the charge appears

The activity row has a `gasCharge` field: `tokenAddress`, the zero address for the native coin, and `amountRaw`, the charge in base units. It is `null` for a payment with no recorded charge.

### Gas credit

If the adapter cannot pay a charge or a refund, for example because a token transfer fails, the amount becomes a gas credit of its payee in the adapter. Read it with `gasCredit(payee, token)` or `readGasCredit` in the SDK. The payee claims it with `claimGasCredit(token, to)`. See [Redeem](https://docs.ana.fi/contracts/redeem.md) for the rules of the gas credit.

## Sponsorship

A sponsor is a partner that pays for an account. For a sponsored account:

* The engine adds no fee transfer.
* The adapter takes no gas charge. `maxCharge` is 0, and `GasCharged` reports a charge of 0.
* The Anafi API relays the Safe owner transactions and pays their gas.

`GET /api/wallets/:aa/sponsorship` returns `{ "feesOn": boolean, "sponsored": boolean }`. In the SDK, call `fetchWalletSponsorship(aa)`. `sponsored` is `true` for an account with a sponsor, also when fees are off.

Anafi sets the sponsor of an account. To pay for your users, write to [hi@ana.fi](mailto:hi@ana.fi).

## Worked examples

All examples use `polygon`, `bps` 20, `floorUsdCents` 20, and `USDC` as the fee token. `USDC` has 6 decimals and counts as USD 1.

### 1. A payment of 500 USDC

* Moved amount: 500 USDC, which is USD 500.
* Rate part: `500 × 20 / 10000` = USD 1.00.
* Floor: USD 0.20.
* Fee: the larger value, USD 1.00, which is `1000000` base units of USDC.

The account sends 500 USDC to the payee and 1 USDC to `treasury`.

### 2. A payment of 50 USDC

* Rate part: `50 × 20 / 10000` = USD 0.10.
* Fee: the floor, USD 0.20, which is `200000` base units.

### 3. A batch of 50, 100 and 1000 USDC

| Recipient | Rate part | Fee |
| - | - | - |
| 50 USDC | USD 0.10 | USD 0.20 |
| 100 USDC | USD 0.20 | USD 0.20 |
| 1000 USDC | USD 2.00 | USD 2.00 |

The payment carries one fee transfer of USD 2.40, which is `2400000` base units.

### 4. A payment of 0.20 USDC

The amount is `200000` base units. It is at the floor of USDC, `200000`, so the engine refuses it. Nothing is sent.

### 5. The gas bound of example 1

This example uses one answer of `GET /api/fees` on `polygon`. The values change with the price of gas and of `POL`.

```json theme={null}
{
  "transferCostBoundBaseWei": "350378308000000000",
  "transferCostBoundPerLegWei": "24098396000000000",
  "rates": [{ "token": "USDC", "tokenUnitsPerNativeCoin": "71010" }]
}
```

The payment has 2 transfers: the payee and the fee.

* Bound in wei: `350378308000000000 + 2 × 24098396000000000` = `398575100000000000`, which is 0.3985751 `POL`.
* Bound in USDC: `ceil(398575100000000000 × 71010 / 10^18)` = `28303` base units, which is 0.028303 USDC.

```ts theme={null}
import { transferCostBoundWei, tokenAmountOfWei, type AnafiClient } from "@anafi-cc/anafi-sdk";

declare const anafi: AnafiClient;

const terms = await anafi.fetchFeeTerms();
if (terms.gas !== null) {
  const wei = transferCostBoundWei(terms.gas, 2);
  const usdc = terms.gas.rates.find((rate) => rate.token === "USDC");
  if (usdc) console.log(tokenAmountOfWei(wei, usdc.tokenUnitsPerNativeCoin)); // 28303n with the values above
}
```

So the account pays at most 501.028303 USDC for example 1: 500 to the payee, 1 as the fee, and at most 0.028303 as the gas charge.

## Related pages

* [Activity](https://docs.ana.fi/concepts/activity.md)
* [Activity and fees in the SDK](https://docs.ana.fi/sdk/activity-and-fees.md)
* [Accounts](https://docs.ana.fi/concepts/accounts.md)
* [Networks](https://docs.ana.fi/networks.md)
* [API reference](https://docs.ana.fi/api-reference/introduction.md)


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