# Fira Market AMM

## Overview

The Fira Market is a BT/FW liquidity pool with a pricing curve specifically designed for fixed-rate instruments. As expiry approaches, BT converges toward FW in value — the AMM's logit-based curve naturally handles this convergence.

{% hint style="info" %}
Each Fira Market is specific to a single BT and curve configuration (`scalarRoot`, `initialAnchor`, `lnFeeRateRoot`). Markets are deployed via the FiraMarketFactory using CREATE2 for deterministic addresses.
{% endhint %}

## How it works

### Adding liquidity

Liquidity providers deposit BT and FW in the correct ratio and receive LP tokens. On the first deposit, `MINIMUM_LIQUIDITY` (1000 wei) is locked to `address(1)` to prevent the first-depositor rounding attack. Subsequent deposits mint LP proportional to the limiting token.

Tokens must be transferred to the market before calling `mint` — the contract does not pull tokens.

### Removing liquidity

LP tokens are burned in exchange for proportional BT and FW. LP must be sent to the market before calling `burn`. This works both pre- and post-expiry.

### Swaps

The market supports two swap directions:

* **`swapExactBtForFw`** — Sell BT for FW. Uses a flash-callback pattern: the market sends FW first, then verifies BT repayment.
* **`swapFwForExactBt`** — Buy BT with FW. Sends BT out first, calls back, and checks FW repayment.

Both swap functions are gated by `notExpired` — no swaps are allowed after the BT expiry date.

### TWAP Oracle

Every state write records a new observation in a ring buffer (if the block timestamp differs from the last). The `observe(secondsAgos)` function returns cumulative `ln(impliedRate)` values for time-weighted average price calculations. Anyone can pre-allocate observation slots via `increaseObservationsCardinalityNext` to support longer TWAP windows.

## Key features

* **Time-decaying pricing** — Logit curve ensures BT converges to FW value as expiry approaches
* **Flash-callback swaps** — Tokens are sent before verification, enabling atomic multi-step operations
* **Built-in TWAP oracle** — Ring buffer of implied rate observations for external price feeds
* **Reserve fees** — A portion of swap fees goes to the protocol treasury
* **Immutable markets** — Once deployed, market parameters cannot be changed (fee overrides are managed by the factory)

## Invariants

* Internal `totalBt` and `totalFw` are never affected by direct transfers — `skim()` sends excess to treasury
* Exchange rate is always >= 1.0 (BT trades at a discount to FW)
* Swaps and mints revert after expiry; burns work at any time

## Related contracts

* [`FiraMarket.sol`](https://docs.fira.money/developers/protocol-contracts/core/fira-market) — AMM contract details
* [`FiraMarketFactory.sol`](https://docs.fira.money/developers/protocol-contracts/core/fira-market-factory) — Factory contract
* [`MarketMathCore.sol`](https://docs.fira.money/developers/protocol-contracts/core/market-math-core) — Pricing math
