Flash Loans

Quick tutorial to take up flash loans on top of Angle Protocol stablecoins

Angle Protocol has implemented a FlashAngle contract that allows to take flash loans on top of some of Angle Protocol stablecoins (like EURA).

This guide has been built with inspiration from Aave guide on flash loans available here

What is a flash-loan?

Flash loans (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. As stated in Aave docs, there is no real world analogy to Flash Loans, so it requires some basic understanding of how state is managed within blocks in blockchains.

Flash loans are an advanced concept aimed at developers. You must have a good understanding of EVM, programming, and smart contracts to be able to use this feature.

What's similar/different with Angle?

The innovation with Angle is that stablecoins given out in flash loans are minted during the flash-loan transaction and burnt at the end of it: this means that the size of the flash loans taken is not capped by an amount of liquidity in a pool but rather by a parameter chosen by governance.

This is similar to what Maker is doing in its flash-mint module.

Like done elsewhere, flash-loan transactions are only valid when the amount borrowed by the address taking the flash-loan is returned plus a fee (governance could vote to set no fees) 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 Angle: it allows borrowers to get liquidity of a single stablecoin. Different stablecoins may be supported by the same FlashAngle contract.

For more details on how Angle contract works, you can look at this specific page which details Angle Flash-Loan implementation.

Execution Flow

For developers, the following needs to be considered when building your flash-loan solutions:

  1. Your contract calls the FlashAngle contract requesting a certain amount of token to be sent to a receiver address of your choice.

  2. After some elementary sanity checks, the FlashAngle contract transfers the tokens to the receiver contract address and then calls the onFlashLoan method of this contract.

  3. 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 FlashAngle 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

  4. All of the above happens in 1 transaction and hence in a single Ethereum (or the chain you're on) block.

Applications of flash loans

Flash loans of Angle Protocol stablecoins 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 AgTokens.

Usually, flash loans are used to facilitate liquidations. The way the Borrowing Module has been built though is that you do not need to bring an initial amount of liquidity to participate in liquidations and so you do not need flash loans for this.

Flash-loan fee and cap

Angle Protocol introduces for each stablecoin different parameters defining the fees that can be taken at each flash-loan and the maximum size allowed for a flash-loan. These parameters can be modified by governance votes.

If you are building flash loans on top of Angle, 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 FlashAngle contract.

Step by Step Guide

1. Setting up

The first thing needed to make a flash-loan is a receiver contract which must conform to the IERC3156FlashBorrower interface.

Also note that 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 flash loan amount + premiums.

2. Checking fees and maximum amount

Prior to making a flash-loan, 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.

3. Calling flashLoan()

To call the flashLoan method on the FlashAngle, 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 step 1.

  1. From an EOA ('normal' ethereum account) or from a different contract: To use an EOA or a different contract, send a transaction to FlashAngle calling the flashLoan function.

  2. From the same receiver contract: If you want to use the same contract as in step 1, use address(this) for the receiver address parameter in the flash-loan method.

4. 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 FlashAngle 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 FlashAngle contract, then the whole operation will fail and the transaction will revert meaning you would have paid gas for nothing.

Last updated