# Market Example

### Example: Market Creation and Swap

Let's walk through a concrete example to see how everything fits together.

#### Market Creation

**Input Parameters:**

* Asset: sUSDe (yield-bearing)
* Expiry: 1 year from now (31,536,000 seconds)
* Rate range: 3% to 7% APY (rate\_min = 0.03, rate\_max = 0.07)
* Initial fee rate: 1% (fee\_rate = 1.01)

**Step 1: Calculate Initial Anchor**

```
years_to_expiry = 31,536,000 / 31,536,000 = 1.0
implied_rate_min = (1 + 0.03)^1 = 1.03
implied_rate_max = (1 + 0.07)^1 = 1.07
initial_anchor = (1.03 + 1.07) / 2 = 1.05
```

The initial anchor (1.05) represents the midpoint of the expected rate range.

**Step 2: Calculate Scalar Root**

```
rate_diff = 1.07 - 1.05 = 0.02
LN_MAX_PROPORTION = ln(0.96/0.04) ≈ 3.178
scalar_root = (3.178 * 1.0) / 0.02 ≈ 158.9
```

The scalar root controls how sensitive the exchange rate is to pool imbalances. Higher values = more capital efficient but narrower tradable range.

**Step 3: Calculate ln\_fee\_rate\_root**

```
ln_fee_rate = ln(1.01) ≈ 0.00995
ln_fee_rate_root = 0.00995 * 31,536,000 / 31,536,000 = 0.00995
```

This is the "root" fee rate that will be scaled by time to expiry for each swap.

**Market Parameters Summary:**

* `scalar_root`: 158.9
* `initial_anchor`: 1.05 (5% APY midpoint)
* `ln_fee_rate_root`: 0.00995

#### Example: SY → PT Swap

Now let's see how fees work on an actual swap. Assume:

* Current time: 6 months before expiry (15,768,000 seconds remaining)
* Market state: 1,000 PT and 1,000 SY in the pool (balanced)
* PY index: 1.0 (no yield accrued yet)
* User wants to swap: SY for 100 PT

**Step 1: Calculate Current Fee Rate**

```
time_to_expiry = 15,768,000 seconds (6 months)
fee_rate = e^(ln_fee_rate_root * time_to_expiry / seconds_per_year)
fee_rate = e^(0.00995 * 15,768,000 / 31,536,000)
fee_rate = e^(0.00995 * 0.5)
fee_rate = e^(0.004975)
fee_rate ≈ 1.00499
```

The fee rate is \~0.499% (half of the 1% annual rate, since we're halfway to expiry). This is the multiplier applied to the trade.

**Step 2: Calculate Exchange Rate**:

```
proportion (p) = 1,000 / (1,000 + 1,000) = 0.5
ln(p/(1-p)) = ln(0.5/0.5) = ln(1) = 0
rate_scalar = scalar_root * seconds_per_year / time_to_expiry
rate_scalar = 158.9 * 31,536,000 / 15,768,000 = 317.8
rate_anchor ≈ 1.05 (calibrated from last_ln_implied_rate)

exchange_rate = ln(p/(1-p)) / rate_scalar + rate_anchor
exchange_rate = 0 / 317.8 + 1.05 = 1.05
```

This means 1.05 PT = 1 SY (PT is at a discount, as expected for positive yield).

**Step 3: Execute Swap with Fee** User wants to swap SY for exact PT amount (e.g., wants 100 PT):

```
Pre-fee exchange rate: 1.05 (from Step 2)
Pre-fee SY needed: 100 / 1.05 = 95.23 SY

Fee calculation (for swap_sy_for_exact_pt):
fee = pre_fee_asset_to_account * (fee_rate - 1)
fee = 95.23 * (1.00499 - 1)
fee = 95.23 * 0.00499
fee ≈ 0.475 SY

Total SY needed (including fee):
net_asset_to_account = 95.23 + 0.475 = 95.705 SY
```

**Alternative way to think about it:** The post-fee exchange rate is: `1.05 / 1.00499 ≈ 1.0448` At this rate, to get 100 PT you need: `100 / 1.0448 ≈ 95.71 SY`&#x20;

**Step 4: Fee Distribution**

```
Total fee: 0.475 SY
Protocol fee (if 50%): 0.2375 SY → goes to protocol reserve
LP fee (50%): 0.2375 SY → goes to liquidity providers
```

**Final Result:**

* User wants: 100 PT
* User pays: \~95.705 SY (includes fee)
* Fee paid: \~0.475 SY (\~0.5% of trade value)
* Effective rate: 95.705 SY = 100 PT (includes fee)
* Pre-fee rate would have been: 95.23 SY = 100 PT

**Key Takeaway:** The fee decreases as expiry approaches. If this same swap happened 1 month before expiry, the fee would be \~0.08% instead of \~0.5%, because `time_to_expiry` is smaller in the fee calculation.


---

# 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.echelon.market/echelon/fixed-yield/mechanics/market-example.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.
