Uniswap V4; with a focus on Uniswap Hooks

Uniswap V4 and Uniswap Hook Incubation Recap · 31 Jul 2024 · 36:26

Lecky Lao gives an introduction to Uniswap V4 and then zooms in on the Uniswap Hook he developed as part of the Uniswap hook incubation program. A practical session for developers and DeFi builders who want to understand how hooks extend the exchange.

Speakers

Topics

From Uniswap V4 and Uniswap Hook Incubation Recap, hosted by Ethereum Engineering Group.

Transcript

hello and welcome everyone my name is Peter Robinson and today I have licky who is going to talk to talk to us about Unis swap V4 um incubation program and talk about um what is the the um hooks in Unis swap V4 and what that's all about so Ley why don't you introduce yourself yeah thanks Peter for the introduction yeah hi everyone my name is Ley l i current is doing the PHD in the University of Technology Sydney in the second year uh researching in the blockchain MV space um planning on the papers around the single chain MV and

the course MV um yeah this talk it was like I was doing the uh the Unis swap they have uh the first um hook incubation I was participating to the cohall one and then while Peter was doing the Unis swap talk and then I was doing the program and then I was saying like maybe I can do a little bit talk about it a bit after the program finished so here here I go yeah yeah well and thank you very much for um sharing your find you know your the findings of doing that incubation program I think it's going to be great

and one day you're going to share the out output of your PhD here too um because I'm sure that'll be exciting too so um yeah please present your slides okay yes so this talk is about the unit SW we4 and the cor incubation that I was participated so please know the talk is not beginner friendly as this is defined by the we for H incubation as some content uh reference from there there

also like they have cohort to open as well basically you could anyone could apply for it but the the selection criteria is um is not a a beginner like they select expert um like so that because the content is quite uh webl so my plan is to do the talk and then leave all the question at the end because like it people might have lots of questions but yeah don't worry about too much I will just talk like a little bit of the yeah the UN yeah the roughly understanding is okay yeah

yes so the program was um eight weeks program and then every week is about 10 hours across uh two days and then at the end there's a hook phone um like a demo day that you present your um demo so so we4 is still um haven't deployed yet as the C is still not cold free it introduced quite a few different functionality like the most popular is the uh the hooks to provide much flexible

functionality and the Singleton contract for efficiency is also innovative way that now it's every everything is out under one contract um which is also related to the flex accounting system that uh it it manage the balance by itself um within the whole contract itself and this become unlimited Fe that no longer are fix tier that that you could customize a dynamic fee or or different fix fee and also support um

Native e rather than just um web e and it's more Community Driven as like you can do lots of different things and um and the hook phone so we force mainly contain the we for core and we for periphery we for core contains the cor libraries it's still not C free when I was doing the uh incubation program like the no offs was was even like not implemented and then we were like talking about the no offs

and we wanted and then until like at the end before the the hook of pH star then they have the no Ops uh shifted into the cbase and currently it's yeah is still I can see they still are developing commuting cods and to talk with the communicate with the V4 core you need to use the weo uh periphery which contain lots of examples um to to to uh basically how you can use use it with for different examp examples like Dynamic fees uh TW Swap and Etc so flash accounting you can see in

the V3 when you're doing this is called like a m single or multi hop swap you're jumping through different contracts which contains the pools and different pool tokens now if we before everything's under one single contract which contains like can be like lots of like maybe dozens of different uh tokens and at the end you just calculate about the Delta which remove the as you can see remove all the extra cost on like jumping through the different uh contracts they also introduce YC

6909 claim tokens basically it's a it's a token for when you don't want your receive your tokens now like it can it can save the it's also around like saveing gas cost for example some of the erc20 token has loss of um extra uh checks and functionality which increase the transfer course like uh the censorship for the usdt need to go through the uh The Blacklist and and Etc so that's why they introduced a this token for meaning and burnings so

that it's it's quite useful for like for example the market makers that you want to do lots of the frequent trading for this uh token or or pairs that you would you would like keep doing the Arbitrage but you because you just want to do the arbitr you don't need the token right away then it's good that you just have this minimum token um then then you don't need the actual token then you can use it later it's like a credit that you can use it for later and then talk about the tick and fees so TI is

um it's it's like the pricing between a token a and token B for example when at the beginning when the t is zero and this is a Formula so X you can see it's a 1.1 to the power of I so I when take is zero then I is when when take is zero so I is zero then the P Price the price will be one which representing a one unit of token a you can get a one unit of token B and then there's a other examples that when the tick is 10 that you calculate like this price

then the one unit of token a you can exchange for 1.1 um token B and same thing with10 then you can have like one so exchange for 99 so why thises a 1.1 essentially the uh the the movement so 0.01% is like the one it's like the one Basics point is like the how much um movement you could uh uh move the price essentially is the fee that you could if you change this is

it will shift bigger and then the the Gap is the the fee that you can have one basis pawn two basic Pawn um Etc okay so here's the uh Q notation yeah I'll the topics I selected is basically some of the important concept and the Highlight on the wei4 um that's why I'm jumping for different uh Concepts but the most important is you just need to understand like what it is

and how how it works I think that that's why I selected it so Q notation is around a question that like a user has uh like two e and they want to create a liquidity position in uh e usdc pool and the current price of Eve is um 2,000 usdc and they want to add liquidity in the range of um the lower bound one 1,500 to Upper bound of 2,500 then how much usdc do they need in fact it's this huge uh formula that is using in the

Unis swap V3 wi paper and it will continue be using in V4 as you can see the formula um so that's the price for the output and X is the input and you can see it's about the price the square root of the price um a is the lower bound and B is the upper bound and then calculate the output so so the important concept is there's a square root um yeah and then you get the Y is a 5,000 through this uh formula but in Unis swap um in in sorry in in solidity square root is not really

uh friendly for solidity especially the uh the roundings that's why they introduce a uh Q notations and a q number it's called a Q 64. 96 uh number so Q 6496 number is a way to represent a rational number and that's the formula there so we to represent one in a q 6496 is 1 time 2 to the power of 96 and that would become like like a

long L um for example like um like the 1.234 in solidity it will just round it to one then you're missing the the extra numbers but in Q notation it will be uh 1.234 times to the Power 96 it become a big number then you run it up uh to just the uh the big integer there then you only missing a small portion that you can still keep the large portion um that would be helpful for the formula on the on the square root uh price and why it's important is because

throughout the system as you can see in the pool manager there's a swap function um it require the the input is in the in the Q Q number and that's why you need to know the c number and how to get it then you can put it as a as a output so in this swap around you can see um te tick spacing is the tick we mentioned before the key is the pool key so each pool you can get the tick spacing 0o for one is a Boolean true means is uh swapping using token zero to token one

and force means uh the other way around amount specify is how much is the input amount and then the square square root price limit is the output amount essentially that's the output plus the slipage so that's why it's important to know um about the Q number then you can calculate your output plus the slip page you want and turn it back to the Q number number as a as a output pram um so that you can use the swap function um next important concept is the

Locking so in this example um on the left hand side is the user so the router contract is the one I mentioned before is the the periphery the periphery is the Custom Contract like you could create um whatever contract you want as a router contract that using the periphery example then you can interact with the the pool manager which is from the we4 core and the hook contract is a it's a it's a optional like you could have the customer hook for the pool or you may not have it but the most important is

the Locking that you need to have the router contract to call the swap then it will has to unlock callback to callback to trigger the Callback back to your uh router contract so that you can do the swap you can call the swap function from from the Callback and then the Callback um trigger the swap function then it will trigger the the the hoop that's after SW swap before swap then it's the actual Swap and then it go back go back to the the um the Callback uh you could do the uh some of the balance data like the

that's the the the no Ops you could you could basically customize the data to do some extra stuff and then go back to the unlock and this is useful that because now it's a single contract and and Flash accounting that you're doing multi HW when you're doing multihop that means um you could have multiple swapping across different assets that's why you need the Locking to better manage the the multi multihop scenarios and you could have different types of hooks the top bit is the the

basic one that you could initialize um before and add liquidity and before and as swap donation I don't yeah donation is a it's a I don't see that really using that frequent is it's more about uh you could contribute to the uh the pool with the extra token uh as a as a as a donation to The liquidity that you they can add the delta so the no Ops is the new thing that when I was in the incubation program that's being acted that was like

in May so which is like two months ago so no no Ops is essentially the changing the data um to to customize the data so by default the Delta is zero when when you return Delta will be zero but when you enable the the no Ops uh hook that you could have uh custom uh data that you want to do it for example this is a uh this is the am amm hook that I Implement as on the demo day it's also open source so in this example you can

see you define the the hooks as a Boolean then you can say which hooks are enable and disabled and then you can see I was using one of the no Ops as a after swap uh return Delta um but yeah that's how you uh Define it and then in the noops in this example in my am amm hook it essentially allow changing the data that you can see in my after swap I enabled the after swap uh Delta that in my example um there's a fee

amount that I calculate from the based on the variable of uh a swap amount and then I can mean the YC 6909 uh token but then because I minted it it will have the negative negative Delta in the in the Min then with my return I need to tell it to minus the the amount that I minus and then in the pool manager uh code it will deal with the Delta track so that it will become delta neutral then it

will pass the track um uh on on the Delta balance check okay the next one is about the hook address uh bit map so they introduce a a a hook deployment uh this is related to the hook deployment but I think don't need to take too much uh into detail because they have a library to help you to uh deploy it basically it can help you to mine the um the hook address um a essentially they would do the hook address

representing the hook that you enabled that's why they have a function to help you to mining the hook address and then Deport it there so for example this a hook address it transfer to the binary like this because nine it from the Bice it convert to Binary as one9 and this is the the bitmap mapping as a binary you read from right to the left so the index zero if is this one that is if this one is one that means it enabled after remove liquidity returns

Delta so same thing like this is like 0 one two three four so that means the index four is one that means it enabled after donate same thing with this one is seven that means it enable the before swap uh hook so that's basically how it works and then conver back to the address is that n that it means it enable like the after uh the before Swap and the after donate uh hook in there and then we look up the the the hook hook address to look up the hook contract and read the contract code from there

next I going to talk a little bit about my submission on the on the hooker phone um so that's the am amm hook the am amm Hook is buil based on the paper um called the am amm uh written by the uh Unis swap team so how it works is introducing a auction for uh the the swap fees uh option for the manager so traditional limitation uh on the fix fee amm is because of fixed fee you could loss to the Arbitrage because of the fixed fee

and also is not a fee optimized for um for uh the amm and then the amm introduce uh the auction uh for the for the uh manager uh the winner as a manager to set a dynamic uh swap fee so which you can optimize the swap fee and has a higher return for uh the lp so this is how it works that it defines some different Epoch the epoch you can Define it like different time frame for example like one hour or one day so say the current Epoch is one

and then there's a future EPO and a manager can comes in to beat using the lp token to beat for EPO 2 um so the the there's a charging for a bat uh as a as a fee that it can back to the uh LP of so say the manager want winning as a winning manager then the bitto token the lp token as a fee will be PID back to the lp and then the manager two comes in can pay 10% more rent um of the lp token and then become the new winner as a Epoch to

then they can design um the the swap fee for that time frame um as a Dynamics fee there and then similar thing that the manager um but oh yes then you can last for K eox as a winner so you can set the uh the swap fee for this KOST um as you are the manager but then the manager free can comes into to beat uh as a winner with a 20% more rent become a new winner from EPO 3 to EO 5 then it will trigger refund for the manager 2 for the epoch 2 um

and then user comes in to do a swap another privilege for the manager is it collect the swap fees um as a claim token so when the user comes into swap then the as a manager free is a winner on the EO 325 it can also collect the this all the swap fees and LP can enter exit the pool anytime so that's basically how it works in the high level for the am amm and this drawback for the this um implementation is it could increase um sandwich attack as the manager's ability

to trade without fees so there's a scenario that the manager can set uh zero fee then they can be they can be the only one to do the Arbitrage because because other people um because they was receive the the the fees and and uh sorry they can yeah they can charge uh yeah how it works is it can charge a high sorry they can charge a higher fee that no other Arbitrage can be profit from it only the accept the manager because the manager collect all the fees so that will increase the sandwich

attack for the manager and also increase the centralization uh risk and also could cause of the volume driven that worst case because of the low volume it could attract no option uh no managers beating for it that means it will still be the same as the traditional constant function amm um yeah so basically that's uh uh the top and the next step as I'm doing the research on the mgv so I will keep finalize the data for the am amm hook and also planning to to do more research

on two uh implementation there one is called The Nest lobins directions and this mean uh what they do is they increase the swof fee for close closer bringing the price close closer to the mark market and and in you reduce the swap feed when it bring back the price close to the uh the market price and increase the SWA fee when it de de deviate to the uh to the market price there's also another paper about uh sandwich resistant

amm uh this is written by uh paradigm um how how they propose is about uh delaying the the price uh uh a reset on by the by the slot to uh resist the sandwich uh attack so that it will it because the weet is a one block DeLay So when they doing a sandwich it will be uh in the loss when they sell it um yeah I'll bring it back to Peter uh that that's pretty

much yeah all right I'll quickly do this and then we'll loop back to questions so the merch store is still open uh next slide and there are these two talks plus a talk on September the 18th so David Highland Wood is going to talk to us about Ai and web 3 and um I'm I've been doing a bit of Forge scripting to actually simulate a blockchain but you can use it for deploying contracts and doing all sorts of things so I'm going to do a bit of a talk about that and then I sh Anonymous um speaker is going to give us a talk

about a recent hack um I think it's w wer wer X I think that's the that's what it was called um so anyway it's a um a hack where quite a lot of money was um taken and so um they are going to explain their analysis um yeah so that should be a really interesting talk um and the next slide and um if you're here today um on um Meetup you can watch this on um YouTube later and you can see the previous talks there's a slack workspace

if you're watching this on YouTube and you want to be ask able to ask the speakers tricky questions then um join the Meetup and come along it's free um and every now and again there are there is solidity um code in and I put the examples in that repo so are there any questions for Ley and I can see there is one question that happened about quarter of an hour ago from David Highland Wood um in the chat yeah thanks Peter I I was just kind of asking the group rather that um

solidity hack to make the calculation of a square root close enough wasn't very similar to some of the approaches we've seen in um in older code especially gaming you see stuff like that and the one I remembered particularly was in Doom where in order to be able to do 3D modeling on some very basic Hardware uh I think one of the things that they did early was to um uh was to estimate the square root in a very similar manner I was just curious if anybody knew that Peter you might know that uh yeah there was all sort been all sorts of that sort of thing of doing

everything in I mean in a certain number domain which is essentially what Unis swap are doing isn't it lucky that that everything is in square root number domain um yeah and the cons it's the consistency that's important versus the absolute number yeah yeah yeah yeah [Music] so I I didn't have so much of a question but as a a thought that I mean I saw the design that you've said about how all of the contracts are coming together and so that's good because there were few contracts and so you've got one pool but

how big is the total contract is it like on the verge of being too big for ethereum yes enable to deploy the we4 core they you have have to have this specific configuration so yeah when when you build it uh locally uh did yeah you need to follow the the configuration um they they they have to build to deploy on your local uh train um to to play around with it um yeah it's it's on the edge and that's also one of the reason why it take so

long there also one uh uh the the the code uh they using it need to be upgraded I forgot the name I didn't put into the deck um it's about temporary storage uh a new OP Cod um yeah so the we4 is uh um pretty heavy because a single contract yes uh I also asked a question like you have one contract contains so much assets would it be risky and that that's my silly question

and they they said ah we are unisoft team and we do lots of security audit so it will be really top Securities I guess it it will be I guess um um but yeah if you are a really good really really good hacker if you could hack the one contract then you will be like billions and trillions of dollars and of tokens yeah yeah yeah it it is yeah well I mean this is the thing so even Unis swap V3 every the contracts were complicated enough but at least then I mean it's easier to comprehend and analyze when you've got less function

ality um yeah I wonder if what it really means is that in in ethereum generally a better way of essentially separate classes and you know like in Java you've got classes and objects and things so that you can compartmentalize um yeah maybe that's what needs to happen somehow I guess that's how they enable uh hooks um but yeah we four is around guas automatization um yeah I think it's a really unique innovated on we4 um yeah we we'll see how it goes when they go live yeah yeah

for sure are there any other questions okay all right well look Ley thank you thank you very much for your talk um that was really very interesting and I think now everyone has a more complete understanding of um Unis swap the whole way out to V4 um which is I think great um so thank you for your time and um everyone have a great day and see you all in two weeks time byebye bye

Related talks