Flash Loan Crypto Arbitrage Step-by-Step | AAVE Flash Loans Solidity DIY Tutorials | Solidity Code

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello in this video I'm going to talk to you about Flash loans the concept of flash loans are becoming more and more popular in the blockchain world flash loans are a form of unsecured loans offered by some decentralized networks like Ave now let us see a very practical use of flash loans and trust me in the next 10 minutes you are going to learn flash Loan in a very very simple way so let's get started so look at this decentralized exchange uni Swap this is on ethereum at this point in time during the recording of this video One Eid is equal to 1218 usdc and then once it onto swap is 12 16. so 12 18 and 12 16. what if I get one Eid and convert it into 1218 USD and then go to sushi web and convert the usdc back to The Ether right so I'm Gonna Save to usdc let me redo it what if somebody gave it to me one eat I do not have an eat right I got one eat as a loan and I swabbed eat to usdc I got 1218 usdc and I go to Sushi's web and I paid 1216 and pocket the two usdc and get an ether I returned back one ether with let's say point something as a fee back to the person or the exchange or the platform that paid me or loaned me ether make sense so this is a very basic fund of flash loan let's look at one more time so you have the decentralized exchange like Ave is a platform which is a decentralized exchange it gives you flash loans it provides you that one ether in order for you to do that swapping right so you have Avi and you have a solitary contract we are going to write a solidity contract to swap or get the loan and do the swapping so we get the loan from away one eat or how much ever you want swap it on uni swap from token A to B and then on a different exchange doesn't have to be Sushi swap it can be on anything you can reconvert back to a so get Loan in the form of a stop A to B then again on a different exchange drop B to a and then return the loan with a small fee in that way the lending pool mechanism the pools the decentralized platforms that lend that loan to you expect a fee in return and all of this is done in one single transaction so in solidity you're gonna do the price difference so find out the price difference between exchanges this is a step which we are not gonna look because it takes a lot of time it there might not be any differences so we will code it or simulate it in some fashion we'll look at it later and then what we're going to do is get the loan from away swap token A to B then swap B to a and make profit with repay loan plus fee to away this is what we are going to write in solidity okay so get the loan swap three A to B ax swap B to a make some profit and then repay the loan plus fee so number two number three number four and number five again a quick look flash loans are form of uncollateralized Landing mechanism that are offered by some decentralized platforms like our way flash Lane a flash loan encapsulates one entire transaction from borrowing to payback in one single transaction make sense now somebody said one Chinese philosopher said a very interesting thing give a man a question and you feed him for a day teach him how to fish and you feed him for a lifetime let me teach you how you can write this solidity contract very simple I'm gonna make process really simple here and we are going to complete end to end all right so create a solidity contract and inherit flash loan simple receiver base now you would say what is this flash loan simple receiver based if I go to away documentation and the link of which I am going to put it in the video description section below so you go to this dark dot are we on flash loans and read through it I'm just doing what they have written over here so you have flash loan simple and you need to always mandatorily Implement execute operation just as it is written here so if you click on flash loan simple it will tell you how to request in order to request a flash loan these are the input receiver address assets amount parameters and referral code so we are going to write this function to request the loan and then this function execute operation is mandatory the two functions flash loan simple functions to request the loan and then execute operation so that whatever is requested is returned back let's do this and you can read this all of the things that I'm going to explain here are available here so this is I flash clone simple receiver let's see how we will implement this read this this is a good documentation all right so I'm gonna Implement create a function to request a loan this is the name of the function that I am going to put it you can have any name to this function so this is for requesting a loan from The Lending tool next I'm going to use execute operation as is function because this is mandatory required by RV so this is going to perform the Arbitrage Arbitrage meaning you know going from one exchange and swapping it to another exchange and repay loan Plus premium back to the lending pool from you where you operated so what is this lending pool all right so lending tools are in our way that provides you that one eat or one usdc or 2020 usdc in Return of a small fee in one single transaction assuming that one of the steps in the solidity contract does not complete or fail the entire transaction is reverted back so that the lending pool does not end up losing any money all right so I'm going to show you a few things so if you go to flash there's the same documentation and look at the test net addresses which is on this file you would be able to see these are my test Nets okay if you scroll down you would see that you have to look for Lending pool address provider pool address provider away if you scroll down this is the contract provider address pool let's look at this the contract ends in 15D you want to copy this and paste it somewhere so pool address provider away and then in this contract we are going to play around with usdc so for test usdc and other uh coins you have the links but for test usdc this is your this is your link so you want to copy this as well so I copied two things the usdc as well as pool address provider because this is going to be needed while you create the contract so step number one flash loan the simple receiver base inherited number two create a requesting function for the lending loan a loan from The Lending pool and then use execute operation to perform the Arbitrage and repay the loan all right few things to remember from this so usdc so whenever you have usdc in your wallet you need to have so this is oops this is mainnet I need to switch to go early testnet I have 19971 usdc but in solidity you wanna represent one USD with six decimal places all right make sure you remember that when you're deploying the contract you would need the pool address provider so the pool address provider that we copied was 15D remember I asked you to copy the 15D address of pool address provider do not forget that and transfer one USD for the deployed contract so once you have deployed your contract which you are going to see do not get worried we're gonna go step by step once you transfer one USD for the fees once you have deployed the contract you need to transfer one usdc as fees in order for entire thing to work so I'm going to show you how it how it will work and then this is the usdc contract address remember this and then let's follow the steps right so let's look at it together all right so we got that in one slide this is how we will create the solidity and these are the things to remember so as simple steps we are going to come back refer to this and then thing to remember and then we will execute the entire thing so that you know what we are doing right let's get started here so number one I go to solidity this is my Solarity console and premix.ethereum.org and then I'm going to create a new workspace uh I'm going to make it default blank and I'm going to say flash loan example and please note that there's a GitHub link that will be having this code what I'm going to show it to you here all right so you got the flashlight example I'm going to create a first file I'm going to call it as simple flash load in here I'm not going to do any Arbitrage so I'll come back to this so in this first one I'm gonna do the step number one step number two step number three be only I'm not gonna do step number three a in the first I'm gonna do the second one I'm gonna then we'll do the Arbitrage as well so I'm Gonna Keep it super super simple all right so let's do that all right so I'm gonna get a little bit of real estate by doing like this and let's start coding right so import away contract flash loan simple receiver base dot Soul okay and we are going to see what it's gonna do this is ipull address provider and then this is erc20 token because I'm going to use this token right so three things flashlight simple provider I pull address because you're gonna request on the landing pool and then the irc20 next we are doing a simple flash loan and this is owner so I'm gonna create an owner whoever is requesting for this uh who contract right okay this is the Constructor so the Constructor is gonna be like this very simple Constructor takes in the address provider and that address provider is nothing but I pull address provider this means that while constructing the Constructor is the first function that is hit when you deploy a contract so this is the address pool provider address that it is going to look for so this is the address we are going to push in and it's going to create a flash clone simple receiver base that's it okay next is I'm going to create the function request loan so let's do this how function request flash loan and we know from our documentation which is here if you go to the flash clone simple this is the documentation and you would see that this is your execute operation right and then if you go back to the documentation you would also see The Flash loan simple which gives you these parameters receiver address assets amount params referral code with their type and it had describes what it all does okay so I'm gonna create the same thing address which is going to be the address of this contract asset amount params and referral code make sense now this is the landing pool so pool dot flash loan simple it's gonna take in the same thing receiver asset amount atoms and referral code so I'll tell you what it does it creates a function that takes in the address of the token which is gonna be your usdc and how many usdc with six zeros remember the zeros going back again one more time six zeros so it's going to take the address token of usdc and the amount and this is going to convert into these parameters so address of the contract the token amount the token address usdc and the amount and these are blank and then it's going to request the pool the pool is coming in from the ipool address provider.soul and I'm gonna we are gonna look at it in a few minutes this is going to request a flash loan from this pool make sense all it is doing is taking the tokens and assigning these three things address of this contract token an amount and this is requesting the pool for a loan of the amount requested very simple right this amount it is going to request the loan from the pool very very simple and next this execute operation I am just copying it from here just copy it copy do not have to worry about anything and you can see that function pool is also here so simple just copy it so I copied this execute operation and then it's going to return true right this is very very simple it Returns the Boolean if you go in here it returns a Boolean right I'm gonna say return true all right I'm doing nothing here and then here I am going to say in the execute operation you have asset amount and premium it says give me the premium so I'm going to say all right no do not worry about it I'm going to give you the premium the total amount that I need to pay back after that execution is amount that I requested plus the premium because it gave me that loan and then I'm gonna say approve approve the asset asset means approve the usdc to be transferred to this pool remember this pool where we requested the loan from and the total amount so let's do it one more time request for the loan using address and token pool it requested for the amount and all I'm doing is sending it back to the pool that's it in the first one as I said we are not doing any Arbitrage we are requesting the loan and repaying the loan with the premium make sense that's it okay okay perfect so this is the contract now let us deploy this contract so I'm gonna compile this it's going to take some time to compile it'll give you a couple of warnings ignore you do not have to worry about it it says that there are some unused parameters perfect that is not a problem ignore right do not worry about it next is go to deploy and connect with metamask it's not the remix VMS will not work because all of these addresses that it's gonna be asking is for The Godly test Network so I've connected my metamask 816 ending in 816 to the girly right here and make sure that you have usdc if you do not have usdc go to this faucet staging dot Ave I'm gonna put this address also in the description section of the video below and request for ether you can request for usdc you can request for die request they are very generous about it they give like 10 000 usdc per 10. this is only testing right don't feel so happy right it's only per test uh and and doing your work make sense okay now let's deploy remember things to remember deploy the contract with the address pool of this right so I'm going to deploy the contract with this address pool that we have copied so remember ending in 15D I'm gonna paste it over here ending in 15D I said deploy as soon as I say deploy a pop-up comes up and it says confirm and it is going to confirm if if you view on it in either scan girly it's gonna take some time so I'm gonna pause the video while it's deploying if you look at it I have this contract which is now deployed I copy this or you can also copy the contract from here and go to your metamask and transfer one usdc this one usdc transfer will cover for your fees if I say send and I'm gonna say paste the contract address ending in 5e the contract ending in 5e transfer one USD like this and you would see that it's transferring now what you could do is I mean I could have written a function over here which would have said and given a balance of usdc but I don't want to confuse you all right so what I will do is it's it's sent so we go back to your girly uh ether scan and click on the contract go to erc20 tokens and you would see that one usdc here so one usdc is now transferred to this contract that will help us do our flash loan make sense now I will say request now remember I have transferred only one but I want five usdc's right so I have transferred to this contract not to the pool you one usdc will only serve for paying fees and other things right so let me now request a loan of five usds usdcs so I go back in here so this is the usdc uh token ID so this is b43 b43 the token ID comma How much I am requesting let let me be a little greedy I'm gonna request for 20 usdc's and I need to put six zeros one two three one two three so this is six zeros followed 20 followed by six zeros that is 20 usdc I have on this contract only one but I'm I can request 20 right so I click on this function and it says confirm and it's going to take some time uh I can click on view on etherscan it's going to take some time so I'm gonna quickly recap what we are doing so the first simple loan is requesting the loan and paying it back I'm not doing 3A right now we'll do three a later so all you would see is requesting a loan from lending pool 20 USD and repaying it back with premium make sense imagine this contract had one usdc and now we are able to leverage 20 usbcs so look at this from this pool let me show you how this tool come up so this is the address 15D remember the 15D that you put over here while deployment and pool is 7 F6 make sure and then from this pool it is coming to this contract and the contract is 5e so look at this the contract is ending in 25e or 625E and you look here 625E so from the pool it came to this contract with 20 usdc's and then the contract 625E returned back to the same pool 20.01 that 0.01 USD is the P the premium so this was very simple get the loan and repay back the loan let me let us make it more complicated let us add Arbitrage so what I'm gonna do is I'm gonna say that for a loan with some program on the uni swap I'm able to make 10 percent so I'm without going to UNI Swap and Sushi swap I'm gonna say ten percent let me let me show you how so I'm going to create a new I'm going to create a new file I'm going to say whoops not here I'm going to create a new file I'm going to say Arbitrage flash loan not so and I'm gonna copy the entire simple flash loan and paste it in Arbitrage and let us see what changes we are gonna make the first one I am going to make an owner so we are going to create that owner uh in the in the Constructor so I'm gonna say owner equals payable so I'm saying that whoever sends the contract or whoever deploys the contract would be the owner and then I'm going to create a function for Arbitrage so it's a very very simple function for Arbitrage so Arbitrage ustc whatever is my token address and whatever is the amount that means the usdc USD USD usdc and the amount when it comes in I get 10 percent of profit so this is my arbitraged amount can you get a 10 percent of profit probably not right can you get two percent maybe yes but just for the sake of our discussion we are assuming that it's a simple math whatever the amount is coming in I am taking the 10 percent and saying this might be charged amount and that amount I am returning back to the owner as a profit so Arbitrage amount imagine this as a profit so this is chain number two so change number one was the owner equals to payable page number two is the Arbitrage amount how do you do real in real time using uni swap this Arbitrage is I'll send this link as well it says swap swap exact tokens for tokens so this is the big another solidity which I do not want to confuse you with uh remember I mentioned at the start of the video that give a man a fish and you feed him for a day teach him how to fish you feed him for a lifetime so you can go in and and learn how to swap so up this is a very simple thing and for sushi swap it's nothing but an implementation of V2 uni swap V2 is Sushi swap so again do not want to confuse you whatever Arbitrage I am doing if you want to do it in real time you can use that uni swap protocol at this point in time I'm keeping it simple simple maths whatever the amount is I'm making 10 percent calculation assuming this is my profit makes sense and then what I'm going to do is uh call that function from the execute contract that's it three changes all right so I go to the execute operation you see this this contract now at once requested your logic goes here there you go so this is your arbitraged amount make sense so it is going to call arbitrary pure DC by passing on the asset and amount and it Returns the transfer once it is done you also return it back to the pool so this is how it works I'm gonna return the status which is also a Boolean okay so what what happens is again let's run through it request alone now I've also implemented three years so request a loan from The Lending pool this function is requesting the loan as in the simple flash loan right next sorry next perform Arbitrage so this is a small Arbitrage function that assumes that whatever the amount I put in I'm gonna get 10 percent profit so this is a very simple math calculation this is the only step which does all the trick this is transferring that token back to the owner and then here I am calling this Arbitrage assuming you are going to call the uni swap or whatever right and then this Remains the Same it returns back to the pool very very very simple now let's deploy this contract all of the other things are valid uh that means one decibel places deploy everything Remains the Same all right so let's do this I go back in here compile Arbitrage flash loan it's compiled it will give you the error the the warning and then you deploy using this address you're gonna copy this address and you're gonna deploy using this address Arbitrage drone deploy and you would see that this is deployed it's going to take some time for this to deploy so this is the contract I'm going to pause it for a second here the contract is deployed this is the contract this is the contract simple flash loan I should have renamed it to Arbitrage loan I'm gonna rename it now but obviously it's not gonna uh I have to redeploy it I'm not going to do that but please remember that in the GitHub it will be Arbitrage back flashlight this is The Flash loan which was deployed the reason I know it is because it's ending in 8 DB and it's going to 8 DB just to avoid confusion I'm gonna remove this so this is 8 DB now exactly the same thing I'm going to copy and go to metamask and transfer usdc one usdc to this particular contract so once it is done I'm gonna do the same validation I go to the contract here let me make sure that this is sent uh it's taking a little bit time it's good to pause the video here appears that it is done I click on the contract go to erc20 tokens and you would see that this token this contract has one usdc's now I'm gonna do the same thing again I'm gonna request five USBC now right I have only one one is only for transaction p and premium right do not worry about that I am requesting from The Lending pool five usdc's one two three one two three so five usdc's I'm requesting this time from the lending pool I want to confirm and this is going to be different right let's see let's see how it is going to be different uh this time so I click on view etherscan and the transaction is still indexing I'm gonna pause the video again now if you look at it you have five usdc's requested from the pool so this is the pool 5ff and 8 DB 58 DB is our contract right 58 DB is our contract so the first step it requests for five usdc's from the pool so request five years received from the pool this is arbitrage so from this contract to the owner of the contract 816 that means to my meta match uh I go back in here and if you look at it look at this 0.5 USD remember I had 70 19970 you can rewind the video and look at it I had 19970 usdc a transfer to the contract but from an Arbitrage perspective I got 0.5 USD look at this this 0.5 is transferred over here this is my 816 uh ending in three eight one six right this is my and then the last one from 58 DB the contract it's returned to the pool with this much of Premium this is exactly how you do flash load I hope this is simple this was simple explanation go to the GitHub copy this code make sure that everything remains same do exactly in the video make sure you rewind the video and look at it this is the concept of simple flash clone and Arbitrage loan thank you for watching
Info
Channel: BharatiDWConsultancy
Views: 47,723
Rating: undefined out of 5
Keywords: Cognos Training, Qlikview training, Tableau Training, Informatica Training, Machine Learning, Artificial Intelligence, Google Cloud Platform, Tensorflow, BigQuery
Id: FJEVuhyywI8
Channel Id: undefined
Length: 32min 52sec (1972 seconds)
Published: Sat Dec 24 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.