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

# README

# Hearth — Technical README

Hearth is Fuego's native XFG/HEAT market. This document explains the v11 architecture: what was built, why it is novel, and how it works end to end.

## 1. What Hearth is

Hearth is a **per-block call auction plus a constant-product AMM**, implemented as a consensus rule on a privacy (CryptoNote/RingCT) chain. **One inventory, two uses** — not AMM *or* depth band. `m_ammPool` (`reserveXfg × reserveHeat`) is the balance sheet (LP shares, direct swaps, mint-spot fallback, post-auction backstop). The depth band is that same inventory quoted as 0xF0 makers in the auction (`generatePoolOrders()`, `HEARTH_DEPTH_BAND_PCT` of reserves, adaptive 30–300 bps). Two-sided price discovery is on-chain, at block time, from tx-extra-backed user limits plus those pool makers.

Three properties define it:

1. **Price discovery, not price smoothing.** The clearing price is the crossing point of committed bids and asks that maximizes executed volume (exchange opening-auction rules). The AMM does not set the price — users do, by committing funds to competing quotes.
2. **Orders are transactions.** A limit order is a `TransactionExtraLimitDeposit` (XFG or HEAT committed to escrow buckets). There is no gossip, no message signatures, no mempool of unsigned orders. Everything resting in the book is backed by funds that already settled on-chain.
3. **One price, once per block, for everyone.** The auction is batch-cleared: no ordering races, no MEV by transaction ordering, no front-running a queue.

## 2. Why this is more than "an AMM on a UTXO chain"

| Dimension | Typical UTXO-chain DEX | Hearth (v11) |
| - | - | - |
| Price source | AMM curve (reserve ratio) | Two-sided auction of committed user orders **plus** pool depth-band makers |
| User orders | Often swaps-only, or off-chain signed orders | On-chain tx-extra deposits — backed, signed, deterministic |
| Execution | Continuous per-tx | Batch, once per block, one clearing price |
| Maker compensation | Often none | 30% of the 1% taker fee rebated to makers |
| Protocol revenue | Fee to treasury/liquidity | 70% of the 1% taker fee → HEAT CD yield pool |
| LP mechanics | Single-sided often allowed | Balanced-only (no dilution), fair pro-rata shares |
| Manipulation surface | Sandwich attacks on mempool ordering | Batch VWAP-free clearing; self-trade exclusion; min price tick; fee-gated sybil |
| Backing | Reserves only | User orders escrowed; pool makers backed by the same AMM reserves used for swaps/backstop |

The unusual parts worth calling out:

* **Price-time priority on a privacy chain.** The auction orders fills by price, then by deposit block height, then by order hash — a fair queue without revealing the owner (the owner is only an `addressHash`, a hash of spend+view keys).
* **Self-trade exclusion by construction.** A key may participate on only one side of the auction per block (its latest order's side wins), closing the trivial wash-trade path without KYC.
* **The mint price reads discovered prices.** The HEAT mint TWAP is fed from `P_clear` (the auction's last price), falling back to the pool ratio only during bootstrap. Money creation is priced by what people actually paid, not by an inventory curve.
* **A treasury that is a market maker.** The Treasury LP Manager takes protocol fee income in both assets, pairs them at the pool ratio, and deposits as the protocol's own LP position — compounding, and driving bootstrap repayment by an owned-reserves check in both legs.

## 3. Architecture

```
                      ┌──────────────────────────────────┐
   Limit deposit txs  │  escrow buckets (pendingXfg/     │
   (tx-extra backed)  │  pendingHeat) + LimitDepositInfo │
                      └───────────────┬──────────────────┘
                                      ▼
                        Per-block processOrderbookForBlock
                                      │
                   ┌──────────────────┼───────────────────┐
                   ▼                  ▼                   ▼
            height expiry      CALL AUCTION          AMM backstop
            (claimable)    (user deposits + 0xF0     (leftover user
                                pool depth-band)      deposits vs x*y=k)
                                ▼
              P_clear = max-volume clearing price
                                │
                                ▼
                    mint TWAP ← P_clear (rolling 8-block)
```

### 3.1 Call auction

* Books are built from resting deposits: `BUY_XFG` (HEAT budget) → bids; `SELL_XFG` (XFG) → asks. Prices are in canonical scale: HEAT atomics per XFG atomic × COIN (1e7).
* Clearing price `p*` maximizes executed volume; ties break by imbalance, then proximity to the previous block's `P_clear`, then lower price.
* Fills settle in price-time priority, rationing the larger side at `p*`.
* User-vs-user fills move escrow buckets only — **AMM reserves are untouched**. Pool depth-band fills (`orderId` prefix `0xF0`) **do** debit/credit `reserveXfg`/`reserveHeat`; those makers are the pool quoting its inventory, not a second book.

### 3.2 Fee model (1% taker, versioned split)

* The taker side (the rationed side) pays 1% of its fills' price value.
* The split is versioned (`hearthCdSharePct`): **v11 pays 30% CD / 70% maker** — the pool and the book are new, so the fee buys depth — and **v12 flips to 70% CD / 30% maker** once the market is liquid. Consensus passes the block's own version so reorg replay recomputes the split that block settled.
* The CD share accrues to `cdHearthFeeAccumulator` (HEAT-denominated) and is minted into the CD yield vault (`CD_APY_POOL`) at the epoch boundary; the consumed XFG is burned 50/50 (Eternal Flame / SWF).
* The maker share is the maker rebate: distributed to the maker side pro-rata by fill value, with cumulative floors so conservation is exact.
* Exact volume ties have no taker: no fee, no rebate (pure exchange).
* On backstop/AMM fills the pool is the maker: the maker share stays in LP reserves, the CD share goes to CD yield.

### 3.3 AMM backstop

* Constant-product (`xfg × heat = k`): inventory for LP, direct swaps, depth-band sizing, and the unfilled remainder after the auction.
* Volume-capped per block at `HEARTH_BACKSTOP_MAX_BPS` (500) of the block's auction volume; uncapped when no auction volume exists (bootstrap).
* LP deposits are balanced-only; shares mint pro-rata (`min(amountA·T/reserveA, amountB·T/reserveB)`), never single-sided (single-sided mints nothing — no dilution).

### 3.4 Determinism and rollback

* Every fill (auction and backstop) is recorded in `m_blockOrderFills` with exact moved amounts, fees, and rebates; `popBlock` reverses them in reverse order.
* The epoch snapshot is captured at block start; dir-1 swap CD-fee HEAT equivalents are recorded per block for exact reversal (pop-time pool rates differ from push-time).
* No RNG, no float, no wall clock: the entire exchange is a pure function of chain state.

## 4. Economic flows

| Flow | Route |
| - | - |
| Taker fee (auction) | 1% of taker notional → 30% CD yield accumulator / 70% maker rebate pool at v11 (70/30 from v12) |
| Taker fee (backstop/AMM) | The CD share is debited from LP reserves → CD yield; the maker share stays with LPs |
| Swap fees (cross-chain) | 80% CD yield / 20% bonus vault at v11 (pre-v11 replay: 69/11/20) |
| Mint premium | 1% of every HEAT mint's burn (`HEAT_MINT_PREMIUM_BPS`) → withheld premium XFG to the AuntPoL XFG leg (`m_treasuryLpPendingXfg`) and the HEAT it buys to the HEAT leg (`m_treasuryHeatReserve`) |
| Treasury LP Manager | Premium XFG (XFG leg) + premium HEAT (HEAT leg, plus donations) → ratio-paired LP position; compounds; repays bootstrap |
| CD creation fee | 0.1% of the deposit, charged in XFG → dev fund |
| Burns | Every XFG→HEAT conversion burns consumed XFG 50% EF / 50% SWF; all burns tally in `total_burned_xfg` |

## 5. Key constants (v11)

| Constant | Value |
| - | - |
| `HEARTH_FEE_BPS` | 100 (1% taker) |
| `HEARTH_CD_SHARE_PCT_V11` | 30 |
| `HEARTH_CD_SHARE_PCT_V12` | 70 |
| `HEARTH_BACKSTOP_MAX_BPS` | 500 |
| `ORDER_PRICE_TICK` | COIN/100 |
| `BOOTSTRAP_BLOCKS` | 144 |
| `HEARTH_POOL_SEED_XFG/HEAT` | 10,000 XFG / 1,000 HEAT (10:1) |
| Price scale | HEAT/XFG × COIN (1e7), integer-only |

## 6. Key files

| File | Purpose |
| - | - |
| `src/CryptoNoteCore/OrderbookAuction.{h,cpp}` | Call-auction matcher (clearing price, priority, self-trade, fees) |
| `src/CryptoNoteCore/Blockchain.cpp` | `processOrderbookForBlock`, settlement, rollback, treasury epoch work |
| `src/CryptoNoteCore/AmmPool.{h,cpp}` | Constant-product math, LP shares, spot price |
| `src/CryptoNoteCore/OrderbookMempool.*` | User + pool depth-band orders; pool makers execute in the auction |
| `src/CryptoNoteCore/PoolOrderOrchestrator.*` | Regeneration timing + adaptive spread (volatility from pool spot) |
| `src/CryptoNoteCore/HeatMintEngine.*` | Mint validation at the canonical price |
| `src/CryptoNoteConfig.h` | All constants |
| `src/Wallet/WalletRpcServer.cpp`, `src/SimpleWallet/SimpleWallet.cpp` | User-facing commands |

## 7. What was retired (v11)

* The in-band CLOB matcher (`OrderbookMatcher`), `MIN_DISTINCT_PARTIES`, and gossiped orders — replaced by the backed auction.
* Single-sided LP mints — balanced deposits only.
* The old 50/50 fee-split and LP "spread reward" formula — replaced by the versioned taker/maker model (30/70 at v11, 70/30 at v12) and pool P\&L.
* Pre-v11 price scales (Q64.64, 1e8, 1e18) — canonical 1e7 everywhere.

## 8. Status

Phases A–C shipped and verified (guardian-verified, 57/57 auction tests + full suite). Post-only orders and maker-rebate fee credits (for fee-less makers) are backlog items — see `docs/plans/backlog.md`.


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