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

# AMM Mechanics

> Constant-product formula, LP share calculation, and protocol fee mechanics.

Bitrune implements a **constant-product automated market maker** on Bitcoin L1. Each pool holds a BTC reserve and a Rune reserve, bound by the invariant `x × y = k`.

## Constant-Product Formula

```
reserve_btc × reserve_rune = k
```

Every swap preserves `k` (net of fees). The price of one asset in terms of the other is the ratio of reserves.

## Quote Calculation

Given an input amount, the output is computed as follows:

```
fee_in        = amount_in × fee_bps / 10_000
amount_in_net = amount_in - fee_in
amount_out    = (amount_in_net × reserve_out) / (reserve_in + amount_in_net)
```

* **fee\_bps**: pool fee in basis points. Default is 30 (0.3%).
* Fees remain in the pool reserves. They accrue to liquidity providers proportionally.

## Price Impact

Price impact increases with trade size relative to pool depth. A swap that consumes a large fraction of `reserve_out` will receive a significantly worse rate than the spot price.

## Slippage Protection

Every swap accepts a minimum output parameter. The transaction is not constructed if the computed output falls below this minimum. This protects the user from front-running and stale quotes.

***

## LP Mechanics

Liquidity providers deposit both assets into a pool and receive **LP shares** representing their proportional ownership.

### First Deposit

```
shares = sqrt(amount_a × amount_b)
```

The first depositor sets the initial price ratio.

### Subsequent Deposits

```
shares = min(
    amount_a × total_shares / reserve_a,
    amount_b × total_shares / reserve_b
)
```

Depositing in a ratio that differs from the current reserves results in fewer shares (the excess is a donation to existing LPs).

### Withdrawal (Burn)

```
amount_a = shares × reserve_a / total_shares
amount_b = shares × reserve_b / total_shares
```

Burning shares returns a proportional slice of both reserves.

### LP-Rune

An on-chain Rune token represents LP shares for pools that have undergone LP-Rune etching. LP-Rune balances are tracked separately from the general balance ledger. LP-Rune tokens can be staked into mining pools to earn BITRUNE rewards.

***

## Protocol Fee (kLast Accrual)

The protocol fee is accrued on every LP mint or burn event. It captures a fraction of the fee growth that occurred between LP events.

### Calculation

```
root_k      = sqrt(reserve_a × reserve_b)       # current
root_k_last = sqrt(k_last)                       # snapshot from previous LP event
```

If `root_k > root_k_last` (the pool grew from swap fees):

```
treasury_shares = total_shares × (root_k - root_k_last) / (root_k × (phi - 1) + root_k_last)
```

### Parameters

| Parameter      | Default | Meaning                                 |
| -------------- | ------- | --------------------------------------- |
| `phi`          | 6       | Fee split denominator                   |
| Protocol share | 1/6     | \~16.67% of fee growth goes to treasury |
| LP share       | 5/6     | \~83.33% of fee growth stays with LPs   |

The `k_last` snapshot is updated after every mint or burn. Swap-only activity accumulates growth silently until the next LP event triggers accrual.
