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
Transferevent. 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 holdsTransfer(payer, payee, amount)of the token, with the exact amount (logsHoldErc20Transfer). lowestPaymentBlockguards 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
signalis not aborted, thatwalletClient.getChainId()ischainId, and thatwalletClient.getAddresses()[0]is the payer. A failed check answersstopped_before_requestwith a reason. A wallet client withoutgetAddressesorgetChainIdmakes the method throw. - A wallet rejection rejects. When the payer rejects the request, the method rejects with the error of the wallet.
OwnerRequestStopReason
sendErc20Payment
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
Throws: a failed read, a receipt that names no mined transaction, and a public client with no
getTransactionReceipt or no getTransaction.
scanErc20PaymentTransfers
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 anError:
- a
staged.adapterthat is not the configurederc20_allowrow; - a
staged.accountthat is not the payer; - a
cap.tokenthat is nottoken; - a cap window under 3600 seconds;
- a staged document that pulls another token than
token.
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
- 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. - If the chain holds a commitment that the plan does not expect, end with
unexpected_commitment. - Build the list of the steps that are still absent.
- 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. - Report the step that the read now shows with
onStepConfirmed. - If the list is empty, end with
completed. - Run the stop checks. For the
commitstep, end withattestation_lapsedwhen 300 seconds or less remain to the attestation deadline (ERC20_ALLOW_SUBSCRIPTION_ATTESTATION_MARGIN_SECONDS). - Call
onRequested(step)and wait for its promise. Ask the wallet for the first step. CallonSubmitted(step, hash). Wait for the receipt. - For
payment: a receipt with theTransferevent confirms it (onStepConfirmed). Any other verdict ends the setup. For another step: a reverted receipt ends withstep_reverted. - Go to step 1.
Erc20AllowSubscriptionOutcome
setUpErc20AllowSubscriptionInBatch
- Runs the plan checks. A wallet client with no
sendCallsor nogetCallsStatusanswers{ kind: "batch_refused", code: null }. - Reads the facts once and builds the same call list. An unexpected commitment answers
unexpected_commitmentwithpayment: null. An empty list answerscompletedwithpayment: null. - Makes a new random 32-byte batch id. Runs the stop checks, and for a batch with the commit, the 300-second attestation margin.
- Calls
onRequested(batchId)and waits for its promise. Store the id here. - Calls
sendCalls({ account: payer, calls, id: batchId, forceAtomic: true }). - A wallet error with one of the codes 4100, 4200, -32601, -32602, 5700, 5710, 5740, 5750, 5760 proves a refusal before acceptance:
batch_refusedwith thatcode. A user rejection (4001) rejects. Any other error can hide an accepted batch, so the setup reads the status of its id. - Calls
onSubmitted(id)when the wallet accepted the batch. - Reads
getCallsStatusevery 2 seconds while the batch is not final, for up to 180 seconds. - Classifies the status. A payment that the wallet claims counts only when the receipt from your public client proves it, at or above
lowestPaymentBlock. - After the proof, or after a final batch with no payment, reads the facts again and lists the steps that are still absent.
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
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
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:
- A status code from 100 to 199 is
pending. - A successful receipt that holds the
Transferevent oftransferispayment_proven. This is the word of the wallet, not a proof. - Status 400 or 500 of an atomic batch is
nothing_landed. - Any other status is
unknown.
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
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.