
Blockchain for Enterprise
People tend to overestimate how easy it is to create a blockchain. Just because you were able to deploy a network doesn’t make you an expert on blockchain. As a matter of fact, even an intern can do it in minutes. Here, try it. You know what else is easy to deploy? A webpage. Creating a blockchain is easy, and you can do it at zero cost and effort for as long as you don’t care about the design and spec of your network. Understanding the engineering constraints to design a secure and functiona...

Can They Really Sell Your Eyeball Scans? A Technical Review of World
Here I am, resurrecting my blog like a dusty necromancer coming back for one last summon. And what brought me back from the digital grave? Larpers. Everywhere. People posing as crypto 'experts' when they haven’t done the actual work of researching whatever the hekk it is they are talking about. It’s all vibes and appearances and no substance. Lately, the Orb and World has been made an antagonist in the Filipino crypto scene. And everyone suddenly became a data privacy expert and mor...

Blockchain Legos: The Modular Stack
If you’ve been here long enough, you would have already heard of the blockchain trilemma where you can only pick two out of three between security, speed, and decentralization. But that is so 2020. Some years ago, we expect one single blockchain to perform various functions for us. For instance, Ethereum has become congested because it was juggling between validating incoming transactions, arranging them into blocks, executing them, and finally keeping all these growing records available at a...
A Friendly Donkey


Blockchain for Enterprise
People tend to overestimate how easy it is to create a blockchain. Just because you were able to deploy a network doesn’t make you an expert on blockchain. As a matter of fact, even an intern can do it in minutes. Here, try it. You know what else is easy to deploy? A webpage. Creating a blockchain is easy, and you can do it at zero cost and effort for as long as you don’t care about the design and spec of your network. Understanding the engineering constraints to design a secure and functiona...

Can They Really Sell Your Eyeball Scans? A Technical Review of World
Here I am, resurrecting my blog like a dusty necromancer coming back for one last summon. And what brought me back from the digital grave? Larpers. Everywhere. People posing as crypto 'experts' when they haven’t done the actual work of researching whatever the hekk it is they are talking about. It’s all vibes and appearances and no substance. Lately, the Orb and World has been made an antagonist in the Filipino crypto scene. And everyone suddenly became a data privacy expert and mor...

Blockchain Legos: The Modular Stack
If you’ve been here long enough, you would have already heard of the blockchain trilemma where you can only pick two out of three between security, speed, and decentralization. But that is so 2020. Some years ago, we expect one single blockchain to perform various functions for us. For instance, Ethereum has become congested because it was juggling between validating incoming transactions, arranging them into blocks, executing them, and finally keeping all these growing records available at a...

A Friendly Donkey
Share Dialog
Share Dialog

Subscribe to 0xDanki ( Tin Erispe )

Subscribe to 0xDanki ( Tin Erispe )
<100 subscribers
<100 subscribers
In the beginning, the only way you could buy crypto was either through bargaining with another human or going through the hoops of centralized exchangez. And it’s all good, but manually finding a seller that will meet your quantity and price is difficult and centralized exchanges are prone to mismanagement.
So from these inefficiencies, DEXes are born. And safe to say that as of the moment, the most popular DEX that is catering to hundreds of thousands of crypto users is Uniswap.
This has been one of my autistic obsessions. Uniswap’s upgrades have always marked something new for DeFi. And now we’re on v4, it seems that the exchange has matured to a point that it may truly be able to cater to sophisticated market making strategies that are usually only found in TradFi.
Warning: Long post ahead and may involve some math. Do not read beyond the asterisks if you get seizures from seeing graphs and formulas.
********
At this point DEXes are just dawning and we have a simple market maker that uses the ubiquitous x * y = k formula. The most prominent characteristic of V1 though is that it only allows for swap between native ETH and another ERC-20 token. So, if say you want to swap your USDT for some MATIC, then you will have to go the route of swapping USDT to ETH, and then swapping that ETH to MATIC. There is no getting around it. You pay double fees just to make that trade, which iz sadt. And slippage sucks because there is no way to read what is happening on the other pools to find the most cost-efficient way to trade your tokens. And that is how V2 entered…
Uniswap V2 fixes problems from V1 by giving the users the ability to create a custom ERC20-ERC20 pool, do a direct swap inside that pool, or use the Router.
Ok that’s new. Since Routers were introduced, we now have a smart contract that can look to every liquidity pool contract that was permissionlessly created using Uniswap V2 protocol. It enables users to choose the “route” of their transaction. For example, somebody may opt to swap their USDT for MATIC through the USDT-DAI / DAI-ETH / ETH-MATIC route. It may sound crazy because who in their right mind will complicate the routes of their trades, but this is especially useful for arbitrage which is a technique MEVs use to profit from price discrepancies between pools. I’ll have a separate article for this maybe, but no promises. The important thing you need to know is that arbitrageurs help these pools become more liquid so the slippage is lessened when you make a trade between less common tokens in Uniswap.
There are actually many updates in V2, but I’ll only talk about one more coz we’re gonna hang around V3 and V4 much longer: V2 now has an on-chain oracle price feed.
This improvement is not actually for Uniswap direct users but for other markets and protocols that will use their exchange prices as basis for transaction.
Anyway, the problem is that many pools in the Uniswap fluctuate wildly, and they’re not even hard to manipulate because you can easily borrow millions of dollars from a flashloan. The attacker can use an arbitrage bot to pocket the discrepancy from the real price. So wat did Uniswap do?
V2 then made the oracle to measure prices at the end of the block so that any manipulator wouldn’t be able to arbitrage to their choice of protocol without first making a bad trade in Uniswap at the end of a previous block.
Anyway, after a block is mined, there will be a few seconds before the next block comes. This elapsed time will be measured and then multiplied to the end-of-block price (and now we have the time-weighted price). And the process starts again on the next block. You simply sum these time-weighted prices up for whichever time interval your external contract wanna measure.
A little visual to illustrate TWAP math:

Notice that we’re simply computing the average price movement like normal, which is the cumulative price on that period over the total elapsed time. You will later on learn that this is not a very accurate formula when you wanna compute for the average change in price in terms of percentage (and not numbers).
I’ve waited like 3 posts to talk about this LOL. V3 is my personal favorite upgrade because it’s solving a huge problem in capital efficiency and at the same time, it has opened new market making strategies aside from just passive liquidity providing.
Remember the problem I’ve mentioned in Constant Product Market Makers? Well, those liquidity distributed at extreme prices may never be utilized. So, V3 introduced concentrated or range-bound liquidity where liquidity providers can pick at which price range they are willing to provide. Now they can find themselves always in the middle of the liquidity graph where most of trades happen and consequently where most of commissions are earned. And it benefits the user too, because more liquidity in their trading prices means less price impact on their trades.

This design has opened some new questions though because the liquidity will be irregularly distributed across this curve. For example, how will Uniswap even compute for the exchange rate? Surely, we can’t just x * y = k the whole thing to compute for the price in any arbitrary point in this curve. And how do you even determine at what exact values can the liquidity be placed?
I scratch my earz, cause we’re getting to ticks. Just take a look:

These little markings along the liquidity curve represents a price point that you can use to either define which price you can place an order, or define the range where you can place your liquidity. Imagine ticks like a pixel on your computer. You can’t have two colors inside a pixel the same way you can’t have two different liquidity in between ticks.
So, basically, you have a definite k value in the x * y = k for each space between ticks.
The catch is that ticks are not expressed in terms of currency. They are a different unit of price, because ticks and their spacing still have to be initialized and determined depending on the fee tier of the pool.
So, how does Uniswap compute for the tick and how can you convert these ticks into a price value?
The relationship between the price and tick is this:
Tick = log1.0001(Price)
Don’t let this formula intimidate you tho, it only means that each tick is the power that you have to raise 1.0001 to in order to receive a certain output price.
If we rearrange this formula to get the price at any given tick, then it looks simpler:
Price = 1.0001^τ
Uniswap pools always have a range of ticks between -887272 and 887272, which equates to 2^-128 to 2^128 in price, which is practically 0 to infinity and covers the whole range of possible prices.
But it is not advisable to use the Price = 1.0001^τ formula in computing price because of precision loss due to integer constraints. So we have a variable called sqrtPriceX96 to compute for price.
Without going into much details, sqrtPricex96 is exactly what it says: the product of the square root of the price and 2^96 (as it is expressed in bits). Why this formula tho? It was the largest value for precision that allowed for the best gas efficiency.
Anyway, going back to concentrated liquidity... If you’re a degen looking for market making opportunities, you have to set the narrowness of your range depending on how actively you wanted to manage your LP positions. If you don’t want to watch it too often, then set your range wide enough to capture the bulk of price movement (and fees). If you’re the more of an active LP-er, then you can narrow your positions so that your liquidity earns more fees for less, but you have to move it an pay gas when the price has gotten out of your range.
There is so much more to this, but I guess this whole V3 thing has opened up a bunch of new possibilities in terms of innovation (comparatively) in traditional orderbooks and market making strategies on crypto, and it is a long topic so it will be for another post. I promise it’ll be less abstract and more visuals and stories from there!
Just as I thought that it couldn’t get any wackier in V3, Uniswap announced its release of V4 drafts just a few days ago. And boy, it was so different. For one, the Router is gone. The pool factory is nil, too. Instead it has a singleton contract with just about 500 lines of code that holds all of the pools you could ever use in Uniswap 🤯🤯🤯 This saves gas big time, as you no longer have to pay double fees and gas even if your most efficient route takes more than single swaps.
If that’s not different enough, pools can now have their own rules, too. Pool creators can now use this thing called hooks, which are externally deployed contracts where you can write your custom logic and execute it at a specific point of trigger from the pool. When a trigger event happens, Uniswap’s hook contract calls back the specified hook of the pool creator.
This deadly combination of having a Singleton contract to hold all pools and having Hooks to create custom rules for each of them can have several use cases for the traders and liquidity providers:
-they can have oracles of all shapes and sizes, making it easier to create tools for both manual and algorithmic traders alike
-pools can have volatility-based fees
-LPs can automatically send their inactive liquidity somewhere else to make more $$$
-autocompounding of LP rewards
-redistributing internal MEV profits to the LPs
…These are just some suggested by Uniswap Labs themselves. There are definitely more. Danki haz a few ideas like active cross-pool liquidity management, but maybe and I’ll share more of these once I’m totally convinced of my POC.
Geez. If you’re still here, then congratulations, you now have more knowledge of AMMs than most DeFi degens. I’d consider you a fren.
And since you’re frens with Danki now, I’ll tell you a secret: I expect V5 to be more boring than V3 and V4. Maybe a super cheap crosschain swap for later or some improvements in privacy. Though who knows, my brain iz too little to wrap around all the developments in the space and the new ways you could connect them together. But my bet is that we’re transitioning to a new meta after Uniswap has already solidified its most important features. The ground has already been laid for new financial products to thrive.
Or if that’s not it… maybe some AI or ML integration will reopen the Pandora’s box for DEXes, but I dunno how that will happen. I’d like to have you share some insights too! 🐴🌟
In the beginning, the only way you could buy crypto was either through bargaining with another human or going through the hoops of centralized exchangez. And it’s all good, but manually finding a seller that will meet your quantity and price is difficult and centralized exchanges are prone to mismanagement.
So from these inefficiencies, DEXes are born. And safe to say that as of the moment, the most popular DEX that is catering to hundreds of thousands of crypto users is Uniswap.
This has been one of my autistic obsessions. Uniswap’s upgrades have always marked something new for DeFi. And now we’re on v4, it seems that the exchange has matured to a point that it may truly be able to cater to sophisticated market making strategies that are usually only found in TradFi.
Warning: Long post ahead and may involve some math. Do not read beyond the asterisks if you get seizures from seeing graphs and formulas.
********
At this point DEXes are just dawning and we have a simple market maker that uses the ubiquitous x * y = k formula. The most prominent characteristic of V1 though is that it only allows for swap between native ETH and another ERC-20 token. So, if say you want to swap your USDT for some MATIC, then you will have to go the route of swapping USDT to ETH, and then swapping that ETH to MATIC. There is no getting around it. You pay double fees just to make that trade, which iz sadt. And slippage sucks because there is no way to read what is happening on the other pools to find the most cost-efficient way to trade your tokens. And that is how V2 entered…
Uniswap V2 fixes problems from V1 by giving the users the ability to create a custom ERC20-ERC20 pool, do a direct swap inside that pool, or use the Router.
Ok that’s new. Since Routers were introduced, we now have a smart contract that can look to every liquidity pool contract that was permissionlessly created using Uniswap V2 protocol. It enables users to choose the “route” of their transaction. For example, somebody may opt to swap their USDT for MATIC through the USDT-DAI / DAI-ETH / ETH-MATIC route. It may sound crazy because who in their right mind will complicate the routes of their trades, but this is especially useful for arbitrage which is a technique MEVs use to profit from price discrepancies between pools. I’ll have a separate article for this maybe, but no promises. The important thing you need to know is that arbitrageurs help these pools become more liquid so the slippage is lessened when you make a trade between less common tokens in Uniswap.
There are actually many updates in V2, but I’ll only talk about one more coz we’re gonna hang around V3 and V4 much longer: V2 now has an on-chain oracle price feed.
This improvement is not actually for Uniswap direct users but for other markets and protocols that will use their exchange prices as basis for transaction.
Anyway, the problem is that many pools in the Uniswap fluctuate wildly, and they’re not even hard to manipulate because you can easily borrow millions of dollars from a flashloan. The attacker can use an arbitrage bot to pocket the discrepancy from the real price. So wat did Uniswap do?
V2 then made the oracle to measure prices at the end of the block so that any manipulator wouldn’t be able to arbitrage to their choice of protocol without first making a bad trade in Uniswap at the end of a previous block.
Anyway, after a block is mined, there will be a few seconds before the next block comes. This elapsed time will be measured and then multiplied to the end-of-block price (and now we have the time-weighted price). And the process starts again on the next block. You simply sum these time-weighted prices up for whichever time interval your external contract wanna measure.
A little visual to illustrate TWAP math:

Notice that we’re simply computing the average price movement like normal, which is the cumulative price on that period over the total elapsed time. You will later on learn that this is not a very accurate formula when you wanna compute for the average change in price in terms of percentage (and not numbers).
I’ve waited like 3 posts to talk about this LOL. V3 is my personal favorite upgrade because it’s solving a huge problem in capital efficiency and at the same time, it has opened new market making strategies aside from just passive liquidity providing.
Remember the problem I’ve mentioned in Constant Product Market Makers? Well, those liquidity distributed at extreme prices may never be utilized. So, V3 introduced concentrated or range-bound liquidity where liquidity providers can pick at which price range they are willing to provide. Now they can find themselves always in the middle of the liquidity graph where most of trades happen and consequently where most of commissions are earned. And it benefits the user too, because more liquidity in their trading prices means less price impact on their trades.

This design has opened some new questions though because the liquidity will be irregularly distributed across this curve. For example, how will Uniswap even compute for the exchange rate? Surely, we can’t just x * y = k the whole thing to compute for the price in any arbitrary point in this curve. And how do you even determine at what exact values can the liquidity be placed?
I scratch my earz, cause we’re getting to ticks. Just take a look:

These little markings along the liquidity curve represents a price point that you can use to either define which price you can place an order, or define the range where you can place your liquidity. Imagine ticks like a pixel on your computer. You can’t have two colors inside a pixel the same way you can’t have two different liquidity in between ticks.
So, basically, you have a definite k value in the x * y = k for each space between ticks.
The catch is that ticks are not expressed in terms of currency. They are a different unit of price, because ticks and their spacing still have to be initialized and determined depending on the fee tier of the pool.
So, how does Uniswap compute for the tick and how can you convert these ticks into a price value?
The relationship between the price and tick is this:
Tick = log1.0001(Price)
Don’t let this formula intimidate you tho, it only means that each tick is the power that you have to raise 1.0001 to in order to receive a certain output price.
If we rearrange this formula to get the price at any given tick, then it looks simpler:
Price = 1.0001^τ
Uniswap pools always have a range of ticks between -887272 and 887272, which equates to 2^-128 to 2^128 in price, which is practically 0 to infinity and covers the whole range of possible prices.
But it is not advisable to use the Price = 1.0001^τ formula in computing price because of precision loss due to integer constraints. So we have a variable called sqrtPriceX96 to compute for price.
Without going into much details, sqrtPricex96 is exactly what it says: the product of the square root of the price and 2^96 (as it is expressed in bits). Why this formula tho? It was the largest value for precision that allowed for the best gas efficiency.
Anyway, going back to concentrated liquidity... If you’re a degen looking for market making opportunities, you have to set the narrowness of your range depending on how actively you wanted to manage your LP positions. If you don’t want to watch it too often, then set your range wide enough to capture the bulk of price movement (and fees). If you’re the more of an active LP-er, then you can narrow your positions so that your liquidity earns more fees for less, but you have to move it an pay gas when the price has gotten out of your range.
There is so much more to this, but I guess this whole V3 thing has opened up a bunch of new possibilities in terms of innovation (comparatively) in traditional orderbooks and market making strategies on crypto, and it is a long topic so it will be for another post. I promise it’ll be less abstract and more visuals and stories from there!
Just as I thought that it couldn’t get any wackier in V3, Uniswap announced its release of V4 drafts just a few days ago. And boy, it was so different. For one, the Router is gone. The pool factory is nil, too. Instead it has a singleton contract with just about 500 lines of code that holds all of the pools you could ever use in Uniswap 🤯🤯🤯 This saves gas big time, as you no longer have to pay double fees and gas even if your most efficient route takes more than single swaps.
If that’s not different enough, pools can now have their own rules, too. Pool creators can now use this thing called hooks, which are externally deployed contracts where you can write your custom logic and execute it at a specific point of trigger from the pool. When a trigger event happens, Uniswap’s hook contract calls back the specified hook of the pool creator.
This deadly combination of having a Singleton contract to hold all pools and having Hooks to create custom rules for each of them can have several use cases for the traders and liquidity providers:
-they can have oracles of all shapes and sizes, making it easier to create tools for both manual and algorithmic traders alike
-pools can have volatility-based fees
-LPs can automatically send their inactive liquidity somewhere else to make more $$$
-autocompounding of LP rewards
-redistributing internal MEV profits to the LPs
…These are just some suggested by Uniswap Labs themselves. There are definitely more. Danki haz a few ideas like active cross-pool liquidity management, but maybe and I’ll share more of these once I’m totally convinced of my POC.
Geez. If you’re still here, then congratulations, you now have more knowledge of AMMs than most DeFi degens. I’d consider you a fren.
And since you’re frens with Danki now, I’ll tell you a secret: I expect V5 to be more boring than V3 and V4. Maybe a super cheap crosschain swap for later or some improvements in privacy. Though who knows, my brain iz too little to wrap around all the developments in the space and the new ways you could connect them together. But my bet is that we’re transitioning to a new meta after Uniswap has already solidified its most important features. The ground has already been laid for new financial products to thrive.
Or if that’s not it… maybe some AI or ML integration will reopen the Pandora’s box for DEXes, but I dunno how that will happen. I’d like to have you share some insights too! 🐴🌟
No activity yet