---
description: Understanding Automated Market Makers
---

# Understanding AMM

Automated market makers (AMMs) are part of the decentralized finance (DeFi) ecosystem. They allow digital assets to be traded in a permissionless and automatic way by using liquidity pools.

## Liquidity Pools and Liquidity Providers

A liquidity pool is a shared pot of tokens. Every user can provide liquidity to liquidity pools with SPL tokens and the price of the SPL tokens in the pool is determined by a mathematical formula.

### Constant Product Formula

The constant product formula is a simple mathematical formula that can take many forms.&#x20;

The most common one:

`tokenA_balance(p) * tokenB_balance(p) = k`

and popularized by Uniswap as:

`x * y = k`

The constant, represented by “k” means there is a constant balance of assets that determines the price of tokens in a liquidity pool. For example, if an AMM has RAY and SOL, two volatile assets, every time RAY is bought, the price of RAY goes up as there is less RAY in the pool than before the purchase. Conversely, the price of SOL goes down as there is more SOL in the pool. The pool stays in constant balance, where the total value of RAY in the pool will always equal the total value of SOL in the pool. Only when new liquidity providers join in will the pool expand in size. Visually, the prices of tokens in an AMM pool follow a curve determined by the formula.

![AMM Curve](https://4105775969-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxGUcNnEa83O0iV8q1s1e%2Fuploads%2FOVNESwjb2azKcbWw0Gli%2Famm-curve.png?alt=media\&token=587e7bc6-46d0-49ea-b4e2-11a38627ef8a)

In this constant state of balance, buying one RAY brings the price of RAY up slightly along the curve, and selling one RAY brings the price of RAY down slightly along the curve. The opposite happens to the price of SOL in a RAY-SOL pool. It doesn’t matter how volatile the price gets, there will eventually be a return to a state of balance that reflects a relatively accurate market price. If the AMM price ventures too far from market prices on other exchanges, the model incentivizes traders to take advantage of the price differences between the AMM and outside crypto exchanges until it is balanced once again.

## Relationship Between Liquidity Pool and Price

In order to understand the relationship between the liquidity pool and the price, I will explain with 2 different examples.

**Starting pool for both examples**

**Pool info:**

* USDC = 100.000
* SOL = 1000
* Constant Product = 100.000.000
* Market Price = $100

### **First example**

Swap 10.000 USDC for SOL

After swap:

* USDC = 110.000 (because we added 10.000 to the pool)
* Constant Product = 100.000.000 (stays the same)
* SOL = 909,09 (constant product / new usdc amount)

SOL recieved = 90,91 (old sol amount - new sol amount)

Price per SOL = $121

### **Second example**

Swap 100.000 USDC for SOL

After swap:

* USDC = 200.000 (because we added 100.000 to the pool)
* Constant Product = 100.000.000 (stays the same)
* SOL = 500 (constant product / new usdc amount)

SOL recieved = 500 (old sol amount - new sol amount)

Price per SOL = $400

{% hint style="success" %}
As seen in the second example, the dollar amount in the liquidity pool has doubled but the price of SOL has made 4x.
{% endhint %}

## Price Impact

Price impact is the difference between the current market price and the expected price for a trade. Price impact is primarily determined by the size of your trade relative to the amount of liquidity in the pool. As the number of tokens you buy from the pool increases, the price of the token increases as well. This unfavorable change in price is called price impact.

If you are swapping in a pool with very low liquidity, you may receive a very poor price for your swap. If you see a high price impact when swapping, try trading a smaller amount.
