How Decentralised Exchange (DEX) works — part II

Canary Exchange
7 min readNov 29, 2021

--

This is the second part of the material about what happens under the hood of DEX. This time we’ll talk about the mechanisms that allow the liquidity pools to protect themselves from the depletion and how the price of assets remains fair in DeFi ecosystem. Beside the fact that this is going to be interesting we also hope that you will use this knowledge to your own benefit. If you have just bumped into this page we strongly recommend you to go through the first part so the below makes total sense from the very start. Enjoy the read!

Ok, in the end of the previous section we explained the basics of DEX and talked a bit about liquidity pools — what is it, why and how they are created. Just to recap — liquidity pool is a pool of usually two tokens which are provided by people or institutions thorough DEX platform to other people or institutions to trade/swap. The amount and the ratio of the provided tokens are locked in a smart contract which has a specific logic to follow and we’ll focus on it today. Once the pool is created people can swap tokens in that pool interacting with the respective smart contract. Well, in reality people use trade/swap interface of DEX and do not care about anything else. DEX makes sure they have that smooth experience.

Liquidity providers can think of DEX as a way for themselves to be a private banker — you lend your assets to the smart contract so others can make swaps between tokens that you provided while you make profit via commission and staking liquidity tokens (more about farming in the next releases). Traditional bank will take your money via deposits, use your assets to allow others to borrow and trade/exchange, charge those folks with high fees and give you back just crumbs, putting the difference into its pocket. As we know DEX and blockchain based solutions in general remove middlemen. DEX distributes what would be the bank’s cut between liquidity providers (higher rewards) and its clients (lesser fees).

As mentioned, you can read about all that in more details with the examples in part one. Ok, where were we? Ah, yes, last time we paused our conversation when someone asked:

But Sir… what if someone just comes and buys out the whole amount of one token in the pool?

Well, here comes the constant product formula hardwired in the contract which does not allow whales to do this without painful consequences. Bear with us, it is not that complicated but important to understand so that you, future or actual (congrats!) whale, can avoid unnecessary costs.

We’ll explain the constant product formula in the example. Imagine our pool consists of 1,000 CNR x 1,000 DAI.e therefore the price in this pool is 1 CNR = 1 DAI.e. If someone wants to swap 100 DAI.e for CNR in this pool you would think that the pool will give that person 100 CNR because the ratio/price is 1:1. If this would be true then someone could swap 1,000 DAI.e for 1,000 CNR and the pool would have only 2,000 DAI.e… what kind of pool it would be then?

This is where the constant product formula kicks in. In our example the constant product of the pool is 1,000 CNR tokens multiplied by 1,000 DAI.e tokens = 1,000,000 LP tokens. 1,000,000 is the constant product. This logic is the same for every pool — volume of token A multiplied by volume of token B provided to the pool. The price in the pool is never fixed for everyone, it is floating depending on the volume of a specific trade. The smart contract calculates the price of each trade using the constant product to protect the pool from depletion. The price will be defined based on the requirement to maintain the same product, i.e. in our case 1,000,000.

If someone inputs 100 DAI.e for the trade, the smart contract will do this simple calculation:

New amount of DAI.e in the pool would be 1,000 + 100 = 1,100 DAI.e. The pool smart contract then needs to solve this equation: 1,100 DAI.e x ? CNR = 1,000,000 to keep the constant product… hm, constant. Therefore 1,000,000 / 1,100 = 909 CNR should stay in the pool to maintain 1,000,000. Thus for 100 DAI.e the pool can give you only 1,000–909 = 91 CNR. So the price in this trade will be 100 DAI.e / 91 CNR = 1.1, i.e. 1 CNR = 1.1 DAI.e

The principle here is that the more you want to buy from the pool the higher price you will have to pay for each token. This way the smart contract prompts you to buy only in such amounts which do not significantly impact the balance in the pool. You already can conclude yourself that it is more efficient to swap tokens in the pools where the liquidity is relatively large to the trade you intend to make. Let’s come back to the example above, if you plan to spend only 10 DAI.e then the price for you would be 1,000,000 / 1,010 = 990.1 thus 1,000 - 990.1 = 9.9 and 10 / 9.9 = 1.01 DAI.e per CNR, i.e. this is almost the same as the initial price in the pool.

Below you can see the illustration of how the price would change for a single trade in 1,000 DAI.e x 1,000 CNR pool.

Please note that the below chart is absolutely specific to the hypothetical tiny 1,000 DAI.e x 1,000 CNR pool that we use solely to illustrate the point of correlation between the price and the volume of trade.

Obviously if you now imagine that the pool is 1,000 times larger and the numbers on the horizontal axis are ‘000s then you can safely swap 100 or 1,000 DAI.e almost without any impact… on price.

Yes, this is what stands behind the ‘Price impact’ percentage that you can see in the ‘Swap’ window. The more tokens you want to buy in one go the higher the price impact will be.

‘Slippage’ in this context is your limit to which you can tolerate the price impact while making a swap. If price changes more than the slippage percentage you set for the intended swap then the swap will not be executed protecting you from making inefficient trades.

Ok, fine, but… how then the price stays fair in DeFi ecosystem?

Indeed, by now you might think that if the liquidity pools are such deterministic then how we can be sure that the price stays fair in the decentralised financial ecosystem? Well, you can be sure because there are not many things in the world stronger than human greed. As long as people are greedy the prices in DEXes will be fair.

You see, the pools at DEXes are independent, i.e. DAI.e x CNR pool at Canary is not connected to DAI.e x CNR pool created at another DEX. If you buy 91 CNR at Canary, this pool will have 1,100 DAI.e x 909 CNR which means the price of CNR in this pool will be1,100 / 909 =1.2 DAI.e. Someone can see that, swap 100 DAI.e for 91 CNR at another DEX where the pool still has 1,000 DAI.e x 1,000 CNR, then go to Canary and swap 91 CNR for 109 DAI.e (91 x 1.2) earning 9 DAI.e (yes, it will be not exactly 1.2 DAI.e — it will be a bit less because of the price impact but still there will be a profit). This is what called an arbitrage opportunity — making profit due to difference in prices in the different pools. Arbitrage traders push the balances in different pools towards the market average.

As a result of all those ongoing trades in various pools the balances between tokens, i.e. ratio, i.e. price remains the same across many pools and thus fair, i.e. representing real universal demand for the token. If there was just one pool in the ecosystem then it would be quite feasible to manipulate the price of the token. That is why it is important for any ecosystem to have many DEXes instead of just one or two. The more independent pools in the blockchain the fairer price in the ecosystem. We’ll leave you here for now. In the next releases Canary will explore different strategies to grow your wealth in DeFi. Subscribe to our Medium account or follow us on Twitter to get notified of the next Canary Academy publications.

--

--

Canary Exchange

Canary is a decentralized exchange for farming, staking, swapping assets on Avalanche.