Flash Loan
Last updated
Was this helpful?
Last updated
Was this helpful?
A flash loan is a mechanism that enables you to borrow an asset instantly and without collateral, under one critical condition: the borrowed amount (plus a small fee) must be repaid within the same transaction. If repayment does not occur, the entire transaction is reverted.
Borrowing: Your transaction requests a specified amount of a particular asset from the Aave Lending Pool. The pool checks if enough liquidity is available.
Execution: After receiving the funds, your transaction can perform operations such as arbitrage trades, collateral swaps, or debt refinancing, all within the same transaction.
Repayment: By the time your transaction is nearing completion, you must repay the borrowed principal plus a fee (referred to as the flash loan fee). If repayment fails or is insufficient, the entire transaction is canceled (reverted), as if it never happened.
Your contract that receives the flash loaned amounts must conform to the IFlashLoanSimpleReceiver or IFlashLoanReceiver interface by implementing the relevant executeOperation()
function.
Also note that since the owed amounts will be pulled from your contract, your contract must give allowance to the to pull those funds to pay back the flash loan amount + premiums.
To call either of the two flash loan methods on the Pool, we need to pass in the relevant parameters. There are 3 ways you can do this.
From an EOA ('normal' ethereum account)
To use an EOA, send a transaction to the relevant calling the or function. See the docs for parameter details, ensuring you use your contract address from for the receiverAddress
.
From a different contract
Similar to sending a transaction from an EOA as above, ensure the receiverAddress
is your contract address from .
From the same contract
If you want to use the same contract as in step 1, use address(this) for the receiverAddress
parameter in the flash loan method.
Once you have performed your logic with the flash loaned assets (in your executeOperation()
function), you will need to pay back the flash loaned amounts if you used or interestRateModes = 0 in for any of the assets in modes parameter.
Paying back a flash loaned asset
Ensure your contract has the relevant amount + premium to payback the borrowed asset. You can calculate this by taking the sum of the relevant entry in the amounts and premiums array passed into the executeOperation()
function.
You do not need to transfer the owed amount back to the . The funds will be automatically pulled at the conclusion of your operation.
Incurring a debt (i.e. not immediately paying back)
If you initially used a mode=1 or mode=2 for any of the assets in the modes parameter, then the address passed in for onBehalfOf
will incur the debt if the onBehalfOf
address has previously approved the msg.sender
to incur debts on their behalf.
This means that you can have some assets that are paid back immediately, while other assets incur a debt.