Learn to execute a profitable ERC-20 Flash Loan on Ethereum Blockchain - Earn $5000+ Weekly
Video Source: Trading
welcome back to solidity repository everybody I'm Jeff your host and as always we've got a very exciting topic to discuss today I've been receiving an overwhelming number of requests from our followers many of who want to learn how to perform and execute their very own flash loan on the ethereum blockchain so today I will show you how to utilize a flash Loan in order to harvest profits from Arbitrage or a price differential between LP token pairings utilizing any erc20 token and wrapped ethereum this is possible as long as the token pairing is available for trading via the Unis swap protocol so without further Ado let's jump right into the content and one more thing if you like today's video be sure to like comment and subscribe down below and be sure to follow us over on x.com for any future exciting updates I would just like to make a few key distinctions before we begin while flash loan attacks often differ in their methodology and scope from one another a common attri rute that they share is that flash loans involve manipulating protocols that use a spot price from a decentralized exchange as its sole price Oracle defi Protocols are typically looking to maximize their decentralization and censorship resistance centralized price oracles undermine this objective by acting as a singular point of failure so here's an example of the type of flash loan funded attack on a defi lending protocol namely Unis swap that we will be using so so in Step One the attacker borrows a large amount of token a from a protocol supporting flash loans two the attacker swaps token a for token B on a decentralized exchange lowering the spot price of token a and increasing the spot price of token B on the decentralized exchange the attacker deposits the purchase token b as a collateral on the D5 protocol that uses the spot price from the above decentralized exchange as its sole price feed and then it uses the manipulated spot price to borrow a larger amount of token a than should normally be possible step four is the attacker uses a portion of borrowed token a to fully pay back the original flash loan and keep the remaining tokens generating a profit from the protocol's manipulated price feed also known as the Arbitrage differential and the last step is as the spot prices of token a and token B on the decentralized exchange are arbitraged back inside the true market-wide price the defiled protocol is left with an uncollateralized position this Nets us a profit in the process okay now let's dive right into it okay now we are ready to execute our very own flash loan so the first step we are going to head over to our internet browser I highly recommend and personally use Google Chrome so once we have our internet browser open uh we are going to need to ensure that we have copyright or web3 wallet installed on our browser if you do not have this already installed I would recommend heading over to copyright doio which is their official link and downloading for Chrome I will put a link to this in the video description down below next we are going to need a code IDE that allows us to code in the solidity language solidity is the native ethereum coding language so for this we will need to head over to remix.in remix link remix.ogg so now that we have our IDE open the first step is to head over here to the file tab the file explorer and then we are going to select new file so we are going to name our file flash loan. o and then press enter so this brings us a new file where we can edit and input our very own code to launch and execute our very own flash loan I have prepared The Flash loan code that you will need and I will have a link in the description down below for you to access this code so what you're going to do is you're going to copy the entire string of code all the way to the bottom and you're going to copy and you're going to paste this into your remix IDE and it's letting us know that we have just pasted a code snippet or cont contract into the editor and to make sure that we fully understand the code before deploying or interacting with it okay now we will do a quick inspection of our source code so as we can see here this is our Unis swap V3 flash loan contract uh feel free to have a read through the entire contract a couple of key lines that we want to memorize here It lines 57 and 58 so essentially what this is is this is uh showing us that we have the wrapped ethereum token address here and this is allowing us to perform our flash loan with the wrapped ethereum token pairing and we will actually copy this contract address here and paste it into etherscan.io blockchain Explorer and as we can see this is indeed the contract address for wrapped ethereum so continuing on the next step we have to take is we have to head over to the solidity compiler Tab and we have to compile our contract and as we can see this smart contract has been written in pragma solidity version 0.6.5 so we must remember to select that from the drop- down menu here so solidity version 0.6.3 so the next step we were going to need to compile our contract here compile flash loan. so and great as we can see here indicated by the green check mark compilation is indeed successful so the next step is to head down to the next tab here on the left hand side to the deploy and run transactions page and once we click this first things first we are going to make sure our selected environment is injected provider copyright okay and that shows that our account is active a c 22c so then we open our copyright up here and see C 22c okay we can confirm that that's the same wallet that's connected our flash loan wallet okay The Next Step here is from the contract menu here we want to make sure that we have Unis swap V3 flash loan selected flash loan. so now we're going to need to press the arrow next to the deploy tab here in order to put in our required inputs so we're going to select this and it's requiring us to enter two different parameters the first one we need to enter is the contract owner so in this field here we will need to enter our wallet address as we are the owner of the smart contract so I'm going to copy here then I will paste my wallet address in this field and again to confirm c22 C are the four final digits and c22 c and we're also just going to quickly confirm that we are on the ethereum main net before we proceed any further okay now the next step is we have to enter our Target token pering that we are going to Target along with wrapped ethereum in order to make profit on the Arbitrage so for my flash loan today I have decided to select usdt or tether this is going to be the erc20 tether token on the ethereum network and as we can see we can view the contract here on etherscan.io and this shows that this is tether USD and it is indeed the contract so we will select our token address we can either select that here or we can select that over here on ether scan so I'm going to copy this address and I'm going to paste it here in my target you guys can use any token you choose you could use for instance uh Harry Potter Obama Sonic 10 Inu you could use Pepe token or anything of the like so now that we have both the contract owner address which is our wallet address and the target address entered we are going to click transact okay we are now receiving a message we are about to create a transaction on Main Network confirm the details to send the info to your provider so we will have a little bit of a gas fee here as this is a very heavy smart contract in order to launch our flash loan so we're going to click confirm and this will open a prompt in our metam mask window it's going to cost approximately $16 in gas or 0.009 ethereum so we're going to collect confirm okay and as we can see down here our contract has successfully compiled and launched so we have a few options down here we have our start function when we press this function this is going to start the flash loan then we have a second function here which is going to stop our flash loan and then we have our third function here which is going to withdraw our initial deposit as well as any profits back to our initial deployer ad address which is here and down here we have our owner function and if we call her owner function as we can see here the owner of the smart contract is indeed c22 C again our wallet address as we see here and our Target the token we are targeting is indeed tether usdt ending in 1 ec7 and as we can see here ending in 1 ec7 okay so it looks like everything is set up properly and ready to go so the next step in the process is we are going to have to fund our flash loan with an initial ethereum offering in order to be able to profit off of the difference so what we are going to do is we are going to click the copy here and this will copy our contract address of our Unis swap V3 flash loan and then what we are going to to do is we are going to send ethereum that we would like to use in order to make profit so after I've clicked copy I'm going to open up my copyright wallet and I'm going to click Send and I'm going to paste the address that I just copied from here which is indeed my contract address and I'm going to fund it now given that the ethereum network has gas fees and processing time I highly recommend starting with at least one ethereum as this will allow your flash loan optimal speed and enough space to operate within the M Pool and it will allow for a larger profit return so now what we are going to do is we are going to click next in order to fund our contract it's going to cost us some gas fees approximately 0 76 and we going to click confirm so as we can see we have a contract interaction pending excellent so we've sent our one ethereum so again we have our contract address here now we will take our contract address and we will paste it into etherscan.io and see as we can verify here we we are indeed the contract Creator again at c22 C c22 C and we have funded our contract with our one ethereum so the next step for this process is for us to head back over to our remix.aac as we Press Start we can see we have another gas fee we're going to click confirm and this will open another gas confirmation in copyright and we will click confirm okay and as we can see here our transaction has successfully completed so now we're going to give it a few minutes to allow our flash loan to run so we'll check back in a few minutes now that we've taken a quick break let's check back in and see how our flash loan has performed okay the next step of the process is we are going to stop our flash loan and see what kind of profit we have earned so next we're going to press the stop function we are going to confirm our transaction and and we are going to confirm we're just waiting for our transaction pending as we can see we have 0.16 ether just stand by and allow this to process okay and our transaction has completed so uh generally when you stop the flash loan uh the smart contract is coded to automatically withdraw the profits back to your copyright wallet however sometimes it may not automatically happen uh in that case you will have to press the withdrawal button okay so let's go ahead and see just how much profit we earned if you guys remember we started with 1.17 ether and and we deposited one ether into the smart contract so let's see just how much profit we've earned whoa 1.86 ethereum so in the span of about 14 minutes we managed to earn a little over 0.7 ethereum wow we managed to earn over $1,000 American USD in just 14 minutes using this flash loan process now sometimes it's a little bit more profitable sometimes it's not as profitable however it generally works very very well in my experience in the last 30 days I've earned well over 20 ethereum from this method I'm glad I'm able to help you guys and share this method with you as well uh if for any reason you want to be able to you close this out and you want to be able to redeploy your uh flash loan smart contract you will need to load contract from address here and you will have to copy and save your contract address from here and then you can load it there for future use so that's pretty much it guys uh very simple very easy to understand uh there are more difficult and more Advanced types of flash loans out there with higher profitability and better margins however this is a very userfriendly very simple tutorial and as you can guys can see earning 0.7 ethereum or a little over 1,000 American dollars in 14 minutes that seems to be sufficient for me all right guys thanks for tuning in and I appreciate it well everybody that's it for today's video I appreciate you folks tuning in and I hope you like the tutorial please leave some feedback down below whether this tutorial helped you or if there's anything I could do to better improve the content in the future for you the user thank you very much and be sure to like comment and subscribe follow us over on x.com check out our website at solidity l.org and also be sure to check out our ethereum giveaway in the comment section down below