How Decentralised Exchange (DEX) works — part I
Canary DEX continues its educational series on DeFi and today we would like to guide you through the key principles of DEX. Understanding the mechanics behind swaps will help you navigate more confidently in the decentralised finance space and also hopefully make you a bit wealthier comparing to another version of you in the parallel universe where you did not bother to read this article.
Before we go into exploring the fundamentals of DEX let’s first understand the basics of the centralised exchange (CEX).
Centralised Exchange (CEX)
CEX provides services to those who are willing to sell certain valuable assets (shares, commodity or currency) and those who are willing to buy those assets. The core services that CEX provides are:
- Platform where both sellers and buyers can meet (physically or electronically)
- Means for expressing the price they wish to sell/buy an asset so everyone is clear about the conditions of the trade (order book)
- Payment infrastructure (accounts, deposit and payment processing)
- Security of the trade that the parties will not con each other
So CEX essentially creates the convenient place for people to trade but this comes with price:
- CEX will require personal identification of those who would like to trade and they can decline your application or close your existing account based on their own internal decision which is not always transparent
- Once you make deposit — you do not own those money anymore and you can lose access to those money if something goes wrong with CEX or if CEX decides that you might be doing something that they do not like
- CEX employs people and rents/owns offices so they will charge commission for their services to pay for running the business and generating profits for its owners
Thus CEX is a mandatory middle man who can decide whatever they want within the legislation of course and you depend on their decision to permit you to do the trade.
Now, let’s talk about the basics of trading at CEX. As soon as the seller and buyer agree on the price the trade is made. CEX will take the money from the buyer and the asset from the seller and pass these to the parties not forgetting to take a commission for its services. The price for the asset is defined in the open market, i.e. by sellers and buyers. If buyers offer an unreasonably low price or sellers ask a price that is too high then there will be no trade. In reality there is always someone who needs to buy something urgently and offers a bit higher price than everyone else and there is always someone who needs money and sets price a bit lower than all other sellers. Others see that and get into FOMO (Fear Of Missing Out) state of mind and start adjusting their offers. This way the bidding price and asking price are going towards each other until the trade is done.
The more people are willing to buy or sell the asset the more liquid the market for this asset is. It means that there is always someone ready to buy and someone to sell and the trades are happening all the time. The example of the illiquid market would be when there are few people on both sides totally not in a rush and as a result there is a big difference between bidding and asking prices leading to only one or two trades per several days.
Decentralised Exchange (DEX)
DEX is something very different. There is no middle man and no arbitrary decisions. DEX is based on smart contracts which are not humans and they do not care. Smart contracts do not need offices and salaries — no need for high fees on every step to pay the bills. Also DEX does not ask you to deposit money upfront — you trade directly from your wallet therefore you keep ownership and control over you money all the time. DEX’s smart contracts and trades are visible on the blockchain and thus everything is transparent and traceable.
Ok, what is a smart contract in the context of DEX?
In DeFi, smart contract actually represents a pool of assets. When you need to swap assets you interact with the smart contract which has the assets you need locked into it and waiting there for you to come and get it by making a trade.
In order to achieve the efficiencies and benefits of DEX which we’ve just listed above we need to have the assets already available for any trade so there is no one needed to validate, confirm and secure the trade. That is why DEX protocols are called permission-less or trust-less, i.e. there is no one who gives permissions and you do not need to trust anyone to accept your deposit and make sure other people do not con you. This is possible due to liquidity pools concept. Liquidity pool is the pool of usually two assets which are created by users at each DEX and it all exist in the form of the smart contract.
Say, you have 1,000 CNR and 1,000 DAI.e. You do not need to spend them on anything right now so then why don’t you make them work for you for a while?
You go to DEX and provide liquidity, i.e. create CNR x DAI.e pool or add your tokens to already existing CNR x DAI.e pool. In other words you lock your liquidity in the smart contract. In exchange you get the liquidity pool (LP) tokens which represent your share in the pool. If the pool consists only of your CNR and DAI.e tokens then you will have 100% of LP tokens in this pool. If someone else comes later and adds another 1,000 CNR and 1,000 DAI.e then new LP tokens will be minted so that you and that person each will have 50% of all LP tokens minted in that pool.
The great thing is that you can stake your LP tokens to get rewards for providing liquidity. Usually the rewards for staking LP tokens (called farming) are much higher comparing to rewards for staking basic tokens (called single staking). You can check the rewards for staking LP tokens versus staking single basic token at Canary Exchange and see that the difference is quite big.
That is the way for DEX to incentivise users to provide liquidity. We’ll publish a more detailed article about providing liquidity and farming with the description of the necessary steps soon — follow us here or on Telegram or on Twitter to learn when it is released.
Because the pools are available from the start there is no need to find buyer/seller to make a trade. You just go and make the trade in the respective pool, i.e. interacting with the smart contract. If I have 100 DAI.e and I need to buy CNR technically I need to go to CNR x DAI.e pool and give it 100 DAI.e and get CNR in return. In reality you do not need to find that pool, you just go to DEX and click on ‘Swap’ tab and enter the token you have and the token you need — DEX will find the pool for you and will make the swap.
Each pool consists of at least two tokens — that enables swaps or trades between those two tokens. If in the whole world there was only one pool AVAX x DAI.e then you would not be able to swap AVAX or DAI.e for CNR physically. It would be just not possible. If there were at least AVAX x DAI.e and CNR x DAI.e pools then you would be able to swap AVAX for CNR. You would go to DEX and fill in the ‘Swap’ form requesting to swap AVAX for CNR. DEX would swap AVAX for DAI.e in one pool and then swap DAI.e for CNR in the other pool and would send CNR to your wallet.
Hm.. ok, but how the smart contract understands at which price it should make the swap?
When you create a pool or add tokens to the existing pool you do this based on the certain ratio. This ratio represents the price of assets — how they relate to each other. For example, if the current price of 1 CNR is 1 DAI.e then the ratio is 1:1. If you think this is the right price you would create a CNR x DAI.e pool based on this 1:1 ratio (you can create a new pool with whatever ratio you want but believe us — if you create a totally new pool you better do this based on the current market price between those two tokens and you will know why later in the second part of this article).
So you have 1,000 CNR and you decided to create CNR x DAI.e pool — for that you would need to lock 1,000 CNR and 1,000 DAI.e together. If price was 1 CNR = 2 DAI.e you would need to lock 1,000 CNR and 2,000 DAI.e.
Now, once the tokens are provided in the pool people can use this pool to swap tokens and you will get 0.3% commission from each trade plus you can stake your LP tokens.
Right, but what if someone just comes and buys out the whole amount of one token in the pool?
Oh, we are glad that you thought about this question, this is really interesting one and we’ll explain how the liquidity pools protect themselves from such vandalism in the next article but to give you some food for thought… have you heard of the price impact? Stay tuned!