> For the complete documentation index, see [llms.txt](https://docs.fira.money/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.fira.money/developers/protocol-contracts/core/market-math-core.md).

# MarketMathCore

MarketMathCore encodes the time-decaying pricing curve and trade execution math used by FiraMarket. OracleLib provides the TWAP ring buffer.

{% hint style="info" %}
**Source:** `src/fira_bonding/core/Market/MarketMathCore.sol`, `src/fira_bonding/core/Market/OracleLib.sol`
{% endhint %}

## MarketMathCore

This library operates entirely on in-memory `MarketState` structs for gas efficiency. It never reads from or writes to storage directly.

### Pricing model

The AMM prices BT relative to FW using a logit curve:

```
exchangeRate = ln(proportion / (1 - proportion)) / rateScalar + rateAnchor
```

Where `proportion = totalBt / (totalBt + totalAsset)` and `totalAsset` is FW reserves converted to underlying asset units via the BC index.

* **`rateScalar`** scales inversely with time-to-expiry (`scalarRoot × 365 days / timeToExpiry`), making the curve steeper near maturity.
* **`rateAnchor`** is recalculated after each trade so that the current pool proportion yields the last recorded implied rate.

### Liquidity math

* **First deposit:** Mints `sqrt(fw × bt) - MINIMUM_LIQUIDITY` LP tokens, locking 1000 wei permanently.
* **Subsequent deposits:** Mints proportional to the limiting token.
* **Removal:** Returns proportional shares of both reserves.

### Trade execution

`executeTradeCore` loads pre-computed values, calculates the FW amount from the exchange rate, applies fees, and updates state. Fees are split between the swapper (as worse pricing) and the reserve (as a percentage of the fee). The post-trade implied rate is recalculated and stored.

### Key invariants

* Exchange rate is always >= 1e18 (reverts otherwise)
* BT proportion cannot exceed `MAX_MARKET_PROPORTION` (96%)
* Implied rate is always > 0 after a trade
* `MINIMUM_LIQUIDITY` prevents the first-depositor attack

## OracleLib

Adapted from Uniswap V3's oracle. Stores `Observation` structs (timestamp + cumulative `ln(impliedRate)` + initialized flag) in a fixed-size array of 65,535 slots.

* **`write()`** — Appends a new observation if the block timestamp differs from the last
* **`grow()`** — Pre-initializes storage slots to reduce gas during swaps
* **`observe()`** — Returns cumulative values at requested time offsets using binary search
* **TWAP** is computed by the caller as `(cumulative[now] - cumulative[ago]) / duration`


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.fira.money/developers/protocol-contracts/core/market-math-core.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
