# 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.

> [!NOTE]
> 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()`](/aave-v3/smart-contracts/pool#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](/aave-v3/smart-contracts/acl-manager).
- [`flashLoanSimple()`](/aave-v3/smart-contracts/pool#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`](/aave-v3/smart-contracts/pool) contract, requesting a flash loan of one or more assets using [`flashLoanSimple()`](/aave-v3/smart-contracts/pool#flashloansimple) or [`flashLoan()`](/aave-v3/smart-contracts/pool#flashloan).
2. After some sanity checks, the [`Pool`](/aave-v3/smart-contracts/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`](/aave-v3/smart-contracts/pool#flashloan-premium-total) for the current value.

The fee can be split between _liquidity providers (LPs)_ and the protocol treasury. [`FLASHLOAN_PREMIUM_TOTAL`](/aave-v3/smart-contracts/pool#flashloan-premium-total) is the total fee paid by borrowers, of which:

- Fee to LPs: [`FLASHLOAN_PREMIUM_TOTAL`](/aave-v3/smart-contracts/pool#flashloan-premium-total) - [`FLASHLOAN_PREMIUM_TO_PROTOCOL`](/aave-v3/smart-contracts/pool#flashloan-premium-to-protocol)
- Fee to Protocol: [`FLASHLOAN_PREMIUM_TO_PROTOCOL`](/aave-v3/smart-contracts/pool#flashloan-premium-to-protocol)

> [!NOTE]
> At initialization,
> [`FLASHLOAN_PREMIUM_TO_PROTOCOL`](/aave-v3/smart-contracts/pool#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`](https://github.com/aave-dao/aave-v3-origin/blob/main/src/contracts/misc/flashloan/interfaces/IFlashLoanSimpleReceiver.sol) or [`IFlashLoanReceiver`](https://github.com/aave-dao/aave-v3-origin/blob/main/src/contracts/misc/flashloan/interfaces/IFlashLoanReceiver.sol) interface by implementing the relevant `executeOperation()` function.

Because the owed amounts are _pulled_ from your contract, your contract must give allowance to the [`Pool`](/aave-v3/smart-contracts/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`](/aave-v3/smart-contracts/pool) calling the [`flashLoan()`](/aave-v3/smart-contracts/pool#flashloan) or [`flashLoanSimple()`](/aave-v3/smart-contracts/pool#flashloansimple) function. See the [`Pool`](/aave-v3/smart-contracts/pool) docs for parameter details, and use your contract address from [step 1](#section-1-set-up-the-receiver-contract) as the `receiverAddress`.

#### From a Different Contract

As with an EOA, use your contract address from [step 1](#section-1-set-up-the-receiver-contract) 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.

> [!WARNING]
> Never keep funds on your
> [`FlashLoanReceiverBase`](https://github.com/aave-dao/aave-v3-origin/blob/main/src/contracts/misc/flashloan/base/FlashLoanReceiverBase.sol)
> contract. An attacker can use them in a [griefing
> attack](https://ethereum.stackexchange.com/a/92457/19365).

### 3. Complete the Flash Loan

Once your logic in `executeOperation()` has run, you must pay back the flash loaned amounts if you used [`flashLoanSimple()`](/aave-v3/smart-contracts/pool#flashloansimple), or passed `0` in the `interestRateModes` of [`flashLoan()`](/aave-v3/smart-contracts/pool#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`](/aave-v3/smart-contracts/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.
