# Swap Mechanism

## 1. **The Trading Invariant (The "k" Constant)**

<figure><img src="https://2726550812-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjMO51gfM0FaNC6SfYbCR%2Fuploads%2FdlZs6zaCB9jB1Ee4Eq5y%2F29017a4a-5e3e-4a76-973e-f8403f3dd0f5.png?alt=media&#x26;token=441747c7-8838-4bad-b977-afd619fd4ada" alt=""><figcaption></figcaption></figure>

Once liquidity is established in the BT/ITL pool, the protocol enables decentralized trades using the Constant Product Formula. This formula ensures that the pool remains solvent at any price level by maintaining a specific mathematical relationship between the two assets.&#x20;

Every swap must satisfy the "Trading Invariant," where the product of the reserves ($$k$$) remains constant (excluding fees). When a trader adds one token, they must receive an amount of the other such that:&#x20;

&#x20;$$(x + \Delta x\_{in}) \cdot (y - \Delta y\_{out}) = k$$

**Variables defined:**

* $$x, y$$: The current reserves of BT and ITL in the pool.
* $$\Delta x\_{in}$$: The amount of BT sent to the pool by the trader.
* $$\Delta y\_{out}$$: The amount of ITL received by the trader.
* $$k$$: The invariant that must remain unchanged by the trade.

## **2. Calculating Swap Output**&#x20;

Determining the exact amount of tokens a user receives depends on which asset they are selling. The formula rearranges the invariant to solve for the "out" amount based on the "in" amount.&#x20;

| Trade Direction    | Formula for Output                                   |
| ------------------ | ---------------------------------------------------- |
| Selling BT for ITL | $$\Delta y = \frac{y \cdot \Delta x}{x + \Delta x}$$ |
| Selling ITL for BT | $$\Delta x = \frac{x \cdot \Delta y}{y + \Delta y}$$ |

As the input amount ($$\Delta x$$ or $$\Delta y$$) increases, the output amount follows a curved trajectory, ensuring the pool is never fully depleted.&#x20;

## **3. Trading Fees & LP Incentives**&#x20;

To reward Liquidity Providers (LPs) for their capital, the protocol levies a fixed fee of 0.3% ($$f = 0.003$$) on every trade. This fee is deducted from the input amount before the swap occurs, which effectively causes the constant $$k$$ to grow over time as fees accumulate in the pool.&#x20;

**Fee Processing Steps:**

* **Fee Calculation**: $$Fee = \Delta x\_{in} \cdot 0.003$$
* **Effective Input:** $$\Delta x\_{effective} = \Delta x\_{in} \cdot 0.997$$
* **Final Output Formula:** $$\Delta y\_{out} = \frac{y \cdot (\Delta x\_{in} \cdot 0.997)}{x + (\Delta x\_{in} \cdot 0.997)}$$

The accumulated fees remain in the pool, increasing the value of all LP tokens and providing a "yield" for those who provide liquidity.&#x20;

## **4. Price Impact & Slippage**

Every trade shifts the ratio of reserves, which directly changes the price. Price Impact is the difference between the current mid-market price and the actual execution price of the trade.&#x20;

* **Execution Price (**$$P\_{exec}$$**)**: Calculated as $$\frac{\Delta y\_{out}}{\Delta x\_{in}}$$.
* **Slippage:** As the input amount increases relative to the total reserves ($$x$$), the execution price deviates further from the spot price.
* **Liquidity Depth:** Larger reserves ($$k$$) are essential to minimize slippage, especially for high-volume traders.&#x20;

## **5. Native ITL Swaps (Technical Flow)**&#x20;

When swapping the native ITL asset for BT, the protocol follows an atomic sequence to ensure compatibility with the smart contract pair:&#x20;

* **Wrapping:** Native ITL is wrapped into WITL (Wrapped ITL) via the Router to make it compatible with the standard token interface.&#x20;
* **Pathing:** The Router identifies the most efficient path, specifically looking for the BT/WITL Pair contract.&#x20;
* **Execution:** The swap function is called on the Pair contract, transferring the WITL in and sending the resulting BT directly to the trader’s wallet.

## **6. Liquidity Withdrawal (Burning LP Tokens)**&#x20;

Liquidity Providers can exit the pool at any time by "burning" their LP tokens to reclaim their pro-rata share of the current reserves. Because trading fees are never removed, the amount withdrawn typically exceeds the original deposit value (accrued value).&#x20;

**Redemption Formulas:**

* **BT Claimed:** $$BT\_{claimed} = \frac{L\_{burned}}{L\_{total}} \cdot x$$
* **ITL Claimed:** $$ITL\_{claimed} = \frac{L\_{burned}}{L\_{total}} \cdot y$$

This ensures that LPs receive their original principal plus their share of all accumulated trading fees generated since their initial deposit.&#x20;
