Aave V3 Flash Loans
Flash Loans let you borrow an asset without collateral, as long as the borrowed amount and a fee are returned before the end of the same transaction. They have no real-world equivalent, so using them requires a basic understanding of how state is managed within a blockchain block.
Flash Loans require a good understanding of the EVM and smart contracts.
Overview
A flash loan gives access to the pool’s liquidity, for reserves with borrowing enabled, for one transaction. By the end of that transaction, the amount plus the fee must be returned or, where allowed, a debt position must be opened.
Aave V3 offers two options for flash loans:
flash: borrows from multiple reserves in a single transaction. The borrower can also open a variable rate borrow position, backed by supplied collateral or credit delegation, instead of repaying. The fee is waived for approvedLoan() flash, managed by the ACLManager.Borrowers flash: borrows from a single reserve. The fee is never waived and no debt position can be opened, but it uses less gas, which makes it the better choice for simple single-asset flash loans.Loan Simple()
Execution Flow
A helpful mental model when building with flash loans:
- Your contract calls the
Poolcontract, requesting a flash loan of one or more assets usingflashorLoan Simple() flash.Loan() - After some sanity checks, the
Pooltransfers the requestedamountsof thereservesto your contract, then callsexecuteon the receiver contract.Operation() - Your contract, now holding the flash loaned
amount(s), executes any arbitrary operation in its code:- With
flash, once your code has finished, approve the Pool for the flash loaned amount + fee.Loan Simple() - With
flash, depending on theLoan() interestpassed for each asset, either approve the Pool for the flash loaned amount + fee, or have enough collateral or credit delegation available to open a debt position.Rate Mode - If the amount owed is not available, because of a missing balance, missing approval or insufficient collateral, the transaction reverts.
- With
- All of the above happens in one transaction, and therefore in a single block.
Applications of Flash Loans
Aave V3 itself uses flash loans for its liquidity switch feature. Other common uses:
- Arbitrage between assets, without holding the principal needed for the trade.
- Liquidating borrow positions without holding the debt amount, using the discounted collateral received to pay back the flash loan amount + fee.
Flash Loan Fee
The flash loan fee is set to 0.05% at deployment and can be changed by governance vote. Read FLASHLOAN_ for the current value.
The fee can be split between liquidity providers (LPs) and the protocol treasury. FLASHLOAN_ is the total fee paid by borrowers, of which:
- Fee to LPs:
FLASHLOAN_-PREMIUM_ TOTAL FLASHLOAN_PREMIUM_ TO_ PROTOCOL - Fee to Protocol:
FLASHLOAN_PREMIUM_ TO_ PROTOCOL
At initialization,
FLASHLOAN_
is set to 0.
Step by Step
1. Set Up the Receiver Contract
Your contract that receives the flash loaned amounts must conform to the IFlash or IFlash interface by implementing the relevant execute function.
Because the owed amounts are pulled from your contract, your contract must give allowance to the Pool to pull those funds to pay back the flash loan amount + premiums.
2. Call flashLoan() or flashLoanSimple()
You can call either flash loan method on the Pool in 3 ways.
From an EOA
To use an EOA (a regular Ethereum account), send a transaction to the Pool calling the flash or flash function. See the Pool docs for parameter details, and use your contract address from step 1 as the receiver.
From a Different Contract
As with an EOA, use your contract address from step 1 as the receiver.
From the Same Contract
To call from the contract you set up in step 1, use address(this) as the receiver in the flash loan method.
Never keep funds on your
Flash
contract. An attacker can use them in a griefing
attack.
3. Complete the Flash Loan
Once your logic in execute has run, you must pay back the flash loaned amounts if you used flash, or passed 0 in the interest of flash for an asset.
Paying Back a Flash Loaned Asset
Make sure your contract holds the amount + premium for each borrowed asset. For each asset, it’s the sum of its entries in the amounts and premiums arrays passed to execute.
You don’t need to transfer the owed amount back to the Pool. The funds are pulled automatically at the end of your operation.
Incurring a Debt (Not Immediately Paying Back)
If you passed 2 (variable rate) in interest for an asset, the on address takes on the debt, but only if it has previously approved msg. to borrow on its behalf. Stable rate borrowing is deprecated, so 1 reverts.
This means some assets can be paid back immediately while others become debt.