# 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`](/developers/protocol-contracts/core/fira-market.md) — AMM contract details
* [`FiraMarketFactory.sol`](/developers/protocol-contracts/core/fira-market-factory.md) — Factory contract
* [`MarketMathCore.sol`](/developers/protocol-contracts/core/market-math-core.md) — Pricing math


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.fira.money/developers/features/fira-market-amm.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
