Miao ZC - Comparisons of Continuous-Time Swap Designs

EthereumZuri.ch 2024 · April 2024 · 17:37

Miao ZC compares designs for continuous-time swaps, where trading happens as a function of time rather than in one instant, discrete transaction. Miao argues that this approach addresses exposure to maximal extractable value and the difficulty individual traders face in managing price volatility, and explains why it shows features unique to blockchain-based financial systems.

Speakers

Topics

From EthereumZuri.ch 2024, hosted by EthereumZuri.ch.

Transcript

thanks for coming for the talk so uh I just realized that I only have 20 minutes I have to go uh bit fast I apologize for that so um so today I'm going to talk about the comparison of continuous time swap my name is NE I'm from Super flu protocal so my second time actually talk here so uh what you'll hear about today uh is why you should trade time continuously and what does it mean and uh what different designs of uh decentralized exchange uh there are for the time continuous way of exchange right and beneath surface what's uh what's what makes it possible right so uh that's today's talk so I'm a

Trader right I trade all kinds of things including crypto including try stuff uh but nightmares that I have I believe a lot of people also have is uh first of all most of us suck at Market timing right let's just face it right so this one of the good examples right so people buy top and uh and sell low that's kind of the what most people do uh statistically likely not that's how people does uh which is bad right and uh there's also study about uh why you should um uh invest it all the time as opposed to trying to time the market because statistically most people just don't do it well this is one of the data from the uh trefi study about S&P 500 I'm curious if uh uh what kind of data uh is for the crypto investment I would

believe believe it's it's rather simar right and also uh it's very difficult to know which are the best days in the market and uh so what best way for a normal Trader a boring Trader uh like me uh like some of uh some of you would be uh don't time it just keep investing right dollar cost average sometimes people say right that's my first nightmare the second will be very specific to uh uh crypto right so the front run by the mvv Bots right what does it mean right so let's go through a couple of M technology terminology first quickly right so there's a user a normal eim user who sends transactions and Searchers like Advanced etherum users

that specialized in uh finding uh MV opportunities or me like maximum extract extractable value right U and sending Advanced transaction types like BND bundles um then we have also Builders who specialize in construction in the the execution payloads uh rece receed from either users or either from The Searchers and then you have the validators the party who signs and submit the uh transaction to the be uh and it's called proposal sometimes also maybe you heard of this term uh PBS right so prop proposal Builder separation and actually validator and proposal is kind of the same same same thing right

so uh if you look at the supply chain it's like from the user to the Searcher in between there's wallet usually all right from search to the Builder and Builder to the validator in this case is without the PVS right so this is just a setup without PBS but what's the problem of it but the biggest night matter would be sandwich attex right so in the graph above you would see that okay Alice tried to send buy something from the Dex however Bob sees the transaction and triy to squeeze uh two transaction in uh put the ISS transaction in between it's similar to a sandwich and uh then take advantage of that by why because uh it's possible because transaction on uh on the blockchain that we know of are

transparent so there's a chance for the builders anybody in the in the stage of the supply chain actually right so in the diagram below it's bit more than the one that I show a a moment ago because this one has PBS uh building right so actually there's 95% of the validators in AUM uh ecosystem now utilizing me boots to do to do the PBS so there's Builders there's validator there's escrow there's relay and there's users blah blah blah but still doesn't solve this problem because transactions are um um transparent right so that's kind of The Nightmare uh um sort of crypto Trader would have so the the here's one

solution that I'm going to talk about today and with a couple of designs uh that that implements uh such solution so um the gist is that we let the swaps happen every block but virtually we we're not going to submit the transac every block of course it's going to be too expensive but we let it happen virtually every block right we model a swap exchange using time we don't model it in a in a way that it's instant transaction or instant swap right so this way we avoid Market timing right we avoid the first nightmare and the second is Will minimize a single block MV risk and so let's see what kind of design we

can come up with to uh to create this kind of a uh uh solution all right so design one uh I call it the flow swap um so the basic idea is that you add the continuous time elements to the Unis swop V2 contract or the UN V2 concept let's say rather um so first of all just a refresher what is unop V2 it's a very elegant and simple right I think I think it should be the exercise for everyone that learn to program your ownum to do uh by hand yourself because it does show a powerfulness of of of of defi right Simplicity and sometimes but if you look at this diagram there's a curve which

shows uh something called a constant function Market maker and in this particular case the curve is represents a constant function that is constant liquid constant product or some I call it constant liquidity product which is I think it's more accurate so uh basically if you multiply the liquidity a and liquidity B they are constant and then create a curve right so when you do a trade it will be moving from the blue dots you see to the to the Red Dot so it's a instant move right so one transaction down so that's how a un swap V2 transaction looks like in in on that curve right but what if we make it uh smoother right so let's see so we say we don't do an instant move from Blue Dot

to Green Dot anymore it is a Green Dot sorry blue to the Red Dot but we want the Green Dot that moves smoothly conceptu that smoothly over time so time become a function of it right so there's a very scary for it's not that scary scary enough formula there but how do we come up with that that's kind of the uh the we need to find a solution how do we come that I'll show you in moment so there's some magic there um but the idea I just want to visualize you move the points continuously along the along the curve so uh I would recommend uh using the U something called stage math it's a very good uh open source mathematic Library licensed under GPL and uh let me show you quickly how you how you solve

that problem so first you you define the constant liquidity product function uh using symbols or say you have X and Y you have X Prime and Y Prime you say uh the product of them should always be equal so that's your equation that's your places to work with right let's validate if it's correct so let's say we solve the instant swap the unit swap V2 instant swap uh liquidity function so you plug that CLP function with a solver say I want find a solution right in the line of the last fourth line the Sol function would be basically say Okay I want to find the solution to satisfy this equation and this uh Sage mouth will come up this uh equation which is the correct one right if you do Unis V2

yourself that would be the formula you use you probably wouldn't use Sage math in that case but uh but at least uh it it shows that it can work but now let's see how can we do that for when you need continuous time I use a uh time uh uh stage masth again in this case you have to come up with something that uh you know you have to have a leap of faith to come up with some magical variable q and you have to imagine how that formula look like once you come up with that shape of the formula and plug to the server the server will give you that so that's the basis of how to calculate um uh the liquidity movement continuously over time right so now you can define a function say how much liquidity has

moved as a function of time so you don't need to change block uh don't need to do a blockchain transaction anymore because the liquidity just moves every time the block moves right because every time block moves the time St moves time St moves the liquidity moves so that's how how you do it without sending own chain transaction and that's how you come up with the equation you use a sage math right so that's the first uh uh design let's see what's the property so first of all you still need to do liquidity boost STP right means that the tvl will be there will be tvl you have to lock the liquidity that's the same as unisa but in this case supporting of course a continuous time so there's some funds at risk right second of all would be LP complexity it is almost as same as the

unisoft too so uh nothing to be said about more than that the third would be the single block uh me uh so would be safe because now the transaction happens every block so that it's no longer uh subject to the single block mvv kind of risk right so all right so I need to go slightly faster now so second design would be the uh zero intermediate liquidity Market maker in this case uh let's say LP sends two money streams one is one send uh token a the other is token B and they get it back immediately so in this case neutral so the lp the security provider don't actually uh lose anything or win

anything right it's neutral this is this is initial state but then someone send the trader one say I want to swap 100 usdc for each X and the now the lp actually has imbalance now so LP will have to give the trader a certain amount of e and takeing more usdc back right so created imbalance the lp now the money actually moves but there's also a fee there so the lp provides a difference but also take fees from the streams so in this case uh in in this design it's like Aqueduct that kind of routes routing the mining streams right so uh so liquidity boost drop required no in this case there's no money at risk so money simply uh going through the router

s of sort of like routing from one place to the other right uh the comp it wise it's a downside of it because the complexity is quite High because no one actually know how to actually do the upch charge for this system right because it's unconventional so that be the main risk of using this model right the single blocker muv also safe right so there because it also Happ happens over time um every block actually so now we see the third design right uh so now we talk about unit un again in this case we use a tww what is a tww in brief the tww stands tww stands for time weighted average price and T was added in unit swap V2 and was improved in in the V3 right so they use something called a

price accumulative which is recalibrated uh each uh for each Swap and it's value changes also for each for each block right in order to calculate the price uh you use two price cumulatives and you divide by the time window the range of the time s so uh a way to visualize this one is V2 so uh you have two price communities from different time you get the Delta you get Delta over time you divide it you get the price so you can always get the average price between two times uh If You observe the the data so unisoft V3 is slightly uh different here uh but the fundamental

idea is the same I uh if you if you want to know more you should read into the uh uh the paper so um so uh we have actually uh in production uh uh something that tracks The observed uh price and the market price so you can see that the average price if we use this method tracks uh the data from Yahoo pretty well the green data would be the one that we actually track and the Red Data would be the one we retrieve from the uh market so uh how does it work then or the the way works is that the the step one is everyone send money to this liquidity

the the pool so the the you can see that it's like stream of money goes up right over time but then the step two would be that uh you withdraw all those liquidity uh byy someone uh moving the liquidity right the the the the yellow called the liquidity mover they take all those liquidity find the liquidity source and swap it and give it back to everyone right that's how it works but you have to uh satisfy the price that uh uh you observed from Unis swop right you have to give back the amount of money that is satisfies the liquidity constraint so you can't just uh take the liquidity and give back not sufficient amount so it

has to be uh satisfying the the liquidity requirement so that's kind of the design and let's see the property again there's no liquidity bootstrap needed so there's no tvl per se uh it's not entirely true as you see there is a little bit accumulation temporarily but uh it doesn't go up forever so it's always go to Zero from time to time and complexity it's kind of simple right so you just need to satisfy the Benchmark price and it's flexible also you can find the liquidity from anywhere you like to because this system doesn't really care and it's also competitive because everyone kind of competing with that job which is about uh swapping uh liquidity uh satisfying the price constraints so single single Block M why

is kind of safe for the Traders there's little bit challenge to the to the liquidity movers to be competitive and they are still subject to mvv but it's their job to solve that challenge right so there's a design for also I anticipate I don't have enough time to talk about it but if you look at paradig they have another design called uh T1 uh time weighted average Market maker and uh they have a design that uh uh there's some similarity with with that and the with the Torx I just talked about uh but I'll not go into into that so how all this can be possible well the enable is actually you need some sort of composable money stream without all the design you can't really do it so I

actually leave it until the end I don't want to show the super fluid right so uh but you have to have these kind of token wrappers it has to be erc20 compatible you have to be able to wrap existing uh erc20 tokens you also need to be programmable money stream and composable that's most important you have to be just in time liquidity so that you can use the liquidity you just received and use for something else if it's not composable then you can't make it right so that's why it's needed so uh I'm going to skip this one and just do the comparison right away about the three designs uh we have so we have the flow swap we have the zero intermediate liquidity and we have the uh T Oracle based uh only the flow swop needs a

liquidity bootstrap all of them others doesn't need and the lp complex wise the the zero intermediate liquidity is the most complex one and uh the T of is the simplest one I would say and M safety wise all of them are safe with a little bit caveat the tww uh version has to um U the there are liquidity will have to compete and have to solve the the challenge themselves right so there's possibility to solve so it's not a problem really uh for them to find the liquidity Source all right so some additional Theory I mean I just go really really fast this is called something called a functional reactive programming if you interest you can look into it it's really about how to model

everything in a using time continuously as opposed to uh uh otherwise right so FP is actually the the theory behind it and uh so yeah thanks for for your listening yeah uh so follow us on super boring on warcast or super fluid and follow me um uh with my handle and if you want to see the source of the slides you can go to our monor repo let me repeat super fluid Finance protocol mon wikii if you search for continuous time stop you would find this slides uh the source of the slid So yeah thank you very much we actually have time for one quick question if we have any questions okay any question any

question yes no okay I'm sure that if you have other questions you would love to connect with the after his talk let's give him a round of applause thank you so much [Applause]