Introducing Uniswap v4

ETHCC 6 · July 2023 · 25:45

Alice Henshaw and Sara Reynolds introduce the ongoing development of Uniswap v4. They review the protocol's evolution and discuss its design and notable technical features as planned at EthCC[6].

Speakers

Topics

From ETHCC 6, hosted by Ethereum France.

Transcript

hi guys I think we're gonna go ahead and get started uh welcome to uniswap the Vampire Slayer subtitled The Curious Case of Disappearing vampire attacks um so for today I wanted to have a little like spooky unicorn and I asked Phil on our design team to give me a spooky unicorn so this is what we ended up with um because the other ones were a little bit too spooky shout out to Mid Journey for these unicorns um so if you remember nothing else from today hopefully you remember this um but no So today we're mostly going to be talking about V4 which you might have

heard a little bit about um but who we are I'm Sarah and this is Alice we are uniswap Labs protocol devs um so we're gonna give you um kind of a cool narrative about V4 but we'll also dive into some of the technicals here so kind of bear with us through that um uh sometimes you can find Alice and I uh matching occasionally um unfortunately we aren't today um but besides that um yeah again we're developers on the protocol team at Labs um so it will get a little bit technical but um yeah bear with us um all right so what are we talking about today

um really the kind of overarching theme of this talk is about how uniswap V4 is really going to be something we see as kind of fast tracking amm Innovation by making kind of you know easier making it easier to build on top of and making it safer and and kind of faster for developers to be able to kind of Leverage the network effects of an amm so um to tell you really the story of how V4 came to be and why we've designed it in this collaborative way that we will describe we're going to go back in time not to the future but to the 18th of May 2020 which is the day that uniswap V2

launched on mainnet there's a few things that people remember particularly when they think back to like the V2 era um it was known at the time for introducing on-chain Oracle so that other D5 protocols could plug in get the price in a decentralized way and also for various other improvements that it brought to the code over previous version of uniswap how did it do well you probably know some of this already it did very well by the end of 2022 by the end of 2020 sorry it had done nearly 60 billion dollars of volume and today it has now done nearly 450 billion dollars to date

um one of the other things people remember about the uniswap V2 era in time was uh well let's see if you can guess it we've got pancake swap we've got quick swap and there's more and more and there's more than just these these are just a set of examples uh there was a lot of unit swap V2 forks and people talk them about them in a positive way a negative way there's a lot of discussion around forks and what they do for the ecosystem but uh we want to talk about some of the positives of folks today um the reason people Fork a protocol is because they want to add new features they want to add optimizations they see something in the original protocol that

they want changed for some reason or in some way and that's Innovation it's it's how we improve on amms it's how we improve on other protocols too and it's it's seen people adding cool features that users want and really whether the liquidity and the volume moves between these amms is how you see was at Fork successful is this new amm successful does it have the things that users are looking for um so are there any downsides to folks well there's a couple but one I want to touch on today is security when you for someone else's code there might be you might touch one area of the code with your changes and it might affect another

area of code without you realizing it because you might not know the original code 100 well and there'll also be nuances in that code of decisions that the developers have made that you might be unaware of that might lead to um you ending up introducing a bug by accident um so we've got a couple of examples the first example is a router that was a fork of uniswap that added some cool new features but they accidentally removed a check that the caller of their router was a poor a legitimate pool of their protocol and what that actually meant was that anyone could through sort of jumping it through Loops could take any users funds that had been approved on that router

a second quick example is uh a fork that tried to add some farming of uh rewards for liquidity providers and so they added some logic around how LPS get their rewards and they accidentally made it so that LPS couldn't get any rewards at all in there in their Fork um we don't want to sort of stress for a long time on these security issues folks can do amazing things and innovate but they can also increase risk and we'll jump back to why we've talked about this a bit later but um yeah that's what we want to say about folks today great so fast forwarding now to May 5th

2021 uniswap B3 comes out this is a really kind of breathtaking moment for amms and what people might remember from this is the kind of introduction of concentrated liquidity so you know this Innovation really gave LPS this new kind of expressiveness to where they could provide liquidity on that kind of price range and how did uniswap V3 did also uh you know really well in the first week there was 10 billion in volume cumulatively around 1 trillion in volume and just to kind of give a quick kind of polls check on really this power of concentrated liquidity on the left here we see a snapshot of tvl and volume on V2 on the

right is V3 and what you can see is even just you know a few months after liquidity had migrated V3 is doing triple the volume of V2 with half the tvl so really really powerful kind of level of expressiveness that also was able to give swappers some price Improvement um but you know the stock is not about V3 you know you can go read the white paper it is absolutely nine pages of magic and sorcery um and and really I highlight this because there was a lot of really opinionated you know design decisions in building V3 and today I want to highlight two um you know obviously people remember concentrated liquidity but there are

lots of other changes from V2 two of those changes I'm going to talk about are the Oracle and then fee tiers so let's start with the Oracle what changed with V3 oracles um first is that it is a geometric mean Oracle so in V2 you saw an arithmetic mean Oracle won't get into the details here basically the point is is that there's lots of different kinds of oracles different projects might have different opinions about you know what the best Oracle on-chain Oracle is and V3 enshrined this sort of geomine Oracle what else changed uh so in the uniswap

V3 code there are saved and shared accumulator values that are used by integrators that want to build an Oracle and so when I say shared accumulator values that also means that there is some kind of shared cost to put onto swappers of actually going and updating these values every time a pool is touched and just some like little kind of quick math here the Oracle gas overhead is conservatively around like 5 000 gas every block if we assume that a pool is being touched on every block in a year that's around seven hundred thousand dollars in gas overhead and mind you know as a reminder this is you know per pool

um so you know oracles might not even be used on certain pools and so that is just kind of extraneous cost being put on the swapper um the second thing I want to talk about with V3 is fees um so it was quite an opinionated decision you know not only to move away from just one feature in V2 there are now three different um feeds here so we have five bips 30 bips 100 bips um and then on top of this there was also the option that enabled governance to add a fee tier to the protocol um so you know one yeah it was opinionated because there were three tiers and then also it was very opinionated that it was these three tiers

um and what ended up happening is that governance actually voted to add a um a one bit fee tier um and that though passed and it ended up being you know really successful for the protocol um you can see in just a matter of days if you look at the blue um the blue here that uniswap V3 the market share shot up um a lot making uniswap you know very competitive in these stable coin markets um and so the funny thing about this is that actually some early contributors and even Hayden himself thought that the one bit feature was not going to be successful and not worth putting into the protocol and so you know I bring all this up

because I think you know from what we've learned of seeing with V2 and and seeing kind of forks and then also seeing some of the opinionated decisions going into V3 we learned that the design space of amms is huge um and that you know there are you know tons of decisions and opinions being made and built into these amms and you know what we're here to kind of talk about with you more today as we dive into V4 is that kind of innovation happens when you push the boundaries of these opinions and you kind of poke in prod at what you know uh kind of the structure of amms so that brings us to a much more recent date June the 13th 2023 was the day that we

um released the V4 code on GitHub um a lovely V4 um there is also a white paper it's hopefully a little bit less scary it goes into what we changed from V3 what we've what we've built on and how we're trying to make V4 an ultimate platform of collaboration of the whole world being able to innovate on what an amm looks like and helping make these decisions in an open source way so what has changed with V4 uh two main things we're going to be talking about now is one that it is highly customizable and flexible we provide the the base logic in the in the

code but people can customize it um in an amazing way and the second thing is a couple of big architectural changes that we've made to make it more efficient and to make it even better as a as a platform for people people to build upon so that's those are the two things we're going to touch on now great so diving into the first point just about kind of the customizability of V4 how do we do it uh we do it with hooks so you might have heard this term a hook is really just a smart contract that is attached to the pool when a pool is created and to kind of see how hooks enable this

customization let's go back and kind of think about what are the various actions that kind of exist on an on-chain pool so um one you know there's pool creation that I just talked about there's obviously swapping one token for another there's kind of the lp side adding removing changing range and then newly with V4 there's kind of sending money to in-range LPS so yeah all of these different points are are various ways to interact with the core underlying protocol hooks play a part by interacting before before and after each of these points on a pool and again

hulks are the sort of external contract and at each of these points that code is going to be run and we'll kind of look at an example of what that might look like so if we have a hook that has the before Swap and after swap kind of callback enabled it's going to go through this diagram starting the swap checking that before swap flag running the external code coming back finishing the core swap logic checking the after swap running the after swap arbitrary code and then finally ending the Swap and so this is a really powerful primitive because it allows hooks to be able to really enshrine logic directly into the pool

um so let's look at an example this is just kind of a naive example but on V4 you can actually create pools that are enforced full range liquidity so something like a V2 style pool um so in this example we look at the before modify position hook callback it's actually just a few lines of code all we have to do is check that the tick lower and the tick upper for when you provide liquidity are the Min and Max tick that means you know liquidity is going to be spread across the entire price range and if that's not true we just revert and so you're not able to add liquidity unless these params are true and so you know this is kind of a naive example there's obviously a lot more you

can do here but this is going to be executed before any kind of liquidities added into the pool great so um I wanted to quickly touch on a cute little implementation detail with hooks um essentially these callbacks that I just talked about kind of the before and after swap before and after modified position um they are all determined by the hook address so we'll kind of look at what that means um this is a little bit of garbage right here but um this is uh the code in in the core protocol so these are all the flags that we have enabled for um you know the various hook callbacks that I just talked about

um let's say you know to give a little deeper of an example we can say that you know this is some address that we have deployed a hook at it's the address that we're going to use to initialize the pool um what's happening under under the hood is we're actually looking at the first byte of the address so this 2-0 we can see that that translates to Binary to this value here where there's a bit enabled at the third position and that means that the pool has a hook and at the before modified position flag so we would use this address to kind of do this example that I just went over a few slides before and what's really cool

about this is yes it's acute gas optimization but what's great is that when you initialize a pool you cannot change the points that that a pool calls back into the hook so those are immutable at the at pool creation time so that just means that we can hold certain invariants about a hook and about a pool for the rest of its lifetime so all that has to say is that hooks really allow you to build any kind of pool that you want so we just went over kind of this enforced V2 Style full range pool you can build limit orders you can build all kinds of oracles yield

bearing hooks I mean just really everything and switch um really I want to emphasize though um at the core you still get this same simple V3 style swap so hooks are totally optional and they just really allow more things to be done at the protocol foreign so hopefully at least some of that made sense and if you took away anything it was that we've tried to put in ultimate flexibility but maybe you're thinking having all that extra logic all those extra checks that sounds like it must be

expensive um well it can be more expensive and we've tried to counteract that and offset that with some architectural changes that we've also made um to the code so we're going to go over two of those now one of them is a Singleton contract and the other one we call flash accounting so in a traditional deck setup you have one pool for each token pair so if you want to trade usdc for weft that's one pool if you want to trade uni for die that's a different pool and there's a different smart contracts and that means let's say you want to trade usdc for die but the best possible price

you can get at this moment you need to trade via with and that happens in the background in one transaction but what happens is usdc gets sent in at the beginning of the trade weth has to get sent from one contract to another contract and then die comes out at the other end of the trade and this weft transaction in the middle this transfer costs thousands of gas to move the weft from one contract to another the second expensive thing about this traditional setup is that when you want to set up a new poll let's say you've got there's a token it's not yet on uniswap you want to set up a new pool for that token and weft you have to deploy a whole new smart

contract which can be a very expensive thing to do so that's quite a big barrier so now we have a Singleton contract in unit swap V4 all of the pools are within one smart contract and all managed separately within there and that means that when you do one of those multi-hop trades that's trading via another asset no tokens need to be transferred and that saves a lot of gas and then an even bigger gas saving is when you want to create a new pool this little bit of code gets execute all it does is store some initial values of the pool and it's 99 cheaper than it was on V3 to create a new pool which is awesome

um so this Singleton contract allows us to do greater things and the one in particular that we love is something called flash accounting maybe some of you have heard of flash loans where you can have an atomic transaction where if at the end of the transaction someone hasn't paid back their loan the whole thing reverts and it's like you never gave them the loan in the first place so with flash accounting at the beginning users acquire a lock on the contract they can then perform any number or combination of actions whether that's swapping between different pools that's adding liquidity um and the Singleton contract tracks all of the tokens going in and out of the contract overall

using this currency Delta and then at the end of the transaction when you're finished with your actions the contract checks if you've paid all of your debts and you've taken out all the tokens that you were meant to take out and if you haven't then the whole transaction reverts and it's like it never happened and if it if you have paid them then great here's a quick example um so the user requires a lock and that sets up your lock accounting you want to swap one where for usdc first so the user owes one width of the contract and the contract is the user 1800 usdc now the user wants to swap 1 800 usdc for die as like the second hop of that

trade well the user owes 1 800 usdc and is owed 1 800 die and those two usdc on either side can cancel out so it's like the user never has to know that that was going on and this is the final balance of that trade so you can now withdraw the diode you can pay the debt of your one weft that you wanted to trade and you can release the luck and woohoo you paid all your debts so the transaction succeeded and that's at a higher level a high level what flash accounting can do in V4 and that's enabled by this Singleton model that we have um the best way to do this is through uh something called transient storage which

a lot of you may not have heard of it's an EIP still at this moment called 1153 and what it does is its storage values that are specifically only going to exist for the duration of a transaction they're zero at the beginning they're going to be zero again at the end and so you can make that guarantee that no one's ever going to have to actually store those values at the end of the transaction and that allows us to make the code much cleaner and much cheaper from a gas point of view this EIP is as I said not yet on mainnap but it is slated for the Cancun Fork which should happen at some point um so why does all of this matter everything we've been talking about uh

we talked about forks and how they allow people to be so Innovative and in the same way uh with V3 people around the world had thoughts that they wanted to change about V3 and for some cases they did through governance and through others it was conversations that were had um but there's so many opinions out there of how things should be and we wanted to maximize the way in which the entire world as a community can can build what the future of an amm looks like and V4 hopefully encourages amm Innovation through this infrastructure that we've built so hopefully this is what the future of V4 looks like

okay um just to kind of go over a little bit of the timeline here um you know V4 is still in development we release the code about one month ago there's already been 100 issues and pull requests open which is really awesome to see we've gotten some high quality contributions what's also really cool to see is that there's already 30 contributors on the repo with merged code and just as comparison V2 had seven so really kind of embracing the sort of collaborative effort and kind of pushing the next amm forward and just to highlight a few really cool feature ideas from the community

uh there these are two specific ones kind of a pool no up for hooks an arbitrary call a kind of arbitrary dating pass data passing to hooks these are really really cool because they kind of open up a whole new kind of use case category for hooks something that we hadn't thought of so um you know kind of shout out to these community members for for these ideas um also you know as expected uh you know this protocol is still in development there are bugs to be found this was a really uh great submission from someone in the community um and you know helped us find these bugs quicker um

so that we can fix them um so yeah come come build with us check out the repo um we have a few different repos for V4 um on GitHub we'd love to hear your thoughts submit PRS and issues and also we'll be around today there's a few other Talks by a few other uniswap folks today and um you know we'll be walking around so if you have questions come get us and yeah thank you so much [Applause]