Automated Market Maker (AMM) | Dune Arcana #5

This week, in honor of the ongoing `dex.trades` migration, we will cover the basics of AMM (automated market makers) and offer a starting point on what data analysis we can do with AMM's on-chain data.


Jackie  0:00  

Thank you. Hello, hello, welcome to Dune Arcana #5, where we explore Web3 news and trends with on-chain data. So, I'm agaperste with me is Al-Muthanna. And we've got our featured guest today, Austin Adams research from Uniswap.

Austin  0:22  

Yeah, happy to be here. Hopefully, I can shed some light on the confusing world of AMMs.

Jackie  0:28  

Yeah, thanks for being here. So yeah, let's get started with today's topic. Okay, so in honor of the Great Migration for dex.trades from V1 to V2. Today's topic is automated market maker or AMM. 

So, if you want to know more about what is up with this text migration and know more about the Dune bounties, I've got a few links on the board here that you can go explore. If you're wondering about the relationship between Dex and AMM, you're in the right place. We're gonna dive right into it. 

Okay, so we're going to use this mirror board throughout the session to help us go through all the different terminologies and understand that relationship better. 

If you're following along, you can just click on the mirror board. I'm going to go into the mirror board side now. We're going to start from the very top (the exchanges), right? Here, I have some apples and some gingerbread. 

So, it's full-time, and I have some apples, but I now kind of want some gingerbread, so I can go to exchange directly to exchange for some gingerbread. So, that's an idea of an exchange basically. 

More formally, Investopedia defines exchange as a marketplace where securities, commodities, derivatives, or other financial instruments are traded. But more simply put, we're just exchanging x for y in this picture or apple for gingerbread. 

Pretty simple. Underneath exchanges, we've got the dex and the CEX, right? So, what are they? So, CEX is centralized exchange. 

It facilitates the buying and selling of cryptocurrency. It functions as a trusted intermediary in trades, and it often acts as a custodian by storing and protecting customer funds. In order to utilize the centralized exchanges, as a customer, you need to go through some checks, such as KYC. Know your customers. 

So, think about giving your addresses, your social…just making sure you're who you are, who you say, and then you are eligible to do the streets. But then the good side is that they have dedicated support. 

If they're the custodian of your funds, they're actually responsible for making sure your funds are safe. Right? So, on the opposite side, right, that's the centralized exchanges. 

And then, as opposed to centralized exchanges, we've got decentralized exchanges, right? As the name implies, decentralized exchanges are decentralized and permissionless. It allows you to trade cryptocurrencies in a decentralized way. 

It works without a central authority like Coinbase or Binance, right? It replaces a central authority with a smart contract. So, with open source code, you only need to trust the open source code and not someone like Coinbase or Binance. 

You don't really need to go through the checks for KYC. The downside is you are responsible for your own funds and your own actions. 

Cool. So, that's central exchanges versus decentralized exchanges decks. Underneath it, we can see there are mainly two different ways that you can facilitate exchanges, right? 

You can facilitate this market-making process. There's the automated market making one way and then there's Orderbook. 

Okay, so we quite often hear this word. Hmm. So automated market makers. So, the exchange places that utilize this automated market-making method are called automated market makers. 

When you hear AMM, it normally refers to these marketplaces. And then just a quick word, since we're here on this visual, you might also hear right Dex aggregators, and then you're like, what's the relationship? 

As the name implies, that's Dex aggregators just like aggregating the trades from or the stuff from the AMS, right? If you have five different AMMs, you can trade this pair that you want, so ducks…their job is to aggregate from these EMFs and then find the best strategy routed correctly for you, so you get the best price. 

But yeah, so that's the next aggregators out there. Okay, so let's just come back to automated market-making and where to book. And then, let’s talk a little bit more in-depth. Switching back to this slide. Okay, so order book versus AMM. 

For order books, as the name implies, you just use an order book to match up buyers and sellers. Just like in traditional finance, right, you've got some buyers that want to buy a certain amount of something, and then you've got sellers who want to sell a certain amount of something. 

And then the job of these marketplaces is to match up the orders from both sides with an order book. I've linked a quick video here if you want to know more about it and then tradfi world. Okay, so with AMM on the other side, right?

So, instead of an order book, you've got a liquidity pool. Instead of a book matching buyers and sellers, you've got this liquidity pool, which is a pool of liquidity, right?

You need to have liquidity providers (often called LPs) to provide liquidity. And then once they provide the liquidity into this pool, then the liquidity providers will receive a token. 

Normally, it can be an LP token ERC20 token, so that’s the fungible type, but then sometimes it can be an NFT (the nonfederal type). That basically represents the position of your liquidity in this pool. 

And then as people start utilizing this pool to start trading, the two tokens are full as LPS. You receive a portion of the fee that is proportional to the liquidity that you provide. 

And then, at some point, if you decide, “Hey, I'm going to close out my LP position,” then you can burn the tokens that you receive to represent your liquidity. You will withdraw that liquidity and the fees that you earned when you were a liquidity provider. 

So, instead of trading against an order book, you're trading against a liquidity pool. It allows traders to buy and sell coins using some sort of algorithm that dictates how expensive it should be based on how much there is. 

That is, you know…hmm…versus books two different ways that you can trade on exchanges. And then there’s a link here for a good talk and comprehensive review of different algorithms that we have in AMM. 

But today, we're gonna just cover a few of them. Okay, cool. So, now we've got the order book down. The next topic that's relevant is how you decide the pricing as a market maker, right? 

Because, when you trade things, you need to somehow find a way to define the price of how much this token should be worth. 

A method that you can do this with…there are two ways you can go about price discovery, and there's the price Oracle, right? So, price discovery just sounds like you're discovering the price. It's based on supply and demand. 

If you think about how much allocation you have for each asset in this pool, the current portfolio allocation reflects which assets have been more desirable at the price being offered.

Yeah, so basically, you have a pool and then the pool doesn't really have an idea of the actual price of this token. It’s through some sort of algorithm, the balancing of the pool allocation, that the pool actually knows how much this token should be. So, you're discovering the price that way. 

And then, an example of an algorithm that uses price discovery solely is constant product market making, which you might have heard about quite a lot already. We're gonna go into it more. But okay, so that's price discovery. 

And then, as opposed to price discovery, you've got to price oracle. Instead of discovering the price, you're relying on some external party to tell you what the price of the assets should be. 

An example of this is constant sum market making. Awesome. So, let's go back to our visual here, right? We've got the automated market making different algorithms. 

And then, as we were kind of talking about a constant product, market-making is a type of algorithm that solely uses price discovery. And then some examples of protocols that utilize this. This is huge. 

Do you want me to go Balancer, Bancor and then price oracle wise. You've got the constant sum as a type of algorithm, right? And then you actually can have a hybrid of in-between, right? 

So, you can have a stable swap as a hybrid of price discovery and price oracle. And then aside from that, you've also got the innovation from Uniswap V3, which uses concentrated liquidity. 

But yeah, so we're gonna go into all of these in more detail, but relationship-wise, this is what you see. Right? 

So, we went from exchanges to DAX and such, and then we talked about, you know, two different ways there. 

Hmm, there's an order book and then within AMM, there are quite a lot of algorithms that achieve this ultimate in market making. And then, we're gonna dive into them. Okay, so we're gonna go back to the slideshow mode. 

So, number one we're going to talk about is the constant product market making. So, this x times y equals to k equation that you see a lot. 

Okay, so what is visually speaking that you can conceptually understand? So, the x and y-axis. On the x-axis, we've got the input amount of the token, and then on the y-axis, we've got the output amount of token B. 

So, with this x times y equals 2k equation, what happens is that the prices decrease with larger inputs. Meaning, if you think about it, we’re going back to our example of apple and gingerbread, right? 

So, if more people want to have gingerbread than Gingerbread, we'll get more expensive, right? As gingerbread gets more expensive with the same amount of apples, you can trade for less gingerbread. 

Yeah, as I said, it's a supply-and-demand concept. Basically, if you don't want to think of mathematical equations, just supply and demand. That's what it is. 

And then visually speaking, you can see this:  the slope. It kind of gradually decreases and gets flatter, so the amount of token V that you can get gets smaller and smaller. 

That's a constant product and then constant sum, right? So constant sum, you rely on a price oracle externally. The price oracle will tell you exactly how much this token price should be so how much of this token you can trade. 

You can see you've got a constant slope, right? So, let's say, one USDC you should always get one USDC. It's pretty set. 

But the problem is…if you are just slightly out of a few prices or slightly wrong, and the supply and demand don't really reach exactly as it is, then the one side… that kind of has more demand and will keep running, running, running until the point where you no longer have that. 

So, you know, you no longer can get the coin on the site that has more demand. That's kind of the problem with the Constant Sum. But visually speaking, this is the custom input and output visual that you can think about. 

And then we've got the hybrid, right? An example of the hybrid I was mentioning earlier, it stays stable swap, which is introduced by the curve.

The idea is they are trying to move from constant sum to constant product as the reserve gets more out of balance. 

I was kind of talking about USDC. But then, if the pool gets really out of balance, then you're gonna run out of tokens on one side to kind of alleviate and solve this problem. 

They're just making it a bit more dynamic with the amplifier parameter. Oops. So yeah, that is the hybrid. And then quickly, let’s also mention, right, with Uniswap V3. We get to know this concept of concentrated liquidity, which is a great innovation, really. 

Instead of just a pure cousin product that you're providing as a liquidity provider, right? So before, if you look at this visual here, this is the price of the token. You can go from zero to infinity pretty much.

And then, in Uniswap V2, which is just purely a simple budget, a pure custom product world as a liquidity provider. You're providing your liquidity fully distributed on this entire curve. 

But then with the concentrated liquidity, the LPS has an option. They can choose an upper and lower range for a tick, right? So, they can choose that there are protected lower tech arranged to provide their liquidity. 

So essentially, they're concentrating their liquidity on this curve. And as a result, the capital that you provide is much more efficient.

Here, I have linked a great visual by Austin. Okay, cool. So, with this visual, we can appreciate the beauty of concentrated liquidity, right? Let’s first orient ourselves. 

On the x-axis, we're picking a pool, the ETH USDC pool. And on the x-axis, we have the ETH and USDC. We get the ETH and USDC exchange rate, right? 

How much ETH is worth in USDC terms? And then, on the y-axis…maybe I should make this bigger. Can I do this? Oh, I can do this. Okay. On the y-axis, we've got the liquidity at the tick. 

So, a tick is pretty much if you go back to…sorry, if you go back to this concept, right…a tick is a way to convert this continuous line, right? 

It's a continuous concept in discrete portions. So, one tick is usually one bit. So, there are just ways for you to actually discreetly divide up this curve. 

But yeah, so coming back here using the tech, you can measure at this particular position how much liquidity is being provided. 

If you were in a Uniswap V2 world, just like the constant product, you're providing liquidity, regardless. You don't really care about where you are on the curve, the liquidity would just be like a horizontal line. 

But with a v3 concentrated liquidity, you can see that it actually changes because you can choose where you're providing your liquidity. So, you get this. I don't know if it's called a phenomenon. 

You get this visual where the liquidity depth actually kind of chases toward the exchange rate of wealth and USDC because you want to make sure that the liquidity that you provide is actively being used, and then you're making money as a liquidity provider. 

You can kind of see this fun visual that's playing in front of you. And then maybe later on, when we have Austin in the second half, he can further elaborate on the concentrated liquidity, but there is a beautiful visual, I enjoyed watching it just as a ghost. Awesome. 

So yeah, those are the four types of ultimate AMM algorithms that we went over. We're going to just go over those today. 

Okay, so up next. Let's see. We kind of went over the right terminology. What are these things like ducks? Hmm, like the constant product? Concentrated liquidity? Let's just see a little bit about how do we start doing some on-chain analytics with the knowledge that we have. 

We're gonna go to this Dune dashboard that's complementary to this talk today. Okay, so this dashboard is really meant to serve as like a beginner for you. We’ll show you some examples that you can do to have AMM analytic city.

And yeah, it's definitely not meant to be comprehensive. But today, let me make this bigger. Oops, sorry. Today, there are three metrics that we're gonna go into. 

How do you calculate the total value locked? How do you calculate the fees for LPS? And then how do you calculate the volumes? Then, before we go into a query that kind of shows you exactly how to do this, let's just make sure we're on the same page about these terminologies. 

Okay, so total value locked or liquidity, basically, is how much liquidity or the total token amount in US dollars that is currently present in a pool. If you think about the ETH USD Z pool, it’s basically just some of the current in the poll in USDC terms (in USD). 

You then sum up the USDC amount and convert it to USD terms, and then add the two together. And that gets you liquidity, right? 

So basically, to calculate the liquidity or the TBL, you need to first get the token deposits into this pool, then you get the token withdrawal from this pool, and then you net the two. 

You put the deposit minus the draw, and then adjust whatever the token amount that's left with the dollar price, and then you'd get the result total ballot locked. 

A bit of simplified mathematically speaking, you've got the token, a deposit, minus withdrawal of the token, an adjusted USD price, and then do the same exercise for token B and then sum the two together. This is a simplified version. 

That's conceptually how you can get the liquidity or TVL. And then in terms of fees, yeah. So, the way you can calculate fees is actually not very hard. What's harder is conceptually talking about fee returns, but we're gonna just get to it right now. 

So, we were talking earlier about a liquidity provider providing liquidity to these pools, right? You are rewarded for providing your liquidity because you can use the liquidity that you have elsewhere. You can either hold it right or invest it somewhere else. You're being rewarded for choosing to provide liquidity to the pool.

And the way you can make money — so people come to this pool that you provide liquidity to — they come and conduct trade.

The pool actually charges a fee. So, in Uniswap V2’s case, it's just like a flat fee of 0.3%. There are actually more advanced fees like in V3 of Uniswap. You can decide what feature you want to provide. 

But basically, you get the fee percentage that's been charged. And then, the way you can get the fee is you use the volume that flows through this four times the fee percent for a specific period. Let's say 24 hours, one day, seven days, whatnot, and then you get the fee for the LP for that particular period. 

And then you might be tempted to think, “I really want to know about the annual percent yield, return for now,” like everybody kind of wants to know, but that's just not a really good measurement for AMMs.

The reason being right, like the fees, which are your returns for that particular period. It fluctuates all the time, and it’s dependent on the volume that flows through this pool, right? So, that's problem one. 

And also, in general, when you calculate something, right, you've got like a principal that you're dividing by, right? So, here is our liquidity that's available in the pool which also fluctuates all the time. 

So you can't really get a good handle on APR. But if you insist on having some sort of annualized metrics, we can do this. We just take the 24-hour fee and then annualized it to one year, and then divided by the liquidity to kind of get the APR. But again, this is just not really a good way to measure. 

If you resist, you can also do this. And then the third category of measurement, we're gonna illustrate today is volume. So, with the volume, we're really talking about how much trade flows through this port, right?

And then traditionally, when we think about the volume, we think about it and the US dollar amount. So, how much money was traded in this hole in a particular period? 

But I just want to highlight…I always like to think about volume in two ways…not only in the dollar amount but also in the number of transactions. 

Why is that? Because if you think about it and just US dollar amounts, it's a little bit problematic because when you get to the dollar amount, you're actually adjusting how many tokens are being traded by the price of the token for that particular day, let's say. 

So, it’s kind of like you’re colluding the metric with the price of that token. So, if the price of the token goes up or down or really affects the dollar volume that flows through this pool… 

Another angle to look at volume is the number of transactions how many trades were being done in this pool during a particular period? It’s just like two different angles and looking at the volume, I would say. 

But yeah, without further ado, let's dive into a query and see how exactly we can conduct this. Okay, so let me just minimize this. With this query, the problem statement is we want to see Uniswap V2 on the Ethereum chain. The top pairs liquidity with the fees. 

So, one more time, we're looking at Uniswap V2 on the Ethereum chain. We're finding the top pairs on the use of the BTC theorem chain. 

And then we're gonna see how we can calculate the total value lock where the liquidity and the face.

So, number one, how do we DeFi the top pairs? Here, we're going to define the top pairs by the volume, the USD volume, and then narrow down the top pairs by let's say top 200. Okay, cool. 

So I'm not really going to run it in this session, but I'm going to just walk through line-by-line through the exact story. And then the story actually covers all three concepts that we were just talking like the volume, the fees, and then the liquidity. 

Okay, so first things first, we are going to get all the Uniswap two pairs ranked by USD volume in the entire history. How do we do that? We are going to do our normal select right, and then from what table, we are using this beautiful dex.trades Spell. 

Okay, so and then filtering as normal, we're going to do one equals to one just to set it up correctly. Not correctly but I like to set it up this way. 

Filtering to optimize for this time, we should, if possible, give it time. So here, we know that Uniswap V2 start date, it's the contract that was deployed on May 5, 2020. 

We can filter by the block time that way. And then we're looking for Uniswap as a project, and the version we're looking for is to filter out anything weird. We're going to just make sure that the amount is not equal to zero. 

Okay, cool. So, we've got our filtering down and the columns. Are we selecting to get the pair's by the total volume?

So, we first need to grab the current project contract address. That is pretty much the port address that you see here. 

And then next, we are going to grab the token pair that is basically the full name (like the illegible). You can actually know what is going on in this pool. And if you are wondering, why am I doing the case statement? 

So there are some reconciliations that need to be done on the source data side. In these two cases, the two pool addresses correspond to two different pool names. 

Here, I'm just manually reconciling the pool name before the source data gets corrected. So, we're just grabbing the pool address in the pool name. 

And then we're summing up the USD amounts that are being treated that way. Those that flow through this pool as the total USD, and then we need to rank them. So we're just applying a rank function. 

We're doing a rank-over. And then we're ranking by the sum of the amount of USD that flew through the pool in descending order because we want to grab the top pairs, right? And then we're naming this as the pair rank. Awesome. Okay, cool. 

So, we set up the all pairs in Uniswap V2, and then next, one more thing I should have done here I probably should have filtered by blockchain equals to Ethereum. 

That's something I should have done. But yeah, so once we get the pairs ready, the next step that we can do is to use the pairs, right? So here, we're now selecting again from this temporary table, the CTE that we just created called all pairs. 

And then we're just grabbing the top 200 pairs that have the most volume. And we're just grabbing the pool address, the pool name, and then the USD volume to use it for later. Okay, cool. 

So we've got the top 200 pairs. So now, we're going to grab some supplemental information here. What are we doing? 

We were saying that, in order to get the total value left, you need to adjust the token price to the current US dollar amount somehow, right? 

So you need to have the knowledge of how much this token is worth at the current moment. How do we do that? We have another beautiful Spell this table that we can use — prices are USD (latest). 

In this table Spell, we have the token with its address, a symbol, and then the decimal for that particular token. So, with Ethereum, the decimal is 18. Right? 

You need to know the decimal to adjust the role token to what it should actually be. Then beautifully, we have the price for this token. And then here, I'm just filtering for the token on the Ethereum chain because I'm only looking at the Ethereum chain stuff right now. 

But yeah, so now I've got the latest USD price as a temporary table for me to use. Okay, so now, let's get to the very exciting part. We can now actually start netting the deposits and withdraw and get to our total value locked. 

Okay, cool. So, mathematically speaking, how do we get the total value locked, right? You plus the deposit minus withdrawals? Right. So first, to get that we need to use this other table spell called ERC20 underscore Ethereum dot EBT underscore transfer. 

So, what this table gives you is…okay, so when you transfer a token…it should emit an event to the blockchain node operator, that's listening. 

“Hey, so and so, from this person to this person, how much of this token is transferred at what time? So, this is an event that's being broadcasted?” 

And then if we listen, and utilize the events that are being emitted we will know? 

Yeah, how many tokens are being transferred from where to where? So, utilizing this table, we will be able to find out the token flow for this particular contract, right? Okay, so we're using this table all while we're doing a left join on the top bears because we only care about the relevant polls, right? 

We already paid for 2200 posts that we kind of care about. So, how are we joining the two tables? We are doing a join on the token pairs project contract address. The pool addresses are equal to the events transfer — we're calling it D for deposit dot two. 

So, what are we doing here? We have a pool, right? And then the pool has an address, and we're finding all the transfers to that pool. As a liquidity provider, when you provide liquidity, you are providing liquidity to that particular pool. Right? 

You're matching up the pool address with the event that sends stuff to that pool. Make sense? Cool. Okay. So, once you've got this joining working, you can just find the relevant columns that you want, right? 

You want the contract address, which are the pool addresses, which I'm calling LP contract address here. And then you got the token payer, which is like the name that's more readable. 

And then you've got the token, right? If we have a WETH USDC pool, then that token would be like the address for WETH and the address for USDC. Right. And then, actually, we can… 

Yeah, I'm not even gonna talk about like the LP, but yeah, just like different tokens that get sent into this pool. You got the value, and you're going to get some of the value of the token that's being sent to this pool as the token deposits. 

Right. And then just to optimize a little bit, because your ERC-20 Min transfer table was quite low, you can filter it by the time, so you know that Uniswap V2 got deployed on this particular date. 

You don't care about anything that happened before this date. You can filter by the state to optimize a little bit and then this group buy because we're just summing up the token in the entire history that's deposited to this pool (and only this type of token), right? 

Now, here with this sub-query, we have this view that we temporarily created. We've got deposits into each of these, like top pair pool addresses. Awesome. Cool. 

So next step, what we need to do is find the withdrawals, basically like the same thing, except now instead of matching it on the two, you're matching it from y because you're taking away money from this port, right?

You're looking for the event transfer and looking for the withdrawal event that actually pulled liquidity from the pool. So, you're matching up the pool address with the form in the transfer event. 

And then other than that, literally the same thing, get the pool address, the pool name, and then the particular tokens address. And there are as many of the particular tokens values as the token withdraws. 

Awesome, and then we're now at the point where we can do the net. So, the net (as it implies), we're just netting up the deposit or withdrawal. How are we doing that? 

Right, so first, we are joining the deposit and withdrawal together on the client LP contract address, basically the pool address, and then also on the token equals to the token right because there are different types of tokens in this particular pool. 

If you're wondering, why am I doing a left join, an inner join, or a civil left join? You might notice sometimes with your own wallet, people just send you random stuff, right? So here, there's just a way that I find this easier, it's just like filtering out all the irrelevant deposits. 

Because normally, if you have a big pool where there are deposits, there should always be withdrawals. 

So yeah, just INNER JOIN to filter out irrelevant information. And then also here, we are doing like a left join on the price that we previously were getting as supplementary information. 

So, we can get to know the latest USD price for a particular token for us to use when we find the total value locked, and then here, I'm doing a process where the price is not just for now. I'm filtering out when Dune doesn't have…when filtering out for only tokens…where there’s a price feed that largely works for these like top pools. 

But if you're actually interested in investigating smaller posts, there are ways that you can get the price without the price feet as well. It's just a little bit more complicated. But yeah, that's out of scope for the current query. 

So, coming up here, once we set up where we're grabbing the information from, all that is left to do is actually grabbing the negative amount. What are we doing? Just pretty regular stuff…like the contract address, the pool address, the LP per name, the pool name, and then the token deposit, and a token is withdrawn. 

Okay, so in the next part, we are plus the token deposit, minus the token withdrawal as the net token. Why are we doing a coalesce? Because sometimes if the withdrawal is not right, you don't want your equation to fail. 

The coalesce basically is a function that's being applied, it will take the first non-null value as the result of this function. 

So here, if the withdrawal is null, you will put zero as the token withdrawal. So, that's your net token. That's the roll amount for the particular token. But as you know, we alluded to the way that blockchain stores things. They can't really do decimals. 

In order to achieve the decimal effects, they just pad with a bunch of zeros. So, there's some information out there that you can get to tell you how many decimals you should adjust this for. 

A lot of times, it's actually like 18 decimals, but it could be different. So with the same table, actually, this price table (USD price table), it tells you that decimal for the token, right? 

What we can do is divide by the power 10 to that particular decimal to get the actual token amount, and then once you find that, one more step you need to do is just adjust to the dollar price, so you just time the price for the token that we already got. 

And then, that's the net USD amount for that particular token in this particular pool. Amazing, cool. And then just one more step that we can get the total value for this pool, we are going to skip this part and come back a little bit later. 

But the final step of this query. So, pretty much we've got the NAT table, right? Then that temporary table that we just got equals to plus deposit minus withdrawal. And then with that, we can then sum up the net USD. 

So, like all three different tokens in this pool, pretty much. And then that would be the total value locked in USD amount.

And then we're just grouping it by a pool like a post address or post name, so the group I wanted to. Then, here, just so we can like easily click, click. We're creating an information link, so you can go to the pool address directly. 

I honestly always copy/paste the syntax. But the syntax lets you achieve the result where you click on the thing. Okay, why don't we just show that if you click on it, and then you can open this actual Uniswap UI page associated with that poll? 

So, you can check whatever information or start training if you want to. Yeah, so that is how you get the liquidity, aka the total value locked. And then just to finish up with the fee side of things. Let's go back to the volume. 

Okay, so we were talking about terms of fees, right? We need to define a period. So here, we're going to use the period as  24 hours, and then we're going to just sum up the amount of USD that's flowing through this pool. Again, we're going to use the decks that trades spell that we have readily available. 

There are definitely other ways that you can do this. But we already have a simple table for us to use, so we're going to do this. 

Yeah, so pretty much you are just trying to group by each of the pool addresses, and take the total amount that's flowing through in the past 24 hours, right? So, block time equals two, now, minus the interval of 24 hours. You really care about Uniswap and only the two. 

And then one more thing, we only care about the pool address that's already being selected, right? So, we did all this work to make sure we only care about these two pools that we already calculated. 

So we're going to just select these pool addresses that we are concerned about, and only look at their one-day volume pretty much. Yeah, we first get the past 24-hour volume and USD. 

And then, this is the one-day volume sub-query or temporary table. Right. So, we are joining the knots table that we previously did with the one-day volume table right? What contract address equals the pool contract address? 

So, then with this, you can calculate this with Uniswap V two. You know, you can assume that it's like a 0.3% fee, so all you need to do is some of the one-day volume and time and then multiply that by the fee. So, 0.003 is the last one for our fee. 

And then, if you insist on getting some sort of annualized rate, you really shouldn't. But if you really insist this is what you can do. 

If you annualize it, it would be just tiny by 365 and then you divide it by the liquidity which here we are already summing up the net USD volume, and then you get some sort of percent. So, this is the result that you are doing. 

Okay, cool. So I will. And now if you have any more questions, feel free to like reach out to our Discord channel and stuff, but I'm gonna turn it over to Austin. 

And let Austin help answer any questions you guys have with Uniswap with concentrated liquidity and whatnot. Okay, cool. I will bring Austin on.

Austin 40:37  

Hi, everyone. Yeah, happy to be here, you get to see my really cute NFT and my cute little Uniswap Labs etching. So, I guess livid about me, I am a researcher at Uniswap Labs. On four, I worked at Uniswap Labs, I was a researcher at the Federal Reserve and liked economic modeling and things like that. 

So, I've written a few papers on Uniswap V3. I talked to Dune Khan as well on Uniswap V3. And I have another blog coming out today. So, I look forward to sharing that with you guys. Let me know if you've any questions. 

I love chatting about Uniswap. I chat in Discord a lot with people about their Uniswap questions, so I'm always there. 

But I am happy to sort of answer any questions that you guys have. It could be about Uniswap. It could be about like data. It could be about…I guess anything, as long as it's like a leak or any. So, feel free to ask me a question.

Jackie  41:42  

Well, thanks for being with us here today. I always wonder, actually, like, how was the concentrated liquidity idea born? I mean, why should we render? It seems like oh, obviously, but how? How did people actually think about it?

Austin  41:59  

Yeah, I think again, Robinson and Noah came up with Noah and the V3 team came up with it. I think it sort of makes sense. If you think about how a hybrid AMM order book would come about as sort of natural to merge these two ideas and just sort of get the best of both worlds. 

I think that was sort of why I think that in the team. We always are just sort of riffing on ideas, what we can do, and what can we think about. 

And I suspect that just came from one of those riffing sessions where they're like, “Well, what if we just let people decide to put in a range?” 

And then fun things like that. And then they talked about it then. I wasn't there, though. That was before my time. Sadly. Got it. Got it. 

Jackie  42:50  

So creative stuff…let's see…I have a Dashboard Background, and it's very useful. Someone wants you to explain a jet.

Austin  43:03  

Well, yeah. So, I actually have a blog about…well, I co-authored a blog about jet. I don't know exactly what part of it you want me to explain. But I think that this sort of blog…I don't know how to put it in.

Jackie  43:20  

What's on the right-hand side? Oh, I can do it. Alright.

Austin 43:24  

So yeah, just in time, liquidity is where one LP sees a swap in the mem pool. And in response, they meant to position it to ensure that the only person trading against it and the other person, and then after that swap goes through…they sort of burn their position right after it. 

So they're like the only person who trades against that one swap. We think we quantified how much there has been, and it really hasn't been as much G liquidity as Twitter wants you to think. It's less than 1% of all the volume that has ever happened on V3. 

It's not like it's not growing. It's not like economically feasible for it to grow, either. There are constraints on the returns of a strategy. And so it's not clear. If gas fees were zero, they would have gone up. 

It's not quite what happened. So, we don't really think about it too much. But I had to make the blog because I was tired of people talking about solid information…

Jackie  44:37  

And I think your Dunecon talk was on Jet and examples. Right? 

Austin  44:41  

Yeah, we have a Dune query. We actually wrote it in Dune. So if you want to see a pretty complicated query, I made it about a day or two today. But it's a jet query, and it's about how it works. 

We sort of quantify it severely. If you didn't like looking at how jet works and how you think about it, this is what it's all about. Yeah. So you can look at it pretty long, but it's fun.  Yeah. 

So if you go back to the GIF, I've gotten so much use out of that gif. It's great. It's from one of our research pieces that are coming out in the next few months. 

Those spikes are limited orders functionally. You would think if an order book or someone put just one token in me then Uni V3 pools. When they get crowded and the price goes through that sort of big, big spike, they get the other asset. 

So, this function is similar to like a limit order and limit order book. It's pretty interesting that people do that. There are actually not a lot of them. 

And I think only a few billion dollars have ever been traded against them. But yeah, so people were trying to sort of limit order book style trades on V3, which is sort of why concentrated liquidity is a good innovation from an AMM because it gives you some of these order book properties. It gets you some of these properties from the AMM as well that you sort of know and love.

So that's sort of what those spikes are in the GIF. What is the Uniswap V3 position? NFT? Yeah, so I think the team was trying to position NFTs in a way for us to manage these really complicated internal V3 variables. 

It was sort of an attempt to grow from ERC20s that we're all the same sort of baking. Like how your ticks were positioned and your own liquidity and hyper positions were sort of different and had different variables. 

So, that NFT just represents your position. Some are similar to an ERC20 LP token and Uniswap V2. And it's just sort of ease of use. You actually don't have to use the NFT manager. You can do it without it. But usually, people don't really complicate it, and it's very easy to lose our money. 

Usually, people use the NFT manager just because it's sort of a convenience. And it's easy. My cat thinks I'm talking to her. She wants to say hello. But yeah, she got me off there. 

Yeah, so you don't actually have to use the NFT manager. But it's really commonly used, which is sort of just an easy way to represent positions in Uniswap V3.

Jackie  48:04  

So, that you don't have to use NFT ever…

Austin 48:07  

It's sort of interesting. You don't actually have to…there’s this internal way that the pools will represent your liquidity. 

And all the position manager is doing is…it's rare. It's like taking that internal representation and making it into an NFT for you. But very few people actually don't use the NFT because it's sort of like…

Jackie  48:32  

Yeah, I didn't even know that you could just not do that.

Austin  48:35  

You’ll have to see it sometime. Yeah.

Jackie  48:38  

We got another one for you. Do you think it makes sense for Uniswap to have its own chain —  the Unichain?

Austin  48:45  

I'm not sure. I think there are benefits to a uni chain. There are drawbacks to a uni chain, just like any decision that's made. I think that will be interesting. I don't know what we'll do specifically. 

And I haven't really heard any plans, or I don't know, I'm not in those talks. But I think that it'll be interesting to see when dy/dx is functionally doing this. 

And they're doing a chain of their own, sort of giving back some of the volumes that they create to their token holders. And it'll be interesting to see how that goes. 

I think it'll be an interesting place in the future of what happens like looking at sort of these smaller masses…smaller DY/DX…really large but well…there'll be a lot more of these app chains. 

It'll be interesting to see what happens to them, how they work, and how they function.

Jackie  49:39  

Makes sense. Makes sense. Something else I wonder is if someone asked you…how do you think about return to Uniswap? Like what? What is your mental model if someone asked you to? Can we talk about returns for AMM or Uniswap?

Austin  49:56  

What do you mean by returns?

Jackie  49:59  

Yeah, I guess just like what do you think? Like returns as an LP? What is your mental model? What do you think about digging into the challenging data to find out?

Austin 50:09  

Yeah, I think it's easy for one position. Um, I guess like, there are a lot of papers out there about a permanent loss on…there is not a lot. 

There's one paper out there about like an impermanent loss on Uniswap V3. And like, I don't think that it's a good metric for every single position. 

Because every position is grouped together, you don't actually understand what all of the determinants that were going into that position were like. What if they were borrowing? What if they were hedged? 

Like, it's theoretically possible that someone lost money on a Uniswap E3 position, but if you take into account the hedges, they made money. We see this quite frequently in traditional finance. 

You go to FTX when they have their position. They have their P&I leaderboard. All of the firms that are at the bottom are huge like Wintermute and things like that because they're losing money on FTX. But they're all hedges. 

And so if you take into account their other positions, they're making money. That’s why we think of returns against a permanent loss is difficult. You don't actually know what's happening off-chain. You don't know what's happening and other positions. You don't know if it's a hedge. 

And we think of that V3, these range properties, the payoff of the position, which is very unique. A lot of firms use it as hedges, essentially. 

It’s really hard to talk about returns because you're looking at one slice of a huge book. I think it's possible to do it as a personal person, and I use revert finance. I will shout them out. 

Because you understand your own payoff. You understand what you paid. You understand your hedges. You can do it yourself, but it's difficult to do returns on a wide scale because you don't actually know what everything looks like. A good example with the spikes and liquidity distribution that we see in this GIF… 

Like those positions touching me lose like millions of dollars because — if you there's a trade — it's a functioning trade. It's not really a position to be taking into account all of those limit orders like they lose hundreds of millions of dollars. They’re not really losing hundreds of millions of dollars. 

They never intended to be an LP position to make money on fees. They're intended to be a trade. And so like that's why this paper by Bancor is difficult to think about because we don't really know what I was trying to do. 

We’re accounting all these v2. Everyone's doing the same thing. They're all trying to make more money from fees. But in V3, people can use LP positions as options. 

They can use them as hedges. They use them to sell them in orders. It's so much more complicated and the payoff structure is so different. To lump them all together and sit, they're all trying to do the same thing.

Jackie  53:11  

Got it? Got it. We've got a question. Another question? Should everlasting options be used to hedge impermanence loss?

Austin  53:20  

I don't know about everlasting options. If I'm honest, I think a good example of something that can be used to hedge permanent loss is the following…

There's a paper by the a16z team who was close with about lever, which is an alternative to impermanent loss. It's about how… if you were hedged perfectly…what would your gains be on AMM. I think that's more of what I would expect. 

Like I think a permanent loss really misses the full picture. But I think levers are better as a lot of industry practitioners were talking about lever instead of impermanent loss. 

And there are places like Panoptic, which is an options protocol coming out of Uniswap V3 that let you sell your impermanent loss functionally as options. I think that's interesting. I don't really know about everlasting options if I'm honest. 

So, I gave you two alternatives because I don't actually know. But they'll probably work. I don't know what the payoff curves look like, honestly, for everlasting options.

Al 54:27

One more thing before you leave, I said a little bit about your blog post. Could you tell us a little more and where we could find it?

Austin 54:35  

Yeah, so it comes out…and actually, I've heard the imperative before this meeting. We like to launch I guess. The next blog post is about TWAP oracles on Uniswap V3. 

Oracle's are a really hot take…like a really hot topic right now because it is proof of stake. The way that validators choose transactions is really new and complicated. And it's theoretically possible that a validator could try to manipulate to swap. 

And it's much easier now. If you have a large market share for a validator, like a large validator share, we calculate how possible it is. Like how much money you wouldn't need and who could really do it. 

It’s much more difficult than people think it is. There's a wide range of liquidity and a lot of big pools. And that wide range of liquidity makes it infeasible to like people who have your low market share.

And you need like 5% of valid market share…like two people have that. So, it's unlikely that those two don't want to build systems where it's possible to manipulate it. But there are two people who could do this, and it's Lidar and Coinbase. 

And they're not going to do it, just because like, why would they? But I think like we also talked about innovations for Oracle's and how you could build Oracle's for a POS environment where it's resistant to manipulation entirely. 

Yeah, it's a fun blog post. It's 25 pages.

Jackie  56:24  

Where can we find it? Why should we be on the blog?

Austin 56:28  

So, it'll be in the same place as the JIT liquidity? Just like I will tweet about it. I will so follow us.  

Yeah, I will put it out there. So, it's very long. It started out with like six pages on my face. Actually, I was a sick patient on my flight to Berlin for Dunecon. Oh, wow. 

And then it just grew and grew and grew and grew until it was the monster that it is now. So yeah, it's an interesting question to talk about. So, I am looking forward to putting it out and getting comments on it.

Nice, nice. So, Mullen Gusa found a fun optics protocol. It’s genius. It is spike ‘em Lambert. He's a very, very smart guy. He's a professor of physics at Cornell. And he's like, yeah, he's really good. And he has thought about it. And he's a big LP. 

So, he's like, “Why can't I sell a lot of these payoff curves to people as counterparty?” 

And he's like, “I'll just make it.” 

So, he's really good. I like one of the projects that I'm most excited about. I think they're launching pretty soon…

Jackie  57:50  

I don't know if we have time. Is this a one-minute question? How do you price your time?

Austin  57:55  

So, I've been reading a lot, and I'll be a little late to this point. How do you price me? I also think the market does it I guess. Like you don't actually have to worry about it I think with Panoptic. It's nice because the market itself is price. 

What's an impermanent loss calf curve look like? This is a complicated question. In general, I think you have to do some acid volatility, modeling, and stuff, which is a little complicated. 

But the good part is the market gets to price it. If you're wrong, you lose money. And if you're right, you make money. So, I think like for Panoptic, they'll be models and they'll be people who learn from the protocol to figure out how to price criminal laws.

AI 58:43

One question for you….want to figure out what you think about the future of AMMs? Do you see concentrated liquidity becoming the de facto way to use an AMM? Or do you see a market still for a v2 base…just a normal price curve?

Austin 58:59  

Yes, I think that concentrated liquidity can be a V2 stat. You could have a V2-style pay-off curve on concentrated liquidity. 

And I wrote a paper about how many pools are actually better to do…I don't know what we’ll call it…passive or it's wide-range as possible…I started a V2 style pay-off position on V3. 

Many of those are actually better than they are on V3 than they are on V2 because of this increased amount of volume that concentrated liquidity comes with and that contrary, liquidity brings.

So, I think that there's a world, and I think that the world will be concentrated just because you can have a V2 style pool in V3, but you could have people who are willing to trade at a more concentrated price. 

I think everyone benefits from that. V3 or V2 is one to trade on because there was not very much like there's a lot of price impact and a lot of the liquidity was not being used. 

But like V3 allows us to get rid of that. It allows people to still use V-style payoffs, but it also lets people concentrate on who is more sophisticated or why one takes more price risk. So, I think that the world is gonna become concentrated. 

I think that there are places for more sophisticated passive market makers like Arrakis and Gamma and X-token who do a lot of the complicated rebalancing. And a lot of the management for you is like a passive liquidity provider. I think that will grow. 

Because you get the benefit of concentrated liquidity, but also you to be passive. So, I think that will be the next big innovation. But I do think girls concentrate. I think it's just like better in every single way. 

It really is. I mean, it's better for traders. I've shown them. It's better for LPS, and it's better for one, so would you use it if it's better? Complicated? 

I'll try. I understand. I understand why people don't want to use it. It's really complicated. I understand that. 

And I think that's very fair. So, I think that we're trying to talk about how to use it. I have another blog post coming out in two or three weeks about how does Uniswap V3 work? Like, how is the math all work?

Jackie  1:01:38  

Yeah, amazing. I will be reading all about math. 

Austin  1:01:41  

So, hopefully, I break it down, so a lot so people can understand it. That will be very helpful, I think. 

I think this sort of content where we're trying to help the community, as a whole, will be very good. 

Because they'll figure out how it works, and they can appreciate the beauty of constrained liquidity. Though I understand, it's very complicated. I understand why people don't like it. And I'm very…

Jackie  1:02:06  

We're gonna get there. We're gonna get there as a Web3 community. Yeah, we will be there. Yeah. Well, thank you so much for being here with us today. So, follow Austin Adams on Twitter for more info. 

And also subscribe to our Dune YouTube channel if you enjoyed this type of content. And if you want more on-chain data and other sponsored content in the future. Lastly, join our Discord to get access to thousands of other wizards who are doing this type of work every single day.

Austin  1:02:37  

So, I will help you with your Dune and your Uniswap questions, so I will be there to help if you need me.

Jackie  1:02:44  

Amazing, cool well, right. That will end today's Arcana. Thank you for coming.

Austin  1:02:49  

All right. Thank you for having me. Bye bye.