# Constant Product

### Formula

The constant product function is based on the following equation.

$x * y = k$

The x here represents the amount of one asset, and y is the amount of the other asset. As the name of the functions hints, the principle is that the product of the asset amounts remains constant - the k in the formula. So if you want to swap asset one for asset two, x will increase, and y will decrease so that k is kept constant.

Constant product AMM is suitable for volatile token pairs because they enable trading at all possible prices. That means it can facilitate trading even during market fluctuation. It is also beneficial for primary markets, where traders are more likely to guess and act based on information from outside the market, despite higher slippage.

Following is the resulting price curve of the constant product AMM function. As you can see, large trades relative to the size of the pool move prices further along the curve in both directions. It is an elegant solution well-suited to most crypto assets that behave more like traditional stocks.

However, while the constant product market maker model is a great leap forward for DeFi, it is not ideal for trading pairs of USD stablecoins or any other two same-valued assets. For such trading pairs, it is desirable to exchange in a 1:1 ratio between them. In a constant product AMM world, it requires high liquidity to ensure low slippage, and even then, it can fall short of the set expectations.

This is where Stableswap comes in.

### Constant Product Zap-in Functionality

To provide a better user experience we allow providing liquidity only in one of the assets. Under the hood, the pool performs a swap of some of the provided tokens to get the value balanced before performing a liquidity addition. The swap fee is still paid from this swap, but both the swap and the add liquidity happen in the same transaction, allowing the user to save on one agent fee.

### Constant Product Zap-out Functionality

It is likewise possible to remove liquidity in only one of the assets instead of withdrawing it in the ratio of the pool's balance. Mathematically it is equivalent to withdrawing the liquidity in the pool's ratio, and then immediately swapping all of one of the tokens for the second one. Similarly to the zap-in functionality, the swap fee is still paid from the swap, but the agent & TX fees are only paid once.

Last updated