Thunder Loan

AI First Flight #7
Beginner FriendlyFoundryDeFiOracle
EXP
View results
Submission Details
Severity: high
Valid

WETH-denominated fee applied to token exchange rate

Description

Normal behavior

The AssetToken exchange rate should reflect the ratio of underlying vault tokens to total shares. Any increase to the exchange rate must be backed by an actual inflow of underlying tokens, measured in the same unit as the vault balance.

Specific issue

ThunderLoan.deposit() computes getCalculatedFee(token, amount), which returns a WETH-denominated value based on the oracle price. It then passes this WETH value directly to AssetToken.updateExchangeRate(). That function treats the fee as if it were an inflow of underlying tokens, inflating the exchange rate by a WETH-equivalent amount that does not match the actual token backing.

// src/protocol/ThunderLoan.sol
function deposit(IERC20 token, uint256 amount) external revertIfZero(amount) revertIfNotAllowedToken(token) {
AssetToken assetToken = s_tokenToAssetToken[token];
uint256 exchangeRate = assetToken.getExchangeRate();
uint256 mintAmount = (amount * assetToken.EXCHANGE_RATE_PRECISION()) / exchangeRate;
emit Deposit(msg.sender, token, amount);
assetToken.mint(msg.sender, mintAmount);
@> uint256 calculatedFee = getCalculatedFee(token, amount); // WETH-denominated
@> assetToken.updateExchangeRate(calculatedFee); // treated as token-denominated
token.safeTransferFrom(msg.sender, address(assetToken), amount);
}
// src/protocol/AssetToken.sol
function updateExchangeRate(uint256 fee) external onlyThunderLoan {
uint256 newExchangeRate = (totalSupply() + fee) * EXCHANGE_RATE_PRECISION / totalSupply();
if (newExchangeRate < exchangeRate) {
revert AssetToken__ExhangeRateCanOnlyIncrease();
}
exchangeRate = newExchangeRate;
}

Risk

Likelihood

  • The deposit fee is applied on every deposit and uses the oracle-derived WETH value.

  • Any token whose WETH price differs from 1e18 will cause the exchange rate to diverge from the actual backing.

Impact

  • The exchange rate no longer represents a 1:1 ratio with underlying tokens.

  • LPs may be unable to fully redeem, or the vault may hold excess unclaimed value depending on price direction.

  • The accounting invariant vaultBalance >= totalShares * exchangeRate / PRECISION is violated.

Proof of Concept

The PoC deposits tokens and shows that the exchange rate increases by the WETH-denominated fee, while the vault only receives the token-denominated deposit amount. The implied LP claim exceeds the actual vault balance.

// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;
import { ThunderLoan } from "src/protocol/ThunderLoan.sol";
import { AssetToken } from "src/protocol/AssetToken.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
contract AssetTokenShareTest {
ThunderLoan thunderLoan;
IERC20 token;
address lp = address(0x1);
function test_F12_wethFeeInflatesExchangeRate() public {
token.mint(lp, 50e18);
vm.startPrank(lp);
token.approve(address(thunderLoan), 50e18);
thunderLoan.deposit(token, 50e18);
vm.stopPrank();
AssetToken asset = thunderLoan.getAssetFromToken(token);
uint256 actualVaultBalance = token.balanceOf(address(asset));
uint256 totalShares = asset.totalSupply();
uint256 exchangeRate = asset.getExchangeRate();
uint256 impliedClaim = (totalShares * exchangeRate) / asset.EXCHANGE_RATE_PRECISION();
@> assertGt(impliedClaim, actualVaultBalance, "exchange rate claims more than vault holds");
}
}

Recommended Mitigation

Do not mix units in the exchange-rate update. Convert the fee to underlying token units before calling updateExchangeRate(), or remove the deposit-side exchange-rate update entirely.

// Option 1: fee in underlying token units
uint256 calculatedFee = (amount * s_flashLoanFee) / s_feePrecision;
assetToken.updateExchangeRate(calculatedFee);
// Option 2: remove deposit-side fee accrual
// (requires compensating LPs through a separate fee distribution mechanism)
Updates

Lead Judging Commences

ai-first-flight-judge Lead Judge about 2 hours ago
Submission Judgement Published
Validated
Assigned finding tags:

[H-02] Updating exchange rate on token deposit will inflate asset token's exchange rate faster than expected

# Summary Exchange rate for asset token is updated on deposit. This means users can deposit (which will increase exchange rate), and then immediately withdraw more underlying tokens than they deposited. # Details Per documentation: > Liquidity providers can deposit assets into ThunderLoan and be given AssetTokens in return. **These AssetTokens gain interest over time depending on how often people take out flash loans!** Asset tokens gain interest when people take out flash loans with the underlying tokens. In current version of ThunderLoan, exchange rate is also updated when user deposits underlying tokens. This does not match with documentation and will end up causing exchange rate to increase on deposit. This will allow anyone who deposits to immediately withdraw and get more tokens back than they deposited. Underlying of any asset token can be completely drained in this manner. # Filename `src/protocol/ThunderLoan.sol` # Permalinks https://github.com/Cyfrin/2023-11-Thunder-Loan/blob/8539c83865eb0d6149e4d70f37a35d9e72ac7404/src/protocol/ThunderLoan.sol#L153-L154 # Impact Users can deposit and immediately withdraw more funds. Since exchange rate is increased on deposit, they will withdraw more funds then they deposited without any flash loans being taken at all. # Recommendations It is recommended to not update exchange rate on deposits and updated it only when flash loans are taken, as per documentation. ```diff function deposit(IERC20 token, uint256 amount) external revertIfZero(amount) revertIfNotAllowedToken(token) { AssetToken assetToken = s_tokenToAssetToken[token]; uint256 exchangeRate = assetToken.getExchangeRate(); uint256 mintAmount = (amount * assetToken.EXCHANGE_RATE_PRECISION()) / exchangeRate; emit Deposit(msg.sender, token, amount); assetToken.mint(msg.sender, mintAmount); - uint256 calculatedFee = getCalculatedFee(token, amount); - assetToken.updateExchangeRate(calculatedFee); token.safeTransferFrom(msg.sender, address(assetToken), amount); } ``` # POC ```solidity function testExchangeRateUpdatedOnDeposit() public setAllowedToken { tokenA.mint(liquidityProvider, AMOUNT); tokenA.mint(user, AMOUNT); // deposit some tokenA into ThunderLoan vm.startPrank(liquidityProvider); tokenA.approve(address(thunderLoan), AMOUNT); thunderLoan.deposit(tokenA, AMOUNT); vm.stopPrank(); // another user also makes a deposit vm.startPrank(user); tokenA.approve(address(thunderLoan), AMOUNT); thunderLoan.deposit(tokenA, AMOUNT); vm.stopPrank(); AssetToken assetToken = thunderLoan.getAssetFromToken(tokenA); // after a deposit, asset token's exchange rate has aleady increased // this is only supposed to happen when users take flash loans with underlying assertGt(assetToken.getExchangeRate(), 1 * assetToken.EXCHANGE_RATE_PRECISION()); // now liquidityProvider withdraws and gets more back because exchange // rate is increased but no flash loans were taken out yet // repeatedly doing this could drain all underlying for any asset token vm.startPrank(liquidityProvider); thunderLoan.redeem(tokenA, assetToken.balanceOf(liquidityProvider)); vm.stopPrank(); assertGt(tokenA.balanceOf(liquidityProvider), AMOUNT); } ```

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.

Give us feedback!