# Virtual AMM (vAMM)

A virtual Automated Market Maker (vAMM) is a system that provides synthetic (or virtual) liquidity, allowing traders to buy and sell derivatives entirely on the blockchain.

Traditional AMM-based exchange has two types of users: liquidity providers to provide tokens, and traders to swap available tokens. The prices at which swaps occur are determined by a mathematical formula.

As the βvirtualβ part of vAMM implies, rather than swapping real tokens, vAMMs are used to swap virtual, synthetic assets like derivatives contracts. No real assets are stored inside the vAMM itself; instead, traders are able to make leveraged trades based on collateral stored in a smart contract vault. vAMMs are used for price discovery in handling leverage.

Using vAMM as a basis for derivative contracts bring the following advantages:

- No Liquidity Providers (LP) required, thus allowing to run more markets with less slippage and more liquidity.
- vAMM market is always fully collateralized
- Due to no real assets being swapped, it allows for easy launch of non-crypto related products, such as Commodities and NFT trading

## Exampleβ

This example does not account for trading fees

An AMM is using `x * y = k`

formula. Suppose, Waves price is 8 USDN per Waves, and vAMM is
initialized with 100000 initial liquidity.

`USDN WAVES K`

100000 12500 1250000000

Now, Alice makes a trader of buying (long) Waves with x2 leverage, usiing 2000 USDN as a collateral. This way USDN side of the pool will be increased by 4000. As per AMM formula, `k`

needs to stay constant. Let's commute an amount of Waves Alice will get.

`104000 * (12500 - x) = 1250000000`

x = 480.777

Now, Alice have a position of 480.777 Waves, and vAMM have a following new state:

`USDN WAVES K`

104000 12019.22 1250000000

Another trader (Bob) comes in and longs Waves using 2000 USDN as a collateral, but with x3 leverage. Again, we will add additional 60000 USDN to USDD side of the pool, and commute new vAMM state and size of the Bob's position as following:

`110000 * (12019.2 - x) = 1250000000`

x = 655.564

Now, Bob have a position of 655.564 Waves, and vAMM have a following new state:

`USDN WAVES K`

104000 11363.656 1250000000

Alice closes her position of 480.777 Waves, so 480.777 Waves is added back to the pool. How much USDN Alice will get in return? Let's find out.

`(110000 - x) * (11363.656 + 480.777) = 1250000000`

x = 4465.19

Remember, Alice was using 4000 USDN as a collateral, so PnL = 4465.19 - 4000 = 465.19. Alice made a profit of 465.19 USDN. vAMM is now in the following state:

`USDN WAVES K`

105534.81 11844.4 1250000000

Now, Bob closes his position of 655.564 Waves. How much USDN will he get?

`(105534.81 - x) * (11844.4 + 655.564) = 1250000000`

x = 5534.52

Remember, that Bob was using 6000 USDN as a collateral. 5534.52 - 6000 = -465.48. Bob made a loss of 465.48. Note, that Alices profit is exactly the same as Bob's loss. That's because in decentralized futures exchange PnL is settled between the traders (not trader and clearing house).