Skip to Content
docs

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:

  • flashLoan(): 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 approved flashBorrowers, managed by the ACLManager.
  • flashLoanSimple(): 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.

Execution Flow

A helpful mental model when building with flash loans:

  1. Your contract calls the Pool contract, requesting a flash loan of one or more assets using flashLoanSimple() or flashLoan().
  2. After some sanity checks, the Pool transfers the requested amounts of the reserves to your contract, then calls executeOperation() on the receiver contract.
  3. Your contract, now holding the flash loaned amount(s), executes any arbitrary operation in its code:
    • With flashLoanSimple(), once your code has finished, approve the Pool for the flash loaned amount + fee.
    • With flashLoan(), depending on the interestRateMode passed 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.
    • If the amount owed is not available, because of a missing balance, missing approval or insufficient collateral, the transaction reverts.
  4. 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_PREMIUM_TOTAL for the current value.

The fee can be split between liquidity providers (LPs) and the protocol treasury. FLASHLOAN_PREMIUM_TOTAL is the total fee paid by borrowers, of which:

At initialization, FLASHLOAN_PREMIUM_TO_PROTOCOL 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 IFlashLoanSimpleReceiver or IFlashLoanReceiver interface by implementing the relevant executeOperation() 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 flashLoan() or flashLoanSimple() function. See the Pool docs for parameter details, and use your contract address from step 1 as the receiverAddress.

From a Different Contract

As with an EOA, use your contract address from step 1 as the receiverAddress.

From the Same Contract

To call from the contract you set up in step 1, use address(this) as the receiverAddress in the flash loan method.

Never keep funds on your FlashLoanReceiverBase contract. An attacker can use them in a griefing attack.

3. Complete the Flash Loan

Once your logic in executeOperation() has run, you must pay back the flash loaned amounts if you used flashLoanSimple(), or passed 0 in the interestRateModes of flashLoan() 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 executeOperation().

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 interestRateModes for an asset, the onBehalfOf address takes on the debt, but only if it has previously approved msg.sender 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.