Discovering Decentralized Exchanges
Ever purchased crypto from giants like Binance or Coinbase? Most of us have, right? But, did you know these centralized exchanges have their set of nightmares? Hacks, company disappearances, and tragic tales of lost money — these stories are all too real.
Remember Mt.Gox? It once commanded 70% of all Bitcoin transactions. But in 2014, they “lost” a mind-boggling amount of Bitcoin now worth billions. Or take QuadrigaCX. The founder passes away and with him goes nearly $200 million of user funds. Shocked? That’s just the tip of the iceberg.
Hence the Web3 mantra: Not your keys, not your crypto. It emphasizes the importance of controlling your own crypto keys.
Enter Decentralized Exchanges (DEX)
Think of DEX as a marketplace where users trade crypto directly using smart contracts, without relying on a middleman. Sounds cool? It’s even more fascinating when you dive into the tech behind it.
Uniswap, the hero of our story and Ethereum’s star dApp, is at the forefront of the DEX revolution. Inspired by Ethereum’s founder, Vitalik Buterin, Hayden Adams birthed Uniswap in November 2018. And oh, what a game changer it’s been!
So, Why Not Just Digitalize Traditional Exchanges?
Traditional exchanges use what’s called an order-book system. For instance, Alice wants to trade 100 of ‘TokenA’ for 50 ‘TokenB’. Bob agrees. They match, and the trade happens. Sounds simple? Well, when you bring this system on-chain (like Ethereum), things get gas-guzzling expensive!
Enter Uniswap. It presented a fresh way to trade, ditching order-books and allowing users to swap tokens in a fluid and economical manner. Bonus? You can even earn using Uniswap!
Uniswap Through Time
From its debut, Uniswap has gone through multiple versions:
v1 (2018): Allowed swaps between ETH and other tokens.
v2 (2020): Direct swaps between any two tokens.
v3 (2021): Enhanced capital efficiency for users.
v4 (2023): New features that drastically reduce gas costs and offer customization.
Uniswap and the Magic of AMMs
Uniswap: Behind the Curtain of an Automated Market Maker (AMM)
1. What’s a Market Maker? Think of a busy market square. You’re trying to exchange apples for oranges. Market makers are like the stalls that have both. In the digital currency world, they ensure there’s always a balance of assets so trading can happen.
2. The Uniswap Way: Traditional systems have big entities pouring in money to keep things moving. Uniswap turns this on its head. With their system, anyone can jump in and add assets, making it decentralized.
3. \*Core Concept: x*y=k***
— Imagine a seesaw with ETH on one side and TOKEN on the other. No matter how much you add or remove from one side, the product of both sides remains constant.
— Want to swap? This equation tells you how much you get in return.
4. Why this Works?
— The balance is maintained because the equation prevents either side from hitting zero.
— This means no one can drain the entire pool or drastically shift the balance.
5. Setting Prices: When a new token arrives, who sets the price? The first one to jump in and provide assets! But after that, others must follow the ratio set by the first.
- The Role of LP Tokens:
— Just like a shop gives loyalty points, Uniswap gives LP tokens when you add assets.
— Got more tokens? You’ve got a bigger slice of the pie.
— They can be exchanged back for your assets plus any fees you’ve earned.
7. Fees Magic:
— Instead of taking an explicit fee, a bit is deducted from what traders put in.
— These small fees accumulate, making the asset pool grow over time.
— More assets in the pool means more rewards for those who provided them!
Solidity Sneak Peek:
For the tech-savvy, here’s how Uniswap does its magic in code:
Let’s try to write that code in Solidity:
function calculateOutputAmount(uint inputAmount, uint inputReserve, uint outputReserve) private pure returns (uint) {
uint outputAmount = (outputReserve * inputAmount) / (inputReserve + inputAmount);
return outputAmount;
}