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

Loading player…

💸 Uniswap v4 Dynamic Fees

ETHGlobalWed, Oct 9, 2024, 12:00 AM

Join saucepoint from Uniswap Foundation for a workshop titled, "Dynamic Fees." Create a hook with dynamic swap fees! Get alpha on dynamic fee use-cases Peer into how hook contracts are defined learn about the flow on dynamic swap fees successful hook testing. This workshop took place at ETHGlobal London 2024, a 3-day hackathon held at the Truman Brewery in London from March 15 to 17, 2024. The event 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.co​m Email us at: hello@ethglobal.com

Transcript

[Music] today we're going to be talking about uh Dynamic fees um and I guess show of hands how many people have heard of V4 and hooks all right how many people have built on hooks or like written solidity for hooks okay so a handful of you guys um cool um so I'm SAU or sauce Point uh I do developer relations at the foundation um I know a lot about V4 so if you have any like V4 questions that's that's me and so today we're going to go into uh Dynamic fuse and like how to do Dynamic fuse with hooks um and before we start we're just going to like quickly kind of touch on like the one of the biggest changes in V4 and so in V3 there was originally three fixed tiers uh five bips 30 bips and 1% uh governance voted to add one uh so we have four and those are sort of just the fees that you are sort of restricted to like LPS have to pick one of these um in V4 this is now fully continuous so you could have like 4.9 bips you could have 10 bips but uh V4 also adds uh Dynamic fuse which is super flexible and super interesting um these can be changed at sort of on any sort of spectrum so like you could change it once a year you could change it for the block you could even do it at the transaction level so for every swapper they could have possibly a different sort of fee that they pay and uh this logic is defined by the hook itself um so it's like really any sort of arbitrary solidity um you could in theory like look at the chain link Oracle and dependent on if the price is above or below the the Unis swap price you could set your fee based on that um so it's super flexible and like the hook has full control over how the fee is set um but before we you know go into that like we'll just do a quick refresher on like how hooks work and what Hooks look like uh so whenever I say hook it's kind of overloaded at this point it's a contract but it's also a function and then it's also like a feature as well uh so A Hook is so this is an example of like a hook contract it's super Bare Bones you sort of adhere to a certain function interface and so in this example we have like before Swap and so the logic inside that function will execute before swap happens um in this case we're just incrementing a number it's pretty pretty standard um this is kind of what uh hooks look like greater picture where uh at the start of the swap we check if there's a before swap if so we'll run it then we'll do the uh the V3 sort of concentrated liquidity math and then you can also do after swap as well uh and you're not just restricted to the swap hook functions you're you could also do liquidity like before and after liquidity gets modified uh before and after a pool gets created uh just because you use before swap doesn't mean you're restricted to before swap only you could use all 10 hooks that we have um here's another picture of what Hooks look like uh so user does a swap uh it touches the swap router swap router then uh connects to the single tin pool manager calling Swap and in this example we have an after swap function uh I would like to highlight that hooks like the better hooks essentially service like all sorts of different trading pairs it's not restricted to one trading pair so in this example you can see that like eth usdc and uni usdc work with with this hook um there might be cases where like your hook should be specific to like stable pairs um but in general one hook contract should service multiple trading pairs that's a something worth calling out um so now yeah uh Dynamic fees um we're just going to go through like a super super easy example um just so you guys are kind of familiar with like how it looks and so if the block number is even we're going to set it to 69 bips if the block number is odd it'll be cheaper um I don't know why you would do this in production but I don't know could be cool uh so this is what it looks like um so we have our hook contract our hook function uh in the function set Dynamic fee this isn't like a standard interface you can kind of name it whatever you could provide whatever parameters you want the only important thing that you really want to be aware of is uh you essentially tell the pool manager saying like hey for this trading pair for this key this is the value this is the swap value um and then the pool manager will cach that value and then whenever you're running swap logic uh it'll you know reference the swap amount uh and so if you wanted to do it on you know every single Swap you would just you know call this function before swap so before every single Swap happens we'll just update the fee um in this example uh there's actually this cool optimization where you could do because the fee is really only set at the Block Level um and so you could really you really only need to set the fee at the top of the block swap so for the first swap that happens you set the fee and then for all remaining swaps within that block there's no reason to update the value because you'd be updating it for the same thing so that's a little small optimization that you could do but again like yeah you could do it every transaction you could do a top of block you could do it whenever you want like maybe you manually just call set Dynamic fee from your wallet um and then to Showcase another hook right after initialize uh you can set the fee there because whenever you initialize a pool with a dynamic fee it's fee is actually set to zero so if you're not updating it before every swap you'll want to set it at some point setting an initial value so you could use the after initialize hook function so pool gets created a bunch of storage changes happen it'll call into your contract in the after initialize and then that's when you tell the pool manager that the fee is whatever value um and so this is kind of so there's a lot of back and forth with setting fees and this is what it looks like uh so user you know touches the swap router swap router then calls the pool manager. swap uh inside the pool manager it looks to see if we have a before swap function and then in our case we do so then it'll call out to our contract and then our contract will say like hey the fee is changed the fee is equal to um whatever value you set it to and then it returns back to the pool manager to run that actual like concentrated liquidity math using um the new Dynamic fee um and really that's that's all there is to it for dynamic fees they're super simple but also like hyper flexible and I think I wanted to cover uh testing here because right now V4 doesn't really have an interface and like testing should be on like test Nets locally or with uh founder unit tests it's so I'll just kind of quickly touch on it and if you are thinking on building uh for the hackathon I highly recommend using V4 template uh I have a link at the end but essentially does all this really nice uh code setup for you where um we deploy the pool manager we deploy all like the test routers um we create test tokens for you with all the approvals that you need there's an example on how to deploy a hook within Foundry unit tests because um hooks encode how they like which functions they use inside their address like the first few characters have an address signal which functions you're using and so there's this nice little like mining uh tool that you can use within V4 template to deploy your hook locally um also things that we do in setup is we initialize the pool in this case you can see that uh whenever you create the pool you need to have a special like fee value to signal that it's a dynamic fee um and so there's a special flag for that and we create that pool for you uh we also see need some liquidity initially so that uh you can kind of just jump right into it and like write tests for your uh for your hooks or for your Dynamic fees um in this example we're sort of setting the block number to something even we're running a test Swap and then we're confirming based on the output the number of tokens that we get if the fee was correctly charged so on the even blocks we see that 69 bips was charged and then for good test coverage uh on the odd blocks we see that you know five was charged um so that's a little note on sort of hook testing and verification and definitely definitely recommend using Foundry because if you do it on test net sometimes you get a reverted transaction that isn't very clear on like what went wrong so uh the unit tests are a lot better for uh stack traces and stuff and so now yeah for different ideas that I have around Dynamic fuse because I have been saying that they're like Super Hyper flexible um so you can do things like volume based fees to incentivize like swap activity so if like swap activity is down like maybe the fees should be lowered um you could sort of try to mitigate me or like Front Runners um which always prefer being like earlier in the block and so you could do something where at the top of the block the fee is a lot higher and then like later on in the block or as more transactions happen um you could sort of decrease the fees that way uh you could do sort of directional taxes for like mem coins and ship coins where you know if you're dumping your shitcoin you you know you charge a higher fee to sort of disincentivize that uh you can do fee discounts for liquidity providers for you know high volume Traders uh because like I said you could do fees at the transaction level and at every single Swap level um I don't know if you guys are familiar with am amm it's the new sort of auction amm design from uh Dan Robinson and Austin Adams where essentially they auction off the rights for somebody to set the dynamic fee like your you as a bidder will bid to be able to set that Dynamic fee and so some sort of variant of that sort of applies here and then lastly uh the VR gdas from t1s where you know you kind of have like this like decaying auction that can also increase in price as well dependent on like demand and volume um so those are some cool ideas related to uh Dynamic fuse and then quickly on prizes that we're offering at the foundation uh this weekend uh so we're offering three prizes for what I call First Class hook features these are things that are supported like really well and like natively within V4 hooks so Dynamic fuse is an example you can do custom Curves in V4 it's a little bit messy right now because there's a pending change and I'd only recommend it for the brave uh but it is possible and if you are interested in custom curves come talk to me uh you can do noop uh you could have hooks charge their own fees or do sort of different like value redistribution uh using hook fees and then in the other category we have hooks and external Integrations where you know you can combine a hook with like maybe a ZK co-processor or combining you know V4 hooks with worldcoin um to sort of you know change the Dynamics of swappers and LPS and so we're offering prizes for that and then the last track is just kind of like this open track where if you're not developing a hook directly but you're you know integrating or doing something tangential to V4 that will apply to you so anything on like me lever just in time uh on the public goods stuff uh there's like Oracle hooks that are cool uh liquidity staking or like liquidity incentives on V4 haven't been built yet and then lastly we have like pool operators which are like uh periphery contracts SL like swap routers like you could like maybe design a contract that does atomic Arbitrage in a portion of the proceeds get donated to liquidity providers or something like that and yeah so those are the three tracks and the three prizes um here are the resources um there's so all that Dynamic fee example exists under my repo uh sauce point on GitHub the template is under the Unis swap Foundation highly recommend using that and then we have uh V4 by example which is I don't know if you've guys seen it but it's a fork of uh solidity by example where we have a bunch of nice little uh Snippets and um different sort of uh solidity Snippets related to to V4 you know there's you know information on Swap fees if you're you know lost on like how do you create liquidity there's you know examples and solidity Snippets for you to work through um oops I don't think this is reloading but uh yeah that's all I had I am s point on Twitter and on telegram so feel free to reach out to me and then you know I'll open up uh the floor for any questions if anybody had any questions yeah message. sender um there used to be a really good way but then Labs ripped it out um conveniently it's here on on V4 by example So within hook functions there's uh you have an option to provide like arbitrary call data and so this arbitrary secure though would that not be secure though because like then the user could just pass in some arbitrary value and then yeah yeah there's like I mean I guess you could write a uh prery contract that like guarantees that you know the message sender gets uh passed to the hook but yeah this is currently really the only way to access like the wallet's address at the moment so the lock there's like some sort of function that's like loock um yeah you can't call do loock from wallets anymore you used to be able to and then that information would get provided to the hook but I think they took it out for like gas reasons um well cool if there are no other questions um I think we're just about on time all right great thanks guys w [Music]

Automatic transcript — names and jargon may be misspelled.