Skip to main content
After you read this page, you can decode a redeem transaction, check its engine signature yourself, compute its gas charge, and tell from the receipt why it reverted or why its legs failed. A redeem is one call of redeemAsEngine on an adapter. It carries a payload of legs, the gas terms, a nonce, a deadline and an engine signature. This page describes only what the contracts check and do.
Both adapters have this function with the same signature and the same GasTerms struct. The payload has a different shape for each adapter.

The flow of one redeem

Any address can submit a redeem. The engine signature is the whole authorization. In practice, Anafi submits the transaction.

The signed digest

The adapter does not use EIP-712 or EIP-191. The engine key signs a raw keccak256 digest of abi.encode of 13 values:
The chain id and the adapter address are in the digest, so one signature is valid at one adapter on one chain. Every field of GasTerms and the deadline are in the digest, so a sender cannot change them.

Check a redeem signature yourself

Test vector: with the values below, the digest is 0x04e7a47e8fe1ff58013d96c937f5c85e4bdc2b70205b3daeaa3c5512e2873a16.

Who signs

The signer must be AnafiRegistry.engineSigKey() at the time of the call. The adapter reads the key on each redeem. After a key change at the registry, a redeem that the old key signed fails with InvalidEngineSignature(). The signature is 65 bytes. A malformed signature reverts with an OpenZeppelin ECDSA error: ECDSAInvalidSignature(), ECDSAInvalidSignatureLength(uint256) or ECDSAInvalidSignatureS(bytes32).

Nonce and deadline

A revocation does not cancel a signed redeem. If the owner grants the permission again before validUntil, a signed redeem that did not land can land. Only validUntil bounds it.

The order of checks

Each step that fails reverts the whole transaction. Then the adapter spends no nonce, takes no charge and runs no leg.

The payload and its legs

Safe adapter

The adapter calls avatar.execTransactionFromModule(target, value, data, 0) for each leg, in order. A leg that returns false fails all legs with AvatarExecutionFailed(legIndex). The Safe adapter has no leg types: each leg is a plain call from the Safe.

ERC20Allow adapter

The adapter runs the legs in order. Each leg counts against the cap of its pulled token. A transfer leg calls token.transferFrom(account, to, amount). A swap leg goes through AnafiSwapExecutor and pays the output to the account.

Kinds of payment

All legs of one payload succeed together or fail together. One engine signature covers the whole array. For the full list of the ERC20Allow pull rules, the caps and the block switch, see AnafiDelegationAdapterERC20Allow.

The gas charge

Each redeem charges the account the gas of the whole transaction, also when the legs fail. A maxCharge of zero is a sponsored redeem: no gas token moves and GasCharged shows a zero charge.

Collection before the legs

The registry owner admits a gas token with setGasOverhead(token, overhead) on each adapter. Read gasOverhead(token): zero means the adapter refuses a charge in that token.

The charge formula

Example, from the test vectors of the contracts: gasUsed = 123456, baseGas = 54321, overhead 98765, floorGas = 0, gas price 1500000000 wei, tokenUnitsPerNativeCoin = 1700123456, maxCharge = 25000000. The billed gas is 276,542, the cost is 414,813,000,000,000 wei, and the charge is 705234 token units.

Payment after the legs

  1. The adapter pays charge to receiver, with 100,000 gas.
  2. The adapter pays maxCharge - charge back to the account, with 100,000 gas.
  3. On the ERC20Allow adapter, it subtracts maxCharge - charge from the cap of the gas token. It does this only when the same cap still holds the reservation.
A payment never reverts the redeem. A failed payment becomes a credit of its payee, and the adapter emits GasCreditKept(payee, token, amount). The payee claims the credit with claimGasCredit(token, to). The claim needs no grant. A token payment passes only when the call did not revert and returned no data, or exactly one 32-byte word equal to 1. Each other result is a failed payment. On the Safe adapter, a native refund runs code of the Safe. So the adapter checks the Safe again before it sends a native refund: the code hash and the singleton must still be admitted. When the check fails, the refund becomes a credit of the Safe, and the adapter sends no native coin to the Safe.

Revert or failed legs

A redeem ends in one of three ways. These causes revert the whole transaction:
  • RedeemReentered(), AdapterNotAllowed(), RedeemWindowExpired(), InvalidEngineSignature(), the ECDSA errors, NonceAlreadyUsed().
  • A payload that does not decode.
  • Safe adapter: SafeNotAdmitted(address), SafeHasModuleGuard(address).
  • ERC20Allow adapter: OwnerBlocked(address), and CapExceeded(address,address) for the gas reservation.
  • GasTokenNotSupported(address), GasChargeCollectionFailed(), GasForLegsTooLow(uint256,uint256).
  • An arithmetic overflow in the charge formula.
These causes make the legs fail, with the error in legsRevertData:
  • Safe adapter: UnsupportedMode(bytes32), AvatarExecutionFailed(uint256), a Safe error such as GS104 when the module is not enabled and maxCharge is zero, a malformed leg, or the inner call out of gas.
  • ERC20Allow adapter: UnsupportedLegKind(uint8), CapExceeded(address,address) for a leg, the error of a token for a low allowance or balance, SafeERC20FailedOperation(address), the swap errors SwapExecutorNotSet(), SwapRouterNotAllowed(address,address), SwapLegNotValid(), SwapInputNotReceived(address,uint256,uint256), SwapInputDebitNotExact(address,uint256,uint256), SwapOutputBelowMinimum(address,uint256,uint256), the executor errors RouterCallFailed(bytes) and SwapReentered(), or the inner call out of gas.
On the Safe adapter, a Safe that did not enable the module reverts at the collection step with GasChargeCollectionFailed() when maxCharge is above zero. When maxCharge is zero, the legs fail with GS104.

Watch redeems

The redeem events do not name the account. Decode the payload from the transaction input to get it: the first ABI word of the payload is the account on both adapters. The ERC-20 Transfer events of the same receipt show the moved funds. For the status of a payment as the Anafi API reports it, see Execution.