New Ethereum talks, every Monday. The week's conference uploads by event, in your inbox.

Loading player…

Hands on with Uniswap v4 - ETHGlobal Bangkok 2024

ETHGlobalThu, Oct 9, 2025, 12:00 AM

Join us for a workshop with saucepoint from the UniSwap Foundation titled, "Hands on with Uniswap v4." The workshop is a walkthrough of Uniswap v4 & hook development for beginners. ETHGlobal Bangkok 2024 was a 3-day hackathon that took place November 15 to 17, 2024, and brought together the most skilled web3 developers, designers and product builders from all around the globe for a weekend-long adventure to advance the Ethereum ecosystem! ----------------- Follow us: https://x.com/ethglobal Check us out: https://ethglobal.com/ Email us at: hello@ethglobal.com

Transcript

all right cool uh we can get started um hey I go by sauce I'm the protocol engineer at the Unis swap Foundation I've worked quite extensively on like the core code base of Unis swap V4 and today's talk is really just like kind of like an intro but also just me just spewing some fun and random ideas around Unis swap V4 um so this is me on Twitter I'm also known to be like this like ship poster on the GitHub um so if you have ever crawled the the PLL requests there's some there's some funny stuff in there uh okay quickly so V4 is more than just hooks um it's kind of like a big feature in V4 um but we have a Singleton design so what that means is it's a single contract where all the tokens are sort of held and custodi by the single contract versus like the V3 design uh this is a lot more gas efficient and because of this Singleton design we have the this thing called flash accounting or some people also call it like deferred balance accounting and the idea is like you can stack a bunch of operations that create like credits or debits and at the very end of the transaction you do that final token settlement uh whereas normally like if you were to swap and then add liquidity you would have to swap do token transfers and then add liquidity another set of token transfers but with flash accounting that all just kind of happens at the very end uh ERC 699 it's essentially a wrapper for ERC 20s um it allows you to keep your erc20 is within the core contract and allows you to uh efficiently trade value back and forth so it's really useful for like me Bots or like high frequency Traders uh the last or another feature is native eth so in V2 and V3 you have to wrap your ether in order to to trade eth but in V4 we Al uh directly support the native eth token also again for gas efficiency uh there's this new thing in V4 called donations and it's related to liquidity provision where you can kind of donate uh Revenue to inrange LPS it's a mechanism possibly for like value redistribution so that's another interesting aspect of V4 um and then the last C uh the last thing with V4 is like there are no fee tiers you can have uh all sorts of different fees like four basis points 22 basis points and then another area that we see developers engage with is really Dynamic fees this allows developers to freely Express uh and change the fee up or down there's obviously limits to it but again it's not hardcoded by the protocol at all I think there's some other amm out there where they support Dynamic fees but they have like some hard corded for uh formula to it but in v before it's all set by the developer and then lastly hooks which is kind of like the majority of of our talk today uh so what are hooks uh hooks are a way to arbitrarily attach uh valid solidity in the swap life cycle um the swap life cycle is more than just swapping right it's like the whole end to end process so like whenever a pool gets created like you could check and verify certain things or after a pool gets created you can kind of configure it um during liquidity provisioning and liquidity modification you can also run logic such as you know checks or modifications whoops um ah why does this okay cool uh sorry and then of course you have swapping so before a swap happen you could update the dynamic fee um you can modify liquidity to like you know concentrate liquidity um and then after a swap happens you could write like storage for like an Oracle and then also as I mentioned in donate I haven't seen a lot of uh hooks around donate so that is also like another interesting area to explore and so this is what Hooks kind of look like it's what you would expect so uh before a swap happens it does this check to see if the pool is configured for it um if before swap does exist it makes this external call to uh you know your hook contract so where it pulls off to the right that's a totally separate contract that you would Implement and then it returns back to the core contract to run the swap logic uh this is what the code looks like I'll do kind of a deeper dive but again like a hook is really a deployed contract that adheres to a certain interface where you can then you know add some some logic to the swap life cycle uh and then quickly like it's worthwhile to explain the difference between Hooks and pools um a hook is a pool but a pool is not always a hook is the way to describe it and so what I mean by that is like pools are simply independent unique sets of liquidity positions uh it's a a collection of two assets to trade against right uh pools have at most one hook contract um a hook contract implements multiple hook functions and for these hook functions like before Swap and after swap you can mix and match and choose as you want you know if you use before swap you're not excluded from using after swap you can you know you could use all of them you could use one of them it's totally fair game and and just to highlight like the like where Hooks and pools are related to each other so this is the initialized function so if you're creating a pool you specify the pool key and its starting price um The Pool Key which again uniquely identifies a set of liquidity positions is defined by its trading pair so the two currencies its fees um some configuration parameter called tick spacing and then as you can see like hooks so every pool has at most one hook contract um and then so just diving a little into like before Swap and how hooks work um it's kind of what their name implies before swap gets executed before swap happens after swap gets called after a swap happens um it's worth noting here that in before Swap and in after swap you get access to the pool key and So within before swap you have you have knowledge on like what trading pair you're working on the direction of the trade and like the size of the trade as well and so you can in theory Design One hook contract that services like eth usdc eth tether eth BTC so like one hook contract can service and facilitate you know different trading pairs um another interesting data that's passed to after swap is like the swap result so you get access to the input amount as well as the output amount and so if you wanted to do like oracles or like May maybe different like value redistribution mechanics uh you would have access to like the volume uh same thing is true for liquidity hooks it's split into four different functions here but again sort of as their name implies it's logic you can execute during uh the liquidity life cycle uh now on to like the fun random ideas that I have around hooks uh so obviously as I mentioned we have like Dynamic fees and there are ways that hooks can actually charge external fees we call them like hook fees and so there are ways to do value redistribution or different sort of incentive schemes for incentivizing either swappers or liquidity providers um there's a whole body of research from Dan Robinson um and so there are some ideas around building like an amm that's resistant to sandwich attacks and uh these slides are a little bit old but I think like two weeks ago Robinson like had a paper on prediction markets and like he essentially came up with this like really fun custom curve that would be very very conducive to like providing liquidity on prediction markets man why why is it doing that whatever I'm just going to roll with it um blah blah blah there are some things you can do with automated liquidity management as I said so like maybe in before swap If You observe like a large trade size you can like concentrate liquidity um with V4 like we can you know directly support you know rebasing tokens um not natively but like you can roll a hook to you know support like it would do automated wrapping so for like Lio Stak ether in before swap let's wrap it into wrap staked ether you could also design custom curves for uh different asset classes and like rebasing tokens um and that's sort of like the two two big areas and the two big themes that the foundation is super excited about related to hooks is like one something that like improves LP profitability so around like fee optimization or liquidity management and then the other side of the things that we're interested in are like hooks specifically designed for certain asset classes and asset types um another big research area is application controlled execution I think people also call it app specific sequence ing or ass um and this is kind of like related to again Den Robinson where like in a hook you can in theory like control the order of uh transactions that are being fulfilled and so you could maybe order the transactions based on some auction or some sort of fee um oh and then so that's for Hooks and then for V4 more generically because our our prizes are not just hooks but also it includes V4 um there's liquidity mining with subscribers this is like a new feature that's a little bit different than B3 um we maybe something around like creating oracles for liquidity positions those don't really exist yet and that's kind of like a cool fun project uh like I said with these like 699 tokens that are like minted by the core contracts maybe like building some sort of like staking mechanism for these would also be pretty neat uh developer tooling of course is always like a very fun topic uh this could be anything from like testing kits I think uh and like back testing like LP performance just anything to like really streamline and like supercharge like the hook building experience are some you know really really great ideas to build in V4 um another area is like indexing as well like maybe setting up a Ponder schema uh would be would be really cool for us and then yeah so lastly I guess just to go into our like prizes um so our biggest prize is hooks um if you just Implement a hook that you know has a dynamic fee or a custom curve um or you know some sort of like fun like value redistribution thing like you know anti- adjust in time and then our second category is around Integrations kind of like anything where you might be using V4 as a dependency for your project but you're not directly building a hook is totally eligible and then our last category is obviously for uni chain it's just you know build some fun defi app on uni chain um and that's that's me on oops uh that's me on Twitter and telegram so you can reach me there we are super active on the Discord we have docs if you are interested in building a hook I recommend our V4 template it you know sets up the dependencies gives you a boilerplate hook the tests are all set up um so that's a really really nice place to start and then yeah I can open up the floor for any questions okay uh so his question was for the usall front end if you want to swap through a hook do you call the hook directly or a contract um that's a good question uh you could call the hook directly it's not like a very common pattern um the hooks are sort of like abstracted away such that like in theory like if I wrote a swap router or Unis swap Labs has a swap router and they sort of specify that the trade should go through your pool it will automatically like execute your hook Logic for you um but you could you could in theory design your hook in a way um so the question was if for the hackathon you make your own hook do you have to roll your own swap router uh no you don't have to we have Universal router and I believe I will be merging documentation on it and then there's a like really really simple test router that's included in V4 template and it has like an example for how to use it and that's good for like a single pool trade like you can like directly just trade on on on your hook oh yeah H just a really simple one um just I've never done it before H just wondering how do you connect a hook to like a a pool is it you just need to like register the hook like the address of the hook it's a separate cont yeah um so registering a hook so whenever you create a pool you specify your pool key here and then inside the pool key you have IH hooks which is it's it's an address uh um so whenever you create a pool you specify the hook address okay so you like deploy the pool manager by itself and then in that you register pools is that it works yeah correct yeah uh so before you create a hooked pool you have to deploy your hook address it's an entirely separate contract and then so you register it on the core pool manager like this is the core contract and as part of like registering registering or creating this pool you specify the hook address cool thanks cool so general question like if we all here build a hook for a pool we all create separate pools maybe for the same pair how does the liquidity doesn't segregate like yeah I get this question a lot um so yeah in theory a trading pair eth usdc will exist as a lot of pools um I think there's generally Market forces that you know where liquidity providers will naturally sort of congregate around like the best performing pools like the pools that get the most volume um in general it's a problem that does exist we don't expect it to materially exist um I've sort of recommended developers that if you are trying to compete on eth usdc you might have a bad time um but essentially like one of our categories right is like Des finding pools and tokens together right Hooks and pools or Hooks and tokens together and so like if your token is only predominantly traded on this one specific hooked pool then you'll get like most of the volume and most of the order flow thank you I think there was a question in the back the the gentleman can you it thank you am I right so there is no way to remove already existed pool or add to existing pool some hooks um yeah there's no way to remove a pool after it's gets created I mean I guess if there's no liquidity there then it's technically like removed um if you initialize a pool um there's no way to attach a hook afterwards it's like at initialization that the pool and the hook are coupled together cool if there were no other questions I will be hanging out at the booth um so yeah come and find me [Applause]

Automatic transcript — names and jargon may be misspelled.