Comment on page
Performing a Flash Loan
Flash Loans are special uncollateralised loans that allow the borrowing of an asset, as long as the borrowed amount (and a fee) is returned before the end of the transaction. There is no real world analogy to Flash Loans, so it requires some basic understanding of how state is managed within blocks in blockchains.
Flash Loans are an advanced concept aimed at developers. You must have a good understanding of Ethereum, programming, and smart contracts to take advantage of them.
For developers, a helpful mental model to consider when developing your solution:
- 1.Your contract calls our
LendingPool
contract, requesting a Flash Loan of a certainamount
of areserve
. - 2.After some sanity checks, our
LendingPool
transfers the requestedamount
of thereserve
to your contract, then callsexecuteOperation()
on your contract (or another contract that you specify as the_receiver
). - 3.Your contract, now holding the Flash loaned
amount
, executes any arbitrary operation in its code. When your code has finished, you transfer the Flash loanedamount
ofreserve
back to ourLendingPool
. - 4.Our
LendingPool
contract compares the balance of thereserve
before and after your code execution, ensuring that the balance of thereserve
is exactly the same as before plus the Flash Loan fee.- If the balance of the
reserve
before and after your code execution is correct, then theLendingPool
function continues to completion. - If the balance of the
reserve
after your code execution is incorrect, then the function reverts, undoing any state changes your contract made.
- 5.All of the above happens in 1 transaction (hence in a single block).
Aave Flash Loans are already being used in the wild. Some examples include:
- Arbitrage between assets, without needing to have the principal amount to execute the arbitrage. Example: ArbitrageDAO.
- Swapping collateral of loan positions, without having to repay the debt of the loan positions. Example: Collateral Swap, DeFiSaver.
Depending on your development environment, the following tutorials may be helpful.
If you have an existing development environment and want the most straight forward approach to implementing Flash Loans:
If you have a Truffle project and want to implement Flash Loans:
If you want to keep it simple and use Remix to test and deploy:
The following are resources that Aave developers have created or conducted.
Last modified 3yr ago