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

# How batched settlement works

> How Circle Gateway aggregates payment authorizations and settles them onchain in a single transaction

Circle Gateway enables nanopayments by batching many individual payment
authorizations into a single onchain transaction. Instead of settling each
payment separately (and paying gas each time), Gateway collects signed
authorizations offchain, computes net balance changes, and applies them in bulk.

This document explains the batching lifecycle, the security model that keeps it
non-custodial, and how balances move through the system.

## The economics of batching

Settling a USDC transfer onchain costs gas. Even on low-fee blockchains, gas
costs can exceed the value of a sub-cent payment. Batching solves this by
amortizing gas across thousands of payments:

| Approach              | Gas cost per payment              | Viable minimum payment            |
| --------------------- | --------------------------------- | --------------------------------- |
| Individual settlement | Full gas per transaction          | \~\$0.01+ (depends on blockchain) |
| Batched settlement    | Gas / number of payments in batch | \$0.000001                        |

By settling net positions rather than individual transfers, Gateway reduces both
the number of onchain transactions and the total gas consumed.

## Payment lifecycle

A nanopayment moves through five stages from initiation to settlement:

```mermaid theme={null}
sequenceDiagram
    participant Buyer
    participant Seller
    participant Gateway as Circle Gateway

    Buyer->>Gateway: 1. Deposit USDC (one-time onchain tx)

    Buyer->>Seller: 2. Request resource
    Seller-->>Buyer: 402 Payment Required

    Note left of Buyer: Signs EIP-3009 authorization (offchain, zero gas)

    Buyer->>Seller: 3. Retry with signed authorization
    Seller->>Gateway: 4. Submit authorization
    Seller-->>Buyer: 200 OK + resource

    Note right of Gateway: Collects authorizations

    Gateway->>Gateway: 5. Batch settle (single onchain tx)
    Gateway-->>Seller: Funds credited to balance
```

<Steps>
  <Step title="Deposit">
    The buyer deposits USDC from their wallet into a Gateway Wallet contract.
    This is a one-time onchain transaction that establishes the buyer's Gateway
    balance. Once deposited, the buyer can make gasless payments from this
    balance.
  </Step>

  <Step title="Request and negotiate">
    The buyer requests a paid resource. The seller responds with `402 Payment
            Required`, including payment details. This follows the standard [x402
    protocol flow](/gateway/nanopayments/concepts/x402).
  </Step>

  <Step title="Sign authorization">
    The buyer signs an [EIP-3009](/gateway/nanopayments/howtos/eip-3009-signing)
    `TransferWithAuthorization` message authorizing Gateway to transfer USDC
    from their balance to the seller. This signature is created offchain and
    costs zero gas. The buyer retries the request with the signed authorization
    attached.
  </Step>

  <Step title="Settle and serve">
    The seller (or a facilitator) submits the signed authorization to Gateway.
    Gateway verifies the signature, locks the buyer's funds, and credits the
    seller's pending balance. The seller serves the resource immediately,
    without waiting for onchain settlement. Neither the buyer nor the seller
    pays gas for this step.
  </Step>

  <Step title="Batch and onchain settlement">
    Gateway periodically collects pending authorizations, computes net balance
    changes across all participants, and submits a single onchain transaction
    that applies the changes. After onchain confirmation, pending balances
    become available. The seller can then withdraw funds to any supported
    blockchain.
  </Step>
</Steps>

## Security model

Gateway's batching system is non-custodial. Gateway never has arbitrary control
over user funds. The security model relies on three components:

### Trusted execution environment (TEE)

A Trusted Execution Environment (TEE) is an isolated hardware enclave that runs
code in a secure, tamper-proof environment. Gateway uses an AWS Nitro Enclave
to:

* Verify every EIP-3009 signature before including it in a batch
* Compute net balance changes across all payments in the batch
* Sign the batch result with the enclave's private key

The enclave's signing key is protected by AWS Key Management Service (KMS) with
attestation-based access policies. Only the audited enclave image can access the
key. Even Circle operators cannot extract it outside the enclave.

### Onchain verification

The Gateway Wallet smart contract verifies the TEE's signature before executing
any batch. If the signature is invalid or comes from an unauthorized signer, the
contract reverts. This ensures that only correctly computed batches are applied
onchain.

### Cryptographic attestations

AWS Nitro Enclaves produce cryptographic attestation documents that prove the
enclave is running a specific, audited code image. These attestations can be
independently verified, providing transparency into the batching process.

## Balance states

Gateway tracks funds through multiple states as they move through the batching
pipeline:

| State       | Description                                                                      |
| ----------- | -------------------------------------------------------------------------------- |
| `available` | Spendable balance. Set after deposit or after receiving a settled batch payment. |

### Example: Alice sends 10 USDC to Bob

| Event                   | Alice (sender)   | Bob (receiver)  |
| ----------------------- | ---------------- | --------------- |
| Initial state           | `available: 100` | `available: 0`  |
| Authorization submitted | `available: 90`  | `available: 0`  |
| Batch settled onchain   | `available: 90`  | `available: 10` |

When Alice submits her authorization, Gateway locks her funds internally. Once
the batch settles onchain, Bob's available balance increases.

## Withdrawal

After settlement, sellers (and buyers) can withdraw their Gateway balance to any
supported blockchain. Same-chain withdrawals are instant. Crosschain withdrawals
use Gateway's standard minting infrastructure and are also near instant.
