đ How Flash Loans Work: Atomic Transactions
Understand borrow-execute-repay in a single blockchain transaction
Borrow millions with no collateral in seconds
Your Progress
0 / 5 completedâī¸ How Flash Loans Work
Flash loans leverage blockchain's atomic transaction property: either all operations succeed, or everything reverts. This guarantees lenders get repaid with zero risk.
đŽ Interactive Transaction Simulator
Watch a flash loan transaction flow through all stages. Click "Run Simulation" to see the process.
Borrow: Request flash loan from protocol
Your smart contract calls the flash loan provider (Aave, dYdX, Balancer) and requests a specific amount. The protocol instantly transfers funds to your contract.
Smart Contract Execution Flow
Request Flash Loan
Your contract calls the provider's flashLoan() function with the amount and callback address. Provider transfers funds immediately.
Execute Callback
Provider calls your contract's executeOperation() function. Here you implement your custom logic: trades, liquidations, swaps, etc.
Approve Repayment
Your contract approves the provider to pull back the loan amount + fee. This happens automatically at the end of your callback function.
Validate Repayment
Provider checks if your contract has sufficient balance. If yes, transaction completes. If no, entire transaction reverts (no loan ever happened).
đ Atomicity: The Secret Sauce
Flash loans only work because of blockchain's atomic transaction property. In an atomic transaction, either ALL operations succeed or ALL operations fail. There's no middle ground.
â Success Path
- 1. Borrow 1M USDC â
- 2. Arbitrage nets 2K USDC â
- 3. Repay 1,000,900 USDC (0.09% fee) â
- 4. Keep 1,100 USDC profit â
- Result: Transaction confirmed
â Failure Path
- 1. Borrow 1M USDC â
- 2. Arbitrage fails (front-run) â
- 3. Can't repay 1,000,900 USDC â
- 4. Entire transaction reverts â
- Result: Nothing happened (like time travel)
đĻ Flash Loan Providers
- âĸAave: 0.09% fee, largest provider, supports most tokens
- âĸdYdX: 0.05% fee, specialized for margin trading
- âĸBalancer: 0.05% fee, flash loans from any pool
- âĸUniswap V3: 0.05-1% fee (per pool), concentrated liquidity
⥠Technical Requirements
- âĸSmart Contract: Must implement executeOperation() callback
- âĸGas Budget: Complex logic costs $100-500+ in gas
- âĸTiming: All steps must complete in ~15 seconds (block time)
- âĸTesting: Thoroughly test on testnet to avoid failed transactions