---
title: "DeFi Credit and Leverage"
book: "Markets III: Commodities, Energy and Crypto"
subject: quant
language: en
chapter: 23
exercises: 8
source: https://one-course.com/books/quant/3/en/chapter/23-defi-credit-and-leverage
---

# Chapter 23 — DeFi Credit and Leverage

On 11 October 2022 a trader bought a thin [token](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-key) on the few exchanges whose prices fed a lending venue’s [oracle](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-contract). Within half an hour the price the [oracle](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-contract) reported rose more than thirteenfold. The trader’s position in the venue, now marked at that price, was worth enough to serve as collateral for borrowing everything the venue held: he borrowed and withdrew more than USD 110 million of other [tokens](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-key) and left the inflated collateral behind. Lending on a [blockchain](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-chain) works without a credit officer: a program lends against collateral at a loan-to-value it knows, liquidates anyone whose collateral falls below a threshold, and prices everything with an [oracle](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-contract). This chapter covers the pools that do the lending, the liquidations that keep them solvent, the [flash loans](#def-m3-defi-credit-and-leverage-flash) that exist nowhere else, the [staking yield](#def-m3-defi-credit-and-leverage-staking) on which much of the collateral earns a return, and the two ways the system has broken: [stablecoins](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-stable) whose peg was an algorithm, and [oracles](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-contract) that could be bought.

## 23.1 Lending pools

**Definition 23.1 (Lending pool, kinked interest-rate curve).**

A *lending pool* is a [smart contract](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-contract) that takes deposits of a [token](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-key) from suppliers and lends them to borrowers who post other [tokens](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-key) as collateral, paying suppliers the interest borrowers pay less a share kept by the protocol. A *kinked interest-rate curve* sets the borrow rate as a function of the pool’s utilisation, rising gently up to a target utilisation and steeply beyond it.

Utilisation is the share of supplied [tokens](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-key) that is lent out, as in the securities lending of One Quant Book 1, chapter 16. The pool has no maturity: suppliers can withdraw at any time, but only from what is not lent. The kink is the pool’s defence of that liquidity. Above it, borrowing becomes so expensive that borrowers repay and suppliers arrive, pulling utilisation back and leaving [tokens](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-key) for those who want to withdraw.

**Proposition 23.2 (Supply rate).**

With utilisation $u$, borrow rate $r_b(u)$ and a reserve factor $f_{\mathrm{res}}$ kept by the protocol, suppliers earn $r_s = r_b(u)\,u\,(1 - f_{\mathrm{res}})$.

**Proof.** Borrowers pay $r_b$ on the lent amount, a fraction $u$ of the supply; the protocol keeps $f_{\mathrm{res}}$ of it. ∎

![A kinked interest-rate curve: 4% at the 90% optimal utilisation, rising by 60 points more to full utilisation, and the supply rate it implies with a 10% reserve factor (). Illustrative parameters of the two-slope form. Data: the chapter’s tutorial.](https://one-course.com/images/onecourse/chapters/quant-3/m3-defi-credit-and-leverage/fig-25b949fc607a.svg)

***Figure 23.1.** A [kinked interest-rate curve](#def-m3-defi-credit-and-leverage-pool): 4% at the 90% optimal utilisation, rising by 60 points more to full utilisation, and the supply rate it implies with a 10% reserve factor ([Proposition 23.2](#prop-m3-defi-credit-and-leverage-supply)). Illustrative parameters of the two-slope form. Data: the chapter’s tutorial.*

**As of September 2026 — A lending protocol’s rate model and flash-loan fee.**

Aave’s documentation describes its v3 interest-rate strategy as based on two slopes, one below an optimal usage ratio and another from that point to 100%, with a base variable borrow rate, per reserve. Positions are over-collateralised, risk is tracked by a [health factor](#def-m3-defi-credit-and-leverage-ltv) with per-reserve liquidation thresholds, and a position with a [health factor](#def-m3-defi-credit-and-leverage-ltv) below 1 can be liquidated by anyone, who repays part of the debt and receives collateral at a discount, the [liquidation bonus](#def-m3-defi-credit-and-leverage-ltv). In the v3 contracts a liquidation may repay at most 50% of the debt (the close factor), and all of it when the [health factor](#def-m3-defi-credit-and-leverage-ltv) is at or below 0.95 or the position is below USD 2 000. The flash-loan fee was set at 0.05% at deployment and can be changed by governance.

## 23.2 Liquidations

**Definition 23.3 (Loan-to-value ratio, health factor, liquidation bonus).**

The *loan-to-value ratio* of a collateral asset is the maximum a borrower may borrow per dollar of it. The *health factor* of an account is the sum over its collateral of value times liquidation threshold, divided by the value of its debt; below one, the account may be liquidated. The *liquidation bonus* is the discount at which a liquidator receives collateral for the debt it repays.

The [loan-to-value ratio](#def-m3-defi-credit-and-leverage-ltv) is a haircut (One Quant Book 1, chapter 6) applied when borrowing; the liquidation threshold, a little higher, is the haircut at which the position is taken. There is no margin call: the borrower is not asked for more collateral, it is liquidated by whoever acts first, and the bonus pays them to act. A borrower with 100 ether of collateral at a threshold of 82.5% and USD 240 000 of debt has a [health factor](#def-m3-defi-credit-and-leverage-ltv) of 1.03 at 3 000 dollars and is liquidated below 2 909.

**Proposition 23.4 (Liquidation arithmetic).**

A liquidator repays an amount $R$ of debt, at most the close factor $c$ times the debt, and receives collateral worth $R(1 + \beta)$ at the [oracle](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-contract) price, with $\beta$ the bonus. The borrower’s [health factor](#def-m3-defi-credit-and-leverage-ltv) after a liquidation of $R$ is $(C_\theta - R(1 + \beta)\theta)/(D - R)$, where $C_\theta$ is the threshold-weighted collateral value, $D$ the debt value and $\theta$ the collateral’s threshold; it rises only if $(1 + \beta)\theta < C_\theta/D$, which holds when the [health factor](#def-m3-defi-credit-and-leverage-ltv) was above $(1 + \beta)\theta$.

**Proof.** The debt falls by $R$ and the threshold-weighted collateral by $R(1 + \beta)\theta$. The ratio $(C_\theta - xR)/(D - R)$ with $x = (1 +
\beta)\theta$ exceeds $C_\theta/D$ exactly when $C_\theta/D > x$. ∎

![Health factor of a borrower with 100 ether of collateral (threshold 82.5%) and USD 240 000 of debt as the price of ether falls. Data: the chapter’s tutorial.](https://one-course.com/images/onecourse/chapters/quant-3/m3-defi-credit-and-leverage/fig-ca3b535b1500.svg)

***Figure 23.2.** [Health factor](#def-m3-defi-credit-and-leverage-ltv) of a borrower with 100 ether of collateral (threshold 82.5%) and USD 240 000 of debt as the price of ether falls. Data: the chapter’s tutorial.*

The proposition has a sting. A position that falls far below one before anyone acts, because the price gapped or the chain was congested, can be pushed deeper by its own liquidation: each dollar repaid removes more than a dollar of threshold-weighted collateral. When the collateral left is worth less than the debt, the pool is left with bad debt that its suppliers bear.

## 23.3 Flash loans

**Definition 23.5 (Flash loan).**

A *flash loan* is a loan without collateral that must be repaid, with a fee, within the same transaction; if it is not, the whole transaction reverts and the loan never happened.

A [flash loan](#def-m3-defi-credit-and-leverage-flash) has no credit risk for the lender because the chain’s atomicity enforces repayment: the transaction either ends with the pool repaid or does not exist. It lets anyone act with a pool’s capital for one transaction. The canonical use is a liquidation by a liquidator with no capital: borrow the [stablecoins](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-stable) to repay, receive the collateral with its bonus, sell it on an [automated market maker](https://one-course.com/books/quant/3/en/chapter/20-automated-market-makers#def-m3-automated-market-makers-amm), repay the [flash loan](#def-m3-defi-credit-and-leverage-flash), keep the rest. In the tutorial, liquidating half of the borrower’s debt at an ether price of 2 800 seizes 45 ether, sells them for USD 124 505 in a pool of 5 000 ether, repays the USD 120 000 borrowed and USD 60 of fee, and leaves USD 4 445. The same atomicity serves attacks: a [flash loan](#def-m3-defi-credit-and-leverage-flash) can move a price, use the moved price, and restore it, all in one transaction. Qin, Zhou, Livshits and Gervais analysed two such attacks, both in February 2020, and measured returns on investment above 500 000%.

![A liquidation funded by a flash loan, with the tutorial’s numbers. Schematic.](https://one-course.com/images/onecourse/chapters/quant-3/m3-defi-credit-and-leverage/fig-068180942852.svg)

***Figure 23.3.** A liquidation funded by a [flash loan](#def-m3-defi-credit-and-leverage-flash), with the tutorial’s numbers. Schematic.*

## 23.4 Liquid staking, restaking and the staking yield

**Definition 23.6 (Staking yield, liquid staking token, restaking).**

The *staking yield* is the return earned by locking a proof-of-stake chain’s [token](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-key) with a [validator](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-pow) ([Chapter 14](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#ch-m3-blockchains-for-traders)): new issuance and a share of fees, less penalties. A *liquid staking token* is a transferable [token](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-key) that represents staked [tokens](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-key) and their accruing rewards, issued by a protocol that stakes deposits on its users’ behalf. *Restaking* is committing already-staked [tokens](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-key), or liquid staking [tokens](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-key), as security for further services, for additional rewards and additional risk of their being destroyed for misbehaviour.

**As of September 2026 — A staking yield.**

Lido’s public API reported a seven-day moving average annual percentage rate of 2.24% for its staked-ether [token](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-key), stETH, on 24 September 2026, and 9.76 million ether staked through it: about 22% of the 43.8 million ether held by [validators](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-pow) on the beacon chain that day.

The [staking yield](#def-m3-defi-credit-and-leverage-staking) is crypto’s nearest thing to a risk-free rate for ether, and the [liquid staking token](#def-m3-defi-credit-and-leverage-staking) is the collateral on which much of the leverage is built: deposit stETH, borrow ether, stake it, repeat, earning the [staking yield](#def-m3-defi-credit-and-leverage-staking) on several times the capital as long as the borrow rate stays below it. The loop’s risks are the ones of any carry trade funded short: the borrow rate can rise above the yield when utilisation climbs, and the [liquid staking token](#def-m3-defi-credit-and-leverage-staking) can trade below the value of the ether behind it when holders want to leave faster than withdrawals from staking allow, triggering liquidations of the loopers. [Restaking](#def-m3-defi-credit-and-leverage-staking) layers further claims on the same collateral: EigenLayer’s contracts accept [liquid staking tokens](#def-m3-defi-credit-and-leverage-staking), beacon-chain ether and other [tokens](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-key), and let each operator allocate a share of its delegated stake to be slashable by a given service.

## 23.5 Algorithmic stablecoins and oracle manipulation

**Definition 23.7 (Algorithmic stablecoin).**

An *algorithmic stablecoin* is a [token](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-key) that aims at a fixed value without holding reserves of that value, relying instead on a mechanism that exchanges it for another [token](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-key) of floating value created or destroyed to absorb demand.

The mechanism holds the peg only while the floating [token](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-key) is worth more than the [stablecoins](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-stable) outstanding; when confidence falls, redemptions create floating [tokens](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-key) faster than anyone wants them, their price falls, and each redemption creates more of them. According to the SEC, Terraform Labs marketed its [stablecoin](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-stable) UST, which was to hold its dollar peg by being exchangeable for its [token](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-key) LUNA, as “yield-bearing”, paying as much as 20% through its Anchor protocol; in May 2022 UST lost its peg and it and its sister [tokens](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-key) fell close to zero.

**Definition 23.8 (Oracle manipulation).**

*Oracle manipulation* is moving the price that an [oracle](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-contract) reports to a [smart contract](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-contract), usually by trading in the thin markets it reads, in order to borrow against inflated collateral, avoid a liquidation or trigger one.

**As of September 2026 — The Mango Markets case.**

The CFTC alleged in January 2023 that on 11 October 2022 Avraham Eisenberg, through two accounts on Mango Markets, built large leveraged positions in a swap on the MNGO [token](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-key), pumped MNGO’s price on three exchanges feeding the venue’s [oracle](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-contract), which reported a more than thirteenfold rise in 30 minutes, and used the inflated positions as collateral to withdraw over USD 110 million, of which about USD 67 million was later returned. A jury convicted him of commodities fraud, commodities manipulation and wire fraud; on 23 May 2025 the trial judge granted his motion under Rule 29, vacating the first two counts and entering a judgment of acquittal on the third.

**Proposition 23.9 (The pump that borrows a pool dry).**

A pool lends up to $L$ against collateral of $q$ [tokens](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-key) at price $P$ and loan-to-value $\lambda$, with the price read from a constant-product market holding $y$ dollars. Pushing that market’s price up by a factor $k$ costs $y(\sqrt{k} - 1)$ dollars and buys a fraction $1 - 1/\sqrt{k}$ of its [tokens](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-key). The attacker can borrow the whole pool once $k \ge k^* = L/(\lambda qP)$; its gain from borrowing and abandoning the collateral is $\min(L, \lambda qPk) - qP - y(\sqrt{k} - 1)$ plus whatever the [tokens](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-key) bought are worth afterwards.

**Proof.** Keeping $xy$ constant while the price $y/x$ rises by $k$ multiplies $y$ by $\sqrt k$ and divides $x$ by $\sqrt k$. Borrowing power is $\lambda qPk$, capped by the pool; the collateral is left behind. ∎

![The oracle pump of : 5 million tokens at USD 1 as collateral (loan-to-value 60%), a pool of USD 50 million, and a source market holding USD 2 million; tokens bought in the pump are assumed worthless afterwards. The gain turns positive at a pump of less than two times, peaks when the pool is empty, and then falls as further pumping only costs. Illustrative parameters. Data: the chapter’s tutorial.](https://one-course.com/images/onecourse/chapters/quant-3/m3-defi-credit-and-leverage/fig-017c365d0e15.svg)

***Figure 23.4.** The [oracle](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-contract) pump of [Proposition 23.9](#prop-m3-defi-credit-and-leverage-pump): 5 million [tokens](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-key) at USD 1 as collateral (loan-to-value 60%), a pool of USD 50 million, and a source market holding USD 2 million; [tokens](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-key) bought in the pump are assumed worthless afterwards. The gain turns positive at a pump of less than two times, peaks when the pool is empty, and then falls as further pumping only costs. Illustrative parameters. Data: the chapter’s tutorial.*

The defences follow from the formula: [loan-to-value ratios](#def-m3-defi-credit-and-leverage-ltv) that fall with the thinness of the collateral’s markets, caps on how much can be borrowed against any one collateral, [oracles](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-contract) that read deep markets and smooth over time, and circuit breakers on sudden price moves. None of them is free: each makes the pool less useful to honest borrowers.

## 23.6 Tutorial: a lending pool under stress

**Goal.** Simulate a [lending pool](#def-m3-defi-credit-and-leverage-pool): the borrow rate as a kinked function of utilisation, a borrower’s [health factor](#def-m3-defi-credit-and-leverage-ltv) through a price fall, a liquidation with close factor and bonus, and a liquidation funded by a [flash loan](#def-m3-defi-credit-and-leverage-flash). **End state:** the chapter’s figures and the numbers of the text.

1. **Liquidation and the [flash loan](#def-m3-defi-credit-and-leverage-flash).** The engine’s two most delicate functions. `def liquidate (self , acct: Account, debt_asset: str , coll_asset: str , repay: float ) -> float : """Repay up to the close factor of the debt of an account whose health is below one; returns the collateral seized, worth the repayment plus the bonus.""" if self .health(acct) >= 1 : raise ValueError(" account is healthy " ) repay = min (repay, self .close_factor * self .owed(acct, debt_asset)) seize = repay * self .prices[debt_asset] / self .prices[coll_asset] * (1 + self .bonus[coll_asset]) seize = min (seize, acct.collateral[coll_asset]) r = self .reserves[debt_asset] acct.debt[debt_asset] -= repay / r.borrow_index r.borrowed -= repay / r.borrow_index acct.collateral[coll_asset] -= seize return seize def flash_loan (self , asset: str , amount: float , callback) -> float : """Lend `amount`; `callback(amount)` must return at least amount x (1 + fee), else the loan reverts (raises) and nothing happened. Returns the fee earned by the reserve's suppliers.""" r = self .reserves[asset] if amount > r.available(): raise ValueError(" insufficient liquidity " ) repaid = callback(amount) due = amount * (1 + self .flash_fee) if repaid < due: raise ValueError(" flash loan not repaid: transaction reverts " ) fee = repaid - amount r.supplied += fee / r.supply_index return fee` **Listing 23.1.** Liquidation with close factor and bonus, and a flash loan that reverts unless repaid. code/firm/lendpool/firm_lendpool.py
2. **The flash liquidation.** Borrow, liquidate, sell the collateral in a pool with `firm.amm`, repay. `def flash_liquidation (eth_price: float = 2_800.0 , pool_eth: int = 5_000 , fee_bps: int = 30 ) -> dict : """A liquidator with no capital: flash-borrow the repayment, liquidate half the debt, sell the seized ETH in a constant-product pool at the market price, repay the flash loan with its fee, keep the rest.""" p = make_pool() acct = Account({" ETH " : 100.0 }) p.borrow(acct, " USDC " , 240_000.0 ) p.prices[" ETH " ] = eth_price result = {} def callback (amount: float ) -> float : seized = p.liquidate(acct, " USDC " , " ETH " , amount) e18, e6 = 10 **18 , 10 **6 usdc = cp_amount_out(int (seized * e18), pool_eth * e18, int (pool_eth * eth_price * e6), fee_bps) / e6 result.update(seized=seized, usdc=usdc) return min (usdc, amount * (1 + p.flash_fee)) repay = 0.5 * p.owed(acct, " USDC " ) fee = p.flash_loan(" USDC " , repay, callback) result.update(repay=repay, fee=fee, profit=result[" usdc " ] - repay - fee, health_after=p.health(acct)) return result` **Listing 23.2.** A liquidation funded by a flash loan. code/markets-3/23-defi-credit-and-leverage/python/m3_defi.py
3. **Run** `flash_liquidation()` , `pump_attack` over pumps from 1 to 40, and `fig_defi.py` .

**What to change next.** Let the price gap to 2 400 before the liquidation and find the pool’s bad debt; add a second liquidator competing in the same block; make the [oracle](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-contract) a 30-minute average and recompute the pump’s cost.

## 23.7 Build: the lending pool engine

**Purpose.** The miniature firm lends, borrows and liquidates on chain: it must know its [health factors](#def-m3-defi-credit-and-leverage-ltv), the rates it will pay as utilisation moves, the profit of a liquidation after the sale of the collateral, and the exposure of a pool it supplies to bad debt.

**Interface.** `RateModel(base, slope1, slope2, optimal)`; `Reserve` with `utilisation`, `rates`, `accrue`, `available`; `Pool` with `supply`, `borrow`, `health`, `liquidate`, `flash_loan`. Collateral sales through `firm.amm`.

**Rules.** Debt and deposits held as scaled balances against indices; borrowing refused above the loan-to-value or the available liquidity; liquidation only below a [health factor](#def-m3-defi-credit-and-leverage-ltv) of one and at most the close factor; a [flash loan](#def-m3-defi-credit-and-leverage-flash) that is not repaid raises and leaves no trace.

**Acceptance tests.** `code/firm/lendpool/tests/`: the kink; [health factor](#def-m3-defi-credit-and-leverage-ltv) and accrual; refusal above the loan-to-value; liquidation of a healthy account refused; seizure with the bonus; a [flash loan](#def-m3-defi-credit-and-leverage-flash)’s fee and its revert.

**Stretch.** Several collateral assets with different thresholds; bad-debt accounting; isolation modes and borrow caps; the rate’s reaction to a run on deposits.

Sources and further reading

- Aave documentation: v3 overview, interest-rate strategy, flash loans; the contract LiquidationLogic in the aave-v3-origin repository. Accessed September 2026.
- EigenLayer core contracts documentation (Layr-Labs/eigenlayer-contracts, docs/README), September 2026.
- K. Qin, L. Zhou, B. Livshits and A. Gervais, “Attacking the DeFi Ecosystem with Flash Loans for Fun and Profit”, Financial Cryptography 2021 (arXiv:2003.03810).
- CFTC, press release 8647-23 (charges against Avraham Eisenberg), January 2023; United States v. Eisenberg (S.D.N.Y.), opinion and order of 23 May 2025 (Doc 220).
- SEC, press release 2023-32, “SEC Charges Terraform and CEO Do Kwon with Defrauding Investors”, 16 February 2023.
- Lido, public stETH APR and statistics endpoints; beacon-chain validator balances from a public beacon node; 24 September 2026.

## 23.8 Exercises

**Exercise 23.1 ★.**

With the chapter’s rate curve, what are the borrow and supply rates at 50% and at 95% utilisation?

**Solution of Exercise 23.1.**

At 50%: borrow $4\% \times 50/90 = 2.22\%$, supply $2.22\% \times 0.5 \times 0.9 = 1.00\%$. At 95%: borrow $4\% + 60\% \times 5/10 = 34\%$, supply $34\% \times 0.95 \times 0.9 = 29.07\%$.

**Exercise 23.2 ★.**

A borrower has 50 ether of collateral (threshold 82.5%) and USD 100 000 of debt. At what ether price is it liquidated?

**Solution of Exercise 23.2.**

$100\,000/(50 \times 0.825) = 2\,424.24$ dollars.

**Exercise 23.3 ★.**

Why does a [flash loan](#def-m3-defi-credit-and-leverage-flash) carry no credit risk for the pool?

**Solution of Exercise 23.3.**

The transaction either ends with the loan and fee repaid or reverts entirely, in which case the [tokens](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-key) never left the pool. There is no state in which the loan is outstanding.

**Exercise 23.4 ★★.**

A liquidator repays USD 50 000 of debt against ether at 2 500 with a 5% bonus. How much ether does it receive?

**Solution of Exercise 23.4.**

$50\,000/2\,500 \times 1.05 = 21$ ether.

**Exercise 23.5 ★★.**

A borrower’s [health factor](#def-m3-defi-credit-and-leverage-ltv) is 0.80 and the collateral’s threshold is 82.5% with a 10% bonus. Does liquidation improve its [health factor](#def-m3-defi-credit-and-leverage-ltv)?

**Solution of Exercise 23.5.**

No: $(1 + \beta)\theta = 1.1 \times 0.825 = 0.9075$, above the [health factor](#def-m3-defi-credit-and-leverage-ltv) of 0.80, so each liquidation lowers it further ([Proposition 23.4](#prop-m3-defi-credit-and-leverage-liq)); the position is heading for bad debt.

**Exercise 23.6 ★★.**

Explain the looping trade on a [liquid staking token](#def-m3-defi-credit-and-leverage-staking) and name two ways it loses money.

**Solution of Exercise 23.6.**

Supply the [liquid staking token](#def-m3-defi-credit-and-leverage-staking), borrow ether against it, stake the ether into more of the [token](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-key), and repeat, earning the [staking yield](#def-m3-defi-credit-and-leverage-staking) on several times the capital less the borrow rate on the debt. It loses if the borrow rate rises above the [staking yield](#def-m3-defi-credit-and-leverage-staking) (utilisation spikes), or if the [token](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-key) trades below the ether behind it and the loops are liquidated.

**Exercise 23.7 ★★★.**

*Coding.* With `pump_attack`, find the smallest pump at which the attack pays, with and without the [tokens](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-key) bought keeping a value of USD 1.

**Solution of Exercise 23.7.**

At a pump of 1.93 if the [tokens](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-key) bought become worthless, 1.72 if they keep USD 1: borrowing 60% of an inflated value and abandoning collateral worth the true value pays as soon as $0.6k$ exceeds one plus the pump’s cost per dollar of collateral.

**Exercise 23.8 ★★★.**

*Find the flaw.* “Our pool is safe: every loan is over-collateralised.”

**Solution of Exercise 23.8.**

Over-collateralised at the [oracle](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-contract)’s price. If the price can be moved, or the collateral cannot be sold near it in a crash, or liquidators do not act in time, the collateral can be worth less than the debt: the protection is only as good as the [oracle](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-contract) and the liquidity of the collateral.

## 23.9 Problem: The Oracle Pump

**Problem 23.1.**

Weekend problem — borrowing a pool dry

A [lending pool](#def-m3-defi-credit-and-leverage-pool) holds USD 50 million of [stablecoins](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-stable) and accepts a thin [token](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-key) as collateral at a loan-to-value of 60%, priced by an [oracle](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-contract) that reads a constant-product market holding USD 2 million and 2 million [tokens](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-key) (price USD 1). An attacker holds 5 million [tokens](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-key).

**Part I — The mechanics.**

1. What can the attacker borrow honestly?
2. By what factor must the [oracle](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-contract) ’s price rise for the attacker to borrow the whole pool?
3. What does pushing the market’s price up by that factor cost, and how many [tokens](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-key) does it buy?
4. What is the attacker’s gain if the [tokens](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-key) bought become worthless? If they keep USD 1?
5. Why does the gain fall for pumps beyond that factor?

**Part II — The thresholds.**

6. What is the smallest pump that pays?
7. How does the answer change if the source market holds USD 20 million?
8. What if the loan-to-value is 30%?
9. What borrow cap on this collateral would limit the attacker’s gain to zero at any pump?
10. What would a 30-minute time-weighted [oracle](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-contract) change?

**Part III — The case.**

11. What did the CFTC allege happened on Mango Markets?
12. What did the trial judge decide after the jury’s verdict, and on what kind of motion?
13. Who bears the loss when a pool is borrowed dry?
14. How does this attack differ from a flash-loan attack?
15. What would a lending protocol’s governance do next?

**Part IV — Judgement.**

16. Is the pool’s design at fault or the attacker’s conduct?
17. Should a pool accept thin [tokens](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-key) as collateral at all?
18. How should a supplier choose which pools to supply?
19. State the *named result* : the pump that borrows the pool dry, and the attacker’s cost against the gain.
20. In one sentence: what is an [oracle](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-contract) to a [lending pool](#def-m3-defi-credit-and-leverage-pool) ?

**Solution of Problem 23.1.**

**1.** $0.6 \times 5 =$ USD 3 million. **2.** $k^* = 50/(0.6 \times 5) = 16.7$. **3.** $2 \times (\sqrt{16.7} - 1) =$ USD 6.16 million, buying $1 - 1/\sqrt{16.7} = 75.5\%$ of the market’s [tokens](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-key), 1.51 million. **4.** $50 - 5 - 6.16 =$ USD 38.84 million; USD 40.35 million if the 1.51 million [tokens](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-key) keep USD 1. **5.** The pool is empty: further pumping only costs. **6.** 1.93. **7.** The attack never pays: pushing a USD 20 million market costs more than any borrowing it enables. **8.** $k^*$ doubles to 33.3, but the attack still pays from a pump of 4.98 and can gain USD 35.4 million: halving the loan-to-value is not enough. **9.** A cap of about USD 5.8 million on borrowing against this collateral: the gain $\min(B, 3k) - 5 - 2(\sqrt{k} - 1)$ (USD million) is then never positive. **10.** The attacker would have to hold the pumped price for half an hour, multiplying the cost and the arbitrage losses while the price is out of line. **11.** That Eisenberg pumped MNGO on the exchanges feeding the [oracle](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-contract), raising the reported price more than thirteenfold in 30 minutes, and used his inflated positions as collateral to withdraw over USD 110 million. **12.** On a motion under Rule 29, which asks whether the evidence could sustain the verdict, the judge vacated the first two counts and entered a judgment of acquittal on the third (23 May 2025). **13.** The suppliers of the pool, pro rata, unless the protocol has a reserve or a backstop. **14.** It lasts longer than a transaction and needs the attacker’s own capital; a flash-loan attack borrows the capital and must complete in one transaction, so it can only exploit prices that move within it. **15.** Freeze the collateral, lower its loan-to-value, cap borrowing against it, change the [oracle](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-contract), and vote on recoveries. **16.** Both: the design made the attack cheap, and whether the conduct was unlawful is for courts. **17.** Only with borrow caps sized to the cost of moving its [oracle](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-contract)’s sources, or not at all. **18.** By the collateral they accept and its caps, their [oracles](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-contract), their bad-debt history and reserves, and utilisation (the ability to withdraw). **19.** *Named result:* a pump of 16.7 times borrows the pool dry; it costs USD 6.16 million in the source market (plus the USD 5 million of abandoned collateral) against USD 50 million borrowed, a gain of about USD 38.8 million. **20.** The only thing it knows about the world, and so the thing an attacker buys.

## 23.10 Interview questions

**Interview question 23.1 ★ trader.**

What is a [health factor](#def-m3-defi-credit-and-leverage-ltv), and what happens when it falls below one?

**Solution of Interview question 23.1.**

Threshold-weighted collateral over debt. Below one, anyone may repay part of the debt (up to the close factor) and take collateral at a discount, the [liquidation bonus](#def-m3-defi-credit-and-leverage-ltv).

*What the interviewer is looking for: the ratio, the threshold, and permissionless liquidation.*

**Interview question 23.2 ★ developer.**

How does a [flash loan](#def-m3-defi-credit-and-leverage-flash) work, and why can it only exist on a [blockchain](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-chain)?

**Solution of Interview question 23.2.**

A loan that must be repaid within the transaction or the transaction reverts. It needs atomic execution of arbitrary code across contracts, which a [blockchain](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-chain) provides and traditional settlement does not.

*What the interviewer is looking for: atomicity as the collateral.*

**Interview question 23.3 ★★ researcher.**

Why do [lending pools](#def-m3-defi-credit-and-leverage-pool) use a kinked rate curve rather than a linear one?

**Solution of Interview question 23.3.**

The pool has no maturity and must keep [tokens](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-key) available for withdrawals; a steep rate above the target utilisation forces repayment and attracts supply before the pool is fully lent, while a gentle slope below keeps borrowing cheap in normal times.

*What the interviewer is looking for: liquidity for withdrawals.*

**Interview question 23.4 ★★ risk.**

How would you set the loan-to-value of a new collateral [token](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-key)?

**Solution of Interview question 23.4.**

From the collateral’s liquidity (depth of the markets the [oracle](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-contract) reads and where liquidators sell), its volatility over the time liquidation takes, the cost to manipulate its [oracle](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-contract), and a borrow cap so that the maximum loss is bounded; revisit as markets change.

*What the interviewer is looking for: liquidation horizon, depth and manipulation cost.*

**Interview question 23.5 ★★ trader, researcher.**

Why did an [algorithmic stablecoin](#def-m3-defi-credit-and-leverage-algo) paying 20% collapse, and what would you have watched?

**Solution of Interview question 23.5.**

The yield was paid, not earned, and the peg depended on the floating [token](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-key)’s value exceeding the [stablecoins](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-stable) outstanding; when demand turned, redemptions created floating [tokens](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-key) that nobody wanted. Watch the ratio of the floating [token](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-key)’s value to the [stablecoin](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-stable) supply, the source of the yield, and redemption flows.

*What the interviewer is looking for: reflexivity and the subsidy.*

**Interview question 23.6 ★★★ developer, risk.**

Design a liquidation bot that competes profitably without taking bad inventory risk.

**Solution of Interview question 23.6.**

Monitor [health factors](#def-m3-defi-credit-and-leverage-ltv) from the chain’s state and pending [oracle](https://one-course.com/books/quant/3/en/chapter/14-blockchains-for-traders#def-m3-blockchains-for-traders-contract) updates; simulate each liquidation with the collateral’s sale path and gas; fund with a [flash loan](#def-m3-defi-credit-and-leverage-flash) and sell in the same transaction so no inventory is carried; bid for position through bundles only up to the expected profit; and cap exposure to collateral that cannot be sold at once.

*What the interviewer is looking for: atomic funding and disposal, and bidding discipline.*
