# Flash Loans

<figure><img src="/files/XdH6q2bMb9H8YCcmTBjC" alt=""><figcaption></figcaption></figure>

Flash Loans let you borrow an asset without posting collateral, provided the borrowed amount plus a fee is returned before the transaction ends. Because everything settles within a single transaction, you may see these referred to as "one-block borrows"\
\
There is no real-world equivalent for flash loans; they rely on how blockchain state is updated within a block. This is an advanced feature intended for developers who are comfortable with the EVM, Solidity, and smart contract patterns.

### Overview

Flash loans allow access to pool liquidity (only for reserves where borrowing is enabled) for the duration of one transaction, as long as either:

* the borrowed amount + fee is returned by the end, or
* a debt position is opened by the end (if permitted).

Tydro exposes two methods:

#### `flashLoan()`

* Borrow from multiple reserves in a single transaction.
* At completion you may \
  1\) repay amount + fee, or\
  2\) open a variable-rate debt position backed by posted collateral or via credit delegation.
* Note: the flash loan fee can be waived for approved `flashBorrowers` (managed by ACLManager).

#### `flashLoanSimple()`

* Borrow from a single reserve for the transaction.
* The fee is not waived and no debt position can be opened at the end.
* Optimized for gas when you only need a basic, single-asset flash loan.

### Execution Flow

Practical mental model for implementation:

1. Request: Your contract calls the Pool and requests a flash loan of specific amount(s) of one or more reserve(s) via `flashLoanSimple()` or `flashLoan()`.
2. Transfer + Callback: After checks, the Pool transfers the requested amounts to your contract and then calls your receiver’s `executeOperation()`.
3. Your Logic: With funds in hand, your contract runs arbitrary logic.
4. Settlement:
   * For `flashLoanSimple()`: before finishing, approve the Pool to pull back amount + fee.
   * For `flashLoan()`: for each reserve, depending on the `interestRateMode` you passed, either\
     a) approve the Pool for amount + fee, or\
     b) ensure sufficient collateral or credit delegation is in place to open a debt position.
5. Atomicity: If the owed amount is not available (insufficient balance, missing approval, or insufficient collateral for debt), the entire transaction reverts.
6. Block scope: Everything above happens within a single transaction (i.e., one block).

### Applications of Flash Loans

Tydro Flash Loans are already used with Tydro for liquidity switch feature. Other examples in the wild (and supported with Tydro) include:

* **Arbitrage**: Arbitrage between assets, without needing to have the principal amount to execute the arbitrage.
* **Liquidations:** Liquidating borrow positions, without having to repay the debt of the positions and using discounted collateral claimed to payoff flashLoan amount + fee.

### Flash loan fee

* The flash loan fee is initialized at **0.05%**. The current value can be queried through `FLASHLOAN_PREMIUM_TOTAL`.
* The total fee paid by borrowers can be shared between LPs and the protocol treasury:
  * Fee to LPs: `FLASHLOAN_PREMIUM_TOTAL - FLASHLOAN_PREMIUM_TO_PROTOCOL`
  * Fee to Protocol: `FLASHLOAN_PREMIUM_TO_PROTOCOL`
* At initialization, `FLASHLOAN_PREMIUM_TO_PROTOCOL` = 0.

### Step by step

#### 1. Setting Up

* Your receiver must implement [`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), including the `executeOperation()` function.
* Because the Pool pulls repayment, your contract must approve the Pool to spend the borrowed amount + premium.

#### 2. Calling flashLoan() or flashLoanSimple()

There are three ways to initiate:

* From an EOA: Send a transaction to the Pool calling `flashLoan()` or `flashLoanSimple()`. See the Pool docs for parameters, and use your receiver address from step 1 as `receiverAddress`.
* From a different contract: Same as above; ensure `receiverAddress` is your receiver from step 1.
* From the same contract: Call the method with `receiverAddress = address(this)`.

> **Security note:** Do **not** keep funds permanently on your receiver / [`FlashLoanReceiverBase`](https://github.com/aave-dao/aave-v3-origin/blob/main/src/contracts/misc/flashloan/interfaces/IFlashLoanReceiver.sol). Idle funds can be griefed (e.g., siphoned during forced interactions).

#### 3. Completing the flash loan

**Immediate repayment**

* If you used `flashLoanSimple()` or passed `interestRateMode = 0` for any asset in the `modes` parameter of `flashLoan()`, you must repay immediately.
* Ensure your contract holds amount + premium for each relevant asset. You can compute this from the `amounts`and `premiums` arrays received in `executeOperation()`.
* You do not manually transfer funds back; the Pool pulls repayment when your call returns.

**Incurring a debt**

* If you passed `mode = 1` or `mode = 2` for any asset in `modes`, the `onBehalfOf` address will incur the debt, provided it has previously approved the `msg.sender` to borrow on its behalf.
* You can mix behaviors: some assets repaid immediately, others converted into debt.

### Notes

* Tydro does **not offer a UI** for flash loans. This is a **developer-only feature** available via smart contracts.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.tydro.com/primitives/flash-loans.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
