# Flashloan Module Integration

The Flashloan module serves as the core minting and burning engine for eUSD.

This [repo](https://github.com/monet-protocol/monet-tokens/tree/main) contains the contract implementation.

In this guide, we explain how to interact with the Flashloan Module and fetch basical information from it. For more in depth information on how the Flashloan Module and its functions work, you can directly look into the code.

## What is a Flashloan?

Flashloans (also called one-block borrows) are special transactions that allow the borrowing of an asset, as long as the borrowed amount (and a fee) is returned before the end of the transaction. These transactions do not require a user to supply collateral prior to engaging in the transaction.

## What's similar/different with eUSD?

The innovation with eUSD is that stablecoins given out in flashloans are minted during the flashloan transaction and burnt at the end of it: this means that the size of the flashloans taken is not capped by an amount of liquidity in a pool but rather by a parameter chosen by contributors.

This is similar to what Sky is doing in its [flash-mint module](https://docs.makerdao.com/smart-contract-modules/flash-mint-module).

Like done elsewhere, flashloan transactions are only valid when the amount borrowed by the address taking the flashloan is returned plus a fee at the end of the transaction.

There could also be a cap on the size of the flash-loan taken.

There is only one simple `flashLoan` implementation in eUSD: it allows borrowers to get liquidity of a single stablecoin. Different stablecoins may be supported by the same `FlasheUSD` contract.

## Execution Flow

For developers, the following needs to be considered when building your flashloan solutions:

{% stepper %}
{% step %}

### Your contract requests tokens

Your contract calls the `FlasheUSD` contract requesting a certain `amount` of `token` to be sent to a `receiver` address of your choice.
{% endstep %}

{% step %}

### FlasheUSD transfers tokens and calls receiver

After some elementary sanity checks, the `FlasheUSD` contract transfers the tokens to the `receiver` contract address and then calls the `onFlashLoan` method of this contract.
{% endstep %}

{% step %}

### Receiver executes arbitrary logic and approves repayment

Your contract now holding the flash-loaned `amount` executes any arbitrary operation in its code. These operations can be specified directly in the call to the `flashLoan` function through the `data` parameter.

* When your code has finished, you need to approve the `FlasheUSD` contract on the token for the flash-loaned amount plus the fee and then return `keccak256("ERC3156FlashBorrower.onFlashLoan")`.
* If the amount due is not available (due to a lack of balance for instance), then the transaction is reverted.
  {% endstep %}

{% step %}

### All happens within one transaction / block

All of the above happens in 1 transaction and hence in a single block (on the chain you're on).
{% endstep %}
{% endstepper %}

## Applications of Flashloans

Flashloans of eUSD may serve different use cases like arbitrage between assets without needing the principal amount to execute the arbitrage. Overall, it improves the general market efficiency for eUSD.

## Flashloan Fee and Cap

eUSD introduces for each stablecoin different parameters defining the fees that can be taken at each flashloan and the maximum size allowed for a flashloan.

If you are building flashloans on top of eUSD, you may want to check these amounts prior to your call by calling the `flashFee` and `maxFlashLoan` function for your token of interest in the `FlasheUSD` contract.

## Step by Step Guide

{% stepper %}
{% step %}

### Setting up

The first thing needed to make a flash-loan is a `receiver` contract which must conform to the `IERC3156FlashBorrower` [interface](https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/interfaces/IERC3156FlashBorrower.sol).

{% hint style="info" %}
Since the owed amounts will be pulled from your contract, your contract must give allowance to the pool to pull those funds to pay back the flashloan amount + premiums.
{% endhint %}
{% endstep %}

{% step %}

### Checking fees and maximum amount

Prior to making a flashloan, you may want to check the fees induced and if the `amount` you want to take is inferior to the maximum amount allowed. The functions to call are `flashFee()` and `maxFlashLoan`.
{% endstep %}

{% step %}

### Calling `flashLoan()`

To call the `flashLoan` method on the `FlasheUSD`, you need to pass the relevant parameters. In all cases, you need to make sure that the `receiver` address passed respects all the criteria from the setup step.

* From an EOA ('normal' ethereum account) or from a different contract: To use an EOA or a different contract, send a transaction to `FlasheUSD` calling the `flashLoan` function.
* From the same `receiver` contract: If you want to use the same contract as in the setup step, use `address(this)` for the `receiver` address parameter in the flash-loan method.
  {% endstep %}

{% step %}

### Completing the flash-loan

Once you have performed your logic with the flash loaned assets (in your `onFlashLoan()` function), you will need to pay back the flash loaned amount of tokens plus the eventual fees associated to the operation.

You do not need to transfer the owed amount back to the `FlasheUSD` contract. The funds will be automatically pulled at the conclusion of your operation.

If your contract does not have the right amount on it, or if it has not approved the `FlasheUSD` contract, then the whole operation will fail and the transaction will revert meaning you would have paid gas for nothing.
{% endstep %}
{% endstepper %}


---

# 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.eusd.xyz/developers-hub/build-on-eusd/flashloan-module-integration.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.
