Article Summary :- Decentralised applications are very new however have taken the DeFi world by storm. Billions flow through such exchanges every day. We will attempt to understand the mechanics of such applications & how they differ from other existing centralised world applications. We will also attempt to ruminate on possible evolution of such applications in near future.
Traditional Stock & Crypto Exchanges
Traditional Stock exchanges & centralised crypto exchanges work on the concept of order book i.e. aggregate buyer & sellers in a table & match prices for trade to execute.
You can dive down in detail here on how Stock & crypto exchanges work.
One of the key goals an exchange needs to accomplish is to ensure liquidity at various price points. Say for example, you placed an order to buy 10 UNI tokens at $20. There is another trader placing order for 5 UNI tokens at $28. Similarly, on the seller side, sell order price at various price points will be placed. For a successful trade to occur, buyer & seller price points must match. Therefore, depending on liquidity of orders at varied price points, successful trade is not always guaranteed . If there are not enough buyer or sellers available , the market is considered illiquid.
Price of assets on such exchanges is dependent on individual market makers quoting Buy & Sell prices.
Automated Market Makers(AMMs)
Before jumping onto Uniswap though, it is prudent to cover the super-set of Uniswap called Automated market makers (AMMs).
AMMs are pre-crypto phenomena. The core of AMMs relies on passive participants with long term view of certain assets who deposit their assets in a common liquidity pool. Subsequently, mathematical formulas are applied to price the deposited tokens for traders looking to exchange assets.
Crypto industry uses a certain variety of AMMs called Constant function Market makers(CFMMs). It might appear all very mouthful however, CFFM simply implies a formula resulting in either constant multiplication result or constant mean or constant sum etc. The formulaic approach removes the need for Buyer to quote prices.
Uniswap, as will see below, implements formula of constant multiplication(a*b=c).
UNISWAP
In Uniswap, a decentralised exchange( DEX) the goal of asset exchange is achieved in very different manner.
Uniswap allows you to swap crypto tokens without the need of an intermediary. Uniswap is built on Ethereum. DEXes such as Uniswap are not controlled by anyone & execute trade with the help of Smart contracts- code less than 300 lines deployed on Ethereum Blockchain. The governance of such platforms is done by the native token holders(UNI token for Uniswap) of the DEX.
It needs no KYC or profile related info. Simply navigate to the portal, connect your wallet & exchange your assets for a small fee.
Uniswap was built by Hayden Adams whose story is quite inspiring.
The Uniswap eco-system has 3 major participants :-
Liquidity Providers
Traders
Arbitrageurs
Liquidity Providers(LPs) :- Any one can be a liquidity provider on Uniswap by depositing a pair of ETH & ERC-20 token. Liquidity providers earn a portion of the trading fee based on their proportion of assets in the liquidity pool. For any DEX, liquidity providers are the most important entity to ensure high liquidity on the platform.
High liquidity is crucial to attract trades onto the platform as we will see below. Currently, over $7 Billion dollars worth of assets are locked in liquidity pool of Uniswap - behind Curve Finance ( another DEX).
Traders :- Traders are any user looking to exchange token on the platform. Traders execute transactions against liquidity pool received from LPs.
Arbitrageurs :- Arbitrageurs are a subset of traders - however, their main aim is to exploit the price difference for profit between pairs on Dex & other exchanges. They perform vital task of keeping the exchange rate between tokens in sync with other platforms.
The above definition might appear too abstract. Please read for further clarification on them though examples.
How does Uniswap work?
a* b = C(Constant).
The above formula captures the gist of the platform.
Instead of relying on order books to determine price based on supply & demand, uniswap relies on simplified algorithms to quote prices. For uniswap, the pricing quoted for 2 assets it holds is done such that when quantity of 2 assets are multiplied together, the result is always constant.
Let’s attempt to explain the above with an example.
Let’s say Uniswap holds 2 tokens in quantity A & B which when multiplied gives a value of k - a constant.
The DEX will quote price for a transaction such that post transactions , if new quantities of A changes to C & B changes to D , still the final multiplied result of a*b is k. i.f.
A*B ( pre-transaction) = k = C* D( post-transaction )
Example
Let’s say in the beginning, we add to Uniswap pool 100 units of token A & 100 units of token B. Anyone can come to the platform & swap Token A for Token B & vice - versa.
Below is the pre-transaction snapshot of the pool.
For our example, we will execute separate trades wherein Token B is swapped for Token A . We will review the post transaction impact on the liquidity pool , the trader & the liquidity provider.
The pricing exchange rate at the start between token A & B is 1:1 & the applying the constant product function to the pool , we get 100* 100 = 10000. The platform will always maintain the value of 10000 irrespective of number of swaps done on the platform.
Trade-1 :- Now, a user- Sminem, comes to the Uniswap platform. Sminem wishes to swap 10 quantities of token B. As a result, the leftover quantity of Token B in the pool will be 90. Since , when multiplied, output of Token A & Token B must always be constant - so the value of leftover Token A needed post transaction from Sminem is - 10000/ 90 = 111.11.
The current Token A in the pool are 100. Therefore, additional Token A needed :- 111.11-100= 11.11.
The price quoted on the platform is - 11.11 token A for 10 token B i.e. exchange rate now is 11.11/10 = 1.11 : 1
Since the exchange rate between A & B is 1:1 & the trader is swapping token B at exchange rate of 1.11:1, the premium of 11.11% is being charged to him compared to what he could have to pay on a centralised exchange.
Trade 2 :- Sminem wishes to buy 20 quantities of token B. As a result, the leftover quantity of Token B in the pool will be 80. Since , when multiplied, output of Token A & Token B must always be constant - so the value of leftover Token A needed post transaction from Sminem is - 10000/ 90 = 125.
The current Token A in the pool are 100. Therefore, additional Token A needed :- 125-100= 25.
The price quoted on the platform is - 25 token A for 10 token B i.e. exchange rate now is 25/20 = 1.25 : 1
The exchange rate between A & B now is 1.25 :1 & hence premium is now 25%.
Summary :- As the trade size increase, the exchange rate imbalance will increase. So , Say Sminem wants to buy 50 Token B. The Token A Sminem needs to deposit now will be 100. The exchange rate now between A & B will be 100/50= 2:1.
The exchange rate needs to double since Token B has been halved where as Token A has doubled in quantity.
Let’s assume the second transaction goes through & active exchange rate between A & B is 2:1. However, in other centralised exchanges working on Order Book structure , exchange rate is still 1:1. This creates price imbalance & arbitrage opportunity.
In summary, larger the trade size, higher the premium that will be charged(see below).
What this implies is that liquidity pool needs to be significantly larger to make trading feasible. In the above example, the pool was quite small & hence the large variation in premium charged for even trivial trade sizes.
Front running
For the traders, Front running is an additional problem to deal with.
Whenever you raise a transaction request, there is a lag before the transaction is mined & confirmed on the blockchain. All such pending transactions sit in the mempool/”pending transactions pool”.
Depending on the traffic & throughput of the native blockchain , the “pending transactions pool” will continue to grow. As the average of transactions pending in pool increases, risks of front running conditions increase.
In front running, since the trades are available to be viewed in the pool before it is confirmed, a large trade with considerable impact on asset prices can be identified. Subsequently, a new trade before this large trade is executed can be done to take advantage of price volatility at the expense of others. The front runner can ensure prior execution by keeping the gas fees of his transaction request high.
Since, Miners often prioritise the transactions with high gas fee that maximises block confirmation rewards, the adversarial node can execute his own transaction before your transaction is confirmed. If the miner itself happens to be front runner, then without even high gas fee, they could attempt to place their own transactions higher & attempt to exclude the original transaction.
For example , say a very large sell order to ETH token is placed by you. Post this transaction execution, ETH price will likely go down. The miner/node can view the transaction in pool & see his/her own ETH asset prior to your sell order execution.
To help with the issue, while placing your order, you can quote maximum price to be charged based on your risk appetite. In addition, order expiry can also be specified to avoid risk of front running by miners.
Arbitrageurs
In the trade example above, we can see that each transaction causes exchange rate between token A & B to move away from 1:1. Assuming the token exchange rate on other exchanges is still in 1:1 ration, the price difference creates arbitrage opportunity.
Internal Exchange Driven Arbitrage Opportunity
Let’s assume a trade wherein 30 units of token B were swapped for token A.
This creates an arbitrage opportunity. A arbitrageur will jump in to buy 42.85 units of token A for 30 units of Token B & bring the pool exchange rate in sync with other external exchanges.
Please note since on external exchanges , 1:1 is the prevalent exchange rate, the arbitrageur receives 12.85 units of token A for free as profit.
AMM platforms such as Uniswap will underprice certain assets( Token A in example above) to attract arbitrageurs & restore the balance. Hence, Traders looking for quick buck play ensure important role in ensuring that prices match with other exchanges ( Centralised or Decentralised) .
External Event driven Arbitrage opportunity
Alternately, say, there is an external event such as launch of a new Product capability for Project Behind Token A, which causes the price of native Token A to appreciate significantly on the other external exchanges(Say Binance) - Token A & Token B exchange rate is now 0.5/1.
The arbitrageur now will quickly execute a trade on Uniswap & attempt to buy cheaply priced Token A still at exchange rate of 1:1 & bring Uniswap on par with Binance exchange rate.
In mathematical term, the arbitrageur will execute trade such that, a*b = 0.5b * b = 10000 is satisfied.
Doing the calculation ,
a*b= 10000
0.5 b * b =10000
b= 141.42 , a = 70.71
Hence, post transaction, the pool should have 70.71 units of Token A & 141.42 units of token B left.
The arbitrageur swapped 29.28 units of token A for 41.42 units of token B.
The trade would cost him 29.28*2=58.57 units token A on an external exchange. Therefore, the arbitrageur practically receives 58.57-41.42=17.15 units of token B for free & is pure profit.
Liquidity Providers
In the above example of liquidity pool contains 100 units of both Token A & Token B. Let’s assume one of the liquidity provider - Bogandoff- had provided 10% of pool i.e. 10 units of token A & token B respectively.
One very important question is still left unanswered . If arbitrageurs are making money by exploiting price differences, who is incurring the loss? After all you cannot make money out of thin air & there are no free lunches.
This is where the concept of “Impermanent loss” comes in. The loss is borne by the liquidity providers itself. The pool gave away valuable Token A at a lower cost & hence the overall value of the pool declined compared to its earlier value & had the Pool Tokens been stored by the LP on a Wallet or a centralised exchange.
The loss is called impermanent because if due to some reason Token A reverts to its earlier low value & exchange rate between A & B becomes 1:1, the pool value is also recovered. Individuals depositing on pools earn money via transaction fees. Hence, as long as “Income earned from transaction fees” > “Impermanent Loss”, the pool provider is in profit.
Post transaction by Sminem wherein he bough 10 units to token B, the pool contains, 111.11 units of token A & 90 units of token B equalling 10000 (constant).
At this stage, 10% for the liquidity provider, Bogandoff, will now equal 11.11 units to token A & 9 units of token B which equals 99.99. Hence, the cumulative value of pool has slightly declined
You can understand that “Liquidity Providers” therefore will prefer AMMs with highest trading volumes.
The advantage
It is important to highlight the potential benefits of such exchange.
To start with, liquidity providers who are generally long term holders, have a mechanism to earn passive income. They can simply add assets to pool & let the fee accumulate.
Since large trade order are not rational for traders, whales are unlikely to transact on DEXes.
AMM Varieties
Since Uniswap, quite a few other players have emerged - each offering its own value proposition.The theme of “Constant function” is consistent in all AMMs though.
Each function will have its own price relationship curve & might be more suited to certain assets. For example, stablecoins are often constant in pricing & hence 2 stablecoins assets are more suitable for functions other than “constant Product function” which Uniswap employs.
Below are few other types of DEXes.
1INCH DEX :- It is an DEX aggregator i.e. it fetches prices of crypto assets from various other exchanges & recommends lowest possible price to traders. Post that traders can execute trade on the selected exchange with lowest price. Over 20+ centralised & decentralised exchanges are integrated on the platform for the users to select from. Even though 1INCH is an intermediary layered on top of various other exchanges , it does not charge users any additional for trade execution. Only the exchange on which finally trade is executed, applicable fee is liable to be paid.
SushiSwap :- Sushiswap is a fork of Uniswap & similar to uniswap.
PancakeSwap :- Pancakeswap is another fork of Uniswap & runs on Binance smart chain.
Curve :- Curve is decentralised liquidity aggregator. Users can earn on curve platform by locking up their funds.Curve uses combination of constant sum & constant Product function to price liquidity pool assets.
We will cover the DEX variants & its respective value propositions in separate article in detail.
The Possible Future
While CFFMs such as Uniswap & SushiSwap appear dominating today. The future of DeFi may or may not look similar to how it does today.
In order to answer it further, analysing participants in AMMs such as Uniswap is required. As mentioned earlier, major participants in Uniswap are “Liquidity Providers”-LPs. They deposit tokens on the platform pool for users/traders to swap against.
The liquidity providers are often long term holders(HODLers) & find earning passive income from transaction fees for each trade attractive. However, if the asset they are holding is volatile, the impermanent loss could dent their profitability in the long run.
The issue though is not mere volatility. Even if an asset is volatile, as long as the asset pairs are both volatile & exchange rate remains similar, LPs are expected to earn high fees with minimum impermanent loss.
Alternately, assets such as Stablecoin exhibit limited volatility & risks of impermanent is reduced considerably.
As a liquidity provider, it makes sense to add highly correlated pairs for the best case scenario.
The pairs such as ETH/USDC i.e. native crypto tokens & stable-coins can be unpredictable & high trading volume with accruing fees might make up for impermanent loss.
The assets which high inversely correlated are the one that should be avoided for LPs to ensure profit. Imagine, that if the price of one asset , Token A, increase by 20% & due to it’s inverse correlation with asset B, price token B declines simultaneously by around 20%, The impermanent loss will be very high.
One of the primary reasons that CFFMs have been very profitable for LPs till now is high volume.However this could change quickly.
In the next article , we will attempt to execute a trade & understand behind the scenes technical aspects in detail.