Weekly Journal #12 - Creating my own Decentralized Exchange!
A guide about the core concepts of DeX and Uniswap contracts!
Hey guys! Welcome for the 14 new members of our little community! 🎉
Since so many people joined us from our last issue, I’ll do a very quick recap of what has happened since we began!
This is my personal journal to track my progress while I’m building my first online product in the web3 space!
The product that we’re building is called Vinea.
In this post we cover more details about it: Weekly Journal #1 A No Code NFT platform.
We want to create a no-code platform for the B2B market, allowing companies to create NFTs as reward for their community.
Whilst we’re learning the best way to do it, we’ve been building some projects to interact with the web3 community, like Vinea Social.
Some weeks ago I started a short degree in Smart Contract development so I can have all the tools to build our product safely!
And that’s it!
We just reached the 3 months mark with this issue! Yeeey!
So if you are curious on how all this will play out, keep in touch! Would love to see you around!
In last week’s issue, the subject was DAOs, where I covered the basics on how this kind of organization works. You can check it out in the link below:
Last week in Resume (TL;DR)
I was creating my own (kind of) decentralized exchange for my 0xMacro’s course. The biggest project so far!
Learned how Decentralized Exchanges work.
Learned about Liquidity Pools.
Learned about the Constant Product Formula.
We did a tweak in Vinea’s social home page.
I was blown away about how this kind of decentralized exchange works and I want to share it with you all!
Decentralized Exchanges
This topic can get really technical, really quickly and I know that most of you reading are not interested in how this works under the hood.
I’ll try to explain this topic as simply as I can, and if you do want to understand more about the technical side, leave a comment and maybe I’ll do an extra issue focusing on that!
So, How do they work?
In simple terms: most of the decentralized exchanges work by building a pool of assets, aka Liquidity Pool, and using a price formula that governs the price of the assets in the pool.
Liquidity Pools
Let’s assume that we created a new coin, called CoolCoin (CC) and we want to create a place where holders of the CC can exchange it for ETH.
One way to do this is to create a Liquidity Pool with these two coins in it and then use a formula to determine its price, called Constant Product Formula.
In practice this is how it works.
We deploy a smart contract holding the following amount of our coins:
100.000 units of CC .
10.000 units of eth.
And in this contract we create a function that a user could call it passing how much of the coin (CC or ETH) they wish to exchange.
Ok, that’s nice! But how the price between these two coins can be determined?
Well, that is where the Constant Product Formula comes in play!
r1 * r2 = K
This is the constant product formula:
r1 is the reserve of CC coins that we have = 100.000
r2 is the reserve of ETH that we have = 10.000
k is the constant product. It must never change (hence the name of the formula)
And with this simple equation, the price between this two coins will be calculated and adjusted.
Here is an example
If we want to buy 15 eth worth of CC coins, how many coins will we get in exchange?
Let’s do the math!
First, lets get K value
r1 * r2 = K
100000 * 10000 = k
1000000000 = k (one billion)Ok, now we have the value of K, which is 1.000.000.000.
Since K can’t change (because it’s a constant) all we have to do is calculate how much the r1 (the CC reserve) will decrease if we add 15 eth to the r2 variable
r1 * r2 = 1000000000
r1 * (10000 + 15) = 1000000000
r1 = 1000000000 / 10015
r1 = 99850.224663Now that we have our new CC reserve value: 99850.224663 we just need to subtract this value from our previous CC reserve to find out how many tokens we got:
100000 - 99850.224663
result = 149.775337 CC tokensThere you have!
15 eth would have bought 149.775337 CC tokens!
Now, the r1 and r2 variables change, because we made a trade, but the constant K remains the same (actually it will increase a tiny bit, but that’s fine)!
This is how Uniswap’s contracts work!
Uniswap is one of the biggest decentralized exchanges out there, arguably the most trust-able. And at its core they follow this formula in order to create this trading experience.
Of course it’s not as simple as that in their case, but this is like their engine, their core! And now you know it!
Pool Providers
Now maybe you are wondering: But how and why these pools are formed and maintained?
Well, these pools are made possible by people that deposit their assets into it and in exchange they receive another token representing their share of the pool.
Just like when you buy a stock, you get a company piece. When you contribute to the pool, you get back another token that grants you your share of assets in the pool.
These decentralized exchanges have a fee on every trade that gets back into the pool growing them over time increasing your share.
It’s really like a traditional investment, with higher risks of course.
Anyway…
That was my attempt to explain to you how this kind of exchange works, hope I didn’t bore you to death, lol.
I’m telling you all about this because I have to pool together (sorry about the joke 😂) a smart contract that does exactly this and I was blown away on how Decentralized Exchanges work!
Hope you enjoyed it as well!
Vinea Social
Shifting gears now, this week we added some cool wallets in the home page of Vinea Social website so you can get inspired with some neat NFTs!
You can check it out here!
As I approach the end of the 0xMacro program I’ll have more time to dedicate towards Vinea and I wish that I can share more news with you about it soon!
Synthwave Music of The Week
This was a big one! Let’s wrap everything up with our music of the week:
I love calm synthwave music, and this one is a great example of this genre!
Hope y’all enjoy it!
That’s a wrap!
Thank you again, for your time and attention! Hope you’re enjoying this weekly newsletter.
Also, if you want to reach out, send me a DM on Twitter or add me on Discord, user: MrLeoni#6212
Have a great week! ✌️







Woow, I had no idea about the constant formula…. Keep the good work of explaining the backstage of crypto.
I loved the music of the week.