Understanding Decentralized Exchanges and AMMs
Explore how decentralized exchanges (DEXs) like Uniswap and PancakeSwap function, focusing on automated market makers (AMMs), liquidity pools, and price determination. Learn about the benefits of D...
TECHNICALUSER GUIDES
12/8/20246 min read


A DEX or Decentralised Exchange is a type of exchange in which one can exchange one cryptocurrency to another cryptocurrency permissionlessly and securely. In a DEX the best elements of CEXs or centralised exchanges and peer to peer exchanges are combined into one, providing the user with a superior experience. A DEX utilises atomic swaps and liquidity pools in order to function which are unique to DEXs, I will explain how DEX’s fuel web 3
A Centralised Exchange (CEX) uses an order book system to execute transactions and maintain sufficient liquidity. If Bitcoin is at $100,000, as an example if Dave deposited 1 BTC onto the exchange and decided that if the price were to reach $110,000 he wants to sell his Bitcoin for US dollars. So he would open a conditional sell order to be executed when the price of Bitcoin hits $110,000. This order even though small in the grand scheme of large exchanges balances out market forces acting in the opposite direction (buy orders) which are pushing up the price. Often on exchanges there are a multitude of conditional buy and sell orders keeping the liquidity of the exchange large enough to handle big transactions without swaying the price of the asset on the exchange too far from the actual price of the asset (Bitcoin in this case). So when the price of Bitcoin is getting high on that exchange there are lots of sell orders at different levels which will push the price back down to an equilibrium level. Similarly if the price of Bitcoin drops, there are buy orders waiting to purchase Bitcoin in order to drive the price back up to an equilibrium. The centralised exchange will often hold adequate amounts of each asset in order to bridge gaps and for low liquidity times when there are few orders in the order book to mitigate their risk. Peer to peer exchanges are simply one party selling at X price and another buying, so there is no need for a liquidity system as each transaction is handled individually by the other side who may be selling or buying (usually both to mitigate risk). On peer to peer exchanges often there will be escrow to ensure financial security and if there isn’t there will be a reputation for the “seller”. The problem with peer to peer exchanges is usually the high fees and the inconvenience. Centralised exchanges are highly convenient but require KYC, are permissioned and lack censorship resistance. Exchanges are legal entities and have to obey the rules of the countries in which they operate. Monero was cracked down upon by exchanges due to heavy government pressure globally, so it no longer trades along with other privacy coins, demonstrating the need for exchanges to not have to bend to the will of governments in order to continue to operate
In a decentralised exchange, from the perspective of the user the best elements are taken from both types as well as improved upon using a unique system consisting of Atomic swaps and Liquidity pools. This makes all of the exchange functions possible through a permissionless protocol embedded within the blockchain. To describe an atomic swap propose we have Bob who owns some Bitcoin and we have Alice who owns some Ethereum and they would like to swap their assets with each other. If they trusted each other, Bob would send Alice the bitcoin and Alice would just send Bob the Ethereum, but the issue here is that there is the possibility for one party to exploit the other and steal the assets without sending their own, trust is required to make this transaction happen, which limits the application. Atomic swapping uses a virtual vault known as a Hashed Timelock Contract (HTLC). In this example we will investigate Party A - Alice holding bitcoin wanting to swap with party B - Bob who holds Ethereum. If party A - (Alice) wants to initiate the swap, she will make a HTLC and deposit her bitcoin into this virtual vault. A private key will be generated and Alice will have access to it, it is then hashed and sent to Bob as proof that the assets have been deposited into the contract. Bob cannot withdraw what Alice has deposited as he only has the proof and not the private key itself. Bob then uses this specific hash sent from Alice to generate a new contract and deposits his Ethereum, he then does a similar process to prove to Alice that the funds are in the contract. Because Bob created the address using the hash of Alice’s private key, Alice is able to claim the tokens Bob deposited. She does this and, in the process, reveals the private key to Bob. If Bob does not complete the transaction before the timelock expires, the tokens claimed by Alice will revert to Bob. Bob now uses the the private key to withdraw the tokens from the contract, thus completing the swap for both parties in a trustless way, either both parties receive their assets back or receive their swapped assets. One party will never receive the others assets without their own being successfully being swapped, any failure to occur results in both parties receiving their initial tokens back.
This is the fundamental understanding of how atomic swaps work using Hashed Timelock Contracts, if this were the main mechanism for exchange alone, each party swapping assets would need to find someone to buy their assets and sell them what they want, and they would both have to agree on a price. This would be like a peer to peer exchange with beneficial changes, reducing the need for an escrow or rating system. We can do much better by combining the idea of liquidity from centralised exchanges into this concept to create a much smoother experience
In order to take this concept to the next level for instant swapping, liquidity pools are utilised which are a bit different to the order book concept but serve the same function.
A liquidity pool works by selecting an asset pair, so for Example we will use Ethereum and USDT. Liquidity providers add liquidity to this pair by adding an equal value of Ethereum and USDT, so they might add $100 of Ethereum (0.025 ETH) and $100 of USDT into the pool. These tokens will be locked and the liquidity provider will receive an LP token which represents the token part of ETH and USDT. Let’s say there is $1000 of USDT and $1000 of Ethereum within this pool. Now a user holds Ethereum and would like to swap it to USDT. If they have $100 of Ethereum, swapping it via this pool the amount of USDT they will receive will be according to this equation
OLD ETH AMOUNT x OLD USDT AMOUNT = NEW ETH AMOUNT x NEW USDT AMOUNT
Substituting the values in we get the following assuming Ethereum has a price of $4000
0.25 x 1000 = 0.25+0.0025 x NEW USDT AMOUNT
Solving the equation we get;
250 = 0.275 x NEW USDT AMOUNT
250/0.0275 = NEW USDT AMOUNT
NEW USDT AMOUNT (IN POOL) = 909.09 USDT
So here we can see due to the swap, the ratios have shifted and the user will receive $91 worth of USDT due to their swap. This is a case where there is very low liquidity, in most cases the with adequate liquidity the user will get $100. Often time arbitrageurs will help fix this price discrepancy by buying on one exchange and selling on another so the ratio of Ethereum to USDT will be reflective of the market. A small fee of the user’s funds is taken and given to liquidity providers for taking the risk of impermanent loss as a fee, which is basically the risk of the value of the unstable asset fluctuating in value. Say if ETH goes up, they would have been better off holding ETH alone, but if it goes down they would have been better holding USDT only so they are rewarded for the liquidity service they provide and risk they take. When there is low liquidity, often times to facilitate a smooth running DEX, there will be liquidity provider incentives, often this will happen when a new chain is launched and liquidity on that chain’s main DEX is very important for the usability of that specific chain.
If there was $1 million of liquidity within the pool the same calculation would be as follows (ETH price $4000)
There would be $500,000 worth of ETH (125 ETH) and $500,000 USDT
OLD ETH AMOUNT x OLD USDT AMOUNT = NEW ETH AMOUNT x NEW USDT AMOUNT
125 x 500,000 = 125+0.025 x NEW USDT AMOUNT
Solving the equation we get;
62500000 = 125 + 0.0025 x NEW USDT AMOUNT
62500000/0.0275 = NEW USDT AMOUNT
NEW USDT AMOUNT = 499900.019996 USDT
We can see here in this example the pool is left 499900.019996 USDT with meaning the user would receive the original pool amount minus the current which would be
500,000 - 499900.019996
Amount = 99.98
After fee for liquidity providers (0.3% on uniswap) = 99.68
As we can see in this example it is in our best interests to pay this small fee to encourage liquidity providers to provide enough liquidity so that we are able to swap and get an equal value of one asset for the other, when compared to the previous example this would be with no fee paid but a very small liquidity pool. This is a lot better than swapping in a smaller liquidity pool so we can see here how important it is for a liquidity pool to have adequate liquidity
Future scope - Upgrades and applications to become widespread within the next few years will include anonymous swapping which will solve front running issues for users doing large transactions. Cross chain DEX development will also become widespread allowing users to swap assets between chains with low fees, this will make transacting even less reliant on services existing outside of the web-3 ecosystem and allowing the web 3 ecosystem to function more independently outside of traditional financial system
Get in Touch
We'd love to hear from you! Reach out for questions, feedback or other enquiries
Reach
info@bitesizedblockchain.com
Bite Sized is not affiliated with these brands in any way










Grab your daily web 3 byte