Skip to main content
After this page, you can build a pay page: a page that a payee shares as a link, where a payer pays an amount of an ERC-20 token one time, or pays now and then each month. You know each prompt, how to prove the payment, how to resume after a reload, and how to send a receipt email.

The pattern

A pay link carries the payee address, the token, the amount and the network. Your pay page reads the link, connects the wallet of the payer, and offers two modes: Where your code runs:
  • A one-time payment is a plain transaction of the payer wallet. It does not use the Anafi API. It works in any browser page.
  • The stage of the monthly rule goes through the Anafi API. Today, the Anafi API hosts answer CORS only for https://app.ana.fi, so stage the rule from your server or from your own same-origin proxy. The stage needs no wallet. The setup after the stage uses only the chain and the wallet, so it runs in the browser of the payer.
  • sendPayReceiptEmail uses the Anafi API. Call it from your server or proxy.

One-time payment

Sequence

Steps

1

Fix the lowest payment block

Read the tip before the request and add 1: const lowestPaymentBlock = (await anafi.resolveReadBlock(null)) + 1n. A receipt below this block is an older transfer and proves nothing about this payment. Store this value with the request.
2

Send the payment

Call sendErc20Payment({ payer, token, payee, amount, lowestPaymentBlock, signal?, onRequested?, onSubmitted? }). amount is in base units of the token.Before the prompt, the SDK checks that signal is not aborted, that the wallet is on the chain of the client, and that the connected account is payer. When a check fails, it returns { kind: "stopped_before_request", reason } with reason cancelled, wallet_on_other_chain or wallet_on_other_account. The wallet client must have getAddresses and getChainId.Then one transaction prompt: token.transfer(payee, amount). onSubmitted(txHash) runs with the hash. Store the hash before you do anything else. The SDK waits for the receipt and follows a replacement that the wallet makes.
3

Read the outcome

A rejection by the payer throws the wallet error. A receipt that does not arrive in time throws too. You then hold the hash from onSubmitted.
The proof of a payment is the Transfer(payer, payee, amount) event in a successful receipt. A successful receipt alone is not a proof: a wallet can replace a transfer with a cancel transaction that also succeeds. logsHoldErc20Transfer(logs, transfer) runs the same check on any logs.

Resume after a reload

  1. Read the stored hash with readErc20PaymentTransactionState({ txHash, token, payer, payee, amount, lowestPaymentBlock }). No prompt. It returns paid, reverted, mined_without_transfer, receipt_below_lowest_block, pending (the node knows the transaction, no receipt yet) or not_found.
  2. On not_found, the wallet can have replaced the transaction under another hash. Call scanErc20PaymentTransfers({ token, payer, payee, amount, fromBlock: lowestPaymentBlock }). It reads at most 2000 blocks (ERC20_PAYMENT_TRANSFER_SCAN_MAX_BLOCKS) in one call and returns { transfers, toBlock, reachedTip }. When reachedTip is false, scan again from toBlock + 1n.
  3. Do not ask the payer to pay again before both reads say that no transfer exists.

Monthly payment on the ERC20Allow kind

The payer account must be an ERC20Allow account: an address with no code, or with an EIP-7702 pointer. The rule pays from that address each month.

Preconditions

Check the committed document of the payer first. A commit replaces the whole rule document of the account. A monthly setup over a document of another app removes the rules of that document.

The rule and the cap

The rule pays amount of the token to the payee each month. The fee token is the same token, because the setup approves only one token. buildStandingOrderDsl builds the text:
The first payment is a separate transfer now, so startAt is the second payment. The cap limits what the adapter can pull from the payer in each window. Each payment pulls the amount, the Anafi fee and the gas charge. Give the cap room for at least one full payment in each window, with periodSeconds of at least 3600. The fee terms give the numbers:
  • Anafi fee: max(ceil(amount × bps / 10000), floor of the token), from fetchFeeTerms(), when feesOn is true.
  • Gas charge bound: tokenAmountOfWei(transferCostBoundWei(gas, 2), rate.tokenUnitsPerNativeCoin), with gas from the fee terms and rate the row of the token in gas.rates. One payment has 2 transfer legs: the payment and the fee.

Sequence of the step setup

Steps

1

Stage on your server

plannedCap makes the stage count the cap as it will be after the setup. Store yaml and salt with the pay request. The same text and the same salt give the same commitment again after a reload. Send staged to the browser. It is plain JSON data. The setup checks that its commitment and tokens match its own text.
2

Build the plan

The setup refuses, before any read: an adapter that is not the erc20_allow row, a staged document of another account than the payer, a cap on another token, a cap window under 3600 seconds, and a staged document that pulls another token.
3

Choose the transport

readWalletAtomicBatchSupport(payer) returns true when the wallet reports the EIP-5792 atomic capability as supported or ready on this chain. Then you can send all steps in one prompt. Else use the step setup.
4

Step setup: one transaction per step

Call setUpErc20AllowSubscription({ payer, plan, paymentConfirmed, lowestPaymentBlock, signal?, onRequested?, onSubmitted?, onStepConfirmed? }).Before each prompt, the SDK reads at one block the allowance and the cap of the token, the block switch, the committed document and the account class. It builds the list of absent steps with buildErc20AllowSubscriptionCalls and asks for the first one. A step that the chain already shows is skipped. The steps, in order:The payment is complete when its receipt holds Transfer(payer, payee, amount) at or above lowestPaymentBlock. Each other step is complete when a later read shows its state. Before the commit request, the SDK counts the attestation as lapsed when 300 seconds or less remain (ERC20_ALLOW_SUBSCRIPTION_ATTESTATION_MARGIN_SECONDS).onRequested(step) runs before each prompt and can return a promise; use it to store state first. onSubmitted(step, txHash) runs with each hash. onStepConfirmed({ step, txHash, blockNumber }) runs when the chain shows a step.
5

Batch setup: one prompt

Call setUpErc20AllowSubscriptionInBatch({ payer, plan, paymentConfirmed, lowestPaymentBlock, signal?, onRequested?, onSubmitted? }).The SDK reads the same facts, builds the same call list, and sends it in one wallet_sendCalls request (EIP-5792) with forceAtomic: true under a new random identifier. onRequested(batchId) runs before the request: store the identifier there. After the wallet accepts the batch, the SDK reads the calls status every 2 seconds for at most 180 seconds. It checks a payment that the status claims against the receipt that its own public client reads.

Outcomes of the step setup

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

Outcomes of the batch setup

A rejection by the payer throws the wallet error in both setups.

Resume after a reload

Store, before each prompt: the pay request, lowestPaymentBlock, yaml, salt, the commitment of each stage, each transaction hash (onSubmitted) and the batch identifier (onRequested).
  1. Batch: call readErc20AllowSubscriptionBatchResult({ payer, plan, batchId, batchHoldsPayment, lowestPaymentBlock }). It sends nothing, reads the status one time, and returns a batch result as above.
  2. Payment: prove it with readErc20PaymentTransactionState, or scanErc20PaymentTransfers from lowestPaymentBlock. When it is proven, set paymentConfirmed: true.
  3. Stage again when the attestation lapsed. Put the commitments of the earlier stages in expectedCommitments, so that a commit of an earlier stage that landed counts as this plan.
  4. Run the step setup again. It skips each step that the chain already shows.

Receipt email

sendPayReceiptEmail({ email, txHash, payer, payee, token, amount, mode }) sends POST /api/pay/receipt_email with { email, txHash, payer, payee, token, amountRaw, mode }. mode is "once" or "monthly". No prompt. One request, no retry. amount must be above 0. The Anafi API checks the payment on chain before it sends a mail. For monthly, it also checks the ERC20Allow facts of the payer for the token, a cap whose limit is at least the amount, and a committed document. It cannot check the schedule, because the document is confidential.

Example

A browser page sends the one-time payment. The monthly setup takes a staged value from your server.