Decoding... Uniswap V2
Ethereum Malaysia·Sun, Oct 9, 2022, 12:00 AM
"Decoding..." is a series exploring on-chain protocols in-depth, including a walk through their source code. In our first episode, we kick off with the most familiar of protocols, Uniswap V2! We will look into its Solidity code, the architecture of the protocol as well as some commonly used functions.
Transcript
hello everyone uh thanks for joining uh this workshop by ekfl it's the first in a series of workshops uh that we are hoping to start um today uh the topic is decoding uniswap v2 for those who are not familiar with ekl we are a group of ethereum and blockchain enthusiasts we focus on the technology and use cases not on price um and uh we host we try to host at least once one meet up a month uh for the last couple years we've been doing it online uh hopefully in the next few months we'll be able to do it in person when things are much more safe and on that note i'll pass the mic to chichwan thank you thank you harif and thank you everyone for coming uh again my name is sichuan citroen from epl so today we are trying to start a series it's called decoding uh we are starting off with unison v2 because it's a it's a commonly known protocol and i believe that everyone should at least know uniform v2 uh in the blockchain space if you do not know uh it's a good time today is uh today is a good time for you to get familiar with unison v2 in the future we are looking towards uh decoding other other things not restricted to only protocols possibly uh in the protocol layer application layer and so on uh but it really depends uh yeah yeah and i'm also we are also open to suggestions if you have something that you would like us to take a look at or if you like to also host one of these sessions and steal your product feel free to but uh of course um we are expecting it to be more technical like harvey pension if we don't we don't we don't uh we don't quite talk about price and stuff like that the main the main purpose is to um have this a more technical top so that everyone is able to benefit uh code coders on encoders like uh coders will be able to like uh read on the code and uh to follow what to understand what's happening behind non-encoders we're able to read the smart contracts and make sure that uh you know what's exactly what what is happening exactly in in the transactions you're making to keep you to keep yourself uh safe yeah because it's um although the code is um it's public uh often people will just take it for granted and not cannot read before they interact with the contract so i believe that it's a skill that you must actually have yeah so today without further ado uh let's start so um again i would like to mention that uh if you have any questions just leave them in the chat i will look at them from time to time and if it's yeah i think they're like a lot of people so probably maybe it might be a good idea to admit yourself and stuff like that so yeah without further ado let's start so okay uh we're talking about minnesota v2 so um for the benefit of those who are not familiar unison v2 is basically a platform for you to swap tokens what do i mean by that so given that we have big wbtc uh basically bitcoin in the ethereum space we are trying to swap bitcoin we are trying to stop bitcoins to maybe a usdt uh usd in other words and uh we we are able to utilize unison v2 on the ethereum blockchain to uh do so right uh tr traditionally uh before pre unison v2 how would we do it uh we will probably go go to a centralized exchange you are you have probably heard of binance uh luno and stuff like that so um this is what this is a screenshot i have taken it's uh an audible mechanism and a typical order book will look like this whereby the top will be the sell order and the bottom will be uh the buy order so this is this was taken a couple days before and what does it really mean so let's take a look so um in the concept of order book there's always a maker and taker and a spread and a maker is basically someone who creates an order so let's look at the sell order for example um hang on someone is trying then yep let's take a look at the sell order for example so basically these orders are uh are placed by the maker and they are like saying and i was saying that hey i would like to sell my my this is eater my eater at uh so and so forth price and uh i believe it's worth that much and and therefore i want to start at this price and the maker for the buyer will be like hey i would like to buy ether at this price and uh and therefore he or she creates the order and the reason why it appears here is because like no one um like there hasn't been like some sort of like a match of price so this is what we call spread so uh in between the uh cheaper sell order and the most expensive buy order there's some sort like uh there's a okay over here it's a one cent price gap uh whereby um whereby if uh if um this thing were to go a bit higher like just once and higher um the the this the the uh this two will match and uh you'll get executed and everyone will we will get their order fulfilled and if you are a ticker you basically come and say hey i don't care like how much i want to buy it for i just want to buy it like now so for the buy you'll get you'll get uh you basically take the order away and this is one that enhance the word taker and uh you'll buy um one if at a three three six zero uh dollars so um earlier uh so uh to recap there's a maker and taker and a spread so uh yeah these are these are fundamentals before we jump into the traditional way of uh of uh swapping tokens on a centralized exchange and uh we we do have a bit of problem over here like what happened if you want to buy like a hundred e for example and you don't care about the price but you want to get 100 beef and uh as you can see if you sum up all the eve over here right it's uh it's not a hundred if basically uh the price goes uh further so basically you're buying 100 if not at three three six uh zero dollars you're getting it uh you're getting one e for this this this amount and zero point five fifties amount and so and so forth so we can see that if we are getting 100 if we are not we are getting an average price of probably higher than a three three six zero it could be i don't know three three eight zero depending on what we call the liquidity of uh of the of the market so so yeah um and this little jump in price is what we call a slippage and a slippage is something that will also happen in unison v2 and but but but the hard thing about um this is that it's very hard for you to like estimate uh the the pr average price that you are getting so this is a bit off and under convenience and the second thing is that you notice that there is a discrete jump between uh the price because it really depends on whoever wants to uh make the market so it's not like a nice like 0.01 two three and four you just jump straight by 13 cents and then jump by one cent and jump by 10 cents and so and so forth so um yeah it works it sort of works but it doesn't have this like nice properties of like continuous uh and savage is hard to um hard to uh predict so so on to say yeah let's see if i have anything i miss out so um yep again the problem is that there are no like efficient uh manner to a pricing formula when it comes to a a motherboard market a order book mechanism and if we were to if you were to do an audible mechanism in a uvm it would be pretty much pretty expensive it will be um with what we call guess inefficient uh because there are a lot of like loops and uh matching and stuff like that that needs to be uh going on uh in the code and in evm uh there will be that will cost you a lot of guests or you need to pay quite a quite a huge sum of uh what you call transaction fee to do it and yeah so that that is basically the motivation for um when you saw b2 uh so uh the original idea of uh a bit of history i know it's not in the slides but the original idea of esop is it's uh by tech vitalik in in some east thesis post so he posted this uh this unison b2 amm model and uh the first person who who took it who write it make it to fruition it's uh hayden uh from right now i think he's the creator of the uni swap team so he wrote the v1 v1 version and so it's actually not his idea it's actually vitalik's idea yeah so what uh unisop v2 really is it's an uh i forgot to admit someone sorry it's an automated market maker now uh we we have seen the word maker before right whereby someone makes an order it could be a buy or sell order so what does auto make up market maker really means that is that unisop will always be the maker and you will always be a taker you will always be taking uh the the orders and whatever orders that you that uh that uh you want your place a unisop will always fulfill that order so that's so to say and hence it's called the uh automated market maker and amm is short and am is some common uh term that is is used uh because people automatic market maker is just too long to say yeah and uh there is this concept of liquidity provider right in in this case these are the liquidity providers like basically you you place a ub of the maker now um in uh in unison v2 uh liquidity providers will sort of like fund the pool so that the pool will will make on behalf of the liquidity providers so um we are able to uh swap tokens uh with the liquidity providers fund and uh what they get is that they get a small percentage of fee from your swap so because they have to uh undertake risks uh associated to uh the price movement and so and so forth so yeah they do earn something uh that's really fair okay uh i see no question so let's move on right uh so um unison v2 has only one uh very core concept and it's only one simple mathematical uh concept and it's this is what what they call the constant product model what it basically means is that um the multiplication of x and y has to be equal to some constant k where x is the quantity of token x and y will be the quantity of token y and these two will will will be multiplied together and this k uh to have this k constant and this k is constant throughout the lifetime of of the uh the latter of the contract so to say so um once k has been determined uh everything the price movement everything will will be fixed uh there'll be a change i mean these two can change but k will never change yeah all right well sorry i got distracted because a lot of people are joining and i have to let them in all right so um what so this is the uh equation model so this is a visual representation of how it look like so uh basically this is an uh inverse graph so if we multi divide x and x right here so we get a y equals to one a k over x and this is basically a graph that uh you'll look it will look like so uh in other words that um if k if a quantity of a token a tokens uh in the pool increases the quantity of uh b tokens will decrease so you'll move like uh to the to the right to the right and uh if the quantity of b token increases because you're you're selling b selling b means you are adding b to the pool and therefore the the quantity of increase the a token that you're buying that means you're taking from the pool will decrease in a in this in this direction like upwards direction so it just follows this curve right and okay uh before we deep dive uh i don't see any questions so uh we will hop in without further ado we'll hop into the code uh and took to look at how things are being done uh in the solidity level yeah okay i see no questions so uh okay to move on okay um yeah so first up uh this is the uh repo the github repo for us uh v3 we are not looking at vcc probably sometime in the future but uh these two are like the most common um the only two repos that we can look at and it's it's uh it's v2 core v2 peripheral v2 core basically uh contains the core contract uh like factory and pairs i will go through that uh later and peripheral is basically uh contracts for you to interact with um factories and pairs so um i have a simple uml graph that i've created but it's not very detailed uh there is a bit of error here whereby the error the arrow has to also point from router to the factory so there is another basically essentially another line here but what we can uh understand from this is that hey the factory will we have this great pair function that creates um this this contract this contract is what we call the unison v2 pair and uh i do have this thing called erc20 v2 s620 being wrapped on top of v2 pair we'll see what it means later but but yeah for now uh just remember that uh v2p is also an erc20 and ultimately as a user we are only we ideally we only need to um there's a spelling error here we only need to uh interact through the router and router will um we'll point our traffic to either the pair or the factory again there is a line that's missing over here just imagine that it's there all right so there's only like uh three main contracts that we look at and a couple of library library contracts which i will go into that later okay again i yup so everyone feel free to leave your questions in the chat i will look at them uh i'll look at them from time to time so without further ado let's look into the code i will start with uh peripheral sorry we have peripheral and we have core i already i have already cloned them into my uh into my local so um this is the factory the the responsibility of the factory is to create a pair now if you were to if you want to swap a token from let's say uh ethereum to bitcoin we essentially need uh the the pair to exist uh in the equal system so if it doesn't exist we need to create it and people and someone need to add liquidity to it and uh everyone else can um can uh can can trade can trade yeah you can trade on it okay so and this is the pair uh we should be created so let's let's look at how it's being created uh for now let's look at this uh createpair function and um what user you need to do again uh take note that we won't be calling this uh function but it's good to know because we'll be heading off to the router which will call this function and it's good to remember what is happening uh behind the hood yeah so there are two addresses these are token token b basically they are um they are the erc20 tokens that uh we would like to create a pair for uh at first it would check if these two are the same because it doesn't make sense to have the same uh erc20 tokens in it as a pair you just there's no this doesn't make sense and uh this line essentially you can see that they're ensuring that um if a is less than b and they order in such a manner and else it will just swap it around what they want to do is that they want to ensure that the address that has a lower value to be a token zero and a token one well it doesn't really matter a lot it it just helps to uh helps to query uh the token pair later so it saves a bit of like uh processes and hence i guess and um then we check to see if uh address if the token zero is address zero now why do we do it afterwards because we have already ordered it so um the smaller value will always be token zero so if this is address zero and it doesn't make sense and then we just uh it is a referred and the next step is to check if the pair has been created so uh again if it's created then there's no meaning in creating another another similar pair and over here it's a bit interesting so what is it essentially doing is that it tries to deploy the pair contract uh from from this uh factory so um we are getting the creation code because uh because uh creed we will need a quick creation code to basically uh deploy a contract and uh this sort is used in create two um so you need you you have the creation code here and then you have the sort and then using create tool you're able to uh determine deterministically uh create the repair of a known address so so afterwards you initialize the pair uh you're looking to initialize later and to tell to tell the uh the pair what tokens are being involved and we we populate we we create like a mapping in over here so that uh we are able to find uh the the pair address when we even is given token zero and token one and we do the reverse yeah and then we we admit uh we made a uh an event to to let people index and uh stuff like that uh hang on there's a question okay uh let me address this question can you go through later in order to form a fully uh working mission and just not just front end which which report we need to take care on uh if i understand your question correctly uh to fork unison v2 you essentially only need uh the call and the peripheral uh yeah the peripheral so these two contracts plus this one and uh whatever libraries that uh that are that are imported you also need to have it yeah so all these things basically you need to uh have those so yeah if you want to follow uh that is this is how you form and you just deploy them to the blockchain and then you if you want you can verify it so that everyone can read the code and yeah that is uh pretty pretty straightforward uh right so none questions clone seems more suitable uh yeah yeah basically that is uh the same the same the same thing that i was suggesting okay moving on so great pair um set fee 2 is basically a unisop team is able to uh set a certain feed that they want to collect uh from the last time i read the docs this has not been set yet so we are not going to go through this in the meantime okay so once the pair has has been created remember that uh we will create this contract so to say and and the first function that we will call is this initialize uh function whereby uh we are letting the pen know uh what are the what are the tokens that are that form the pair that they are going to be traded so token zero token one so note that this is called in the same um contract call as the uh the deployment and therefore uh this this one will always be uh be correct if you are not doing if you are calling it in a separate call then people will come and maybe probably put something in and uh it will be you'll have all sorts of weird things yeah so so uh the the the better way the bet the best way is to just call this function uh actually in the factory so that you would just handle everything for you including an initialization of and i i did mention that uh this is also an erc20 so look let's look at that now so if you look at the uh top of the contract there is this is thing is is um in uh programming it's something like uh like parent para plus i it's interface uh it's nothing so we just look at uh it's inheriting um from this erc20 contract so over here he has a standard erc functionality so that um what what you why you need it because um liquidity provider will be given some sort of token as sort of like a share uh that they hold in the pool so that when they withdraw they're able to refer accordingly uh yeah it will be the the value the the the the tokens they have plus the fees that they earn so it will all be uh uh is it for for easy accounting purposes they have this erc20 and of course because it's an erc20 you can uh you can give it you can trade it you can translate into all sorts of uh defined defined stuff yeah okay so right so these are the uh the factory creation um methods so now let's head over to the peripheral uh peripheral v2 peripheral to look at the router because we essentially what we will be doing is that we won't be calling the if you want to add lp or you want to create a pool you you won't be needing to call this manually instead what actually you'll be doing is that you go to this v2 router and then you'll call this add liquidity uh function so there are a few uh there are liquidity and liquidity if uh the difference is that uh if you are you are um adding interstate if the native token enhancing over here is payable and at liquidity it means that uh you are adding in terms of erc20s like ref it's a it's a red if so to say and ref is pfc20 if it's not erc20 you need to wrap it to ref to make it the rc20 yeah so um we look at this one because they are fairly similar so what what happens at first it's that um oh yeah okay before i go on there's a question i think the latest yep uh we don't need a safe mess with 0.8.0 because this is way older and therefore they have to use this mesh good point and uh all right so first first thing first is that uh the user you need to specify like what tokens uh that they want to add to the pro token token b and the amount that they desire and the minimum amount they want to get from it the address two that means who won who is the liquidity provider it could be it could be someone else aside from you then the token will be given to him but normally it's uh it's yourself and the deadline now daylight is important because um you know if in case your transaction transaction got stuck there for i don't know how long if if you put like a low gas price and uh you like change your mind you said hey i no longer it's no longer profitable for me to add to this pool and if your deadline is set correctly it will fail afterwards and your funds will be safe if you put a too long deadline you'll unless you cancel the transaction you will go in and uh you might not it may not be something that you want already so they do have this deadline and uh this modifier over here that ensures that uh the date line is greater than the block block timestamp yeah if it's otherwise it will fail and says that the the transaction will exist by it okay moving on so the first function the first internal function that you'll call it's uh okay sorry uh that's another interesting question if your color proxy pattern used by yourself oh unity doesn't use proxy pattern yeah you use a crate tool to directly deploy the good question uh add liquidity uh internal function basically uh helps you to calculate um the amount of token you you need to put in so over here we okay before that uh notice that i mentioned that we don't don't have to go through the factory to uh to create a pair because uh this liquid underscore liquidity has already done it for us so if if we check that the pair doesn't exist it goes to address zero it will create a pair for us by calling this create function and if you recall earlier that we have really gone through the the logic in there and you'll create the the pair for us so it's already there yeah it's guaranteed to be there if you were to call this correctly yeah and then you check the reserve and uh there are two cases that can happen the first case is that when results are zero so this happens uh when the pool is empty normally when you just create a pair and you just directly add the amount uh to the pool so now that so now uh the pool will have this amount of a this amount b so basically this this determines your starting price so if you were to initialize it at the at a bad rate uh you are giving uh arbitrages a huge opportunity to uh for profit they will just arbitrage your funds away and yeah it's not something you want so uh if you first initialize two i mean i mean first create a pair through this uh electricity function uh make sure that you are giving it a a amount that is sensible okay and this is something that we normally will look at because all pools are normally uh have funds and uh yeah so the first step is you'll call this library code function code function basically calculates how much token b that you have to put in the pool given the token a that you you want to put in and uh i do have this library uh open so let's look at that there you are so uh it will uh do some basic checking and uh over here is uh this is like a ratio you can think of like uh amount a over amount b has to equals to a amount a reserve a over reserve b so you just uh sort of like multiply across to get the amount b uh that that you that you are supposed to put in so with that amount b it checks if it's uh if it's uh lesser than the desired amount so if it is you'll go this you'll go to this uh this this branch if it's not you'll go to this branch so basically what it really means is that uh do can i get can i get uh oh hang on let me let me let me uh sorry for rephrase uh can i get the less than the dessert from it doesn't make sense nevermind okay let's go here and then there's this required step that's that checks that hey can i get more than the minimum amount that i'm willing to get yeah that makes much more sense so if i can i will i will immediately execute this one if i cannot then it would just uh just say hey uh i'm i want too too little i want uh too much too much amount b and uh and if i were to add uh add liquidity to this pair i wouldn't get uh the amount that i want so it will just fail and yeah and uh and then uh it will just return uh the the values that you have to add in among a and b and this is the opposite you will then check if you if this is not successful you will check for the other branch so these are just mirror images and yeah so uh basically what uh what you can think of in the higher level is that this internal function gives you the amount that you have to put into the liquidity pool so if you didn't quite get all this logic uh just remember that by calling this internal function uh with the correct parameters you will get the the amount that you should put in uh into the pool so given x amount of a you are required to put in x a y amount of b and yeah something in there okay uh write the questions and then uh it will then transfer to the pair uh x amount and amount a amount b and then uh it will you call your min uh some lp tokens to you and this this is what and this is what you will represent your liquidity yeah so this basically this is how we add uh lp uh into the pool plus create a repair if it doesn't exist now one interesting thing that i have to mention is that you notice that um we transfer to the pool and then we min so let's take a look at the bin function because uh it's a bit interesting all right where is my coin oops there you go so this is the main function so notice that we are transferring to the pool and then we call the main function but in the router it's being done in one transaction so it's fine but if you were to do it in separate transactions notice that uh this main function it's uh it's not protected anyone can call it so let's say i want to i want to like deposit token token b directly into the the contract without calling the main function like anyone can come and call this function to sort of like claim the lp uh for themselves so what you essentially really want to use the router because it does everything automatically yeah so yeah this is something to take off and uh that is slot mechanism uh basically it's just a it's just something something like a reentrancy guard uh because um some erc20 tokens have this uh that allows a callback you can like you can call you can create a callback to this main function and do all sorts like uh malicious uh things so that that is uh what people call the reentrancy uh attack or hang yeah so this lock basically uh prevents that and and then um one interesting thing they have to mention that uh it it means it means uh liquidity here and uh notice that this uh that this uh square root sorry uh let me let me let me take a look yeah so when initially when the total supply is zero uh this this is very funky uh manner of maintaining liquidity and uh this sub minimum liquidity kind of thing and why why do they have to do this in a very funky funky manner so according to the white paper i had to read this up also um let me let me just no i won't draw another one let's see if i can try to recall uh it's too sort of like mean extra token and this extra token will be sent to the address zero so no one has access to why because um they want the total amount of liquidity to be at a higher value so it's it's harder to celebrate manipulate the lpa values so and so forth yeah but that's a very high level uh explanation uh yeah the white paper has a better explanation but yeah i just find it interesting because they have this uh thing here and i got me curious but normally uh if the total supply is has already uh i mean the pool has already has total supply uh this is a formula that you you get um to uh for the liquidity um they implement uh it's not very clear here but uh they implement the solar call it the i think the square root mean or something geometric mean or something so that uh the value is uh i can i would say more correct again this is uh explain in more detail in the white paper as to why they are doing uh these kind of things yeah and uh some checking and then finally you'll just mint lp tokens uh to your true address so yeah so again uh for those who do not follow this logic min basically gives you lp token and lp token represents your share of the pool uh after you have added the liquidity to okay uh that's that's basically half of the uh solar equipment okay yeah so the minimum liquidations are constant yeah in this case yes and they have a reasoning behind why this number so uh for those who want to know more uh the white paper is a place to go yeah that's correct uh right uh okay before we move on let's see if i have any question uh in the chat uh no i don't think so cool okay so what we have is actually done is that we have covered the uh creation of the pair as well as uh adding liquidity to the to the pool uh what the moral story is to use the router because uh you have done a lot of uh atomic work for you so that you don't have to call separate transactions at different times and people will take advantage of of this time gap so uh always use the router because it has all the logics there next we'll talk about swaps which is also an interesting feature again um the main uh the main function for swap is this one it's a bit hard to read but i'll go through it anyway uh because there are some interesting things that uh that uh that i wanted to point out so essentially what happened is that oh yeah okay before before i uh i remember that there was some sort of penalty for not depositing equal amount of lp tokens i don't i don't know if it's a penalty or you a transaction will fail yeah basically uh i guess it will fail but yeah you can you can try it out okay so what how to call this uh this is sort function again notice that it has this re-entrance guy uh you you specify the amount that that you want to um to get and uh who who who gets the amount and this data it's it's something interesting that we'll go through later so this amount out is a token amount that you want to get out of the pool and it will check if there is sufficient liquidity and if not it will say that hey uh the pool doesn't have enough funds for you to uh to swap yeah and then yeah it gets the uh this one token zero and token one and it stores into a memory why they want to do that is because um [Music] doing this uh like okay so underscore open 0 it's a it's a mem call it's a memory call if you're reading direct if you're reading no underscore token zero you're reading from the storage and uh reading from the memory is cheaper than reading for the storage so if uh if they didn't do this step and they were to just uh say that hey everything is just like that uh every token zero call is a it's an s load which is more expensive so you have to spend a bit more gas so this is something like a caching to the memory uh kind of way of doing things right and uh they do a checking to see that if they are if the tool is correct i mean so you don't send you don't send to the tokens right you know what you don't like say that okay i want to sort like uh i'll swap ptc and then send back to the btc contract itself because that's not not correct and then um now these two steps is interesting they optimistically transfer tokens notice that um there's no calculation involved yet right they just say hey i want i want one million uh ethereum and they'll just give and they will just transfer it to you now why is that this is something that the unison v2 has it's what they call the flash swap basically uh they trust that you will return um you'll return the funds later on uh here down here into the pool and basically what it does is that it's something reflection whereby it allows you to gain a lot of funds to do all sorts of all sorts of things like arbitrage and stuff like that so traditionally uh people use ave to uh to do a flash load and uh and ave uh it's it's not easy to use and over here you can just like hey you can just say how much you want and then they'll just give it to you and uh hence hence this data come to play so if you have a data data specified it will call this function and then it will give you and then you can execute whatever you want why do i say that because this this tool could be your contract address or whatever address that you want right so if it's a contract address your contract could have uh this function to call and then this function has logics so you can say that hey um i i'm taking like one million if i'm sending over to my contract and my contract has logics to to do like other things right and uh and therefore what it really means is that unisop is borrowing uh tokens to you first without checking all right you and at the end of the day you need to uh return your balance um to the contract to make sure that uh the math is preserved the the the the reserve are there and stuff like that everything everyone checks out if you do not uh the entire tradition will fail and uh nothing happens that you just have to use pay gas so this is an interesting kind of thing whereby uh you can pass in data and then you can call your own contract and your contract can do all sorts of funky stuff okay a current function in fp or in general used to make functions composable yes something like that yes and yeah and um and then this is the method sort of like check if you have returned your your balance or if you do not specify the data you just go through this this uh normal process whereby it gets how much you have to put in to get how much you want to put out yeah basically the math it's uh let me go back to the slide uh the math here is a bit a bit complicated but let me go back to the slide so that i can explain it better [Music] oh yeah there you go so remember our constant product model whereby x multiplied by y has to be equals to some constant k so whatever you take out x minus x sub 1 for example multiply by y plus y sub 1 has to be equal to k so if you if you take out some amount of k you have to uh give some amount of uh sorry some amount of a you have to give in some amount of b so that the k constant is satisfied so so what's happening essentially in over here is that it's doing the math with some concern to make sure that it checks out yeah uh okay enough time so i'll go to that detail and this is yeah make sure that it uh it maintains okay and this update is also an interesting thing that i i i would like to mention uh what it means what what this update does is that it sort of like update the price zero cumulative last and people can use that to uh as an oracle uh it will it will um update the last price and uh and also update the last uh block uh the timestamp that is last updated and using uh the last price like maybe five blocks ago and livestreams now you can sort of like get a average weighted average uh kind of thing so yeah it has a sort of like a built-in oracle if you want to know how to like write beta oracle on top of uh by utilizing this uh cryptid flash uh usa v2 documentation has a very good uh good guide but we won't go into that today okay so uh this is the gist of swot so again uh the high level overview is that all right so um let's see where there you go yeah so you give token in you expect to get token out and uh the console mod console product model has to be uh satisfied and you can do a bit some funky thing called flash swap uh in this sort function yeah so that's uh that's something that you if you don't understand everything these are the things that that you can you that you only need to understand so let's move on to uh the peripheral to see her hug the router to see how it's being done so over here there are [Music] there are four functions essentially it's soft exact token for token sub tokens for exact token sub exact if for tokens and slot exact tokens for example thought talkers for exactly like you may you may ask like what on earth or functions they're all swaps yeah they're all slopes um except that if you call x sub exact tokens for tokens that means you're giving you're specifying that you want to sort maybe one if and the output could be like any any any values that you that you want right and it's a minimum value uh that that's because of the page and sub tokens for exactly tokens is that given some x amount in you would want to get specifically how much token out uh from from the pool and uh this uh equivalent it's just that instead of giving especially you are giving it if instead and then you do the conversion uh in there so we won't go into these two uh because they are like similar so why do we do this um i can foresee that uh for example in my smart contract i would like to interact and stop tokens in unison i would like to maybe calculate us dollars exact us dollars in terms of uh token price and i and before all this funky math it's hard for me to like know how much i'm getting so i'll be calling um maybe five us dollars and i don't care how much you know i get back i just want to get some eater and then i can use what the eater to do whatever i want and over here it's like i don't care how much ether i put in i just want to get like five dollars worth of uh of eater so it creates this kind of flexibility for you to interact uh i find it a bit useful uh for smart contract developers who who would like to interact with the router to uh to have access to the pool yeah so what it does is that uh it's it calculates the amount out over here amount in over here uh using the constant product model here the constant product model uh this is the concept model i know it's a bit fast but it is that you can take a look later and then uh it it at the end it calls this slot underscore swap which which uh we should ultimately call the swap function in each pool pair now i have neglected this path parameter what what is this form this path is basically for when you are trying to sort among like many many pools so if you are only swapping between let's say uh if and die the the path will be breath and uh and die there only be two um two addresses in the array but if you like to swap like weft to die maybe to usbc to sdt and so and so forth at the end you you will specify a longer path to sort of so that the contract will know like what post do you want to navigate through and they will sort accordingly uh yeah so over here there's this a four for loop that sort of gets the path and then uh and then uh for each part they'll they'll swap get an intermediary token and then you perform another swap and so and so forth until you get the final final output that you want uh we specify as the last uh last address of the path so uh i hope that that is clear uh any questions no okay awesome all right uh it's it's okay yeah it's the multiple yes it is atomic yes uh all transactions if blockchains are ethereum atomic so if one fails the environment yep it is uh okay um there's one more thing that i didn't mention it's a it's a burn function learn function is basically the uh remove liquidity so um once you want to remove your liquidity as an lp provider you call this burn function you specify like how much liquidity you want to burn basically how much lp token you want to burn and it will call the the burn function in the pair so um here and then it will it will return you with the current uh [Music] amount of your of your of your tokens uh token zero and open one yeah uh again this this uh this this formula it's follows the ratio like like how much how much data you have and then you follow second how much you'll get after reserve again i'm sorry i don't have time to just to go through the math over here it's slightly complicated and uh i think that is about it we do have a couple of minutes for questions so uh yeah uh happy to uh to answer any questions and uh and feedbacks if you want if you have any yeah i'll refer back to you question about something earlier okay okay after question uh when a swap function is defined there's a underscore swap function i didn't see underscore function defined uh normally uh conventionally this underscore thing really means an internal function so uh followers would like to know more what is an internal function it is uh sorry it's not here it is uh a function that's not been able to call by by by outsiders it's only be able to call by uh functions within the contract so maybe hang on let me get to the peripheral because this underscore stop is there so the underscore swap this in here here and this underscore shot uh helps us to um [Music] to pick the path and then you'll call the uh sort function in the in the pool pair first so it's sort of it you can think of it like it's like a wrapper whereby you have like you want to trade between like this pair and then you want to trade like maybe if to die and die you want to buy you want to buy uh maybe btc with diet and then you trade to btc and then so and so forth so you specify the path and then you have this for loop to nicely called the subfunction uh in order so uh yeah uh sorry i missed the early part but what is the benefit of using a constant product model uh basically uh the benefit is that i can think of uh is that it sort of like helps you predict uh how much token you're getting out of it uh there is a lot of um other uh other ways of training token but uh amm is sorry the concept bullet model is basically the easiest to implement in usa b3 um they have this they're also using constant product model but uh it's a bit different whereby uh each this thing called like regions or ticks each stick will have a different liquidity concentration but but the formula in this take is uh using concern a product model and uh in curve is something else in balancer is something uh it's a some other uh model so i think the benefit is i think uh you can quote me on this i think the benefit is that it's really easy to understand and implement um maybe one more thing to add for uh why am constant product models is that uh for those of you who are around in 2018 2017 2018 when there was the first decentralized exchanges uh it was order book but it was very cumbersome uh every um order you want to make any kind of approval any kind of action takes uh gas and has to be done manually so that like created a lot of friction and amms just made it very very easy for people to uh to swap you don't have to like uh do like three or four transactions just to get your transaction confirmed um yeah so that's one of the benefits of amms thanks sorry thanks sorry appreciate that i didn't know that and it's very expensive for the book yeah yeah okay so i think that's uh pretty much it for today unless there are any final questions um before people go i would like to send out this form let me just copy the link um so just a feedback form uh for how it was uh today uh so if you've liked anything or if you didn't like anything um or if you have any suggestions for another protocol to dive into under the decoding series or you just want some other topic let's say this is too advanced uh or you're interested in something else uh there's no in the form and we can see if we can do a workshop for that yeah uh yeah those are basically decoding series uh basically try to cover like everything under the blockchain uh of course uh i may not be the best person to explain about this but i think later on um it's only more like a discussion so yeah things that i'm pretty sure i'm not familiar so i definitely uh that's all that everyone like in this in this chat can always always give you feel free to give that back it's a it's like a two-way two-way kind of thing yeah and uh before uh uh answers the final question uh just just to as a note for everyone like we're all learning here uh even if you've been in ethereum for seven years there's always something new to learn so anyone feel free to uh suggest any topics and if you want to lead the discussion uh we're happy to as well it's a community meetup you know there's no leaders here uh or at least we'd like it to be and yeah so before we end uh teacher i think you can answer alex's final question and please remember to repeat the question all right sure so alex alex question is um he was wondering if uh he was wondering how to reduce slippage costs and uh if you want to reduce the cost how what what are we looking to optimize so slippage really um relies on uh the liquidity in the pool so if you have like a huge liquidity uh let me get the slides because the constant product model really explains everything uh where did it go [Music] so imagine that you have this large x and large y basically large x and large join basically means that we have a lot of liquidity so if you are if you are like if you are um we are swapping a small it's something a sum up it can be a big small big sum and the y will will also change like in a small manner so that really means that we the slippage is very low but if you have a very low low liquidity like you can think of like small x and small y changing x is the same way as we were to change it just now we'll we'll cause like the y to change in a very drastic very drastically and hence the slippage will be uh very very high so the the only way uh uh to to reduce a slippage is for the pool to have like large liquidity in unison v2 and hence uh judicial v3 has this thing called a concentrated concentrated liquidity whereby at certain range some liquidity will be much much higher and therefore if you were to trade in those like concentrated range the slippage will be will be significantly reduced yeah and that basically that's uh that's that's it uh stupid always about liquidity that they have all right uh thanks thanks everyone for joining and thanks a lot to john for sharing uh if you have any other questions uh feel free to go on telegram and and kind of continue the discussion there and um yeah if you haven't answered the feedback form yet please do you can leave yourself anonymous um and yeah we just want to like have a better experience for everyone yeah so uh the main purpose of uh trying to introduce equality series is to introduce more technical stuff i i personally learned a lot from my uh from my research on community shop v2 i i didn't know like some of the things that it exists and uh yeah i i had fun i hope you had to all right thanks a lot guys thanks bye thanks for having me
Automatic transcript — names and jargon may be misspelled.