# Mechanics

### The AMM: Logarithmic Pricing

Echelon's AMM uses a **logarithmic pricing curve**.

#### The Math (Simplified)

The exchange rate between PT and SY is determined by:

```
exchange_rate = e^(ln_implied_rate * time_to_expiry)
```

Where `ln_implied_rate` is tracked by Echelon's Fixed Yield oracle and updates with each trade.

#### Key Parameters

**Scalar Root:** Controls capital efficiency vs. tradeable range

* Higher scalar = more capital efficient, narrower range
* Lower scalar = less efficient, wider range

**Initial Anchor:** The reference rate used for fee calculations

* Acts as a "neutral" point
* Fees increase as rates deviate from anchor

**Rate Anchor:** The current market-implied rate

* Updates with each trade
* Used to calculate dynamic fees

#### Dynamic Fees

Fees are calculated from a `ln_fee_rate_root` parameter set at market creation. The fee rate scales with time to expiry:

```
fee_rate = e^(ln_fee_rate_root * time_to_expiry / seconds_per_year)
```

Where `ln_fee_rate_root` is derived from the initial fee rate when the market is created. The fee rate decreases as the market approaches expiry (since `time_to_expiry` decreases).

### Token Mechanics: SY, PT, YT

#### SY (Standardized Yield)

SY is a wrapper around yield-bearing assets. It maintains a **1:1 relationship** with the underlying asset.

**How it works:**

* Deposit 1000 sUSDe → Get 1000 SY-sUSDe
* Redeem 1000 SY-sUSDe → Get 1000 sUSDe

#### **SY Exchange Rate (for PY index):**&#x20;

The protocol uses an oracle-based exchange rate (`underlying_price / base_price`) to checkpoint the PY index. This is NOT the redemption rate - SY always redeems 1:1. The exchange rate is used to track how the underlying asset's value changes relative to its base asset (e.g., sUSDe vs USDe price).

**Key Properties:**

* 1:1 with underlying (always)
* Can be redeemed for underlying at any time
* Can be split into PT + YT
* PY index uses SY exchange rate as a checkpoint for yield tracking

#### PT (Principal Token)

PT represents a claim on SY at expiry. The relationship is simple:

```
1 PT (at expiry) = 1 SY
```

Before expiry, PT typically trades at a discount to SY when there's positive implied yield. The discount represents the locked-in yield rate.

**Pricing:** PT price is determined by the market AMM. The exchange rate is calculated as:

```
PT/SY_exchange_rate = e^(ln_implied_rate * time_to_expiry / seconds_per_year)
```

Because implied APYs are positive, you'll need more than 1 PT to get 1 SY (meaning 1 PT < 1 SY, i.e., discount).

**Key Properties:**

* Fixed expiry date
* Redeemable 1:1 for SY at expiry (guaranteed)
* Typically trades at discount before expiry (when implied yield is positive)
* Price converges to SY price as expiry approaches
* Market-determined pricing

#### YT (Yield Token)

YT represents all yield from SY until expiry. It uses a **PY index** to track yield accrual.

**PY Index:** The PY index starts at 1.0 and increases as yield accrues:

```
py_index = max(sy_exchange_rate, previous_py_index)
```

**Yield Claim:** YT holders can claim yield proportional to the PY index increase:

```
claimable_yield = yt_amount * (py_index_current - py_index_when_minted)
```

**Key Properties:**

* Accrues yield continuously
* Can claim yield anytime before expiry
* Becomes worthless at expiry
* Leveraged yield exposure

### Liquidity: Constant Sum AMM

#### How It Works

Liquidity providers add PT + SY in a specific ratio. The AMM maintains:

```
PT_balance + SY_balance = constant (for LP purposes)
```

**LP Token Minting:**

```
lpt_amount = sqrt(PT_added * SY_added) - minimum_liquidity
```

### Yield Accrual: The PY Index

The PY index tracks how much yield has accrued since market creation. It's what actually increases over time, not the SY exchange rate.

#### Calculation

The PY index is updated using the SY exchange rate (from oracle) as a checkpoint:

```
py_index = max(sy_exchange_rate, previous_py_index)
```

The SY exchange rate (`underlying_price / base_price`) reflects how the underlying asset's value has changed. For rebasing assets like sUSDe, this increases as yield accrues. The PY index is the maximum of the current SY exchange rate and the previous PY index, ensuring it only increases (monotonic).

**Why monotonic?** This ensures that YT holders always see their yield increase.

#### Distribution

When YT holders claim yield:

1. Calculate their share: `yt_amount / total_yt_supply`
2. Calculate total yield: `(py_index_current - py_index_claim_time) * total_sy`
3. Distribute: `user_share * total_yield`

***

### Market Expiry: The Finale

When a market expires:

1. **Trading stops:** No more swaps allowed
2. **PT redemption:** 1 PT → 1 SY (guaranteed)
3. **YT expiry:** YT becomes worthless (all yield distributed)
4. **Liquidity withdrawal:** LPs can remove liquidity
5. **Market closes:** State is finalized

**After expiry:**

* PT holders: Redeem for SY
* YT holders: Nothing (already got all yield)
* LPs: Remove remaining liquidity

***

### Security: Rate Limiting

Echelon includes rate limiting on liquidity withdrawals to prevent:

* Flash loan attacks
* Sudden liquidity drains
* Market manipulation

**How it works:**

* Each LP token has a rate limiter
* Limits withdrawals to `max_qty` per `window_duration`
* Exempt addresses can bypass (for integrations)

***

### Integration Points

#### For Protocols

* **Lending protocols:** Use PT as collateral (it has fixed value at expiry)
* **DEXs:** List PT/YT/SY for trading
* **Yield aggregators:** Automate yield strategies

#### For Users

* **Wallets:** Display PT/YT positions
* **Portfolio trackers:** Track yield positions
* **Analytics:** Monitor market data
* **Automation:** Set up yield strategies
