BLOCKCHAIN / INTERMEDIATE / +210 XP

Flash loans are architectural constraints

Borrow and repay in one transaction. The constraint shapes everything above it.

Borrow and repay in one transaction. The constraint shapes everything above it. Flash loans are not just a DeFi primitive — they are an architectural constraint that eliminates counterparty risk entirely. If you cannot repay, the transaction reverts. No debt. No liquidation. No bad debt.

The design insight is that within a single atomic transaction, capital has zero opportunity cost. You can borrow a billion dollars, use it for three milliseconds, and repay it — all for the cost of gas. This changes the economics of every financial operation.

THE DEEP DIVE

Flash Loan Mechanics

// Flash loan contract interface
interface IFlashLender {
  function flashLoan(
    address receiver,
    address token,
    uint256 amount,
    bytes calldata data
  ) external returns (bool);

  function onFlashLoan(
    address initiator,
    address token,
    uint256 amount,
    uint256 fee,
    bytes calldata data
  ) external returns (bytes32);
}

// Usage pattern:
// 1. Borrow via flash loan
// 2. Use the capital (arbitrage, liquidation, collateral swap)
// 3. Repay with fee
// 4. If step 3 fails, everything reverts

Architectural Patterns

Flash loans enable three architectural patterns: atomic arbitrage (find price discrepancy, execute, profit), liquidation (borrow, buy collateral, sell, repay), and collateral swap (unwind position, move collateral, rewind position). Each pattern follows the same structure: borrow, act, repay.

PRINCIPLES

  1. Flash loans are not free money. The fee is small, but it exists. Factor it into every calculation.
  2. The atomic constraint is a feature, not a limitation. It eliminates the need for trust.
  3. Flash loan recipients must implement onFlashLoan. The callback is the execution environment.
  4. The flash loan fee is paid in the borrowed token. Ensure you have enough to repay.
  5. Flash loans can be composed. Multiple flash loans in a single transaction enable complex multi-step strategies.

IN PRACTICE

Atomic Arbitrage

Borrow 1000 ETH via flash loan. Swap ETH for USDC on Uniswap at $1800. Swap USDC for ETH on SushiSwap at $1810. Repay 1000 ETH + 0.09% fee. Profit: ~9 ETH minus gas. All in one transaction.

Collateral Swap

Borrow USDC via flash loan. Withdraw ETH collateral from Aave. Deposit ETH collateral to Compound. Repay USDC flash loan. The user's DeFi position has moved from Aave to Compound in one transaction, with no capital at risk during the transition.

LIVE SIGNALS

These items surfaced from the intelligence pipeline at generation time.

ANTIPATTERNS

  • Using flash loans for trading without a guaranteed exit. If the price moves against you during the transaction, you lose.
  • Ignoring the flash loan fee. At scale, 0.09% on billions of dollars is significant.
  • Building flash loan protection into the frontend. A determined attacker will call the contract directly.
  • Assuming flash loans are the only way to get atomic capital. Atomic swap contracts provide similar guarantees.

CHECKLIST

  • Flash loan amount includes the fee
  • Execution logic handles the case where the profit is zero or negative
  • The contract implements the flash loan callback correctly
  • Gas estimation accounts for all operations in the atomic transaction
  • The strategy is profitable after gas and fees

YOUR MOVE

Write a smart contract that executes a flash loan arbitrage between two Uniswap pools. Deploy to a testnet. Verify that the transaction reverts when the spread is negative.