> For the complete documentation index, see [llms.txt](https://tidal-flats.gitbook.io/tidal-flats-whitepaper-en/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tidal-flats.gitbook.io/tidal-flats-whitepaper-en/whitepaper/black-market-2-road-to-fidelia/black-market-dex/vamm-virtual-amm.md).

# vAMM (Virtual AMM)

## Overview

The Virtual Automated Market Maker (vAMM) system is the core of Black Market's exchange. vAMM facilitates exchange between sSPL and VALOR tokens to ensure price stability and adequate liquidity.

{% hint style="info" %}
The below models can be updated due to the balance update
{% endhint %}

### Key Definitions and Variables

* **Volatility Parameter**: Parameter to control volatility. Calculated with C and k value
* **k**: Constant to give minimum stability to the system
* $$S$$: Total supply of specific sSPL tokens.
* $$S\_{old}$$: Total supply of specific sSPL tokens before the calculation.
* $$S\_{new}$$: Total supply of specific sSPL tokens after the calculation.
* $$\Delta S$$: represents the trade-induced change in supply.
* $$P\_{int}$$: Price of the sSPL when initialized.
* $$P\_{old}$$: Price of the sSPL before calculation.
* $$P\_{new}$$: Price of the sSPL after calculation.
* $$P\_{avg}$$: The price of the sSPL applies to a specific trade.

## Models

The vAMM model dynamically adjusts the token price to provide appropriate stability and volatility while ensuring symmetry in trading.

### Symmetric Model

vAMM calculates the price after the trade using the symmetric model.

* Calculation based on sSPL amount

$$P\_{new} = P\_{old} \times (1 + \frac{\Delta S}{k + S\_{old}} )$$

* Calculate based on VALOR budget

$$\Delta S = \frac{B}{\frac{P\_{old} + P\_{new}}{2}} = \frac{2B}{P\_{old} + P\_{new}}$$

$$\therefore P\_{new} = \sqrt{P\_{old}^2 + \frac{2B \cdot P\_{old}}{k + S\_{old}}}$$

## Average Price Model

vAMM swap transaction works based on the average price before the trade and the adjusted price after the trade.

$$P\_{avg} = \frac{P\_{old} + P\_{new}}{2}$$

## Price Adjustment Due to External Changes

If the supply of sSPL tokens changes outside of vAMM trades, the system adjusts the price using the following equation:

$$P\_{\text{new}} = \begin{cases}  P\_{\text{int}} + \frac{(P\_{\text{old}} - P\_{\text{int}})(2k + S\_{\text{old}})}{2k + S\_{\text{new}}} & \text{if} ,, P\_{\text{old}} > P\_{\text{int}} \ P\_{\text{int}} - \frac{(P\_{\text{int}} - P\_{\text{old}})(2k + S\_{\text{old}})}{2k + S\_{\text{new}}} & \text{if} ,, P\_{\text{old}} \le P\_{\text{int}} \end{cases}$$

$$k = 100000 / P\_{int}$$

### Initial price

| Token                   | Initial Price ($VALOR) |
| ----------------------- | ---------------------- |
| Voodoo Doll ($VD)       | 0.46118                |
| Gold Teeth ($GT)        | 0.72776                |
| JB Whiskey ($JBW)       | 0.94118                |
| Canteen ($CT)           | 1.07882                |
| G Badge ($GB)           | 1.55294                |
| Holy Water ($HW)        | 2.32353                |
| Used Engine ($UE)       | 3.41176                |
| Enhanced Bullet ($EB)   | 3.43529                |
| Oil Lighter Case ($OLC) | 6.82353                |
| Oil ($OIL)              | 0.00750                |
| MRE ($MRE)              | 0.00750                |

## Examples

1. **Swap**:
   1. **Buy**: Paying VALOR to buy 1 sSPL
      * Stability constant $$k = 10$$ sSPL (after buying)
      * Current supply $$S\_{old} = 0$$ sSPL
      * New supply $$S\_{new} = 1$$ sSPL
      * Current price $$P\_{old} = 10$$ VALOR
      * New price $$P\_{new} = 10 \times (1 + \frac{1}{10 + 0} ) = 11$$ VALOR
      * Average price $$P\_{avg} = \frac{10 + 11}{2} = 10.5$$ VALOR
      * Fee: $$10.5 \* 0.0005 = 0.0525$$ VALOR
      * **Result**: Paying 10.5525 VALOR to buy 1 sSPL
   2. **Sell Transaction**: Selling 1 sSPL to get VALOR
      * Stability constant $$k = 10$$ sSPL (after buying)
      * Current supply $$S\_{old} = 1$$ sSPL (after buying)
      * New supply $$S\_{new} = 0$$ sSPL
      * Current price $$P\_{old} = 11$$ VALOR (after buying)
      * New price $$P\_{new} = 11 \times ( 1 - \frac{1}{10 + 1} ) = 10$$ VALOR
      * Average price $$P\_{avg} = \frac{11 + 10}{2} = 10.5$$ VALOR
      * Fee: $$10.5 \* 0.0005 = 0.0525$$ VALOR
      * **Result**: Receiving 10.5525 VALOR for selling 1 sSPL
2. **Price Adjustment**:

   <figure><img src="/files/i8wlqoYwlRsaNMnShD9s" alt=""><figcaption></figcaption></figure>

   1. **On Mint:**
      * Stability constant $$k = 10$$ sSPL (after buying)
        * Current supply $$S\_{old} = 1$$ sSPL
        * Supply after mint $$S\_{new} = 5$$ sSPL
        * Initial price $$P\_{int} = 10$$ VALOR
        * Current price $$P\_{old} = 11$$ VALOR
        * Adjusted price $$P\_{new} = \frac{|11 - 10|(1 + 2 \times 10)}{5 + 2 \times 10} + 10 = 10.84$$ VALOR
   2. **On Burn**:
      * Stability constant $$k = 10$$ sSPL (after buying)
        * Existing supply $$S\_{old} = 5$$ sSPL
        * Supply after burn $$S\_{new} = 1$$ sSPL
        * Initial price $$P\_{int} = 10$$ VALOR
        * Current price $$P\_{old} = 10.84$$ VALOR
        * Adjusted price $$P\_{new} = \frac{|10.84 - 10|(5 + 2 \times 10)}{1 + 2 \times 10} + 10 = 11$$ VALOR
