Uniswap protocol for beginners!

Uniswap protocol for beginners!

Easy maths behind the decentralized exchange

Β·

5 min read

Cryptocurrencies or the hyped Web3 is taking the world by storm or, at least Twitter. Since its inception, decentralized finance aka DeFi has become the talk of the town. Within DeFi, we've witnessed the rise of decentralised exchange solutions. One of these protocols is Uniswap.

I got curious. How the heck does a protocol run all by itself, trading billions worth of crypto every day! Let's check out Uniswap's secret sauce.

πŸ¦„ What is Uniswap anyway?

Uniswap is a decentralised cryptocurrencies exchange protocol. How is it decentralised? Well, Uniswap is run by a thousand nodes on Ethereum blockchain, NOT by a single entity. Which makes it non-upgradable and censorship-resistant. This is in contrast to a centralised trading platform like Coinbase. The protocol is rooted in the idea conceived by Alan Lu of Gnosis, popularised by Ethereum founder Vitalik Buterin and finally shipped under name of Uniswap by Hayden Adams. The foundational algorithm is known by the name Automated Market Maker. Essentially, the protocol can:

  1. create liquidity pool of tokens
  2. provide liquidity and,
  3. swap tokens

Head over to the app and play around the interface a bit. It's easy, just connect your wallet, select what you want to sell and what you are buying in return and boom.

uniswap_2.png You have swapped one crypto for another. That's ridiculously simple. But we are engineers, we make spider webs look like rainbows. How come it showed me 0.00687 UNI for 0.01 ETH? Let me turn up your curiosity too and let's get behind the scenes.

After digging into the protocol for a few days, I encountered the secret sauce behind Uniswap: x*y = k. Let me help you with how it works with an easy-to-absorb analogy.

A short story: John Doe opens an exchange

Imagine a planet of art enthusiasts. There are writers, poets, painters, sculptors, actors and many more. Writers and painters are quite popular here. With artists we also have entrepreneurs. John Doe comes up with a startup idea. Why not provide tools for the most in-demand artists? "Pens and Paintbrushes," he says. But instead of selling them for bucks, he wants to enable trade. Say Alice wants to do some painting, she can simply trade her pen for a paintbrush. Cool, right. Easy, quick and cheaper trade.
John did some maths and came up with a magical formula.

x * y = k

In his case, it means:

(number of πŸ–‹) * (number of πŸ–Œ) = constant

Why is this equation so magical? We'll see soon. To kick-start the exchange, now John needs a stock of pens and brushes. It would be really great if folks in the town help him with this. In return, he could give them a share of his exchange market fee.

automated market maker.png

Cool. The initial pool has 5 pens and 5 brushes with the grace of kind investors. Let's say 1 dollar for each item. The exchange rate would be:

1 πŸ–‹ = 1 πŸ–Œ

At this point let's feed our magical equation:

5 * 5 = 25

k = 25 which has to remain constant. John repeats, no matter what happens to the liquidity pool, k has to remain 25. Alice, a young artist, visits the store to exchange her 1 pen for a brush. She adds 1 pen to the stock. Note that the number of πŸ–‹ now equals 6.

How does John calculate the exchange rate for Alice?

(number of πŸ–‹) * (number of πŸ–Œ) = 25 or,
(number of πŸ–Œ) = 25/6 = 4.167 or,
the value of 6 pens = 4.167 brushes
AND John gives Alice the remaining amount of πŸ–Œ i.e. (5-4.167) = 0.833 worth of brushes.

🀯 Whoa! Wut?! How come Alice didn't get 1 brush in return for her 1 pen? Okay, what happened is the magic of John's formula. XY = K is magical because it works as per the law of demand and supply. As the pens in the pool increased to 6, their value with respect to brushes dropped. Since we are doing a barter here, 0.833 amount of brush in return doesn't make sense. But you can simply think of it as 1 pen which was priced at 1 $ now sells at 0.833 $ and 1 brush now sells at 1.25 $.
The crux is that x*y=k auto-magically calculates the buy and sell price of tradable items as per their demand and supply in the liquidity pool.

How would John manage such volatility in exchange?

That is an expensive and volatile exchange rate system. If the exchange rate keeps on changing like that on every transaction, people will think that John is doing a shitty startup. Nobody would visit him. John finds a solution- if he increases the stock of pens and brushes in the pool, the slippage becomes significantly less.

Find it out for yourself. Change the initial stock to (number of πŸ–‹) = 100 and (number of πŸ–Œ) = 100. Calculate k. Then, try a similar transaction by Alice. Note how the exchange rate differs from the earlier example. You'll be amazed. πŸ˜‰

πŸ“Key takeaways

You'll come across these concepts quite a lot in DEX space, remember:

  • a stock of two tradable items makes a LIQUIDITY POOL 🌊
  • folks who would invest are LIQUIDITY PROVIDERS 🀝
  • the maths behind buy and sell prices is XY = K πŸ”’
  • more the demand of token, the higher will be its value: Demand and Supply πŸ”ΌπŸ”½
  • more the liquidity, lesser the trading costs or, KπŸ”Ί SlippageπŸ”»

πŸ™ Conclusion

That's it, my friend. The core Uniswap works pretty similarly to John Doe's exchange. We just replace John Doe with an automated protocol running on the blockchain. I know it's not that easy to grasp in the first read but I hope you'll not stop here. This is just one of the cool protocols in decentralized finance out there. Keep learning! πŸ’™
If you like to learn from videos check out this one from WhiteBoard Crypto. It helped me understand a lot of stuff in crypto world.
I'm actively learning Blockchain technology and writing content to make it accessible for people. Feel free to HMU on Twitter. πŸ€—

Β