Skip to main content
After you read this page, you can take a one-time payment, check it on chain, and set up a monthly payment that Anafi runs from the payer’s own address. These methods power a pay page: a payer pays once, or pays now and every month after. The first payment is a plain ERC-20 transfer from the payer’s wallet. The monthly payment is a rule document of an ERC20Allow account. See payments and subscriptions.

Common rules

  • The proof of a payment is the Transfer event. A successful receipt is not proof. A wallet can replace a transfer by a cancel with the same nonce, and that transaction also succeeds. The SDK counts a payment as made only when a successful receipt holds Transfer(payer, payee, amount) of the token, with the exact amount (logsHoldErc20Transfer).
  • lowestPaymentBlock guards against old receipts. Read the tip before the request, and pass the tip plus 1. A receipt in a lower block belongs to an older transaction, and the SDK never counts it as this payment.
  • The wallet must be on the right chain and account. Before each wallet request, the payment methods check that signal is not aborted, that walletClient.getChainId() is chainId, and that walletClient.getAddresses()[0] is the payer. A failed check answers stopped_before_request with a reason. A wallet client without getAddresses or getChainId makes the method throw.
  • A wallet rejection rejects. When the payer rejects the request, the method rejects with the error of the wallet.

OwnerRequestStopReason

sendErc20Payment

Sends one ERC-20 transfer(payee, amount) from payer to the token, and answers what became of it. No adapter, no registry and no Anafi API is in it. Prompts: 1 transaction.

Erc20PaymentOutcome

txHash is the hash of the mined transaction. It differs from the submitted hash when the wallet replaced the transaction. Throws: a wallet answer that is not a 32-byte hex hash rejects before onSubmitted; the transfer can still have been sent. A receipt that does not arrive in 180 seconds rejects; you then hold the hash from onSubmitted. A receipt that names no mined transaction rejects.

readErc20PaymentTransactionState

Reads what the chain shows now of a stored payment transaction, with no wallet request. It reads the receipt. With no receipt, it asks the node for the transaction. Throws: a failed read, a receipt that names no mined transaction, and a public client with no getTransactionReceipt or no getTransaction.

scanErc20PaymentTransfers

Scans the Transfer(payer, payee, value) events of the token from fromBlock to the uncached tip, with one getLogs call of at most ERC20_PAYMENT_TRANSFER_SCAN_MAX_BLOCKS blocks. Only an exact amount counts. A removed log does not count. When reachedTip is false, call again from toBlock + 1n. A fromBlock above the tip reads nothing and answers reachedTip: true. Throws: a log with a malformed field, a failed read, and a public client with no getLogs.

Monthly subscription

A monthly subscription has five steps. The payer sends each one from the payer address. buildErc20AllowSubscriptionCalls builds this list from the chain facts. A step that the chain already shows is never asked for.

The plan

Prepare a plan

Plan checks

Before any read, both setup methods refuse with an Error:
  • a staged.adapter that is not the configured erc20_allow row;
  • a staged.account that is not the payer;
  • a cap.token that is not token;
  • a cap window under 3600 seconds;
  • a staged document that pulls another token than token.
Then they check the staged value as commitStagedErc20AllowDsl does (Erc20AllowStagedDocumentMismatchError, the token checks, and a new swap support read for a document with a swap). They refuse a wallet client without getAddresses or getChainId. At each chain read, they refuse an adapter that the registry does not list, an account that the leg does not serve, and a blocked account (Erc20AllowNotLiveError).

setUpErc20AllowSubscription

Sets up the subscription one plain transaction at a time. Prompts: 1 per step that the chain does not show, at most 5. The loop:
  1. Read the facts of the payer at one block: the allowance and the cap of token, the block switch, the committed document, and the account class. The block is the tip, or the block of the last receipt when the tip is below it.
  2. If the chain holds a commitment that the plan does not expect, end with unexpected_commitment.
  3. Build the list of the steps that are still absent.
  4. If a step that this setup sent and confirmed is still in the list, end with step_without_effect. The setup never asks for a step twice.
  5. Report the step that the read now shows with onStepConfirmed.
  6. If the list is empty, end with completed.
  7. Run the stop checks. For the commit step, end with attestation_lapsed when 300 seconds or less remain to the attestation deadline (ERC20_ALLOW_SUBSCRIPTION_ATTESTATION_MARGIN_SECONDS).
  8. Call onRequested(step) and wait for its promise. Ask the wallet for the first step. Call onSubmitted(step, hash). Wait for the receipt.
  9. For payment: a receipt with the Transfer event confirms it (onStepConfirmed). Any other verdict ends the setup. For another step: a reverted receipt ends with step_reverted.
  10. Go to step 1.

Erc20AllowSubscriptionOutcome

The steps that landed before the end stay on chain in each case.

setUpErc20AllowSubscriptionInBatch

Sets up the subscription with one atomic EIP-5792 batch. Prompts: 1 batch.
  1. Runs the plan checks. A wallet client with no sendCalls or no getCallsStatus answers { kind: "batch_refused", code: null }.
  2. Reads the facts once and builds the same call list. An unexpected commitment answers unexpected_commitment with payment: null. An empty list answers completed with payment: null.
  3. Makes a new random 32-byte batch id. Runs the stop checks, and for a batch with the commit, the 300-second attestation margin.
  4. Calls onRequested(batchId) and waits for its promise. Store the id here.
  5. Calls sendCalls({ account: payer, calls, id: batchId, forceAtomic: true }).
  6. A wallet error with one of the codes 4100, 4200, -32601, -32602, 5700, 5710, 5740, 5750, 5760 proves a refusal before acceptance: batch_refused with that code. A user rejection (4001) rejects. Any other error can hide an accepted batch, so the setup reads the status of its id.
  7. Calls onSubmitted(id) when the wallet accepted the batch.
  8. Reads getCallsStatus every 2 seconds while the batch is not final, for up to 180 seconds.
  9. Classifies the status. A payment that the wallet claims counts only when the receipt from your public client proves it, at or above lowestPaymentBlock.
  10. After the proof, or after a final batch with no payment, reads the facts again and lists the steps that are still absent.
After the request, a failed node read never rejects. It answers setup_state_unread after the proof of the payment, and unknown in other cases.

Erc20AllowSubscriptionBatchOutcome and Erc20AllowSubscriptionBatchResult

payment is the payment that your node proves, or null when the batch held no payment.

readErc20AllowSubscriptionBatchResult

Reads the status of an earlier batch one time, with no prompt, and classifies it as setUpErc20AllowSubscriptionInBatch does. Use it after a page reload, with the id that you stored in onRequested. A read that takes more than 20 seconds, a failed read, and a wallet client with no getCallsStatus answer unknown. It applies the plan checks, and not the check of the staged text, because it sends nothing.

readWalletAtomicBatchSupport

Answers true when getCapabilities({ account, chainId }) reports an atomic capability with status supported or ready. A wallet client with no getCapabilities, a failed read, and any other answer give false. It never rejects. Use it to choose between the batch setup and the step setup.

Subscription helpers

classifyErc20AllowSubscriptionBatchStatus reads a status in the form of viem getCallsStatus. The first rule that fits decides:
  1. A status code from 100 to 199 is pending.
  2. A successful receipt that holds the Transfer event of transfer is payment_proven. This is the word of the wallet, not a proof.
  3. Status 400 or 500 of an atomic batch is nothing_landed.
  4. Any other status is unknown.
For a batch with no payment (transfer is null), a final code (200 to 299, or 400 to 699) is final_without_payment. A status with a malformed field is unknown. Only a status read gives unknown_to_wallet.

Transfer helpers

logsHoldErc20Transfer answers true when a log of transfer.token is Transfer(from, to, value) with 3 topics, the same sender and receiver, and a one-word data equal to amount. Addresses compare without regard to case.

sendPayReceiptEmail

Asks the Anafi API to check one payment on chain and mail one receipt for it. No prompt. It sends one request and does not retry. Route: POST /api/pay/receipt_email. The request body is payReceiptEmailRequestBody(params):
payReceiptEmailRequestBody throws Error("pay receipt amount must be above 0") for an amount of 0 or less, and an Error for an unknown mode.

PayReceiptEmailOutcome

readPayReceiptEmailReply(value) reads the reply { outcome } and throws on any other shape or an unknown kind. A failed request throws BackendRequestError.