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:
Your contract calls our
LendingPool
contract, requesting a Flash Loan of a certainamount
of areserve
.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
).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
.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.
All of the above happens in 1 transaction (hence in a single block).
Applications of Flash Loans
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.
Implementing Flash Loans
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:
... in your projectIf you have a Truffle project and want to implement Flash Loans:
... with TruffleIf you want to keep it simple and use Remix to test and deploy:
... with RemixOther official resources
The following are resources that Aave developers have created or conducted.
Code your first flash loan @ ETHLondonUK (video)
Flash loan workshop @ ETHLondon (code)
Community resources
Also see the community contributed resources.
Last updated