An audit proves the code works as written. It does not prove that attacking it fails to pay.
Picture a protocol with clean audit reports, a tidy test suite, and no known bugs. Then one afternoon an attacker borrows a large sum, nudges a price, borrows against the nudge, repays the loan, and leaves with millions. Not one line of code misbehaved.
That is the uncomfortable pattern behind some of DeFi's costliest incidents. In October 2022, an attacker on Mango Markets pumped the price of the MNGO token across thin markets, then borrowed against the inflated collateral and drained roughly $110 million. The contracts accomplished exactly what they were meant to achieve. The flaw lived in the economics.
An audit asks one question: is the code correct? Economic attack modeling asks a different one: would an attacker make money? A protocol can pass the first while failing the second. This post explains the gap, walks through the attack classes that slip past audits, and shows how to build a basic model you can run before launch.
What audits verify, and what they leave out
A good smart contract audit checks access control, reentrancy, arithmetic edge cases, and whether the stated invariants hold. It is careful, valuable work, and you should buy it.
But an audit treats your protocol largely in isolation. It reads the code and asks whether the code can be made to misbehave. It rarely asks what happens when the code behaves perfectly while the world around it does not: a price feed that follows a shallow pool, a collateral token that can be bought and dumped in one block, or a liquidation engine that works in calm markets and stalls in a crash.
Here is a simple way to hold the distinction:
- An audit answers: can an attacker break the rules of the system?
- An economic model answers: can an attacker play by the rules and still win?
The second category is harder to spot because nothing looks broken. There is no failing test, no reverted transaction, and no suspicious opcode. There is only a sequence of legal moves that ends with someone else's money in your wallet.
The attacker's equation
Attackers are rational. Before they act, they run a calculation, even if only in their head:
Expected profit = extractable value − (capital cost + fees + slippage + risk)
For years, the capital term kept most attacks in check. Moving a market large enough to exploit a protocol required serious money, and serious money was scarce and expensive.
Flash loans changed that. Because a flash loan is borrowed and repaid within a single transaction, the capital cost of an attack falls to near zero, which is just the fee. The question is no longer, "Does the attacker have enough money?" but "Is there any scale at which the math works?"
That reframing is the heart of economic attack modeling. You stop asking whether an attack is possible and start asking at what size, in what market conditions, and against which parameters it becomes profitable.
Read it left to right, then down: no step breaks a rule, which is why the audit finds nothing.
Four attack classes that pass audits
Each of these can exist in code that is, by every conventional measure, correct.
1. Oracle manipulation
If your protocol reads a price from a pool with shallow liquidity, that price can be moved by anyone with enough borrowed capital. The attacker pushes the price, uses the distorted number to borrow or mint against it, and unwinds. The oracle returned exactly the value it was designed to return. The design assumed the price was hard to move.
What a model flags: the cost to move the price by X percent, compared with the borrowing power that move unlocks.
2. Flash-loan governance and collateral attacks
In April 2022, Beanstalk lost roughly $180 million of assets after an attacker flash-borrowed enough funds to acquire a temporary supermajority of voting power and pass a malicious proposal in a single transaction. Governance did what governance does: a proposal got the votes it needed and executed. What nobody modeled was how cheaply voting power could be rented for one block.
What a model flags: any privilege, whether votes, collateral status, or reward weight, that can be acquired and used within one transaction.
3. Liquidation cascades and bad debt
Lending protocols set collateral factors and liquidation incentives with typical conditions in mind. Under stress, those numbers interact. Prices fall, liquidators sell into thin books, and the selling pushes prices down further. A deliberate attacker can trigger the spiral, or simply position to profit from it, leaving the protocol holding debt that no longer has enough collateral behind it.
What a model flags: the market depth at which liquidations stop being profitable for liquidators, because that is where bad debt begins.
4. Incentive and tokenomics exploits
Reward emissions, boosted pools, and mispriced collateral all create arbitrage against the protocol itself. If a reward pays more than the cost of farming it, someone will farm it until the treasury is empty. No bug required, only a spreadsheet.
What a model flags: positive-return loops where the protocol's own incentives fund the attacker.
How economic attack modeling works
You do not need a research lab to start. A practical first pass has four steps.
Step 1: Map the value flows. List every place value enters, leaves, or changes price inside the protocol, along with every external dependency: oracles, AMMs, lending markets, bridges. Each dependency is an input an attacker may be able to influence.
Step 2: Define the attacker. Decide what they can do. How much can they flash-borrow? Which markets can they touch in one transaction? Can they act across protocols? Model the attacker you fear, not the one you hope for.
Step 3: Search the parameter space. Sweep your own settings, such as collateral factors, oracle windows, and pool depths, against attack sizes, and look for the combinations where profit turns positive.
Step 4: Stress with real markets. Replay historical volatility and fork mainnet state, then try your best attack against the live configuration.
To make step 3 concrete, here is a deliberately simplified profit check. It is a sketch for building intuition, not a production model:
def attack_profit(position_value, flash_size, pool_depth,
collateral_factor, fee_bps):
# Crude constant-product approximation of price impact
price_impact = flash_size / pool_depth
# Extra borrowing power created by the manipulated price
excess_borrow = position_value * price_impact * collateral_factor
# Flash-loan fee plus slippage when unwinding
cost = flash_size * fee_bps / 10_000 + flash_size * price_impact / 2
return excess_borrow - cost
for size in range(1_000_000, 30_000_001, 1_000_000):
p = attack_profit(position_value=10_000_000, flash_size=size,
pool_depth=20_000_000, collateral_factor=0.8,
fee_bps=9)
print(size, round(p))
Run a sweep like this and you learn something no audit report contains: the break-even point. If the attack turns profitable at $5 million of flash liquidity and your pool holds $20 million, you now have a number to design around, whether that means a time-weighted price, a deeper pool, or a lower collateral factor.
Tools that make this practical
Fork-based testing with Foundry or Anvil lets you replay an attack against a copy of mainnet state. Fuzzing can assert economic invariants, such as "no single transaction can leave the protocol with bad debt," rather than only code-level ones. Agent-based simulation lets you unleash many rational actors at your parameters and watch what they find.
The best place to run all of this is early. Model during design review, again before launch, and again before every governance vote that changes a risk parameter. Economic security is not a one-time sign-off, because every parameter change reshapes the attacker's math.
A pre-launch economic security checklist
Save this one.
- Oracles: Does any price depend on a spot price from a pool an attacker can move in one transaction?
- Liquidity assumptions: What pool depth does your design quietly assume, and what happens at half of it?
- Single-transaction privileges: Can votes, collateral status, or reward weight be acquired and used within one block?
- Maximum extractable value: What is the most an attacker could take, and what is the cheapest attack that achieves it?
- Stress behavior: At what market drop do liquidations stop being profitable for liquidators?
- Circuit breakers: Can the protocol pause or throttle itself when something moves abnormally fast?
- Monitoring: Will you see an unusual sequence while it is happening, not after?
The limits of modeling
No model catches everything. It only covers the attacks you thought to describe, and attackers are creative in exactly the places you did not look. Treat economic modeling as a complement to audits, not a replacement. The goal is not certainty. It is to make the cheapest attack expensive enough that nobody bothers.
The question to leave on your desk
Audits tell you the code is correct. Economic attack modeling tells you whether correct is enough. Every parameter in your protocol, from a collateral factor to an oracle window, is a security parameter, and each one changes what an attacker can earn.
So here is the question worth asking before your next launch: if your largest pool were drained tomorrow, would the attack have been profitable, and would you have known before the attacker did?
If you are building or reviewing DeFi infrastructure and want to pressure-test the economics as well as the code, that is the kind of problem the engineering team at Coinsclone works on. Share your thoughts or the attack classes you think I missed in the comments.

Top comments (0)