What Is a Flash Loan and How Does It Work in DeFi?

What is a flash loan?
A flash loan is a temporary on-chain loan that has to be paid back in the same transaction, including any premium. On-chain means the whole action happens directly on the blockchain.
With a direct flash loan, a smart contract temporarily receives liquidity without the requester having to lock up collateral ahead of time for that flash loan. Liquidity here is the available amount of crypto or tokens a protocol can lend out. So this is not a normal loan that stays open for days, weeks, or months.
Flash loans are mainly a programmable building block within DeFi. DeFi stands for financial apps that run through smart contracts on a blockchain. Because of that, a user usually doesn’t run a flash loan manually like a regular loan. A pre-programmed smart contract requests the loan and then automatically runs all the needed steps.
On Aave V3, a so-called receiver contract gets the borrowed tokens. This is the smart contract that receives the flash loan and runs the planned actions. After that, the protocol calls a callback: a pre-defined moment when the receiver contract runs its own code. That code can, for example, swap tokens, adjust an existing position, and then pay back the loan plus the premium.
The requirement is strict: all steps must succeed within one transaction. That makes a flash loan mainly useful for fast, bundled DeFi actions, not for personal credit or long-term financing.
Key Takeaways
- A flash loan is a temporary on-chain loan that’s usually settled within one transaction.
- With a direct flash loan, no collateral is needed upfront for the temporary loan.
- Smart contracts automatically handle the loan, follow-up actions, and repayment.
- If repaying the principal and premium doesn’t work, the transaction is rolled back.
- Flash loans can be used for things like arbitrage, collateral swaps, and adjusting DeFi positions.
How does a flash loan work?
A flash loan works because a smart contract temporarily borrows tokens, runs pre-programmed actions, and repays the loan plus a premium before that same transaction ends.
The process usually looks like this:
- Start the flash loan: A smart contract calls a protocol’s flash loan function. The protocol sends the chosen crypto to the receiver contract.
- Run the planned action: The receiver contract uses the temporary liquidity for the coded steps. This can be swaps, for example, or paying off and rebuilding a DeFi position.
- Make sure enough balance is available: After those actions, the receiver contract needs enough tokens to cover the principal and the flash loan premium. That premium is a fee for making liquidity available temporarily.
- Enable repayment: The receiver contract gives the pool permission to pull the amount owed. This is called a token approval: the contract allows the protocol to debit a certain number of tokens.
- Finish the transaction: The protocol pulls the principal and premium. Only then is the whole transaction finalized.
Aave V3 has a flashLoanSimple function for one reserve. A reserve is the pool with available liquidity for a specific crypto asset. The more advanced flashLoan function can support multiple assets at once.
With that more advanced function, there is also another route under certain conditions. If that route is set and there’s enough collateral, the borrowed amount can be opened as a regular debt position instead of being repaid right away. That’s different from a standard direct flash loan. This option does not exist with flashLoanSimple.
Why does a flash loan have to be repaid within one transaction?
A direct flash loan has to be repaid within one transaction because that lets everything be executed atomically. Atomic means the whole sequence of actions either fully succeeds, or it fully gets rolled back.
Think of a transaction as one sealed package with multiple instructions inside. In that package, a contract can borrow tokens, swap those tokens, pay off a debt, and repay the loan. Only when every instruction is executed correctly does the package get processed on the blockchain.
This protects the liquidity pool from an open, unsecured loan. At the end of the transaction, the pool pulls the borrowed amount plus the premium. If that fails, the transaction is rejected.
People often explain flash loans as happening within one block, which can be practical, but it’s less precise. The key safety rule is that the whole operation is settled atomically within a single transaction.
What happens if repayment doesn’t happen?
If a direct flash loan can’t be fully repaid, the transaction reverts. Reverting means the blockchain rolls back the changes from that failed transaction.
The protocol is not left with an unpaid flash loan. The temporarily borrowed tokens don’t stick around as open debt on the receiver contract. Swaps, deposits, and other changes that were part of that same failed transaction are also rolled back.
The sender can still lose gas. Gas is the fee for the compute work needed to run a transaction on a blockchain. Some of that work may already have been done before the transaction reverts, so gas fees can still be owed.
Only with Aave V3’s more advanced flashLoan function can a chosen route, under certain conditions, lead to a regular debt position. For that, there has to be enough collateral and the protocol’s checks have to allow it. This does not apply to flashLoanSimple.
What are flash loans used for?
Flash loans are used to combine multiple dependent DeFi actions into one atomic transaction. This can be helpful when one step is only possible after another step is done immediately before or after it.
Common use cases include:
- arbitrage between markets;
- swapping collateral inside a borrowing position;
- paying off debt with collateral;
- closing or restructuring a DeFi position.
A flash swap follows a similar idea. In a flash swap, someone can take tokens out of a pool and only pay or send tokens back at the end of that same transaction. A flash swap and a flash loan are not technically the same thing, but both use temporary liquidity within one atomic execution.
How do arbitrage and liquidity gaps work with flash loans?
Arbitrage tries to take advantage of a price difference for the same or economically similar assets across different markets. A flash loan can temporarily provide trading capital for this, so the executor doesn’t need to already own the full amount upfront.
A simple arbitrage strategy might look like this:
- A smart contract temporarily borrows tokens via a flash loan.
- The contract sells or swaps those tokens on a market where the price is relatively high.
- It buys the same asset back on another market where the price is lower.
- The contract repays the flash loan and premium.
- Any leftover amount is the result.
Example: Say a token can be sold at a higher executable price on one pool than the price you can buy it back for on another pool. A smart contract can temporarily borrow tokens, run the two swaps, and repay the loan. If there’s anything left over after all costs, the transaction was profitable.
A visible price difference isn’t automatically a profit opportunity. The real return has to be greater than the principal, flash loan premium, swap fees, routing costs, gas, and slippage.
Price impact also matters. Price impact is the price change your own trade causes in a liquidity pool. A big swap in a pool with low liquidity can move the price so much that the original price gap disappears.
How can flash loans be used for collateral swaps?
A collateral swap replaces the collateral of a borrowing position in one transaction. Collateral is crypto a user deposits into a lending protocol as security for a loan.
This kind of swap can help when a user wants to replace the collateral in an existing borrowing position without first having enough of their own funds available to pay off the debt. A flash loan can temporarily provide the liquidity needed to do all the in-between steps within one transaction. The exact order depends on the protocol and the smart contract logic being used.
One possible collateral swap using a flash loan looks like this:
- Temporarily borrow the debt asset: The smart contract borrows, via a flash loan, the asset the existing debt is denominated in.
- Pay off the existing debt: The temporarily borrowed tokens are used to repay the debt, which frees up the original collateral.
- Withdraw the original collateral: The smart contract withdraws the freed collateral from the borrowing position.
- Swap the collateral: The original collateral is swapped into the desired new collateral asset.
- Deposit the new collateral: The new asset is deposited as collateral into the lending protocol.
- Open the debt again: Against the new collateral, the debt asset is borrowed again.
- Repay the flash loan: Using those tokens, the contract repays the temporary loan plus any premium.
At the end of the transaction, the user has a borrowing position again, but with different collateral. If a required step fails or the flash loan can’t be fully repaid, the entire transaction reverts.
A related but different flow is paying off debt with collateral. In that case, the contract temporarily borrows the debt asset, repays the existing debt, withdraws the freed collateral, and swaps part of it to repay the flash loan. The goal here isn’t to reopen the same debt position with different collateral, but to fully or partially pay down the existing debt.
Whether such a flow is doable depends on things like supported assets, available liquidity, the LTV, and liquidation parameters. LTV shows what portion of the collateral’s value can be borrowed at most. Liquidation parameters determine when a position is undercollateralized and can be closed out.
Are flash loans still profitable?
Flash loans aren’t automatically profitable, because they only provide temporary financing for a specific strategy. Whether a transaction makes money depends on the final outcome after all costs and risks.
With arbitrage, a strategy only makes sense if the realized return is higher than:
- the flash loan premium;
- gas fees;
- swap and routing costs;
- price impact;
- slippage.
The flash loan premium isn’t a fixed universal percentage. It can differ by protocol, chain, deployment, and configuration, and it can be changed. Gas is also variable. Costs depend on things like the amount of compute work, the base fee, and the priority fee that come with an Ethereum transaction.
Competition also makes small opportunities fragile. Automated searchers can quickly try to exploit the same price differences. On top of that, a simulation or quote is not a guarantee that the real execution will keep the same price and return.
So flash loans aren’t a method for a fixed return. They’re a technical tool that only has value when a specific transaction still works out favorably after all costs.
What are the advantages of flash loans?
Flash loans give smart contracts temporary access to liquidity without the initiator having to lock up the full principal upfront as collateral for that temporary loan.
The main advantages are:
- Temporary access to liquidity: A strategy can be run with a larger amount than the executor has available ahead of time for that specific loan.
- No partial execution: Because of atomicity, all state changes are rolled back if a required step fails. That way, a user isn’t left with a half-changed position.
- Multiple actions in one transaction: Borrowing, swapping, repaying, withdrawing, and depositing again can be done as one package.
- More efficient position management: Collateral swaps and other adjustments to DeFi positions can be combined without needing to finance each intermediate step separately.
No starting capital doesn’t mean no costs. For a flash loan transaction, you usually still need native tokens for gas. You might also need a balance to cover a premium or a bad execution.
What risks and limitations do flash loans have?
Flash loans come with technical, financial, and execution risks, even though the direct loan is rolled back if repayment fails. The atomic setup mainly protects the liquidity provider from an unpaid direct loan. It doesn’t remove all risks for the user or developer.
A flash loan transaction is complex. The receiver, token approvals, swaps, price checks, and repayment all have to work together without mistakes. An error in one part can make the whole transaction revert and cost gas.
Borrowing capacity is also limited. A protocol can only lend what’s available in the relevant reserve or pool. So the size you want isn’t always available.
Also, a transaction can succeed technically and still be a bad deal financially. For example, a swap can go through at a terrible price if the allowed slippage is set too wide.
How can smart contract bugs be exploited?
A flash loan can exploit an existing bug in another protocol at scale because an attacker can temporarily control a lot of tokens. The flash loan doesn’t cause the bug, but it can provide the capital needed to take advantage of the weakness within one transaction.
One risky design, for example, is a protocol that trusts an easily manipulated spot price. A spot price is the price shown in a pool at that exact moment. An attacker can temporarily influence the price with a large trade, perform a dependent action, and then reverse the trade within the same atomic transaction.
Temporary token ownership can also be dangerous when a protocol uses it as a security or governance signal without extra protection. Governance is the process where participants decide on rules or changes to a protocol.
The protection mainly comes down to solid protocol design. Examples include stronger price oracles, time-weighted price data where appropriate, checks on economic limits, and defensive development and smart contract audits.
What role do transaction costs and slippage play?
Transaction costs and slippage often determine whether a flash loan strategy is actually profitable. Even when the main action works, these costs can wipe out an expected return completely.
Gas fees are the costs for the compute work performed on Ethereum. Complex transactions with multiple smart contracts, swaps, and token approvals usually use more gas than a simple transfer. A transaction that reverts during execution can still cost gas.
Price impact and slippage are not the same:
- Price impact is the price change caused by your own trade. This effect is usually bigger when a trade is large compared to the liquidity in a pool.
- Slippage is the difference between the expected swap output and what you actually receive. The price can change between when a transaction is prepared and when it’s executed.
A slippage limit that’s too low can cause a transaction to revert. A limit that’s too high can allow the swap to go through at a much worse price. That’s why gas, premium, price impact, and slippage all need to be subtracted from the gross return before a strategy can be considered profitable.
What’s the difference between flash loans and crypto borrowing?
Flash loans and crypto borrowing are both ways to borrow crypto, but they work in a fundamentally different way. With crypto borrowing, a user usually opens a borrowing position that can stay open for a longer time. A flash loan, on the other hand, is meant for temporarily using liquidity within one blockchain transaction.
With crypto borrowing, a user typically deposits crypto as collateral in order to borrow another crypto asset. The debt then stays open until it’s repaid. Interest can build up on the loan during that period. If the collateral value drops too much relative to the debt, the position can be liquidated.
With a direct flash loan, you don’t have to lock collateral ahead of time for the temporary loan. But the borrowed amount, along with any premium, has to be repaid within that same transaction. If it doesn’t happen, the entire transaction reverts and the changes are rolled back.
The differences are pretty clear:
- Term: Crypto borrowing can create a debt position that stays open for days, months, or longer. A direct flash loan only exists within one transaction.
- Collateral: With crypto borrowing, collateral is usually required. With a direct flash loan, no collateral is needed upfront for the temporary loan.
- Liquidation risk: With crypto borrowing, a position can be liquidated when the collateral doesn’t provide enough coverage. A direct flash loan doesn’t have a long-lived position that can be liquidated: if repayment fails, the transaction reverts.
- Costs: With crypto borrowing, interest can accrue as long as the debt is open. With a flash loan, you typically pay a premium for temporarily using liquidity.
- Use: Crypto borrowing is used to borrow capital without immediately selling the collateral. Flash loans are mainly used for automated DeFi actions like arbitrage, collateral swaps, and restructuring borrowing positions. In short, crypto borrowing is meant for holding a loan over a longer period, while a flash loan provides temporary liquidity for a series of actions executed within one transaction.
Aave V3 adds a nuance here. Using the more advanced flashLoan function, a transaction can, under certain conditions, end in a regular debt position when enough collateral is available. At that point, it’s no longer a standard direct flash loan, but effectively crypto borrowing.
Final thoughts
A flash loan is a DeFi building block that lets a smart contract temporarily use liquidity and pay it back within the same transaction. The power is in atomicity: all steps succeed together, or the blockchain rolls back the entire transaction.
Because of that, flash loans can be useful for bundled actions like arbitrage, collateral swaps, and adjusting borrowing positions. They don’t give automatic returns and they’re not a normal loan for long-term use. The final outcome depends on code, available liquidity, gas fees, price impact, slippage, and the strategy you choose.
Especially for developers and automated DeFi strategies, flash loans are a powerful tool. At the same time, using them safely requires reliable smart contracts, solid price checks, and a realistic calculation of all costs and risks.