Skip to content

Instantly share code, notes, and snippets.

@0xcuriousapple
Last active June 7, 2023 18:30
Show Gist options
  • Save 0xcuriousapple/ce1f4150674f2559e375b401a02dc776 to your computer and use it in GitHub Desktop.
Save 0xcuriousapple/ce1f4150674f2559e375b401a02dc776 to your computer and use it in GitHub Desktop.
One can manipulate `baseRate` using flashloan and pay lower fees inside the redemption

One can manipulate baseRate using flashloan and pay lower fees inside the redemption

Description

Raft allows anyone to redeem an equivalent amount of collateral tokens in return for rTokens using redeem().
The fee applied is the combination of baseRate and redemption spread.
The update done to baseRate is important since it's designed to increase with each share of collateral redeemed. It's calculated as

        uint256 decayedBaseRate = _calcDecayedBaseRate(collateralToken);

        /* Convert the drawn collateral back to R at face value rate (1 R:1 USD), in order to get
        * the fraction of total supply that was redeemed at face value. */
        uint256 redeemedFraction = collateralDrawn * price / totalDebtSupply;

        uint256 newBaseRate = decayedBaseRate + redeemedFraction / BETA;

https://github.com/raft-fi/contracts/blob/18d79fb62dceb730f13a3fc5cf286ce8fbecbd29/contracts/PositionManager.sol#L598-L604

Where totalDebtSupply is rToken totalsupply

_updateBaseRateFromRedemption(collateralToken, collateralToRedeem, price, rToken.totalSupply());

https://github.com/raft-fi/contracts/blob/18d79fb62dceb730f13a3fc5cf286ce8fbecbd29/contracts/PositionManager.sol#L259

Hence baseRate applicable is inversely proportional to rToken's total supply.
All is well until this point.

However, one thing Raft team missed is
rToken also allows users to flash mint
This allows anyone to take
flashloan of rToken,
inflate total supply,
call redeem inside callback with a different set of rTokens,
pay lower fees than intended,
return borrowed rTokens of flashloan

one thing to note is this invalid update would be sustained for future borrows and redeem as well, courtesy to
https://github.com/raft-fi/contracts/blob/18d79fb62dceb730f13a3fc5cf286ce8fbecbd29/contracts/PositionManager.sol#L609

Fortunately, raft only allows flash loan of 10% of the total supply, hence capping the impact on the base rate.
That's why this is a medium-severity issue and not a high one.

Remediation to consider

Consider reverting in redeem if there is a active flash loan.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment