Skip to main content
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. An erc20_allow account always pays the gas of its own owner transactions, in all 3 cases. See Accounts. 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.
The gas object: 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: So the fee is 0.2% of each moved amount, and at least USD 0.20. 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:
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

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

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

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