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