Comment on page
Quick tutorial to take up flash loans on top of Angle Protocol stablecoins
Angle Protocol has implemented a
FlashAnglecontract that allows to take flash loans on top of some of Angle Protocol stablecoins (like agEUR).
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.
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.
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
flashLoanimplementation in Angle: it allows borrowers to get liquidity of a single stablecoin. Different stablecoins may be supported by the same
For developers, the following needs to be considered when building your flash-loan solutions:
- 1.Your contract calls the
FlashAnglecontract requesting a certain
tokento be sent to a
receiveraddress of your choice.
- 2.After some elementary sanity checks, the
FlashAnglecontract transfers the tokens to the
receivercontract address and then calls the
onFlashLoanmethod of this contract.
- 3.Your contract now holding the flash-loaned
amountexecutes any arbitrary operation in its code. These operations can be specified directly in the call to the
flashLoanfunction through the
- When your code has finished, you need to approve the
FlashAnglecontract on the token for the flash-loaned amount plus the fee and then return
- 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.
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.
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
maxFlashLoanfunction for your token of interest in the
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.
Prior to making a flash-loan, you may want to check the fees induced and if the
amountyou want to take is inferior to the maximum amount allowed. These fees should change very rarely as only veANGLE can vote to change this. The functions to call are
To call the
flashLoanmethod on the
FlashAngle, you need to pass the relevant parameters. In all cases, you need to make sure that the
receiveraddress 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
- 2.From the same
receivercontract: If you want to use the same contract as in step 1, use
receiveraddress parameter in the flash-loan method.
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
FlashAnglecontract. 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
FlashAnglecontract, then the whole operation will fail and the transaction will revert meaning you would have paid gas for nothing.