import { DepositProof, DepositProofExpectation, ReceiptLike, UserOperationProof, UserOperationProofExpectation } from '../domain/batch-proof'; import { SponsoredCall } from '../domain/sponsored-calls'; import { WaitableTransaction } from './observable'; /** * Submitting a sponsored atomic deposit batch, from the accepted calls to a * PROVEN deposit — the polling policy and the outcome taxonomy, and nothing * else. No relay vendor, no URL, no wallet: the app's ports do the I/O, and * this decides what each answer MEANS. * * The one rule everything here serves: **an ambiguous result is never * reported as failed.** "Failed" tells a lender to try again, and a batch that * may already have moved the money must never invite a second deposit. */ /** One call of the batch. Structurally `BatchCall`. */ export interface SubmitBatchCall { to: string; data: string; } /** * One read of the submitted batch's status, already NORMALISED by the app's * port — the vendor's status vocabulary stays in the app. */ export type BatchStatusRead = /** Mined: a transaction hash exists. */ { kind: 'mined'; txHash: string; userOperationHash?: string | null; } /** The vendor says, definitively, that it reverted: nothing deposited. */ | { kind: 'reverted'; } /** * Final for the vendor but silent about the deposit, or the status read * was refused: stop polling, outcome UNKNOWN (→ pending). */ | { kind: 'unknown'; code: string; } /** Not there yet, replaced, or a transient failure: ask again. */ | { kind: 'again'; userOperationHash?: string | null; }; export interface BatchSubmissionPorts { /** * Hand the batch to the submitter; resolve with its transaction id once it * is ACCEPTED. * * Throw `BatchRevertedError` when it is known to revert, and * `BatchSubmissionError(code, 'failed')` ONLY when nothing was sent — no * session, a refusal before broadcast. Any other throw is read as * PENDING: this cannot know whether an unrecognised error left before or * after the batch was accepted. Resending the same signed request after an * ambiguous answer is the port's business (it owns the wire protocol). */ post(calls: readonly SubmitBatchCall[]): Promise<{ transactionId: string; }>; /** One status read. A throw is treated as `{ kind: 'again' }`. */ getStatus(transactionId: string): Promise; /** * The mined receipt. Bound it with a timeout of your own; a throw is * PENDING (`receipt_unavailable`) — the batch was mined, its receipt was * just not readable. */ getReceipt(txHash: string): Promise; /** ms epoch. */ now(): number; sleep(ms: number): Promise; } export interface BatchPollingPolicy { /** Delay before the second status read (the first is immediate). */ initialMs: number; /** Ceiling on the delay between reads. */ maxMs: number; /** Multiplier applied to the delay after each read, ≥ 1. */ backoff: number; /** Stop polling — PENDING, never failed — after this long. */ timeoutMs: number; } /** * First read at once, then 3 s backing off ×1.5 to a 10 s ceiling, for up to * 10 minutes. Running out of time is PENDING: the batch was accepted and may * still land. */ export declare const DEFAULT_BATCH_POLLING_POLICY: Readonly; /** A proven deposit: the `deposited` proof and the receipt it was read from. */ export interface ProvenBatch { proof: Extract; receipt: R; txHash: string; } /** What `submitBatch` resolves with once the batch is accepted. */ export interface BatchSubmission extends WaitableTransaction { transactionId: string; /** * Polls to a mined receipt and proves the deposit in it. Resolves ONLY on * a proven deposit. Rejects with `BatchRevertedError` when it definitively * did not deposit, and with `BatchPendingError` for everything else. */ wait(): Promise>; } /** * Submit `calls` and return once the submitter ACCEPTED them; `wait()` then * polls and proves. Use it as the body of a `DepositFlow` `depositBatch` * port, after `validateDepositBatchCalls` and the app's own signing: * * ```ts * depositBatch: (calls) => { * const { tranche, amount } = validateDepositBatchCalls(calls, expected); * return submitBatch( * // `pendingPool`: the strategy's pending pool, so only ITS * // `DepositRequested` can prove the deposit. * { calls, expect: { lender, tranche, amount, pendingPool } }, * { post, getStatus, getReceipt, now: Date.now, sleep }, * ); * }, * ``` * * | what happened | outcome | * | ---------------------------------------------------- | ---------------------------------------- | * | `post` throws `BatchRevertedError` | rethrown — reverted, nothing deposited | * | `post` throws `BatchSubmissionError` | rethrown with its own outcome | * | `post` throws anything else | `BatchPendingError('post_unknown')` | * | `post` resolves without a transaction id | `BatchPendingError('malformed_answer')` | * | status `reverted` | `BatchRevertedError('')` | * | status `unknown` | `BatchPendingError(code)` | * | polling runs out of time | `BatchPendingError('status_timeout')` | * | receipt unreadable | `BatchPendingError('receipt_unavailable')` | * | receipt: user operation reverted | `BatchRevertedError(revertError)` | * | receipt: this deposit not shown | `BatchPendingError('deposit_unproven:')` | * | receipt: this lender's deposit, tranche and amount | resolves `ProvenBatch` | * * Nothing on this path ever produces a `failed` outcome: by the time a batch * has been posted, only a revert proves that nothing was deposited. */ export declare function submitBatch(input: { calls: readonly SubmitBatchCall[]; /** What the receipt must show — see `proveDepositInReceipt`. */ expect: Omit; }, ports: BatchSubmissionPorts, policy?: Partial): Promise>; /** * What became of an earlier submission that ended PENDING — ONE status read, * no polling — for an app's guard against lending twice: * * - `deposited` — mined, and the receipt proves the deposit; * - `not-deposited` — definitively reverted (vendor status or the user * operation's own flag); * - `unknown` — anything else, including a read or receipt that failed. * * Only `not-deposited` may let a new lend proceed unprompted; `deposited` * means the lender is told the earlier one landed (`already-submitted`). */ export declare function checkBatchOutcome(transactionId: string, expect: Omit, ports: Pick, 'getStatus' | 'getReceipt'>): Promise<'deposited' | 'not-deposited' | 'unknown'>; /** * `checkBatchOutcome` for ONE sponsored call — ONE status read, no polling — * for an app's guard against sending the same request twice (a P2P transfer * whose earlier submission ended PENDING, say): * * - `executed` — mined, and the receipt proves the sender's operation * succeeded (`proveUserOperationInReceipt`); * - `not-executed` — definitively reverted (vendor status or the operation's * own flag); * - `unknown` — anything else, including a read or receipt that failed, or a * receipt that does not show the sender's operation. * * Only `not-executed` may let a new attempt proceed unprompted; `executed` * means the earlier one landed and the user should be told. */ export declare function checkSponsoredCallOutcome(transactionId: string, expect: Omit, ports: Pick, 'getStatus' | 'getReceipt'>): Promise<'executed' | 'not-executed' | 'unknown'>; /** * `BatchSubmissionPorts` for ONE sponsored call: the same status, receipt and * clock ports — a relay client written for the deposit batch serves both — * with a `post` that receives the call together with its relay `kind`, so the * app never maps one from the other. */ export interface SponsoredCallSubmissionPorts extends Omit, 'post'> { /** * Hand the call to the submitter (`kind` and `[call]` in the relay's * body); resolve with its transaction id once it is ACCEPTED. The same * throw rules as `BatchSubmissionPorts.post`: `BatchRevertedError` when * it is known to revert, `BatchSubmissionError(code, 'failed')` ONLY when * nothing was sent, anything else is read as PENDING. */ post(call: SponsoredCall): Promise<{ transactionId: string; }>; } /** An executed call: the `executed` proof and the receipt it was read from. */ export interface ExecutedCall { proof: Extract; receipt: R; txHash: string; } /** What `submitSponsoredCall` resolves with once the call is accepted. */ export interface SponsoredCallSubmission extends WaitableTransaction { transactionId: string; /** * Polls to a mined receipt and proves the sender's operation EXECUTED. * Rejects with `BatchRevertedError` when it definitively did not, and * with `BatchPendingError` for everything else. */ wait(): Promise>; } /** * `submitBatch` for ONE sponsored call — the body of a `WithdrawFlow` * `submitCall` port, of an app's cancel or withdraw-at-expiry action, and of * a P2P transfer (`encodeStableTransfer`). Same post, same polling policy, same outcome * table; the proof is `proveUserOperationInReceipt` (the sender's operation * succeeded) instead of the deposit proof, and an unproven receipt is * `call_unproven:`. * * ```ts * submitCall: (call) => * submitSponsoredCall( * { call, expect: { sender: lender } }, * { post: (c) => postToMyRelay(c.kind, [c.call]), getStatus, getReceipt, now, sleep }, * ), * ``` * * The call is checked against its kind before anything is posted — a known * kind, exactly `{ to, data }`, and `data` opening with that kind's selector — * and a mismatch is `BatchSubmissionError('call_mismatch', 'failed')`: nothing * was sent. Build calls with `kasu.calls` (or `encodeStableTransfer`), and * this never fires. * * As on the deposit path, nothing after the post ever produces `failed`. */ export declare function submitSponsoredCall(input: { call: SponsoredCall; /** Whose operation must have executed — see `proveUserOperationInReceipt`. */ expect: Omit; }, ports: SponsoredCallSubmissionPorts, policy?: Partial): Promise>;