The May 14, 2024 Sonne Finance exploit that extracted $20 million from Optimism-based lending markets was not a novel attack — it was a known vulnerability pattern in Compound v2 forks executed with precision timing. For developers and advanced users seeking to understand, detect, and mitigate these vulnerabilities, this technical walkthrough provides a deep dive into the mechanics of donation attacks and the forensic techniques used to identify them. With the DeFi ecosystem holding billions in total value locked and Bitcoin at $61,550, the stakes of smart contract security have never been higher.
The Objective
This tutorial aims to equip experienced developers and security researchers with the knowledge to identify donation attack vulnerabilities in Compound v2-style lending protocols. By understanding the exact mechanism exploited in the Sonne Finance attack, you will be able to audit similar protocols, implement protective measures, and potentially contribute to preventing future exploits of this nature.
Prerequisites
This guide assumes familiarity with Solidity smart contract development, understanding of EVM-compatible lending protocol architectures (particularly Compound v2), experience with blockchain development tools such as Foundry or Hardhat, and basic knowledge of DeFi mechanics including collateral factors, liquidation thresholds, and interest rate models. You should also have access to Optimism Etherscan for examining the attack transactions referenced throughout this analysis.
Step-by-Step Walkthrough
Step 1: Understanding the Compound v2 Collateral Model. In Compound v2-style protocols, each market has a collateral factor that determines how much a user can borrow against their deposited collateral. When a user deposits tokens, they receive cTokens (compound tokens) representing their share of the lending pool. The collateral factor, expressed as a percentage, determines the maximum borrowing capacity relative to the deposited amount. For example, with a 75% collateral factor, a user who deposits $100 worth of collateral can borrow up to $75 worth of other assets.
Step 2: The Donation Attack Vector. The donation attack exploits how cToken exchange rates are calculated. In a standard deposit, a user supplies tokens to the protocol and receives cTokens in proportion to their contribution. However, an attacker can manipulate the exchange rate by directly transferring tokens to the cToken contract — a “donation” that is not accounted for in the standard deposit mechanism. This donation inflates the apparent value of existing cTokens, including those held by the attacker.
When the attacker then uses these artificially inflated cTokens as collateral, the protocol calculates their borrowing capacity based on the manipulated exchange rate, allowing them to borrow substantially more than their actual deposit warrants. The borrowed funds represent real protocol liquidity that is drained from the lending pools.
Step 3: The Sonne Finance Attack Timeline. In the Sonne Finance case, the attack was executed with particular sophistication by exploiting a governance upgrade window. The protocol had passed a proposal to integrate VELO markets, with critical transactions queued behind a two-day timelock on a multi-sig wallet. The attacker monitored the timelock and executed four precisely timed transactions as soon as the timelock expired, setting up the market configuration before the legitimate operators could complete the intended setup sequence.
The attacker then executed the collateral factor increase transaction themselves, which was part of the original governance proposal. This allowed them to set up the conditions for the donation attack under parameters they controlled. The attack transaction on Optimism is fully documented on-chain for forensic review.
Step 4: Detecting the Vulnerability. To detect this vulnerability in other protocols, examine the cToken contract’s exchange rate calculation. The vulnerable pattern appears when the exchange rate can be influenced by direct token transfers to the contract rather than only through the official deposit function. Audit for code paths where totalSupply and totalCash can be manipulated independently of the mint and redeem functions.
Automated detection tools can scan for the specific function signatures and storage patterns associated with Compound v2-style collateral calculations. Look for the exchangeRateStored and exchangeRateCurrent functions and trace all paths that could influence their return values beyond normal market operations.
Step 5: Implementing Mitigations. Several mitigation strategies can protect against donation attacks. The most straightforward is to modify the cToken contract to ignore direct transfers when calculating exchange rates, only accounting for tokens received through the official deposit mechanism. This can be implemented by tracking deposits separately from the contract’s actual token balance.
For protocols using timelocks for governance execution, implementing real-time monitoring during execution windows can detect anomalous behavior before it results in fund drainage. Circuit breakers that automatically pause markets when unexpected exchange rate movements are detected can also limit the damage from successful attacks.
Troubleshooting
When auditing Compound v2 forks, common challenges include distinguishing between intentional exchange rate adjustments (such as those from accrued interest) and manipulative donations. The key differentiator is the source: legitimate exchange rate changes come from accumulated interest over time, while donation attacks involve sudden, large token transfers directly to the cToken contract that do not correspond to any user deposit.
Another challenge is that some protocols intentionally allow direct token transfers to cToken contracts for operational reasons, making it important to understand the specific protocol’s design before flagging potential vulnerabilities. Always verify whether direct transfers are an expected part of the protocol’s operation before recommending remediation.
Mastering the Skill
The Sonne Finance exploit demonstrates that known vulnerabilities continue to claim victims in the DeFi space. Mastering smart contract vulnerability assessment requires continuous learning and practice. Study the attack transactions on Optimism Etherscan, replicate the attack in a local test environment using Foundry, and practice identifying similar patterns in other protocols. The skills developed through this analysis — understanding exchange rate manipulation, governance attack vectors, and timelock exploitation — are transferable to a wide range of DeFi security assessments. As the ecosystem grows, the demand for developers who can identify and prevent these vulnerabilities will only increase.
Disclaimer: This article is for informational purposes only and does not constitute financial advice. Always conduct your own research before making investment decisions.
the collateral factor manipulation via donations is sneaky because it looks like normal user behavior until you trace the full sequence. great forensic breakdown
finally a technical writeup that actually explains the donation attack mechanics instead of just saying exploit happened funds drained. the collateral factor manipulation section is detailed enough to reproduce
the step-by-step reproduction is what sold me. most writeups skip the actual exploit trace and just show the result. this one lets you follow along in tenderly
yeah the reproduction steps are clean. bookmarked this for my own audit checklist. donation attacks in v2 forks are low-hanging fruit if you know where to look
the forensic analysis of how the attacker inflated collateral value in the Sonne pools is worth studying for anyone auditing compound v2 forks. this vulnerability class is not going away
Kim Bora the Sonne attack was $20M but Compound v2 forks hold billions collectively. this vulnerability class could have a much bigger victim next time
used this methodology to audit a v2 fork last month and found a donation vector in the cToken market. if youre not checking for this youre flying blind