The Uniswap V2 Pair smart contract manages liquidity pools through three core functions: mint (for adding liquidity and minting LP tokens), burn (for removing liquidity and burning LP tokens), and swap (for token exchanges with a 0.3% fee). The contract uses the constant product formula (K = reserve0 × reserve1) to maintain price stability, and implements gas optimization techniques like variable packing and low-level calls for efficient token transfers. The price oracle calculates token ratios based on cumulative price tracking, and the contract includes re-entrancy protection and balance synchronization mechanisms.
Uniswap V2 Pair Smart Contract Explained (Part 2)
Added:hello everyone um my name is castrinojuku I'm a software engineer and a blockchain developer so in this video I'll Be taking out time to break down the uniswab V2 pair smart contract so this is the part two of the uh first video I made earlier yesterday so the first video I I uh explained the unit swap um V2 erc20 contract and also the unisa V2 Factory contract and I was able to explain that okay the universal V2 Factory contract is a contract that uh that clones the um the unisa V2 pair contract each time a user is providing liquidity for the first time and I also explained that the unit swap V2 US 20 contract is um it's a a standard erc20 contract that's a an erc20 token that gets minted to liquidity providers each time a user provides liquidity on the unisual V2 pair contract that that token that is minted to the liquidity provider serves as a certificate or serves as um a right that this person uh it's it's partaking in a particular liquidity pool so each time users swap on that um on that pair or strap those two tokens this person will will partake in the 0.3 percent that unit swap charges uh as fee for for trading and all of that so I was able to explain that in the part one of the video so um in this video we are going into the main smart contract of unit swap that handles the swap handles the liquidity proof provision handles the removal of liquidity and it also contains the function that um calculates um that it also contains the unit swap price Oracle which which calculates the ratio of each token for instance let's say let's let's say we are working with Shiba and Dodge Coin so the price or apple will tell you the amount of Shiva Inu token you will get when you bring a particular amount of Dodge Coin and it to also tell the amount of Dodge Coin you get when you bring a particular amount of Shiba in and this contract also handles the unit swap um uh optional fee like okay well like I said in the previous video uniform can choose to turn on or turn off their their fee whereby they'll be getting one over six of the total fee that um that the platform accumulates over time that the each particular player accumulates over time all right so um if you like the content you're getting uh don't hesitate to subscribe to my channel and also turn on the notification Bell so that you get notified each time I drop contents like this so I'll be doing more of this review I'll be reviewing more um uh more complex protocols like um like Master Chiefs matches for pancakes while I've been reviewing um compound protocol and now between synthesis and all of that so stay stay connected so you get notified each time I review a contract and I break down the codes all right so without any further Ado let's Dive In so right now currently I'm on the um Minnesota v2p smart contract so uh this is the left hand side of my screen is the Smart contract itself while the right hand side of my screen is um where I did my documentation when I was studying the smart contract so before we move ahead let's just read a little bit of this documentation so uh what we have here so you need to have V2 pair so the initial V2 ped or so contract handles the heavy lifting of the entire universe V2 platform all right so it handles the heavy lifting it's it's the I think it's the most complex smart contract in the entire industry to um uh protocol so it contains the mean function which handles liquidity provision it has the bond function which handles liquidity removal it has this swap function which controls swapping from one token to another all right so the V2 pair contract functionalities are managing funds handling functions for liquidity providers handling functions for Traders that's swapping managing pool ownership tokens which is the liquidity provider tokens that gets mitted to users each time they provide liquidity and it also handled protocol fee yeah that is the one over six uh the one over six and the 0.3 percent I tell you that each time user swap it takes that fee all right all right so let's go down now here we have code breakdown industry to pay contract so first we are looking at the managing funds so you need swap pair is an exchange between a pair of token for example Dodge Coin and ship I know so we'll be using these two guys as our example these tokens are represented by token zero that's contract address of the token zero and token one contract address of token one in the pair contract so like I said in the previous video for every for every talking you swap on uni swap let's say you want to convert your Dodge Coin to USD to any swap there is a pair contract created for that let's say you want to convert your Dodge Coin to eth there's a fair contract created for that and the the pair contract will contain the contract address of the first token and the second token for University and Dodge country to be usct contract address and Dodge current contract address for ITA and Dodge Coin now if that doesn't have an address so unit swap now make use of what they call W eth so that wbth is called Rapides that's what they used to represent ETA so that ETA is um is that W it is what they use so whenever you are swapping to either or if you're if you're swapping to either that w-e-t-h will um they if you're if you're swapping to either what they use basically is w-eth to to keep track of the token in the pair you understand so they use wets to keep track of the token in the pair but uh at the end of the day the wh is Bond and ETA is given to you so that's what unit swap is doing and all of that so as I was saying the tokens are represented on line 88 the tokens are represented uh by talking zero that's contract address of token zero and talking one contract address of token one in the fair contract so let's look at this before we continue with this reading let's move over here so let's look at the contract so on line one we have our version declaration which is uh 0.5.16 and uh on line three this way we are importing the i unit swap V2 pair so like I said the IUD Swap this is an interface so um I always I always repeat this so you don't get confused there are two ways to use interface in solidity you can use interface for implementation you can use interface for interaction when the interface is imported but it's not inherited by the contract where it's being used that means you're using that interface for interaction but when the interface is inherited that means you're using that interface for implementation you understand that that means you want to implement the functions in that interface and you must Implement all the functions that the interface contains else uh so let's show you error all right so here we are importing the unit sub V2 erc20 token so this is the ic20 token that gets minted to uh liquidity providers when they provide liquidity we are importing it as well the reason why we are importing it and we are inheriting it so that we have access to the functions of this contract so once you import you have access to the functions especially the mint and the bond functions that's what we'll be using mostly here all right so we have the library smart dot so so this smart.so is um it's a library used when you're performing a mathematical operations in solidity so it it helps save against overflow and underflow I won't be explaining overflow on that flow as a smart contract developer or a blockchain developer you should know what overflow and other flow means all right so on line six we have this Library uq112 times one one two dot so so now we all know that um solidity solidity doesn't support um floating numbers like it doesn't support floating numbers and all of that where you have decimals so this doesn't support decimal so this library is used to handle uh those floating numbers used to handle fractions yeah so just take it like that just know that this library is used to handle fractions floating numbers decimal and all of that so you can check this Library out to see how it works I don't know I might I might I might be I might be um happy enough to explain it but let's continue if maybe if if I if I can't explain it I will but for now let's continue all right um so on line seven we have um the ielt20 so this is the um irc20 interface yeah so like I said this guy is also imported like and I think this guy will be used for interaction not implementation because we are not in inheriting it here then on line eight we have the I unusual V2 Factory so this is the interface for the unit swap V2 Factory which which we explained in the previous video in the part one of this video so if you don't understand what the factory does or what the erc20 is 20 dollars you can go back to the part one of this video watch it before coming back to the part two then we have the unisa V2 colleague so this unit V2 colleague is just like when you're swapping uh you did if if the the person that initiated the swap transaction initiates the transaction uh this uh V2 Corey um notifies the the user or the trader that okay your performing a strap event and all of that so we'll see where this guy is being used why we continue in the contract so just take it like just understand that this guy is used to to notify the the the um the trader that is performing a swap transaction all right so uh head off with the import and all of that let's now move to the contract proper all right so on line 11 we have unit swap V2 pair is high in the swap or it's high in the swap with V2 pair so it's inheriting the highest RV to pair and it's also inheriting the uh it has 20 contract that's the use of your extensive contract so here we have um using Save Mart for you int all right so this is uh so there are two ways to use our libraries you can use the library like by just calling the name of the Library dot the function you want to use in the library and make use of it or you can use it like this using Save Mart for you it and using uq112 times one one two four you're in 244. so this is another way of using the library so once you do this you can always use this Library wherever you want to use it okay so let's proceed so if you want to understand the various ways of using libraries and all of that you can study more about libraries all right so now here we have um the minimum liquidity so this minimum liquidity uh it's it's set here so that this this is said and used to avoid division division buys you all right so um when liquidity is being calculated like when uh when uh a liquidity provider provides a liquidity and the and uh you need to have wants to calculate the amount of token that will be minted to the person there's a calculation that is being done that requires that um the the divide by by something to see it when we get to that uh that divides by a particular denominator and it makes use of liquidity to do those Division and if we don't set maximum liquidity it will get to a point where liquidity will be zero so if for instance now you know when you provide liquidity you can also remove your liquidity so if users remove audio and put it at some point becomes zero so but with this minimum liquidity liquidity can never get to zero so the minimum liquidity is this um this uh 10 it is power of 3 which is uh obviously 1000 this is the minimum liquidity so it's used to avoid division by zero then here we have um we have byte four private constant selector so unit swap is not unisa is making use of um low level core when they are performing their transfer um operation so if they want to transfer uh let's say you want to transfer uh in your in you know in your swap or if you're providing liquidity the talking needs to be transferred from one person to the other so but you need stop with uniswap ensure that the they did a lot of gas optimization so because of that the images of uh they are making use of low level calls in their transfer and one of the way to make use of low level call is to use the call function you just have called function and with the uni swap call function you can do something like um API encode width function uh API encode with selector and you pass the function selector of the particular function you want to call then comma you put in the values and all of that and it will call that function for you using the function selector and the values you pass and that is the more gas optimized way to perform that operation and that's why this app is doing that so now what they are saving here is the function selector of the transfer function and if you if you if you have if you're advancing solidity you understand function selector you know that you have function selected basically the name of the function and the function type like the type the the the type of the parameters or the argument of the function so that means this function of taking an address and a unit and mind you whenever you're making use of function uh selected if you're doing stuffs like this you have to type in the type completely you don't we we know that if you type in unit it's uh you is the same thing that's mean two five six but when you're doing this you have to type it completely you mean two five six don't just put you into there all right okay so uh with that said let's move on to line 18. so line 18 is um a state variable called Factory so like I explained in the previous video when you're deploying this contract it is a factory contract that deploys this V2 pair contract right so now this uh a factory variable here holds the address of the factory contract that deploys this contract that's why if you check your Constructor where is our Constructor let's look at it if you check your Constructor uh uh yeah so once you deploy this contract the factory is set to the images or sender and since the factory contract is the guy deploying this contract automatically the images are sender is the address of the factory contract all right so that said yeah so what is being saved here is the address of the factory contract that deploys this pair contract then you're talking zero and the token one so these are the two tokens that this pair will be swapping so uh when uh when this um this pair contract is created the there's a function called immediately after the contract is created that function is the initialize function this is it so this initialize function takes in the address of the two tokens that this pair will be will be swapping like this pair will be handling so this is the address of the first token and the address of the second token so the address of the first token is called token zero and the second one is token one so once this is passed uh it will now set whatever you pass here to the Token zero State variable and whatever you pass it to the Token one state variable so uh this code is giving me notifications let me close it once and for all so we don't get distracted with this guy all right okay so this is it so this guy sets this initialize functions sets this token 0 to this guy and this guy to this guy and yeah and this is called immediately this contract is deployed so that's why we have this variable here so this is just the address of the two tokens that this uh uh Fair contract will be handling all right so let's move over to the um Line to Line 22 where we have the reserve zero so Reserve zero is reserve zero keeps track of the amount of token zero that this contract has you get so it keeps track of the you know when you're providing liquidity you're bringing a particular amount of token zero and a particular amount of token one so that amount of token zero you're bringing when you provide liquidity is what this Reserve is holding and they they they token zero is for example zero and the token one is for reserve one get that so let's let's look at my documentation uh the right hand side of my screen so you can you can get this guy so okay yeah uh okay okay we have talked about tokens are presented by token zero contract address of token zero I'm taking one contract address of booking one in the pair contract so Reserve variables store how much of the token the per contract has so result zero is holding amount of token zero in the per contract and reserve one is holding the amount of token uh talking one in the air contract right so and token zero is a contract address of token zero that's one of the country one of the talking of the pair I'm talking one is the contract address of uh the second token right so uh that said let's look at um uh uh block uh timestamp so block timestamp keeps trap of the last time the reserve was updated so now this is what happens here when you provide liquidity for the first time block times time is up is holds the time that liquidity was provided now when someone when someone provides liquidity again that means this Reserve is updated because new token came into this contract block timestamp also gets updated to the current to that time that was done when liquidity is removed as well that means Reserve will change and this will change block timestamp 2 will be updated to that time when swap operation happens when there's a swap I remember when swap happens that means you're bringing the particular amount of token of token zero to get a particular amount of token one or you're bringing a particular amount of talking one to get a particular amount of token zero so that means if you're bringing tokens zero to get to q1 that means the reserve of token zero will increase and the reserve of token one will decrease so this block timestamp again long time stamp last will get updated again so for every time there's a change in the reserve this guy gets updated so if we look at what I wrote here say this keeps track of the last time the reserve was updated right and if you see unicewap did what we call variable parking here so they ensure that these three variables are in one slot so okay a slot is made up of um uh a slot it's made up of uh it's the maximum amount of a slot is unit two five six so that's the maximum amount of the maximum amount a a slot can take in unit so let's let me go to substitution solidity so let me show so so like I said a slot in a unit slot or a slot in solidity or just sorry I say solid it's not in the evm takes up to unit 256 and if you say type uh you ain't two five six but Max so this is what this is the maximum uh this is the maximum amount of uh units is not too thick this slot you think right so now you this is sweet one one two so if you say uh unit one want to you let's do one one two plus one one two plus thirty two what does that give you in two five six so now that means this guy this guy and this guy since they are grouped together like this they will occupy one slot and this is also a way of minimizing uh gas like yeah this is also a way of uh optimizing gas and all of that so what they did here is called variable packing so you can study more about variable packing uh as in a smart contract developer all of that so let's move on to line 26 so line 26 the the name of the variable is called price zero cumulative last all right so this variable is what the unit swap uh price Oracle uses to perform its magic or its logic so let's look at it so price zero cumulative last this keeps track of the cumulative price of token zero each time there is a change in reserves you you get so what happens is let's see um let's see uh how will I explain this for you to understand okay I'll do the uh I I I I might do this calculation but it's not the most important thing in this contract so if you might want to dig deeper into the board I yeah I'll do the calculation if if maybe I have a lot of time but just know that this keeps track of the cumulative um this keeps track of the cumulative price of token zero each time there's a change in the result and price cumulative one this keeps track of the cumulative price of token one each time there's a change in results so I think I performed a mass here for it uh let's see how unisa price order could work so you see cumulative price each time it gets incremented it gets implemented each time there's a change in uh maybe there's a swap operation or there is uh yes liquidities provided or liquidity is removed this stuff keeps adding to this it adds the new price it doesn't smash and adds to this guy so it's like accumulative with kids incrementing it doesn't go down so but let's just leave it at that for now uh there are a lot of things you need to understand before you understand these two guys all right so let's move over to care last all right so pay attention to this guy remember that I said okay in the part one of this video if you watched it I said that um uni swap make his his uh his an amm it's a decentralized exchange that makes use of the emm mechanism that's the automated uh Market maker mechanism and it's making use of what we call um uh um constant product uh images of constant product uh calculation or constant product mechanism now constant product K here is a constant and the product now what unit swap does basically is the constant here is the is the product of Reserve zero times reserve one that is what is saving here you get so uh imagine let's say the first time we are providing liquidity for this uh contract uh let's say I am the liquidity provider and I want to provide liquidity for 1000 uh Shiba Inu token for 500 Dodge Coin now the chair last the K last at this time when I providing liquidity will be 1000 times 500 so this will be the key last at that time this is the product the the constant product of Reserve zero and reserve one because once I provide liquidity the reserve this Reserve zero will be 1000 and this reserve one would be 500 so The Killers will be this guy which is uh 500 000 yeah so that would be the key last so this Galas is used in a lot of calculations in the contract so uh is it a lot of calculation is used in I think it's used in the mid fee function so we'll see that when we proceed but for now just understand that this is what it holds so this is it's already here uh results have one as of as of immediately after the most recent liquidity yeah demonstration liquidity when when uh liquidity is added or removed so it just does the product of the results have one all right okay let's proceed uh so online 30 we have unlocked equal to one so modifier log required that unlock is equal to one uh Universal V2 log then it sets log to zero uh then proceeding so what this guy does this is just used for re-entrancy this is just used for real Transit that's just the function of this guy this this this whole guy now is used for re-entrancy to guard against re-entracy so just know that just know that so if if if you want to if you want to try and understand how this works so I think I can do what they're explaining so um at first this guy is is uh is unlocked it's equal to one which means unlock is true right so now when you perform when you're performing um a swap transaction when you're performing a swap transaction it first requires that lock is equal or unlock is equal to one which means it is unlocked now once this line is passed you now set unlock to zero that means it is not uh unlocked to zero which means it is now locked so once unlocked is zero that means unlock is false which means it is now locked then once this is done this underscore here means it will now perform the operation which you asked it to do right once it's done performing that operation and that operation has passed it will now set unlock back to one which means it is now unlock you cannot perform another uh operation but for the time this law unlocked is zero you cannot perform any other operation you get and this is for a particular User it's not for like if if it's unlocked for you it's unlocked for you any other person can perform their transaction so yeah well I I really don't want to be explaining these guys all this small small stuff like this modifier you should be able to figure out what this thing does because it will take a whole lot of time and I really don't want this video to take a whole lot of time all right so let's proceed now on line 38 we have the get reserves this is another way of unit swap trying to do gas optimization so what they are doing here is reading from States like if you're if you're performing um operations and you're you're reading from state which means you're reading from Storage directly from State it's it's it costs more gas to perform to perform operations while reading from States so what you did swap is doing here you see this guy is called get results right so it's uh public view returns unit 112 Reserve zero you eat one one two reserve one and you're in 32 block timestamp last so what they are doing now is they are now setting this this this um Reserve zero that is a state variable they are setting it to memory setting this guy to memory and setting this guy to memory so anytime you want to make use of this Reserve that is on that inside the state this guy that is at the state to this guy that is a state which is which are all at Storage level it will just call this function and return them at from memory so it's returning them from memory storage like from memory so you won't have to be performing your operations you won't have to be manipulating States when performing operation you'll be using them at memory and after performing all your after performing all your manipulation your arithmetic whatever you can now update back the state not that when you're performing your whole uh operations you'll be I reading from State uh directly no it doesn't make sense it's it cuts more gas to manipulate States directly so you manipulate it from memory and at the end you update the state so that is what unit swap is doing here so this get reserved is like reading from State uncertain to memory so anytime you want to make use of all these results have one block timestamp last you just call this get Reserve function receiving the the the variables is it's returning and you make use of it and that's it all right so we have the safe transfer now this is where that um low level call was used so now um imagine you depositing uh let's say you want to swap from token one to uh we want to solve for talking one to two key zero and you bring in you bring in token token one in particular about of token one so that you can get a particular amount of tokens you this per contract will have to transfer token uh want to uh talking zero to you if you're bringing to King token one to get to King zero so that means you first transfer talking one into the pair contract and the pair contract will perform its magic or its logic and it's not transferred to k0 to you now this safe transfer is what does that that transfer operation so we first of all get the address of the token that will be leaving the address of the person that is receiving it and the value that will be going to the person so he now said okay bull uh success byte memory data now token which is this token that is receiving here.com this call is a low level call and once you do that call you can now say ABI dot encode with selector and once you say abi.co we select us expect you to pass in a selector here normally you can pass in this guy this guy inside that place but now we have saved it as a constant so what we are passing here now is this function selector that we saved here so you pass in the selector here then you're passing the value that this selector is expecting now remember this selector is expecting an address that is why we have the two and these two will be an address which we have here then value this value now is ue256 which is this guy you see here now the input to five six because obviously you ain't by default is you're in 256 but when you're making use of it inside this function selector you have to put it you in 256 Trader else you encounter issues so you see we are receiving the value here and that's what we are passing here so this guy will perform this call and these guys requiring that this uh this call is successful so require success and data dot length is equal to zero or API dot encode data boom uh so once it's requiring that this is successful else it will tell you transfer field you get so this is what this function is basically used for transfer operations inside the unisa V2 pair contract and it's not making use of the basic transfer you are now you know the normal basic transfer so it's using this guy so unisfab is doing whatever it takes to minimize gas like because if imagine if you're swapping on unit Swap and you're paying a whole lot of gas fee that doesn't make sense so they are ensuring that this gasket is not coming from them from their contract if maybe there's increase in gas fee on unit swap it has to be because of ethereum gas price spike or all of that but not based on the the inefficiency of their own logic all right so let's proceed so on line 49 we have the events the event mean so this is what this event does is it gets emitted each time uh uh a a liquidity provider provides liquidity successfully so this function is being emitted it's called Uh this this event is being emitted and this event is emitted in the mint function and the mid function is the function that gets called when a liquidity provider provides liquidity then the bond event is emitted when a liquidity provider removes liquidity because the bond function is the function that uh gets caught when a liquidity provider wants to remove liquidity so this swap guy this swap guy gets emitted when a Trader successfully swapped from one token to the other and once that uh or transaction is completed successfully the swap event is emitted then the sync event so now for every time there's a change for every time there's a change in um The Reserve this sync event will be uh will be emitted for instance after a swap operation remember when you're performing a swap operation you send in in one talking to get another token so when you're sending that token you know that the reserve of that token needs to be updated and when you send out another token know that the reserve of that token needs to be updated too so now what does this is the update function so the update function will update the reserve and it will now emit this sync event to tell you that okay everything has been successfully synchronized everything has been updated all right so let's move on to the Constructor yeah we all know what Constructor is the Constructor gets called once a contract is being deployed and like I said earlier this V2 pair contract is deployed by the Minnesota V2 Factory contract and once you're deploying it to the Emergency Center at that point in time is the V2 Factory contract uh that is the image of sender so it's setting these images are sender to this the factory State variable that was declared at the top at first yeah so now let's look at the initialize function so this initial like you said here this unit size function is called once by the factory at time of deployment so what this does is the moment the function contract deploys this contract immediately it sets the address of token zero and the address of token one so what it does first of all it ensures that the person the address calling this function is the address of the factory which was set here in the Constructor and if this line passes then it now sets the address of a token that is passed here to this guy and the address will be talking that is passed here to this guy and once that is done voila without have our pair contract set with the two tokens that it will be swapping uh that it will be exchanging and all of that so uh yeah so that is what the initialize function does and all right so um after the initialize function what we have next is um the update function so this update function I won't explain it now because you you won't understand the update function if I explain it now uh and this mint fee function I won't explain it now as well we are going to come back to it so I want to flow I want to explain this contract in the flow of how it works you get all right so uh we are going to skip the update function and we are going to skip the um mint fee function and go to the mint function now from the mean function we will now jump back into the update function and the mid function yeah so that's the flow of how it will work but let me see if I have something to read here uh let's see let's see so okay uh okay okay we have this uh here so this is uh okay the care last the kelas is a product constant of Reserve uh zero eyes have one so let me just put this in bracket and say reserve 0 times Reserve one so that's what this K Love is note notes the pair contracts only keep tracks only keep track of reserves of the two tokens that makes up the pair it doesn't store the actual token yeah it doesn't store the token you only keep track of the the the the reserve of the token in the contract so the pair contract uses the balance of address this and the transfer function to manage tokens yeah we'll see how that works it uses a low level Court to perform his transfer function it gets the function selector of the transfer function then it implement the transfer function inside the safe transfer function using low level call the safe transfer function takes the address of the token to be transferred address of the receiver and amount of token to be transferred so yeah update function let's do this here I'll just do this update function so we'll talk about this from so I already broke down the update function uh so how is the price roughly well that's inside the update function and also I have the mean function and that's called mint fee function so I'll also keep these means fee function to keep this here so we'll come back to this I already performed the calculation for that yeah okay all right all right all right so let's go to meeting and bottom first we'll come back to this guy so meeting and burning yep so this low level function on this other side so we have it on like 109 on the left hand side this low level function should be called from a contract which performs important safety check now pay attention this mid function now like I said before unit swap is made up of two sections the core and the periphery the call is the part that does the heavy lifting like the core is the core contains three smart contracts that's it uh V2 you have security the V2 Factory and the V2 pair you get now but users don't interact with the core directly you're not supposed to interact with the call directly the reason it is because um you might unit swap call the unisa core contract is written in such a way that you cannot cheat unisa you cannot cheat the call but if you're a user you're calling the call directly without using the peripheral which is a router contract you need to know what you're doing else you will lose a lot of money so what's the periphery contract does is the peripheral contract already does that's a router contract it already does all the necessary safety checks for you not to lose money so if you're calling this mean function the router contract will ensure certain things for you so we are going to see that now before you call before the router contract calls this mean function this mean function the router contract already ensures that you have already transferred the tokens you want to use in providing liquidity for instance let's see you want to provide liquidity uh for Shiba Inu Shiba why why did I choose to use Shiva in this explanation we want to provide liquidity for Shiba in I want to provide liquidity for Dodge Coin right and now you want to provide liquidity and you say you want to provide 1 000 or let me just use yeah let's use one thousand you want to provide liquidity you want to say one thousand uh Shiba Inu for 500 Dodge cord this is the ability you want to provide because liquidity involves you bringing an amount of the two pair toolkits and all of that so before this mint function is called the router contract will ensure that your first transferred these two tokens to this pay contract before it will call this mean function so before we proceed I just want us to take a look at the router contract so that way you understand what's Happening Here so let's go to the uh where is it where is it where is it let's go to the periphery contract yeah router yeah router V2 so in this router V2 let's look for the the add liquidity function this is a private function it's not this one we are looking for yeah this is the guy we are looking for so this add liquidity now you see it takes the address of token a the address of token B which is this one will be our toky zero and I took in one the amount desired that's the amount of liquidity you want to provide so for Shiba here will be one thousand four so yeah for cheaper you'll be one thousand for Dodge here will be will be um 500 amounts a minimum you put the amount a minimum the amount being minimize for the amount D minimum the address this is the now this is now the address of the person calling this function so uh this is going to be like the images of sender so this is the address of the person calling this function and the deadline and all of that now let's look at something so uh uh so this is to pay this gets the pay of the of the the pair of the contract you want to provide this liquidity to this is what we are looking at safe transfer from now look at what is happening here it will first of all perform this transfer from it will transfer this token from the person calling it to the pair and the amount and it's not transfer the second token this took particular token from the person calling it to the pair and the amount you get so this is what is now doing when it does its transfer this is where it will now call the mid function you see liquidity is equal to Universal V2 pair the paycontract dot means then it passes this address to which is this address here so I I don't know if you get what is happening here so before this uh I'm going to close this router contract now I just want to use it for explanation so before this mid function is called it will first of all send the token you want to use in providing liquidity here right so let's proceed let's proceed let's proceed so this this this guy is making sense so meeting and Bonnie okay meeting is when in equity provider adds funds that is provided into the pool which needs to maintain of new ownership token that's the healthy token burning is the opposite of meeting this is when a liquidity provider this thing is liquidity non-liability please it's when a liquidity provider uh only the provider withdraws funds and they accumulative rewards and his full ownership token so LP tokens are bonds that's destroyed so yeah meeting function in the mid function at first initial used a gas saving mechanism uh to save gas by transferring okay so now let's proceed to the uh to the function proper so remember where I was explaining this get reserved stuff so you see we are going to make use of the results zero and we are going to make use of the reserve zero and the reserve let me close this side page so you are going to make use of the reserve zero and the reserve one here and instead of reading this guy directly from States we are reading it from memory which is being returned from this get Reserve function that I explained earlier now remember that talking has been transferred to this to this guy so let's okay let's let's just do this guy so uh I'm going to perform a little calculation here so let's let's perform this calculation here so let's do this um let's do this so this mean fee right so at first remember that the the reserve zero and reserve one will be zero because assuming we are providing liquidity for the first time right so let's just say uh that means let me do this here I'm looking for a place to just perform my Mass okay so uh let me perform my maths here all right so let's assume uh okay if liquidity is a brand new pool this is the calculation that will be done right let's just do this let me perform it here right so now our Reserve zero at first because we are providing liquidity for the first time so here our Reserve zero will be zero so let's say uh [Music] Reserve zero is that zero equals to zero and Reserve reserve one would be equal to zero as well right so because you have not provided liquidity for the first time now balance 0 will now be ielc20 dot balance so now our balance 0 will be one thousand because we have sent one thousand like I said before this function is called the token has already entered this contract so balance zero which is uh the amount of talking zero percent is one thousand and balance one balance one will be 500 so this amount of talking one cent so this is our Shiba and this is our our Dodge or a Dodge Coin right so now balance zero is one thousand balance this guy is this thing so now in order to get the amount we actually sent right because first of all we get the balance so we are getting the balance of this contract in order to get the amount we actually sent we now say we now see amount amount 0 is equal to 1000 minus zero because at this point you have zero is zero sorry this guy is supposed to be zero not one and for us to get the amount uh one we sent amount 200 is going to be 500 minus zero right so now if someone else is providing liquidity balance zero will increase and balance one will increase so to get the amount the person sends you're going to say I'm on zero minus one thousand and amount 1 minus 500 you now get the actual amount the person said so but in this case since this is the first liquidity is minus zero and this will return to our amounts our amount zero will be equal to one thousand and our amounts 1 will be equal to 500. so I'm going to perform I'm going to perform calculation for adding liquidity for both um the first time and the second time like the first time at the second life where when the creditors already be provided for the first time so now we have our amount here so which is this guy so fee on so this fee on now what this fee on does is it checks if you need to have turned on fee now if you need swap turned on fee this remember that okay remember that uh let me let me break this thing down so remember that how uniswap knows uh that you're going to participate in the the the sharing of the fee it charges Traders from when swapping is by minting uh LP tokens to you so if it means LP talking to you that means you partake in that fee sharing so what this mid fee does now is you too it will it will go if if the fee is on so let's check the main fee so now like I said in the part where I say once the veto address is set that means meet fees on that is why it's checking this guy that's why it's checking fee on is equal to V2 not equal to address zero so if V2 is not equal to address zero Phi one will be true address V2 i unit swap V2 Factory you pass in the factory and you get the fee too that this Returns the fee to um veto address so and if it's not address zero that means the fee is on so once the fee is on this this this thing you're seeing here this month you're seeing here this is the mass that calculates one over six of the liquidity and it means it to the fito which means this feed to is eligible to collect one over six of the feed is 0.3 percent fee that beauty swap will be getting each time a user swap a user swap uh tokens on despair again so just understand that this means fee means one over six of the liquidity that a user is providing to the Fido address one over six so if you if you if you're a mathematics person like a mass genius you need you know how to perform that uh operation now that calculation so just perform it and check it out so that is what this guy is doing so I'll I'll explain this guy this video is already getting to an hour so but I'm not going to stop this video because how no matter how long it is we don't have to break it so that users will follow to the end so this mint fee guy now is what calculates the fee if this fee is on that means if user said if the veto address is set means fees or not if that is true it will meet 1 over 6 of the liquidity to this video address right so let's go back that's why I said I didn't want to touch this video at first so it is from the mean function that will now go to the filter so if that is done so this is what it does it passes the reserve here and it passes there is a zero here and the reserve one here and remember at this point our Reserve zero is zero and our user one is zero so no fee will be minted to this guy so check it out let's see let's go back to the main thing fixing this guy taking this guy so uh tell us if this so anything you're multiplying by zero is zero you see anything you're multiplying by zero is zero so nothing will be minted at the first instance when you're providing liquidity even if the fee is on nothing will be minted because zero zero is zero everything zero is zero you get so just know that at the first instance no liquidity will be will be will be maintained to uh to unit swap so no fuel limited to install now going to the DOTA Supply this total Supply is the total supply of liquidity that has been minted remember We inherited unit swap V2 erc20 here and once we inherit this we have access to the total supply of this uh of this uh contract and the total Supply is increased each time the total Supply is increased each time this LP token is minted and it's reduced each time the lp token is burnt so each time a liquidity provider provides liquidity into that supplies increase each time the liquidity provider removes liquidity the tax apply is reduced so now since sorry so now since this is the first time we are providing liquidity that means no LP token has been maintained automatically total Supply is zero we get that right so automatically the power supply is equal to zero follow me follow me while I take you through this ride of unit swag V2 breakdown but I'm enjoying this actually so let's go online one two four you see where we have if total Supply is equal to zero that means this is the first liquidity this is the first liquidity that is coming to this to this uh V2 pair and the the the calculation for first liquidity is different from the calculation from subsequent liquidity right hope you get that all right so now um okay so liquidity will now be maths dot square root of this guy this guy this guy so let's perform this this calculation so so liquidity now uh liquidity now will be equal to Max dot Square root square root of what amounts zero and what is our amount zero our zero is one thousand among 0 multiplied by amount one which is 500. remember so are you getting this gist now are you getting this this so I'm a zero multiplied by this guy minus minus the minimum liquidity minus the minimum liquidity so and our minimum liquidity is our minimum liquidity is then raised to the power of 3.
right let's look at it again minimum liquidity yeah 10 raised power of three and if we do 10 raise power three here this solidity shell we get one thousand so minus 1000 are we good are we good are we good are we good so let's proceed back to our our means yeah so this is where we are so that we calculates the liquidity right so now what it does now it now means this liquidity to address zero it means this liquidity this minimum liquidity to to address zero now once it makes this the minimum liquidity to address zero remember that our total Supply will increase to 1000 because it has minted one thousand to address zero it's not meeting it to anybody the reason why it's doing this is so that now subsequently when users starts providing liquidity or removing liquidity your you cannot go below this minimum liquidity liquidity can never get to zero you get because the minimum liquidity has been said and it's not minted to anyone who can come back and remove the liquidity submitted to address zero so an address zero cannot come back and remove liquidity so that's why it's setting this and that means our total Supply the minimum total Supply we can get is minimum equality even if everybody removes their liquidity from the contract is is the logic clear now so let's continue with our our Mass so let's now do um 1000 times 500 this will give us this guy so copy this guy and minus 1000 this will give us this guy so now liquidity I don't know where liability is coming from liquidity is equals to maths dot square root of but the square root of this this guy right so let's let's do this uh this stuff so liquidity will now be uh Equity will now be equal to so let me use my phone to calculate let me use my let me see if my calculator has uh square root I don't know if this calculator has credit if you have square roots please he he doesn't he doesn't he doesn't he doesn't have to go so I'm going to use my I'm going to use my calculator to my phone calculator to perform that Mass so let's see let's see let's go to my calculator and go here and I'll say square root of what do we have four nine nine and we have three zero zero zero zero this will be this will be 706 points three nine nine nine sorry 399 32 3204 um nine eight so solidity doesn't support floating numbers because uh at this point they didn't make use of that um they didn't make use of that um what's it called I didn't make use of that that Library here so I will assume that the liquidity will be equal to seven zero six so I I this is what I will assume because you need to have been shows how the how they handle that floating stuff so I would assume that the liquidity is 706. now once this liquidity is gotten so it means minimal Equity to this address so in here now it will now require that liquidity is greater than zero so it's requiring that what is calculated here is actually greater than zero else it will tell you insufficient liquidity minted you get and if this pass it will now meet to now meet this 706 to the person that is providing liquidity so that means the person that called this mean function will now get 706 of the erc20 token and now the total Supply will now be at this point total Supply will now be 706 Plus 1000.
this will be our total Supply so our total Supply now will be uh will be 1 7 0 6.
so I don't know if you're getting the gist of what's Happening Here this is exactly what this is the mass that these guys are are performing now once this is done once this is done the update function is called the update function is called what pay attention please this update function now is now called and let's look at the update function the update function takes in the balance zero and the balance one remember our balance zero is one thousand our balance one is 500. our Reserve zero is zero and our reserve one is one is zero as well so do not call this update function here which takes in this balance zero balance these guys have this now it now requires that balance 0 is less than or equal to this guy and balance one is lesser this is just a way that it uses this to ensure that a short overflow and all then here he now says okay block timestamp is equal to doing 32 block time stop module this this is but this is where it's performing this what this part now is using here now to perform its price uh update and everything but this is what I want us to look at you see here now it is updating the reserve with the balance so now and to now see Reserve now will now be Reserve zero will now be equal to 1000 and reserve one will now be equal to 500 then block timestamp last will now be the current block the timestamp like this this stuff we performed here it will now set it to this guy and here it will emit the sync since it has updated this guy to emit this sync event which was what I explained before that the sync event is emitted once there's an update on the reserve and all of that so to emit this guy we are going to come back to this price Oracle calculation later so but for now this is what I want you to understand so this update is called and Updates this guy so I you get get what's happening forget about this price Oracle here this is this thing now you're seeing here this is the price Oracle of units to have this small line of code this is where your swap is performing their price or apple that they are performing okay if I give you 200 shiva I'll get so Dodge Coin if I give you 500 Dodge Coin I'll guess but this is where they are performing that that uh that magic or that logic you get so let's go back to our mid function and once this mid function is this update function is passed fee on care last is updated remember our care last is the constant product guy so now since our Reserve has been updated we'll Now update Kayla so tell us now before previous Decay last is zero because there was no the reserve 0 times 0 is 0 but now our Killers will now be 1000 times 500 which would be 500 000. I don't know if you get that logic K last will be updated so at this point our care last so let me just do this so after uh after this right after this comma Reserves are updated okay last is updated to so our new Reserve zero now would be is our zero now be equal to one thousand and Reserve uh one would be 500 and our K lasts at this point will be equal to 500 000.
are we good so you see the logic and after this happens the mint event is emitted you get the meet event is emitted carrying the images are sending the amount and this guy I don't know if you get that the logic so this is you see you see the sweets the the flow of how this logic is working you get so next thing we are going to be looking at is the bond function now uh the bond function is is the opposite of the mean function so it's the opposite of the mean function now like I said the mean function happens when uh users are providing liquidity so pay attention you see remember that the liquidity that is calculated here is now minted to the user which in our case is a 706 we get and this 706 is what shows that okay you provided liquidity and whenever you want to take out your liquidity you're going to bring back this 706 your unusual V2 as 20 token to take back your liquidity and this is what cells that okay you'll be getting zero you're getting a share from a 0.3 percent fee that is being that uni swap accumulates wide swap uh transactions of course are right let's proceed again now remove liquidity this also should be called or should be performed by an import uh by contract which performs important safety check so removing liquidity also is being done by the verifying contract which is the router contract all right so the bond function is called when user is removing liquidity and also also see all this lock see this lock here that I use I told you it's used to prevent reentracy attack so once you call this function lock is activated until that function is passed the lock will now be open again for you all right so same way again the this Reserve is gotten from the get Reserves so the address uh token zero is gotten token one is gotten from state so we can't we won't be manipulating State again so the first thing this guy does here it gets the balance of address the balance of uh token zero for address this right and it gets the balance of token one for uh of this address then liquidity is caused to balance of uh address this so it tries to get the the um balance of actions this does um the liquidity this uh this contract has for the um for the year 20 token that's the unit swap he has a 20 token so it tries to get the balance of this this address so if mid fee is on if mid fees on again it means so at this point Reserve zero is one thousand and is that one is five hundred so it will now meet uh it will it will meet one over six of this guy to the to this uh to this unit swap contract right then it gets a total Supply so at let's just perform this much uh let's perform this match so let's go to the bond function let's go to the point function so okay let's let's read this so we get something the bond function is the exactly stop the mean function the same gas saving mechanism is used just like in the mean function balance zero and balance one are total balances of the pair of the pair tokens in this pool uh balance zero is okay is the balance of addresses and this liquidity balance okay liquidity is the amount of food ownership token that this liquidity provider that the liquidity provider who wishes to cash out has you get so but first of all it gets the liquidity of the balance of this address now question is why why do we why okay okay okay yeah yeah yeah yeah yeah now pay attention to this this this part might confuse you I I intentionally ask this question here so pay attention normally if you want to get liquidity the liquidity you're getting should be the liquidity of the person that is uh calling this contract are not the liquidity of this contract but why is it checking the quality of address this that's the balance of addresses for the years 20 token that was why I asked this question here so but why do why do we supposed to be why do we access the liquidity of the balance of address this you understand so now answer is because the liquidity was transferred to the per contract by the periphery router contract before calling the bond function so this means before this means that the balance of address this for this token will always remain zero so anytime liquidity is to be removed the user that is removing liquidity will first of all transfer their liquidity token that's their LP token to this address before calling this Bond function the same way if liquidity is being provided or first of all transfer the tokens they want to use in providing liquidity to this address before they call the mean function so when they transfer the liquidity to this address given that the balance of address this for the MP token is always zero so any amount it's seen now is the amount of liquidity that the person that is calling this function is sent and at that point in time it will now check if these guys on total Supply it does this so amount okay let's perform this Mass sorry let's just do this Mass here where would I do this okay let me run this Mass here all right so given that at this point our Reserve our Reserve 0 is equal to uh one thousand and our Reserve our Reserve one is equal to 500 right now we have gotten our token address the reason why we are getting this speaking address is if we want to perform a transfer so now balance of balance of address this for this stuff will now be uh balance zero balance zero since no swap has been done since no swap has been done balance zero of talking this guy would be 1000 and balance one so remember these things are just scenarios we are painting not as if this is the actual store we are just painting scenarios of how this would be but before you you know before you remove liquidity trade model must have already a lot of swap must have already occurred so now node swappers or Korea assuming no server has occurred at this point in time and you want to remove your liquidity back right so now remember that the person that provided liquidity uh the degree that was given to the person here is 706 where is it it's 706 right so this person now is going to transfer that 706 let's say the person wants to remove all their liquidity so what will happen here now the liquidity will be equal to 706.
right so fees on it will it's going to meet uh one over six to the uh V2 are the the fetal address now total Supply at this point now will be what will be our total Supply I hope you remember so our total Supply at this point will be this guy so they're supposed to be an equal to here and also yeah this guy so the power Supply will not be forced to this because it mated those guys and that guy all right so we have gotten at the tax apply so amount zero that's the amount I'm on zero among zero will now be equal to CD the the marks here let's perform this Mass so yeah so I want us to perform all this my Amazon liquidity multiply so our liquidity now is this guy right times balance balance you know which is this guy which is this guy now both of these will be divided by total Supply which is this guy who are we good so so so now this is what's going to be addicted so let's perform this Mass here 706 times uh times one thousand equals to this all right so let's copy this let's I like breaking these in steps so that uh this will not be equal to this guy divided by one seven one seven zero six so I hope this division will not cause overflowed or underflow so and it might lead to decimal and if it is to decimal solidity share will draw an arrow so let's check this out I know so this shall try an error so let's just use calculator to perform this guy so here we now have a calculator come on face this guy here divide by 1706 divided by 1.17 06 so this gives us four one three point eight three so amounts zero is equal to four one three points uh can I copy it 8335 so let's just do eight three three five but solidity doesn't support decimal so amount 0 will be equal to four one three right four one three okay okay okay so let's look at uh amount one so amount one will be let me copy this guy I'm Antoine will be this guy am Antoine will now be this guy so let's say I'm out one will not be equal to liquidity is uh seven zero six following those seats multiply by balance one on balance one is 500.
yes so you must buy my balance one put this guy here divide by two parts of Maya the power supply this guy so once you put this guy here so let's do seven zero six times 500 gives you this and uh yep some amount 1 will be equal to this guy divided by 1706.
so copy this guy this thing will stick to the head of others divided by one seven zero six give us Arrow so let's go back to our calculator so clear this guy please this guy divided by one seven seven six hit it this will give us two six to give us two or six so six point nine one six seven so six point nine one six please so amount 1 equals to 206 points nine ones six seven six all right so this will be amounts one will not be equal to three four six so now notice when we're providing liquidity provided the user provided 1500 but why they want to remove liquidity they can't get that guy again because now they are the first person that provided liquidity and minimum liquidity has already been done like the calculation has been done so you can't remove everything you provide you can't so the best way to benefit from this is to allow uh trades to happen for people to keep providing liquidity and all of that so when trades keeps happening you will get your 0.3 percent share of the whole trade that is occurring and whenever you want to remove liquidity you you make money you get interest but if you remove your liquidity immediately when you provided it and being the first person that is providing liquidity you're going to obviously get less because the minimum liquidity has been done like minimum equality has been calculated and all of that so you see in that calculation you can't get exactly what you you imputed so now you see what's not happening now is that's a require that amount 0 is greater than zero and amount one is greater than zero and the insufficient liquidity Bond you understand so it will now burn to now bond this liquidity that you send to this address it will burn it to destroy it and once it burns it the this address the balance of addresses now becomes zero again now it's now safe transfer looking zero to your address and this amount zero which is this guy I need to save transfer to Kin one to your address which is this amount one you get and once this is done remember it has transferred this guy to you that's transfer this guy to you so the address the New Balance will be will change so first of all notes the previous the new balance so balance zero will now be equal to the previous balance zero which is one thousand sorry which is 1000 minus uh this guy this amount right so and let me just take this to solid Fisher and get the value so this return five to balance zero without equal to this guy right this is the operated balance View and balance one now to be equal to 500 minus 2 206 so this will now be the guy this would now be balance oh sorry the last one so this will now be equal to 294.
so this will now be our new Valencia and the last one then now we call update function again I'll remember what update functions does update functions will now take the New Balance zero and balanced one and update the results so our Reserve now will now be equal to our due Reserve uh so once that update is called our Reserve 0 is equal to 587 an hour Reserve these are one will be equal to two nine four and once this is done remember the K loss is updated okay so Reserve will now be five this times this so our killers at this point to to be equal to this guy times two nine four which will now be this this will now be our okay last at this point in time right and the bond function the bond uh the bond event is emitted so I don't know if you if you're following if you're getting the logic of what's Happening Here so this is exactly what's happening here so uh this is this is interesting so uh yeah I don't want to I don't want to uh make this video longer by trying to provide liquidity for the second time like okay what if liquid has been provided and you want to provide the community again so if liquidity has been provided and you want to provide liquidity again this is the mask that will be done this Mass here is what will be calculated in this particular Mass here because total Supply at this point will not be equal to zero so this is the mass that will be used to calculate the liquidity that will be minted to the person provided liquidity at this point in time you get that so you can you can test you can play around this part and see what happens uh and all of that so next we'll be looking at the Swap the swap uh the swap uh operation and let's see what happens here let's see what happens here ah this this is really getting interesting although the video is longer but I don't want to make it short I don't want to cut this video again so we we go to the end of this and all of that yeah okay so all right so um now we we are going into the swap function so uh this is this is really you need to have you need to have unit swap protocol is really an interesting one all right so let's look at the Swap awesome awesome all right so the swap uh let's just look at my notes over here so the swap function is um is used by Traders to swap token so the swap function ensures that the amount of token you're swapping the amount of token you are swapping is greater than zero okay yeah this is on line 168 so it ensures that the amount of token you are swapping is greater than zero it can it can be any of the tokens in the pair so only one token only one of the tokens can have a value it doesn't matter it doesn't matter so it also ensures that the amount of talking you are swapping from and the amount of token you are swapping to is less than the available Reserve hence it will throw its official liquidity so what this means that for instance now if maybe what you have in the reserve is uh it may be what you have in the reserve is 500 for instance talking one you have 500 token one in exam you cannot swap you cannot be swapping uh swapping out 1000 to kill one sorry let's say you have 500 tokens zero in the results you cannot be swapping out 1000 okay zero because it exceeds the reserve of token zero in the contract and similar here if you have 1000 uh took in one the result of turkey one is one thousand it cannot be swapping out two thousand to keep one that is what this guy is showing that you cannot be swapping more than what the result has all right so um all right it shows that okay yeah so if the checks are passed okay it checks if the checks are passed he checks which of the amount out and amount of that is greater than zero okay whatever so what's happening here now is um it holds it sets a a local variable that's a memory but as you and this guy Palace one so this for taking zero and taking one so here now says address token zero is equal to token zero so this like same gas optimization is bringing the state variable and setting it to memory and address token one is uh taking one from State okay so here it says require that the toe address require that I like giving space so that I will see what's happening I require that the two address is not talking zero and two address is not talking one so you cannot be sending uh you cannot be swapping token to the Token address itself so it's checking this so in invalid to address so it's checking that the address that is that is swapping to it's not any of this address so this is just something to check yeah so if amount out is greater than zero safe transfer looking 0 to amount out and if amount 1 is greater than zero save transfer uh safe transfer amounts token one to uh to to the address and amount at now so you have a question if normally you're supposed to ask a question that why is it just transferring out without first of all checking that the the amounts that the person is meant to bring in has already been brought in right so yeah that was done I'll show you where that was done so yeah yeah yeah yeah yeah so let's let's do that so at let's just go to this note so if checks are passed the checks which of the amounts out that's amount out of amount zero out or that is greater than zero then it transfers the amount out to the trader optimistically so note without making sure that the trader has already transferred corresponding token into our balance we can optimistically transfer tokens out because it's swap the swap function have assertions later in the function to check if we receive corresponding tokens the peripheral contract should send in the tokens to the fair contract before calling it for the swap if the pay contract have not received any tokens a session will fail and solidity will revert the entire function so just like in the liquidity Pro the mid function where before you call the mid function the periphery contract has already ensured that you transferred the tokens you are meant to use for providing liquidity into the third contract so here before you swap the the periphery contract that causes swap has already first of all make sure that you have sent in the token you want to bring in in order to get the token you want to take out so it is to first of all ensure that so but here we didn't perform the check here so we first we said if amount out is of token zero is greater than zero transfer this guy to the person and if amount one or out of token one is greater than zero transfer this guy to the person then if data dot length is greater than zero this is where to now initiate this condition so let's look at this thing so the code if data.net is better than zero call it on this guy so we inform the receiver about the Swap this is like where you're performing a swap you see where the the the front end will pop up something like to pop up for you to confirm or do any of those stuff so this colleague will inform the person that is calling this uh swap about this Swap and all of that so once this is this guy is passed then this is where it now does the check he now said balance of uh is 20 of address this and balance of iso20 uh of token so token zero and toky one it checks the balance of Tokyo zero and the balance of token one of this address now it now checks the amount in of token zero and the amount in of token one and so let me just go here I I did this math already so so let's assume that our Reserve is zero is one thousand hours of zero is one thousand right so let's assume we didn't remove liquidity after providing it and our Reserve zero is one thousand and I reserve one is 500. now a user is swapping 100 token zero this means the user is going to get 50 to P1 right using this ratio now we have not got to I'm just assuming this now so now incoming token one is hundred incoming to sorry in coming to k0 is 100 because the user is swapping from 2K 0 to 2K1 so incoming token zero is 100 incoming token one is zero because the user is not swapping from Pokemon so you coming to q1 is zero going to King 0 is 0 because the incoming is token zero and the outgoing of talking zero is zero for the outgoing of talking one is 50 because we are swapping 100 token zero to get 50 toky one at this point since the user has sent in this guy first and as this guy has already transferred this guy out so at this point balance 0 is 1100 because the reserve is is 1000 plus 100 that the person sent is one thousand one hundred but now here now balance one will now be 500 minus 50 which will be 450 because it's sending in is sending out talking one that is why if you see here now if amount out of looking amount out among zero out is greater than zero it transfers to King zero but now amount out of token zero is zero so it's not transferring this guy if amount out of token one is greater than zero it transfers to if you want to the person and amount and here is transferring 50 so if it transfers is 50 that means is 4 500 minus 50 is 450. you get that it does this guy so this guy is still within the function so here now this one is performing his check so now using this formula amount in is equals to uh balance zero is balanced zero greater than Reserve so if uh I'm talking too much so let's see let me just expand this guy so we get to understand what's Happening Here so is amounts in his balance zero greater than Reserve zero minus amount out if that is the case so the balance uh balance zero is uh what amount in its balance zero minus Reserve minus among this guy this whole 10 hour operation gets confusing sometimes so let me just explain this here so amounts in Balance zero at this point is one thousand one now is balance zero greater than 1000 minus zero yes so what is happening here balance zero minus in bracket one thousand minus zero so what you're having is 100.
right now amount in 450 is 450 greater than 500 minus 50. no because 450 500 minus 50 is 450 and this thing here is 400 450 minus 500 minus 50 is uh 450 this will return zero this will return zero so amount in for uh token one is zero but amount in for 2K zero is hundred that is what is happening here you can perform this mass on your own to see how it works so I don't know if I'm able to explain it well so I'll send this dog so you see it so this is where this guy happens now and uh that's what is happening here uh yeah that's what's happening here so it gets the balance it gets the amount that is coming in for token zero and it tries to get the amount that is coming in for token one but since token one no amount came this will return zero why this will return 100 in our case here in our uh youth scenario to require that amount zero in is greater than zero or amount one in is greater than zero else today is sufficient input amount so there's an all statement here it has to be either this or this so now balance adjusted so this was happening here now so if amounts in of either talking of the pair contract is less than zero it will revert its official input amount so now once it checks this if any of this is zero if it's zero this is where this whole function will divide if you didn't send in any amount at first if you didn't sell in any amount this is where the whole function will be back but now the periphery contract ensures that you sending the amount in to get your amount out so you cannot cheat this smart contract so he says if the amount in of either talking of the fair contract is less than zero it will revert insufficient influence and the entire function will revert are nothing will have nothing will have taken place you get that now if the checks passes then it proceeds to the 0.3 fee paid by Traders so 0.3 percent paid by Traders is calculated it does this calculation to check and ensure that the fee was paid note that the fee is also transferred to the pay contract and this is made possible by the router contract so the same way you are transferring your your amount in to this contract the periphery contact also ensuring that you're transferring the 0.3 fee as well into this contract so now this is where we perform the whole calculation of the the adjusted to the uh the the whole calculation of the fee now let's see you are giving a tax to modify this contract so that it will be charging 0.5 percent fee all you just need to do is change these three to five change this guy to five you get or if you are asked to collect 0.1 percent if you change this guy to one change this guy to one you get but now it's collecting 0.3 percent fee and that is why we have three here and the rest of this guy he performed this the rest of this calculation here then it causes update function again because once you're swapping the the New Balance will change because you send things out some token and you collected in some Tokyo then you have to Now update your reserve again once you update your result you swap uh event is emitted now the peripheral control let me show you where the periphery contract ensures that you're sending the fee as well so I'll I'll have to show you that so you see this guy this video is getting to two hours as well I don't know this I can't help it it's just the contract the contract is large you have to understand it all right so in the periphery contract uh uh yeah let's go to the peripheral contract this is the router let's look for where we are swapping foreign for tokens see he says amount is equals to so this is the amount you it wants to calculate the amounts amount out and the amount in and all of that so amount equals to you need to have V2 Library get amounts out to factory amount in part and all of that so now this get amount uh this is where it's calculating the fee so let's locate this Library I want you guys to pay attention let's look at this Library uh you need to add me to library so let's look for get amount out get amount um yeah so now get amount out see what is happening here amount in with fee amount in multiplied by nine nine seven right so now if remember that this guy is if if this this was this this guy is supposed to be uh one one two three but now it is 997 which means 0.3 percent is is calculated here if it was supposed to be 0.2 percent this would be 998 and in your in your pay contract this will be two so I want you to pay attention to this you understand and if you want to cut if you wanted to uh also calculate 0.1 percent feed it should be 999. and here this should be one are you getting the and this should be one this should be one as well so this is where it's calculating the fee whether 0.3 fee is being calculated so um I really beg that I don't want to stretch this video further like I don't want to stretch this video further so uh uh I was I will have to not continue go further into explaining this whole percentage calculation I will just stop it here for the the for the percentage calculation then let me revert this back to 997 so you can you can dive you have already done Justice to explaining this smart contract to how it works and how the the what each of this function does right so you can dig deeper into exploring how this percentage and all these things have been calculated because this video might get to three hours if you want to break down everything uh bit by bit so there are two more functions I need to to touch in the V2 pair contract so before I end this video all right so this is this is the scheme the scheme and the sync function so let's look at the scheme and the sync function uh uh that I did some crazy mathematics here but because of time I won't take this video any longer please so I but I'll be sending this documentation here so you can follow it and try to play with scenarios and see what what's happening so scheme and sync function are needed when balanced on the IRC 20 contract of the exchange token when need on schema sync function are needed when balances on the erc20 control of the SDS token Fallout of sync with the reserve variables so in the pair contract this this can happen for example when someone just trans okay yeah when someone just transferred Dodge Coin to the per contract account for no reason there are two solutions to this to keep the reserve value so what's happening you know the reserve needs to always be in sync with whatever is happening with the balance and all of that so imagine someone just decides to send Dodge Dodge token to the pair contract the person is not performing so the person just send you you know that at this point the balance will be more than the result the balance will exceed the result and the update function will not be called to update the balance with the Reserve at that point in time so this scheme and sync function are used to enforce that the balance is always equivalent to the reserved this function can be called by anyone you get so this function once is called so the scheme function it allows someone withdraw the extra fund from the pair contract anyone can call this function so the scheme function forces the balance to match the result so if you look at it uh it just safe transfer the extra token that is in this balance to the person that's calling it so it forces the so you subtract the result from the balance of and sends it okay now so it forces the balance to be equal to the result same thing with this sync so the sync function sync function forces the reserve to match the balance so it calls the update and and all of that so that is just what this guys this this uh these two function does so I believe I've been able to uh to do justice to this smart contract I think this is the best this is the best unit swap uh smart culture breakdown you get on YouTube trust me I've been able to do justice to this uh uh to the best I can I don't want to I I could have I could have explained this um mint fee function uh remember I said I I would I would have done that but there's no this this is getting to two hours and when the video gets too long it becomes boring and um yeah all of that so yeah uh but I did the explanation in there I did so I documented something in this in this uh docs so you can go through these dogs and you should be able to understand what's happening there so but just know that here is where you described their price Oracle stuff to get to get the ratio of what a particular token will give the other token or the amount of particular thing that will give the other taking and all of that and here this particular place is where uniswap is is calculating the one over six of the entire fee that will go to unit swap uh if he is on that's if the veto address is set so you know so I will be taking one over six of the entire uh 0.3 percent fee that liquidity providers are meant to share that's what this part of this uh this part does so you can place that one over six and mints into the fetal address this is where it meets it to the people address so you can take more time to try to understand what's happening here and all of that so if you want to if you want to manipulate the the amount or the the ratio of what you need swap will be taking each time each time someone provides liquidity or someone um uh removes liquidity you can this is what you you modify you can modify this to three you can modify this to four but before modifying it I need you to understand what is happening here what is really happening here once you're able to understand what is happening here you will know why this fight was used and you can always modify it to anything you want so I've shown I've been able to show you three areas where you can modify the Minnesota contract to suit whatever you want this is one this place is one this uh this uh place is to this is where they are calculating the fee and they the library so this is another place all this place is where they are calculating the fee so if you're changing anything in the uh feed the feedback let's say you're changing it to the 0.2 percent this will have to be eight and here also will be it take note of this because this is what you need mostly when working with unisa a company might just tell you okay we need a decentralized exchange and we need to be taking 0.5 percent fee uh all of that so I'm showing you in the Target points where you need to modify to to achieve those things out and and all of that so what you can do more research to understand what is happening here how this has been calculated and all of that so uh I'll be providing resources for how I was able to study about unit swab University let's say I used to studying this and all of that in the description so if you have any question maybe you got confused at some point you didn't get a particular thing you can do well to ask questions in the comment section and I will so willing and available to provide answers to your questions and um now we have come to the end of this video it was a very long ride if you got to this point uh you're awesome you're lovely and yeah you're you're breaking into D5 so subscribe turn on the notification Bell you get more of quality content like this uh subsequently and peace out yeah you can also follow me on uh bye
Up Next

Build a Crypto Coin Sniping Bot with Node.js and Uniswap
@DappUniversity
56.4K views•2022-02-18

Torrent File Format & Bencoding: A Technical Deep Dive
@AsliEngineering
12.5K views•2022-08-08

Operational Security Essentials: A Guide for Hacktivists (OPSEC)
@hitbsecconf
157.4K views•2012-11-26

Understanding Ethereum: A Comprehensive Beginner's Overview
@99Bitcoins
3.1M views•2018-06-26
Related Study Plans & Knowledge Roadmaps
Structured learning paths in Blockchain & Crypto





































