freecoding.school100% FREE · NO SIGNUP
Yield OceanISSUE #12 of 34

flash loans · atomic uncollateralized credit

Liquidity LuVSThe Sandwich Bot
Liquidity Lu saysFlash loans let you borrow ANY amount uncollateralized for ONE transaction — as long as you repay (plus fee) by the end, atomicity guarantees the loan was good.

A flash loan uses EVM atomicity: borrow any amount, do anything with it in the same transaction, and repay (plus a small fee, typically 0.09% on Aave) before the transaction ends. If you cannot repay, the entire transaction reverts — including the borrow — so the lender is always made whole.

The killer use case is atomic arbitrage between DEXes. The demo borrows 1M, runs a fictional 1% arbitrage, repays principal + fee, and pockets the profit.

Power-ups you unlock

The Sandwich Bot attacks — common mistakes

Boss battleBorrow 1,000,000 via a flash loan, run a 1% arbitrage, repay principal + 0.09% fee, and compute the profit.

Example code

<!doctype html><html><head><meta charset="utf-8"></head>
<body style="background:#06040d;color:#e6e0ff;font-family:monospace;padding:20px"><pre id="o"></pre>
<script>
const FEE_BPS = 9;        // 0.09% Aave
function flashLoan(amount, useFn){
  const fee = amount * FEE_BPS / 10000;
  const needRepay = amount + fee;
  const returned = useFn(amount);
  if(returned < needRepay) throw new Error('FLASH_LOAN_NOT_REPAID (need ' + needRepay + ', got ' + returned + ')');
  return { fee, profit: returned - needRepay };
}
function arbitrage(capital){
  const dexAPrice = 100, dexBPrice = 101;        // 1% gap
  return capital * (dexBPrice / dexAPrice);       // buy on A, sell on B
}
const r = flashLoan(1000000, arbitrage);
document.getElementById('o').textContent = [
  'borrow 1,000,000 → run arbitrage → repay 1,000,900',
  '',
  '  fee paid:   ' + r.fee.toLocaleString() + '   (0.09%)',
  '  profit:     ' + r.profit.toLocaleString() + '   (after fee)',
  '',
  'atomicity: if arbitrage returned < 1,000,900 the whole tx reverts → lender safe'
].join('\n');
</script></body></html>
▶ Open the interactive comic issue
‹ Interest Rate Models · Utilization CurvesMakerdao · Dai · The Peg Stability Module ›