# Stableswap

Stableswap is an automated market maker (AMM) function focused on swapping between two stable assets with minimal slippage and more efficient trading for liquidity providers. The concept was pioneered by Michael Egorov, founder of Curve Finance, in a whitepaper published in late 2019, and this has majorly impacted the DeFi ecosystems.

As you can see on the graph below, Stableswap is the middle ground between the two functions. The constant product described here, and the constant sum function, x + y = k. The latter means the exchange rate between the assets, or price, is always constant, hence the name. Even though it has practically zero slippage, it's not ideal, as the pool can run out of tokens or become significantly unbalanced. Stableswap invariant is a more complex function optimized for minimal slippage and a flatter curve around the optimal peg range. That results in mostly keeping the desirable 1:1 exchange ratio, better efficiency with big trades, and benefiting traders and liquidity providers.

### Implementation of Stableswap

We implemented Stableswap V1 on top of our existing audited V1 contracts. They have been in production since our mainnet launch and make a perfect foundation for this. We kept all the trusted and secure parts and only changed the AMM function used in the contracts to the Stableswap invariant. That enabled us to speed up our development and make relatively few and easily auditable changes.

With migrating to V2 contracts rewritten from scratch in Plutarch, Constant product and Stableswap pools continue to share the same codebase and only differ in the invariant formula. This new V2 codebase has been thoroughly audited as well with the audit results made publicly available.

If you want to dive deeper into the mathematics behind it, check out the Stableswap whitepaper.

### Why is Stableswap necessary?

A common problem when swapping tokens via standard constant product function AMM DEX is that trades between one pegged asset and another are seldom as efficient as they could be. Now, with the new WingRiders Stableswap pools, the advantages are clear:

Traders can swap between stablecoin (pegged) assets with minimal possible slippage and price impact.

Liquidity providers can use Stableswap pools, which dramatically reduce the risks associated with impermanent loss and could lead to benefits resulting in deeper liquidity, higher volumes, and an overall larger pool of fees from which liquidity providers benefit. It's important to note this is only true until the stablecoins maintain their peg.

In crypto, stablecoins are a crucial part of the ecosystem. As we have seen on other blockchains, Stableswap pools are a critical foundation to help other DeFi projects, such as borrowing and lending, grow even more. On Cardano, we hope to continue our mission to be a key infrastructure component for all the other projects building out there.

### Stableswap Zap-in functionality

To provide the best possible user experience, we want to allow liquidity providers to provide liquidity for Stableswap pools with any ratio of tokens they have in their wallet (including using only one of the tokens in the pool). This mechanism performs a swap on the pool before adding liquidity in the same ratio as the pool after the swap. The swap fee is still paid from this swap, however, both swap and add liquidity happen in one transaction only, therefore saving you one transaction fee and batcher fee. This also allows us to more easily balance the pool by adding liquidity, which is beneficial for the traders as it facilitates bigger possible swaps. When adding liquidity to the balanced pool, you are getting more Liquidity Provider Tokens (LPTs). Therefore we have added new transaction settings for adding liquidity to Stableswap pools that allow you to automatically calculate the pool balance.

### Stableswap Zap-out functionality

It is also 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.

#### How is the Stableswap pool balance calculated?

Since Stableswap pools by definition have both assets representing the same value (USD, BTC, ETH, etc.), the balance happens when both tokens are in the pool in a 1:1 ratio. Let's assume we have a Stableswap liquidity pool iUSD/DJED with current values of 60,000 iUSD and 120,000 DJED, so a 1:2 ratio, and let's have a look at what would happen in the following scenarios:

The liquidity provider wants to provide 60,000 iUSD to this pool, therefore he enables zap-in. Since our pool has more DJED tokens than iUSD, our auto-balance will calculate that 0 DJED needs to be added as well to perform an efficient zap-in. Then the smart contract will first take 29,971.78 iUSD and swap those for 30,028.21 DJED (swap fee is paid to the pool) balancing the pool to better balance and afterward adding liquidity in the same ratio of 120,000 iUSD and 120,000 DJED. He receives LPTs which can be redeemed at that moment for 30,028.21 iUSD and 30,028.21 DJED. This means with this efficient Zap-in, our liquidity provider effectively gained 56.42 USD stablecoin value.

The liquidity provider wants to provide 60,000 DJED, therefore he chooses a zap-in. If he has some iUSD in his wallet, the auto-balancing function will calculate that he should provide 120,000 iUSD to balance the pool and get the most possible LPTs from this transaction. Unfortunately, he does not want to provide any iUSD to the pool, so he disables it in the settings and specifies 0 iUSD. We will show a warning that this operation is inefficient and he will receive fewer LPTs so he should either wait for the balance to change or add some of the other currency. If he continues with the operation, the pool would be set to 60,000 iUSD and 180,000 DJED and he can redeem his LPTs for 14,966.80 iUSD and 44,900.41 DJED, effectively losing 132.78 USD stablecoin value.

The liquidity provider wants to provide 20,000 iUSD and 40,000 DJED, which can be done with zap-in disabled. Since the liquidity is being added in the same ratio as the pool has, there will be no swap happening and new pool values would be 80,000 iUSD and 160,000 DJED. He receives LPTs which can be redeemed at that moment for 20,000 iUSD and 40,000 DJED, the same as he had in the first place.

Last updated