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

# Ambi (market)

> The market contract: listing, requests, matching, settlement, cancellation and withdrawals.

`src/Ambi.sol`. Inherits `ReentrancyGuard` and `Ownable2Step`.

## Constants and state

| Name | Value | Meaning |
| - | - | - |
| `DOMAIN` | `keccak256("Ambi.Payment.v1")` | Statement domain |
| `FEE_BPS` | `30` | 0.30% protocol fee |
| `MAX_INTEGRATOR_BPS` | `100` | Highest integrator fee, 1% |
| `MAX_INTERESTS` | `64` | Most open requests per order |
| `MIN_WINDOW` | Immutable, `1800` in v1 | Seconds required before `payBy` at listing and matching |
| `REGISTRY` | Immutable | The `VerifierRegistry` |
| `feeRecipient` | Owner-proposed, self-accepted | Receives fees as credits |
| `offers(id)` | `Offer` | Maker, token, amount, request hash, verifier, `payBy`, `proveBy`, filler, state, bond, verifier code hash |
| `credits(owner, asset)` | `uint256` | Withdrawable balances; ETH is `address(0)` |
| `cancelProposedBy(id)` | `address` | Who proposed a cancel, if anyone |
| `integrators(id)` | `(address account, uint16 bps)` | The order's integrator and its fee; zero if listed with `list` |
| `payoutOf(id)` | `address` | Where the matched filler asked the escrow to go; zero means the filler |

States: `Missing`, `Listed`, `Matched`, `Filled`, `Refunded`, `Canceled`.

## Listing

**list(address token, uint256 amount, uint256 bond, Request request) payable → uint256 id** `anyone`

Escrows `amount` of `token` (`address(0)` for ETH, sent as `msg.value`) and lists the order.

Reverts with `InvalidTerms` if amounts are zero, any of `chain`, `asset`, `recipient` is empty, the token has no code, `msg.value` is wrong, or the verifier's `accepts` returns false. `PaymentWindowTooShort` if `payBy` is under `MIN_WINDOW` away. `VerifierUnavailable` (from the registry) if the verifier isn't approved or its code changed. `TransferFailed` if the market's balance didn't rise by exactly `amount`.

Emits `Listed(id, maker, token, amount, bond, request)`.

**listVia(address token, uint256 amount, uint256 bond, Request request, address integrator, uint16 bps) payable → uint256 id** `anyone`

Lists exactly as `list` does, and records an integrator that earns `bps` of the escrow at release. `InvalidTerms` if `bps` is 0 or above `MAX_INTEGRATOR_BPS`, or `integrator` is zero or the market. Emits `Listed` and `IntegratorSet(id, integrator, bps)`.

## Requests

**requestFill(uint256 id) payable** `anyone but the maker`

Posts the bond (`msg.value` must equal `bond`) and joins the order's requests. `Unavailable` if not listed, past `payBy`, called by the maker, or already requested. `InvalidBond` if the value is wrong. `InterestLimit` at 64 requests. Emits `Interested(id, filler, bond)`.

**requestFillTo(uint256 id, address payout) payable** `anyone but the maker`

Same as `requestFill`, with the escrow credited to `payout` if this filler is matched. The filler still pays from their own address, and the bond still returns to the filler. `InvalidTerms` if `payout` is zero or the market. Emits `Interested` and `PayoutRequested(id, filler, payout)`.

**withdrawInterest(uint256 id)** `a requester`

Leaves the requests while the order is listed. The bond is credited back. Emits `InterestWithdrawn(id, filler, bond)`.

## Matching

**matchOffer(uint256 id, address filler)** `maker`

Selects one requester. `Unauthorized` if not the maker. `Unavailable` if not listed or `filler` didn't request. `PaymentWindowTooShort` if under `MIN_WINDOW` remains. `VerifierCodeChanged` if the verifier's code no longer matches. Every other bond is credited back. Emits `Matched(id, filler, statement)`.

**statement(uint256 id) view → bytes32** `view`

The matched order's statement. `Unavailable` before a match.

**quoteStatement(uint256 id, address filler) view → bytes32** `view`

The statement this order would have with `filler`.

## Settlement

**fulfill(uint256 id, bytes proof)** `anyone`

Settles a Paid order. Requires `Matched`, unchanged verifier code, and `verify(statement, proof)` true (`InvalidProof` otherwise). Credits `amount − fee − integrator fee` to the filler (or `payoutOf(id)`), the bond to the filler, `feeRecipient` the fee and the integrator its fee, where `fee = amount × 30 / 10,000` and the integrator fee is `amount × bps / 10,000`, both rounded down. Emits `FeeTaken(id, token, fee)`, `IntegratorFeeTaken` when there is one, and `Filled(id, filler)`.

**refundExpired(uint256 id, bytes proof)** `anyone`

Settles an ExpiredUnpaid order. Requires `Matched`, unchanged verifier code, and `verifyExpired(statement, proof)` true. Credits the maker the escrow and the filler's bond. Emits `Refunded(id)`.

**refund(uint256 id)** `maker; anyone after payBy`

Closes a listed order. Before `payBy` only the maker can call it. Credits the maker the escrow and every requester their bond. Emits `Refunded(id)`.

## Cancellation

**proposeCancel(uint256 id)** `maker or matched filler`

Opens a cancel proposal on a matched order. Emits `CancelProposed(id, proposer)`.

**acceptCancel(uint256 id)** `the other party`

Accepts the open proposal. Reverts with `Unavailable` if the verifier already reports Paid for the statement. Credits the maker the escrow and the filler the bond; the order becomes `Canceled`. Emits `CancelAccepted(id, accepter)`.

## Withdrawals

**withdraw(address token, address to)** `anyone with a credit`

Sends the caller's whole credit for `token` to `to`. Reverts with `InvalidTerms` for a zero credit, a zero `to` or the market itself, and `TransferFailed` unless both balances moved by exactly the amount. Emits `Withdrawn(owner, asset, to, amount)`.

## Administration

**proposeFeeRecipient(address proposed)** `owner`

Proposes a new fee recipient. Emits `FeeRecipientProposed`.

**acceptFeeRecipient()** `the proposed address`

Completes the change. Emits `FeeRecipientChanged(previous, current)`.

The owner has no other power over the market. It can't move escrow or credits, change the fee rate, change `MIN_WINDOW`, pause the market or upgrade it.

## Payload functions

`matchWithPayload` and `publishPayload` let the matched parties pin an off-chain ciphertext digest. They play no part in normal settlement.


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