> ## Documentation Index
> Fetch the complete documentation index at: https://docs.skia.money/llms.txt
> Use this file to discover all available pages before exploring further.

# Settlements

> Commit/reveal on the SwapERC20 rail, the payment attempt machine, the Order template law, idempotency and nonce discipline — how invoice payments actually settle.

# Settlements

All money moves through one rail: the existing **commit → reveal** lanes on
`SwapERC20`. Invoice payments do not introduce a second settlement path — they
mint **Order templates** that resolve into the same Orders, byte-identical
wire.

## Commit/reveal in one paragraph

Commit has **no confidentiality**: the commit body carries all nine preimage
fields in plaintext; the withheld pieces are the maker signature, the AirSwap
order nonce, and the protocol fee. What commit buys is **equivocation-binding**
— the intent is bound to the taker under an expiry window, and the server's
stored `commitNonce` + `commitmentExpiry` are authoritative at reveal. Reveal
is **single-fire**: no transport retry; an already-revealed commit refuses.

## The payment attempt machine

One invoice maps to a **list** of payment attempts (mapping 1 → N). One
attempt = one payment try:

| Status | Meaning |
| - | - |
| `INTENT_CREATED` | template issued; the 10-minute liveness clock starts |
| `ORDER_SIGNED` | the signed Order is bound to the attempt via the **bind endpoint** |
| `SUBMITTED` | commit accepted on the existing lanes |
| `RECEIPTED` | the Swap event is seen — the settlement receipt |
| `FAILED` | revert or invalid — recorded, nothing corrupted |
| `EXPIRED` | the 10-minute window passed unburned |

`FAILED`/`EXPIRED` are terminal **for the attempt, not the invoice** — the
1 → N mapping simply creates the next attempt with a fresh nonce.

## The Order template law

`POST /v1/invoices/{invoiceId}/payments` returns an **unsigned** 10-minute
Order template. The debtor's agent signs it offline and posts it to the **bind
endpoint** — `POST /v1/invoices/{invoiceId}/payments/{attemptId}/order`
(`ORDER_SIGNED`'s wire path; the `/api` twin is the same handler — one law,
two mounts).

The template's fixed law:

| Field | Law |
| - | - |
| `signerWallet` | the **DEBTOR** (fee payer — the signer pays the fee on top of the signer leg) |
| `senderWallet` | the **creditor** |
| `signerAmount` | the attempt's TARGET + 1 atomic unit — the receipted **dust leg** |
| `senderAmount` | exactly 1 atomic unit — pulled from the creditor and returned to the debtor (`sender.amount > 0` is required on-chain; the zero-value sender leg was refuted by INVALID\_AMOUNT) |
| `nonce` | the attempt's **fresh nonce** |
| `expiry` | mint + 600 (the 10-minute clock) |
| `protocolFee` | the order-struct bps |
| `signatureType` | 0 |

The receipted fee semantics: **debtor-cost = target + 7 bps in the payment
token; creditor-net = target** (dust returns to the debtor). `feeBearer` is a
**pricing term** on the claim — who prices the fee into the deal — never a
protocol path.

The posted Order must satisfy this template law exactly (400 tier). The
signature must recover to the debtor over the exact fields in the SwapERC20
v4 EIP-712 order domain — **the Order signature IS the payment's signature**;
no new EIP-712 type exists for payment.

## Idempotency and nonce discipline

* The `Idempotency-Key` namespace is scoped **per invoice** — uniqueness is
  `(invoiceId, idempotencyKey)`. The same key on a different invoice names a
  different attempt.
* A retried intent with the **same key returns the same attempt** (200 replay)
  — a network retry never burns a second nonce or double-counts an in-flight
  payment. The same key with a **different payload** is
  `409 INVOICE_IDEMPOTENCY_CONFLICT`.
* **Terminal-retry rule**: a key on a terminal attempt
  (RECEIPTED/FAILED/EXPIRED) replays the same terminal record and **never
  re-executes** — a key never resurrects. The next attempt mints a new key
  (or omits the header to take the derived `(invoiceId, attemptSeq)` key).
* **Nonce discipline**: a fresh nonce per attempt, never reused — even when
  the previous attempt expired unburned (the bitmap only burns on success).
  Auditability over cleverness: the nonce column is a complete payment
  history with no exceptions to explain.
* Attempt rows are permanent: replay-by-key never expires; the only TTLs are
  the 10-minute Order clock and the attempt's own terminality.

## The amount law

The template's `signerAmount` targets the attempt's amount:

* **Default** — the full outstanding at intent creation.
* **Partial** — pass the optional `amount` on the intent request (invoice-
  currency atomic units, above zero and at most the outstanding at intent
  creation, else `400 INVOICE_AMOUNT_INVALID`). Partial payment intents exist
  and `PARTIALLY_PAID` is reachable through this declared lane.

The attempt **pins** its target (`intentAmount`, immutable for the attempt's
life — replays return the same template).

The race is **disclosed**: outstanding can shrink between intent and receipt.
The bind's **stale-template guard** refuses an order whose signer leg exceeds
the current outstanding (request a fresh intent instead of overpaying); two
concurrent full-outstanding attempts can both receipt → surplus, settled
out-of-band.

## Settlement receipt and the five-way mapping

The reveal's **Swap event is the complete receipt** — nonce indexed, both
parties, both legs, `protocolFeeAmount`, `blockNumber`. The ledger binds:

```text theme={null}
invoiceId <-> attempt <-> nonce <-> txHash <-> chainId
```

with `chainId` recorded **from day one** alongside nonce/txHash, so Phase 2's
cross-chain settlement is a service change, not schema surgery. Every attempt
also records `paymentCurrency` (null for `swap-direct`) — the day-one
multi-currency settlement-usage metric.

## Payment templates

| Template | Meaning |
| - | - |
| `swap-direct` | payment currency = invoice currency (the default lane) |
| `swap-convert` | currency differs from payment currency — the desk quotes the conversion (maxHops: 1, real conversion inventory) |
| `swap-sponsored` | **RESERVED Phase 2** — requesting it in Phase 1 answers `501 PHASE_GATED` (the surface's only remaining 501), behind the public-bundler receipt gate |

## Two-tier status convention

Refusals come in exactly two tiers — no third tier:

| Tier | Status | Examples |
| - | - | - |
| **Template law** | 400 | a posted order failing the template law; a pinned amount above outstanding |
| **Fold / machine** | 409 | terminal invoice states; expired window; SUBMITTED/RECEIPTED/FAILED/EXPIRED attempt; a different order on an ORDER\_SIGNED attempt; idempotency conflict |

## Where to next

* The claim side: [Invoices](https://docs.skia.money/concepts/invoices.md).
* Sponsored settlement and the fee path: [Sponsorships](https://docs.skia.money/concepts/sponsorships.md).
* The full lifecycle in raw requests: [Invoice lifecycle example](https://docs.skia.money/examples/invoice-lifecycle.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.