Borrow tokens with Aave FLASHLOANS - Solidity tutorial

Investment Rookie
25 followers
Oct 28, 2022
Flash Loans allow you to borrow a very large sum of tokens on Ethereum without providing
any collateral, as long as you repay the loan in the same transaction.
These can be used for doing arbitrage between different decentralized exchanges, as well
as many other really cool stuff with DeFi protocols.
In this video, I will explain how a Flash Loan works, what you can do with it, and I'll
also show you how to borrow tokens in Flash Loans using a Solidity smart contract and
Aave protocol.
Hey, I'm Julien, and on my channel EatTheBlocks, I teach blockchain development and how to
get your first blockchain job.
In a Flash Loan, there are 3 steps.
First a smart contract borrows tokens from another smart contract on the blockchain.
Then with the token you borrow, you can use these tokens in whichever way you want and
we'll see after what cool stuff you can do.
And finally, you need to repay the tokens to the lender.
You might think, well, what is so revolutionary about this?
Well, two things.
First, it all happened in a single transaction.
And second, there was absolutely no collateral involved, unlike lending protocols like Compound.
If you don't reimburse the loan in the same transaction, the smart contract of the lender
is going to throw an error and that will cancel the whole transaction.
That's how lenders can protect themselves, even if there is no collateral.
So now let's see what you can do with Flash Loans.
You can do arbitrage between decentralized exchanges.
If you notice a discrepancy in price for a certain token, then you can borrow a lot of
this token and you do your arbitrage, but with a huge leverage.
You can also refinance loans.
So for example, if you have a loan on Compound at maybe 10% and you notice that it's also
possible to have the same loan at 5% with another lending protocol, what you can do
is you borrow a token with a Flash Loan, you reimburse your loan on Compound, then you
open the loan at 5% and with this money, you can reimburse the Flash Loan.
So you've effectively moved from a loan at 10% to a loan at 5%.
And you can also swap the collateral of an existing loan.
So for example, if you have a loan on Compound and for the collateral, you have a token A,
but you want to swap this against token B, what you can do is you take a Flash Loan for
token B, then you send these tokens to Compound as a collateral for your loan.
Then you take out token A from Compound, you use a decentralized exchange like Uniswap
to change token A into token B, and then you reimburse your Flash Loan.
So next, we're going to talk a bit of Aave, which is the DeFi protocol we're going to
use for Flash Loans.
So Aave started its life as a project called Ethland.
Ethland was a peer-to-peer decentralized lending protocol on the Ethereum blockchain.
Then Ethland changed its name to Aave and it also updated its lending mechanism.
The new version is very similar to Compound.
However, it has two features that are very unique.
First, you can switch between a variable rate to a fixed rate during the lifetime of the loan.
And second, you can do Flash Loans.
And that's what we're going to focus on.
So I'm going to show you how you can create a Flash Loan on Aave in a Solidity smart contract.
So here I am in Remix and I've created a smart contract called Borrower.
And we're going to call the smart contract of Aave to initiate our Flash Loan.
So we're going to go to the GitHub repo of Aave.
That's where there is the smart contract of Aave.
And we're going to click in the contract directory, inside configuration,
lending pool address provider.
And you're going to copy the URL.
Then back to Remix and we're going to import the URL.
So this way we can know the interface of lending pool addresses provider.
And we're going to need this in order to interact with the Aave smart contract.
Then we're going to define a pointer to this smart contract.
So lending pool addresses provider.
I'm going to call this provider.
And also going to need the address of the smart contract of Dai.
Because in our example, we're going to borrow some Dai.
So then in our constructor, we're going to define these two parameters.
The address of the provider smart contract of Aave and then the address of Dai.
So instantiate the pointer to the provider smart contract.
And for Dai, we just need the address.
And then we're going to create a function to initiate the borrowing process.
So we're going to call this start loan.
It's going to receive two parameters.
First, the amount that we want to borrow.
And then another argument that I will explain later.
So first, we need to have the address of the lending pool smart contract of Aave.
That's the smart contract that we need to interact with.
And for that, we're going to use this lending pool addresses provider.
Get lending pool.
And we also need the interface of this lending pool smart contract.
So let's go back to the repo of Aave inside the contracts directory.
And it's going to lending pool lending pool.
So let's copy the URL.
Let's go back to our smart contract.
We're going to import this.
OK.
And so now we can create a pointer to this smart contract lending pool.
Let's get rid of this.
And after to initiate the flash loan, we call lending pool dot flash loan.
So first, the address of the receiver.
So that's the smart contract that's going to receive the loan.
So you can make it so that the smart contract that initiated the loan is
different from the smart contract that received the loan.
But to make it simple, we're going to make it in the same smart contract.
So we're going to receive the loan in this smart contract.
Then the address of the token that you want to borrow.
So in our case, we want to borrow DAI.
Then the amount that you receive.
And finally, the parameter.
So what's going to happen here is that when you call lending pool,
it's going to send all of these tokens specified by the DAI parameter
and the amount parameter to this smart contract here.
And it's going to call a function on the smart contract.
And as the last argument provided to this function,
it's going to forward these parameters.
So you don't have to use this last argument.
You can just provide an empty string.
But if you need to forward some data to this function, then you can use that.
So next, in order to be able to receive this flash loan,
we need to inherit from a smart contract of Aave.
So I'll go back to the repo of Aave.
And I'll go inside the flash loan directory, go inside base,
flash loan receiver base.
So you're going to copy the URL of this file.
Let's go back to our smart contract.
And we're going to import this.
And we're going to inherit from this smart contract.
Flash loan receiver base.
So here, contract borrower is flash loan receiver base.
And then in a constructor, we need to call the constructor of flash loan receiver base.
So we're going to do this like this.
Flash loan receiver base.
Actually, let me go to the next line.
Let's make some space here.
Okay, flash loan receiver base.
And we're going to forward it the address of the provider.
And then we're going to create a function to receive the loan.
So that's going to be called execute operation.
And it's going to receive four arguments.
First, the address of the token that is lent.
So we're going to call this reserve.
Then the amount that was sent.
Then the fee that we have to include when we pay back the smart contract of Aave.
Because this loan is not free, we have to pay some fee.
And finally, the parameters that were forwarded.
So the type of this is bytes.
And this is an external function.
So after, you can do whatever you want with the tokens.
So for example, you can do some arbitrage, refinance a loan, change collateral of loan,
as I explained before.
And once you've done everything you need and you have the token,
then you need to repay the loan.
In order to do this, you're going to call a function called transfer funds back to pool internal.
So you specify the address of the token, the amount.
So you pay back the amount plus fee.
In case you are wondering where does this function come from?
This is actually inside this flash loan receiver base that we inherit from.
So everything I show you in this tutorial is actually a simplified version of the documentation
for developer of Aave.
And if you want to dig into it, you can check out this URL.
If you want to keep learning about DeFi programming and learn how you can integrate
other DeFi projects like Compound or Uniswap,
you can check out this playlist where I have many other really cool tutorials.
I'll see you there.
