Yearn Finance loses $11 million from a flash loan attack

On 4th February at around 10.00pm GMT, the DAI v1 vault on Yearn showed a loss of 1,059%. Yearn, one of the largest platforms in Defi, was experiencing a flash loan attack.

It was reported that a total of nine flash loans were used to attack the vault, causing a total loss of $11 million. Founder Andre Cronje has since recommended that users should take out insurance on their funds.

The attacker was likely to have been an opportunistic arbitrageur, who exploited the protocol after finding a loophole in the contract. A flash loan for 116k ETH was taken from dYdX and 99k ETH from Aave v2. The attacker proceeded to borrow 134 million USDC and 129 million DAI from Compound, using the ETH as collateral. 134 million USDC and 36 million DAI was then added to 3crv Curve pool, before 165 million USDT was taken out from 3crv Curve pool. The attacker repeated the process five times.

After each repeated process, the attacker gained more in Curve DAO tokens, which ultimately amounted to $2.8 million. The attacker swapped the Curve DAO tokens for stabletokens.

"In a nutshell, someone deposited a bunch to Curve 3pool to manipulate DAI price given by the pool,” Curve CEO Michael Egorov told CoinDesk. “Vault somehow was relying on the DAI price given by this pool. Then the contract withdrew after the attack. And repeated many times taking flash-borrowed funds.
A breakdown of the transactions
A breakdown of the transactions

Examining the exploit

At a high level, the exploiter was able to profit through the following steps:

1. Debalance the exchange rate between stablecoins in Curve’s 3CRV pool.
2. Make the yDAI vault deposit into the pool at an unfavorable exchange rate.
3. Reverse the imbalance caused in step 1.

In the final repetition, the attacker redeemed the initial 3crv shares and 2.9m DAI more than what was deposited originally. Slippage on the yDAI pool was set too loose at 1% (i.e. 2.9m DAI), allowing to profit from the price differences on Curve’s 3CRV pool, also facilitated by the absence of withdrawal fees, given that the attack could be repeated indefinitely by abusing the yDAI vault’s earn() function and push deposits into the vault’s strategy at will. The attacker took advantage of a mistake made during the vault migration, luckily the Yearn team mitigated the exploit in 11 minutes saving 24 million of the vault’s 35 million DAI under management.

The attacker previously deposited USDC and DAI in to Curve's 3pool. Then Yearn’s yDAI vault automatically deposited DAI into Curve’s 3pool, which had already been heavily deposited with USDC and DAI. By adding USDT to the pool, Curve's protocol mechanics devalues DAI.

After the attacker withdrew the DAI from yDAI, and the USDT, USDC, and other DAI from 3pool, the attacker gained the rewards of Curve’s DAO Tokens - for providing liquidity when the DAI rate had strayed from the pool's other two assets.

Tether announced that it had frozen part of the funds stolen from Yearn, which mitigated the loss by $1.7 million.