0:02 how do you take a loan without any sort
0:04 of collateral on the Chain the special
0:06 type of loan present on blockchains
0:08 which allow you to take a loan without
0:10 providing any sort of collateral
0:12 provided that you determine it in that
0:14 specific transaction that loan is called
0:16 a flash store and in today's video we're
0:17 going to understand how do we take
0:20 advantage of a flash loan to make
0:22 something happen what's up everyone GM
0:25 gmgm welcome to web3 club and in today's
0:27 video we are finally going to learn what
0:30 are flash loans how do we use them where
0:31 are they useful how do you go about it
0:34 and a bunch of stuff here and there by
0:35 the way today's video is presented to
0:38 you by eth India hackathon each India is
0:40 I guess Asia's biggest ethereum
0:44 hackathon and it is run by devolio so if
0:45 this is something that interests you
0:48 please go ahead check out eth india.co
0:51 I'll link to it in the description down
0:53 below the hackathon will be from 2nd
0:55 December to 4th of December it's only
0:57 going to be invite only so if I were you
0:59 I would just go ahead and apply it today
1:01 so that you get a good chance of being
1:02 accepted it doesn't cost anything to
1:04 apply and a maximum team size available
1:08 is 5 so just go ahead get your team and
1:09 just apply the hackathon will be from
1:12 2nd to 4 December in Karnataka Bangalore
1:15 so if you're looking to explore India's
1:18 Silicon Valley just fly in now back to
1:20 flash loans but before we get started
1:22 please make sure to hit that like button
1:24 and subscribe to the channel if you
1:25 haven't if you find something weird
1:27 something funny just leave it in the
1:29 YouTube comments if you want to send me
1:32 a message sponsor her video want my
1:34 consultation anywhere just send me a
1:36 mail my email address is available on my
1:39 YouTube channels about page I I believe
1:41 it it shows up on the desktop and if you
1:43 have a specific question if you want to
1:45 get your questions doubts cleared come
1:47 join my Discord server there are a bunch
1:49 of people just like you and me who are
1:51 trying to help each other out all right
1:54 with that said let's get started
1:56 started
1:59 first things first what are flash loans
2:01 and as per always documentation flash
2:02 loans are a special transaction that
2:05 allow you to borrow an asset as long as
2:07 the borrowed asset is returned in the
2:09 same transaction generally they come
2:12 with a fee and it is a feature that is
2:15 provided by away so away has a new
2:17 protocol called Huawei V3 it's not
2:19 really new it's it's been there since
2:21 long but I mean new for a lot of
2:24 purposes because it's the latest one and
2:26 it's not on ethereum yet so today's
2:28 video will not be on ethereum but it
2:31 will be applicable on all of these
2:33 networks so optimism arbitrum Harmony
2:36 polygon Phantom and Avalanche all these
2:39 networks we will cover today I mean you
2:40 don't really need to do anything you
2:41 just need to write the code once and
2:44 then deploy everywhere and it will work
2:47 seamlessly everywhere so away V3 has two
2:49 types of flash loans one is Flash loan
2:52 and one is Flash loan simple today we
2:55 are going to focus on flash loans simple
2:58 now how does it execute what what goes
3:00 on in the in the background what happens
3:03 so what we do is we create let's say a
3:06 smart contract that smart contract calls
3:09 away and asks for a flash loan so our
3:11 provides us with the flash loan and
3:15 after that Ave calls back a function on
3:17 our smart contract which we need to
3:20 execute now in that execution what we
3:21 need to make sure is after the execution
3:25 is done we return the loan that we have
3:28 taken and give it back to away and how
3:30 do we give it back we simply approve
3:33 that erg20 token for away to take it
3:36 back if away is not able to take back
3:38 the amount plus fees
3:41 the transaction will fail and you will
3:42 just end up wasting gas you will not
3:45 make any money and you will not be able
3:47 to keep those assets as well now why
3:49 would you want to do this there are a
3:50 bunch of reasons but let's say you want
3:53 to you see some Arbitrage in two dexes
3:55 and what you want to do is take
3:57 advantage of it so you can take a flash
3:59 loan so that you know you can take a
4:01 huge amount without having your having
4:03 it yourself do the Arbitrage make some
4:05 money and return the flash loan back
4:07 similarly if you want to liquidate a
4:09 position let's say for make a dollar for
4:12 Army or anything like that any sort of
4:14 loan that anybody has taken and it needs
4:16 to be liquidated in that scenario as
4:18 well what you can do is take a flash
4:21 loan liquid liquidate the person and
4:23 return the flash loan and keep your
4:26 profit now the flash loan feed sort of
4:29 depends on a lot of things but generally
4:31 you can figure out with this function
4:34 from the RS smart contract now how do we
4:36 do this so to to do this what we
4:39 basically need is this specific uh flash
4:41 loan simple receiver to be implemented
4:44 and Away has given us a base contact as
4:46 well so today we are going to use that
4:48 so to figure out the waste contract what
4:50 you do is you go to our way away V3
4:53 contracts flash loan and Flash loan
4:55 there's a base thing all right so now in
4:57 the base what you need is Flash loan
5:00 simple receiver all right so once once
5:02 you have this what you will do is open
5:05 remix and you will simply write this
5:06 code that I've already written written
5:09 so I'm going to explain I'm going to run
5:11 run you through this code and explain
5:13 what it does all right so the couple of
5:14 things that I've already done is
5:18 imported open Ziplines uh ierg20 and
5:20 safe math okay once we have these two
5:23 things what we also need is this flash
5:26 loan simple receiver base all right so
5:28 to do that we have Ave slash core V3
5:31 slash contract flash loan slash base and
5:33 then flash loan simple receiverbase dot
5:37 Sol this in itself is an abstract
5:40 contract on it so once we have that what
5:42 we do is write contract simple flash
5:44 loan which is Flash loan simple receiver
5:47 base okay now if you check out the
5:49 simple receiver base it has this I pool
5:51 address and everything and nothing
5:53 nothing else you know we need to sort of
5:55 worry about so one thing that we
5:57 basically need to understand is that in
5:59 the Constructor what will happen is we
6:03 will ask for the ipool address all right
6:05 I put address provider and to figure out
6:06 what the address will be what we'll do
6:10 is go here click on B3 we can just go
6:12 here and click on V3 over here and the
6:14 network that we are working with is
6:17 polygon so let's say polygon and then
6:19 once you have this pool address provider
6:21 you can just copy this address and just
6:24 use this address when deploying all
6:27 right so the next thing that we do is uh
6:29 we have this function called create
6:32 slash loan and it accepts two things one
6:34 is the asset and the other is the amount
6:36 so now once we have these two things
6:40 what we will do is we basically you know
6:42 just if you ignore this part what we're
6:45 just doing is calling this pool which is
6:48 from here we have this pool function
6:51 pool object uh so we are using this pool
6:55 object and calling flash loan simple on
6:57 that now flash loan simple
6:59 um takes five parameters
7:03 one is receiver so the receiver is the
7:06 Smart contract where we have the logic
7:10 up that we need to run after we receive
7:13 flash loan so this is the receiver then
7:15 there is acid so this asset is the
7:18 address of the smart contract of the
7:21 asset so that in dye's case you will
7:24 have dies address over here then the
7:26 amount is the number of tokens that we
7:29 want to sort of borrow uh params is
7:31 something that you know we can pass to
7:34 this and this function will again when
7:37 it calls the another smart contract or
7:40 the receiver smart contract it it sends
7:42 the parents with it all right so what we
7:44 need to do is basically have this ABI
7:47 encoded params in bytes format and then
7:49 we receive them over there and just
7:51 decode them all right and then there's
7:53 referral code which you can just safely
7:55 leave to zero so receiver is this
7:57 current address this current smartphone
7:59 right that we're using so this is what
8:02 we have the as the receiver and the
8:04 asset and amount are sent by the
8:06 argument in the parents we are just
8:09 letting it not be anything all right and
8:11 then this is a this is an important
8:14 function which is execute operation now
8:16 in this execute operation is the
8:19 function which is called by the average
8:22 pool it is called once a school
8:25 transfers the tokens to us all right so
8:27 the asset and the amount has been
8:30 transferred and now there's a premium
8:33 that we need to pay back to the away now
8:35 this initiator is the address which
8:38 actually initiated this flash loan so I
8:40 believe this initiator will be the
8:42 address of this specific smart contract
8:44 and then params are the things that we
8:46 send over here so this is the function
8:50 that will be executed uh once we receive
8:52 our smart our flash loan all right so
8:54 what we can do is we can run Arbitrage
8:57 over here integrate with uni swap to do
8:58 that Arbitrage integrate with any other
9:01 decks we can also do liquidation so that
9:02 you know if you want to liquidate let's
9:05 say somebody's maker position or away
9:08 position you can do that over here once
9:10 everything is done what you need to do
9:12 is figure out what is the total amount
9:14 so what you can do is amount Plus
9:17 premium all right so I'm using safe math
9:19 Library just because everybody else uses
9:22 and I'm not 100 certain if I can just
9:26 remove dot add with a plus which is why
9:28 I'm just using dot add over here so what
9:29 you can do is what you're doing is just
9:32 amount Plus premium that is the amount
9:35 that we owe back to away and then what
9:38 we do is we Typecast asset as an ie rc20
9:41 token and then approve the address of
9:44 the pool with the amount that we owe to
9:48 the pool so that the pool can transfer
9:51 these tokens back to itself and once you
9:53 know the approval is done we are just
9:55 logging that approval now instead of
9:59 emitting the amount owed let's emit the
10:02 amount uh that we actually own all right
10:07 so in the asset what we can emit is the balance
10:08 balance of
10:14 address this all right and this is
10:16 another thing that I want to emit first
10:20 before we ask for the flashlight so I'm
10:22 going to emit this before we ask the
10:24 flash loan and after we receive the
10:27 flash load okay so let me just make this
10:29 much more clearer
10:32 okay so now we once we have received our
10:34 flash loan uh this is what it will show
10:37 and then we are just returning true now
10:39 as per the docs we need to return true
10:42 if the if the if the operation was
10:45 successful otherwise we return false all
10:48 right so this is the Smart contract that
10:51 we needed now we need to do some Voodoo
10:53 stuff to actually make this work
10:56 actually test this out because uh we
10:58 what we actually need to do is um
11:02 interact with Avis smart contract so we
11:04 obviously don't have that on remix and
11:07 we don't really want to deploy this on a
11:09 network proper Network because we just
11:12 want to test it out all right so what
11:14 I'm going to do is something called
11:18 4king the network all right but before
11:20 we do that uh let's just compile this
11:23 and I I believe they will we will find
11:25 some issues here
11:27 yeah one issue will be we need to use 8.10
11:29 8.10
11:33 so uh
11:36 to do that we can just use this and then
11:38 compile again all right uh compilation
11:40 is not success successful because I used
11:43 8.10 so make sure that you use 8.10 if
11:45 you want to work with this now it is
11:47 giving us a couple of War warnings which
11:49 we can safely ignore now if we go to
11:51 deploy and run you can see that in the
11:53 environment option we have this thing
11:56 called hard hat provider and that is
11:58 something that we will be using now uh
12:01 what I am doing is I've created a new
12:05 folder and started my my terminal over
12:09 there now I believe you have npx or npm
12:12 node.js installed so if you have what
12:15 you have is also a way to basically run
12:18 this npx command so I believe you can
12:19 run this yeah and it should return
12:22 something for you all right so if you
12:24 don't have npx just go ahead figure out
12:26 how to install node in npm and ipd you
12:29 will receive npx with that so now when I
12:32 once I have npx what I do is write npx
12:34 hard hat
12:36 um in it and this is in the new Flash
12:38 loans directory that I am using so all
12:41 right so once I do that it is saying
12:44 that uh should I install hard hat and I
12:46 want to do that so I will just press e y
12:48 and just move forward so now we're just
12:51 waiting for hard head to install and I
12:52 believe it will install a bunch of other
12:55 things as well with us all right so now
12:56 it is asking us to create a JavaScript
12:58 project so let me just press enter
13:01 and this is where I want it to be the
13:03 root yes do I want to add a git ignore
13:06 yes do I want to install a few other
13:10 things that come with it yes why not so
13:11 again this will take a little time so
13:14 just we don't have any other option just
13:16 but just to wait all right once the
13:18 installation completes I hope what we
13:22 can do is run npx hard hat node and
13:24 let's just try to run this first so now
13:26 this thing actually ran without any
13:27 problems all right so we can just close
13:30 this what we actually did was we started
13:34 a blockchain node right over here and we
13:36 can simply interact with that node just
13:38 like how we interact with other
13:40 blockchains all right so what I'm going
13:44 to do is I'm going to Fork the polygon
13:46 chain so to do that what we'll write is
13:50 npx hard hat node then double hyphen
13:53 fork and what you need to add over here
13:57 is something called the RPC URL so for
14:00 polygon this is the public RPC URL for
14:03 better reliability what you should do is
14:05 you should have your own RPC URL by
14:08 either hosting your own node or you can
14:11 get your RP URL from a provider like
14:14 infuro or Alchemy on it so I'm going to
14:18 just use this one the polygon RPC if you
14:21 want to use let's say any of these other
14:23 networks you need to have an RPC URL for
14:26 those specific networks all right so now
14:28 I just press enter and wait for this
14:31 thing to start and again this time that
14:33 this thing basically has started so now
14:37 uh what I want to do is uh deploy this
14:41 code on this blockchain so what I'll do
14:45 is connect remix to Hard Hat by clicking
14:48 on the hard hat provider option now it
14:50 is saying what is the Json Json RPC
14:52 endpoint and I believe this is the
14:54 correct endpoint
14:56 and you can see over here the endpoint
14:58 is actually eight five four five and
15:00 that is exactly what is written over
15:04 here so we press OK and we wait for it
15:06 to connect once the connection has been
15:09 established uh the next thing that we
15:12 want to do is deploy this simple flash
15:17 loan on this chain okay uh so to do that
15:19 what I'm going to do is go go back to V3
15:22 main net contracts click on polygon and
15:24 we have pool address provider so I'm
15:25 just going to copy this pool address
15:32 by clicking on that and then copying um
15:35 from here okay so now that I've copied
15:36 it I'll just paste it over here and
15:38 click transact
15:40 and once the transaction succeeds you
15:42 can see the transaction is succeeded the
15:45 transition has succeeded um
15:46 um
15:49 we can open this over here and see that
15:52 we have four methods with uh
15:55 address provided as a method pool as the
15:57 method and bunch of things okay so now
15:58 what we can do is just simply call
16:02 create flash load but we don't have any
16:05 assets all right so to fix that what I
16:09 have done is I've gone ahead and figured
16:12 out a couple of Assets Now what you can
16:14 see over here is I have written a test
16:18 all right which basically walks us to a
16:20 bunch of things and basically creates a
16:24 flash loan and executes it but we don't
16:27 have any token for us all right so to
16:29 fix that what I have figured out is that
16:31 we have is die all right this is the
16:34 address of die on polygon and we can
16:37 figure out that this is actually
16:39 the address on polygon
16:41 by pasting it over here and you can see
16:44 that this is the Tie stable coin okay
16:47 and then what I also found was a die
16:50 Veil so to do that what I click did was
16:52 clicked here
16:54 so we have this token page then on token
16:58 page we have this holders and on holders
17:00 what you can do is figure out click on
17:02 any of the holders for example this one
17:06 make sure it is not a contract it is an
17:08 eoa of of
17:11 a user okay and that user should have
17:14 some Matic and then what we can do is
17:16 just copy their address all right once
17:18 we copy their address we can put this
17:21 here in the die well so what we are
17:23 going to do is we are going to use this
17:26 address this is uh die
17:29 um we can because we have forked the the
17:31 node what we can do is override any
17:33 transaction so what we'll do is we'll
17:36 take control of this address and send
17:40 some Diet towards us okay and this is
17:42 the pool address provider that we
17:45 already know for polygon okay so we have
17:47 die and die wheel so now that we have
17:49 these two things uh what I'm going to do
17:52 is create a new
17:55 terminal and here I'm right I'm going to
17:58 write npx hard hat node hyphen hyphen
18:02 Network Local Host so when I do that uh
18:04 and actually I don't want node I want to
18:07 console so when I do this it will create
18:11 a JavaScript control and connect us to
18:13 the the node that is running in the
18:15 different window so the first thing that
18:17 I'll do is I'll just copy these two lines
18:19 lines
18:21 and paste them then I'm going to do
18:25 token equals await ethers dot get
18:29 Contract add Okay and like this is
18:32 something that I know for ethers but uh
18:36 I can simply provide a common interface
18:39 and it will read it okay and I can just
18:41 send the dies address so once I do that
18:45 it did not read it for some reason uh
18:46 let me just figure it out all right I've
18:49 figured it out with some effort so once
18:52 you init the hard hat what you need to
18:56 do is basically
18:58 um install open Zeppelin contracts by
19:01 writing npm open zipline contracts so
19:04 once you do have that what you can do is
19:06 just open the lock.sol that comes with
19:09 it with installing hard hat and import
19:13 this line and just make log is erc20 and
19:16 then put it over here in the Constructor
19:18 so that you know you have an erc20
19:21 functioning contract and once you have
19:23 that then this is such a hacky thing to
19:26 do but what you can do is just write
19:29 ethers dot get uh
19:33 contract Factory and you can just write
19:36 loc over here and why lock because this
19:38 is the name of the contract over here
19:40 and once you do that you can get a lock contract
19:47 uh like this
19:52 and with love you can attach it to
19:56 um do the die okay so once you attach
19:59 this to die what you can do is uh you
20:02 can get like die equals like this okay
20:06 so we have die now and die has let's say
20:08 the symbol if you want to figure out the
20:10 symbol of die we can await this and we
20:13 can see that it returns die so it will
20:16 use all the ierg20 tokens so this is
20:18 something which is a little difficult
20:21 weird to do but this is just that way
20:22 this is we are doing just this for
20:25 testing uh in real world you don't
20:27 really need to do this this is just for
20:31 us to test okay for some reason uh the
20:33 code that you know should work which is
20:37 get contract at and then ierg20 and then
20:40 the die this code should work for some
20:44 reason but it is giving me an error so I
20:46 don't know what is going on which is why
20:49 we are doing this in a very weird way
20:51 which is doing this wire die so I'll
20:52 write this down in the description so
20:54 that you can understand this copy paste
20:56 stuff but this is how it goes okay so
20:58 now that we have die another thing I
21:02 need is I want to unlock the the die
21:04 rail okay and why do I need to unlock
21:07 because I want to use that address and
21:10 get get dye from there so this is our
21:12 own blockchain that we are running so we
21:14 can do this so to do that simply just
21:16 paste this code which will be there in
21:19 the descriptions link somewhere uh which
21:22 is HR await
21:23 hre.network.provider.request and then
21:26 there's an object in this as an argument
21:28 which is Method hard hat underscore
21:30 impersonate account and then in the
21:33 patterns we send die underscore whale
21:35 all right so this way what we can do is
21:37 we can use die Veil
21:40 diwal's address and call whatever you
21:41 want to call with it so the next thing
21:44 that I'll do is I will you create a
21:46 signer with dive in so what we'll do is
21:49 just write signer equals update ethers
21:51 dot getsigner die underscore will just
21:54 press enter so now that we have assign
21:56 it what we can do is just write die dot
21:59 connect okay connect with signer so once
22:01 you have connection uh the next thing
22:07 and the transfer to uh
22:10 accepts the two address so the two
22:12 address that we want is the address of
22:14 our simple flash loan so I can copy this
22:17 over here and paste this over here all
22:19 right and the next thing that I want to
22:21 do is let's say for now let it be 0
22:23 which is the amount of die that I want
22:26 to transfer okay uh before we do
22:28 anything let's just see the balance of
22:29 our address
22:33 okay and we await this so the balance
22:35 you can see is zero so the next thing
22:38 that we'll do is just transfer ethers
22:44 dot utils dot parse ether so path pass
22:46 ether what it will do is it will pass
22:49 this and multiply by 10 to the power 18.
22:51 okay okay so what do you want to do is
22:53 we want to transfer this much amount
22:56 from the signer which is the dival to
22:59 our smart contact address so that we can
23:03 pay some fees from the for the flash
23:05 loan that we will take so I press enter
23:08 the transaction has I hopefully
23:11 completed and if I check my balance now
23:14 you can see that it is much more all
23:16 right so now that we have received the
23:19 balance uh what we can do is simply
23:22 create this flash loan okay so to create
23:24 the flash loan what we need to do is
23:27 just copy the die address and the amount
23:30 so the amount that for which I want to
23:32 take the loan yeah
23:34 yeah
23:38 is uh let's say for um
23:41 500 die okay so I want to take a flash
23:45 loan for 500 that okay and I paste this
23:48 over here and I click on Transat and it
23:49 took a little while for some reason but
23:51 the transaction has gone through and I
23:54 can open this transaction over here now
23:56 you can see that there are logs as well
24:00 so what logs show you is the events that
24:03 we have um emitted so we emitted the
24:07 same event twice okay the events name is
24:10 log so I'm not sure if it is visible but
24:12 yeah hopefully it should be visible now
24:14 so the event's name is log which is
24:17 which we emit uh to see what is the
24:20 current balance of our token so uh
24:22 initially we had like uh I believe a
24:25 thousand tokens then we received a
24:29 transfer uh after that we sort of return
24:31 all right before returning we had a
24:34 flash loan of 500 that was transferred
24:37 so you can see that uh after just after
24:43 the loan we had um 1500 tokens but if I
24:46 check now uh what is the balance you
24:48 will see that it is a little less than
24:51 10 000 because we also had to return the
24:57 fees so this is how you do a flash loan
24:59 the I understand that this would have
25:01 been a little Haywire because of few
25:04 things here and few things there uh but
25:07 if we were to do all of this on hard hat
25:09 you wouldn't have to uh you know go
25:11 through so much pain this is how you do
25:13 flash loan uh once you you know you
25:15 receive the flash loan what you will do is
25:16 is
25:18 um in this function called execute
25:21 operation you can put whatever code you
25:22 want interact with the decks interact
25:25 with a liquidation protocol whatever you
25:29 want to do and you know make sweet like
25:31 make that money make that return
25:33 whatever however you can hopefully this
25:36 was not very complex and you've got the
25:37 gist of this thing if this video was
25:39 helpful please make sure to hit that
25:41 like button and subscribe to the channel
25:43 if you haven't I will make sure to put
25:45 all the code all the links in the
25:47 description down below so that you can
25:48 go through them if there's anything
25:50 confusing please leave it in the YouTube
25:52 comments or join the Discord server
25:53 there are a bunch of people just like
25:55 you and me who try to help each other
25:57 out if you want to send me a message if
25:59 you want to ask me for some consultation
26:02 if you want to sponsor a video please
26:05 send me an email and if you're a
26:08 developer make sure you check out eth
26:10 india.co it's a great hackathon that I
26:13 believe you should attend it's in
26:16 Bangalore from 2nd of December to 4th of
26:17 December so
26:20 I like it's a no-brainer just go ahead
26:23 register and I hope to see you again