Search…
⌃K

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 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 optimised 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 on top of our existing audited 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. 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 to 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, which could lead to benefits resulting in deeper liquidity, higher volumes, and an overall larger pool of fees from which liquidity providers benefit. Important to note this is only true until the stablecoins maintain their peg.
In crypto, stablecoins are a crucial part of the ecosystem. And as we have seen on other blockchains, stableswaps 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, and now also with the addition of stableswap.

StableSwap Zap-in functionality

In order 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 transactions settings for adding liquidity to StableSwap pools that allow you to automatically calculate the pool balance. This is disabled if your wallet has only one of the tokens in the pool.

How is 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 in order 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 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 (Note: we do not support Zap-out, so liquidity can be only removed in the current ratio of the pool). 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 enables zap-in. If he has some iUSD in his wallet, the auto-balancing function will calculate that he should provide 120,000 iUSD in order 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 will disable it in the settings and write 0 iUSD. We will show a warning that this operation is inefficient and he will receive fewer LPTs and 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.