Delayed Compound and Beta interest accrual reduces gamer rewards and affects funds distribution to vaults
During rebalancing, the vault computed its underlying balance in Compound and Beta using cached or unaccrued interest figures, so the reported balance was lower than the real one. Gamers therefore received reduced rewards, and the amounts pushed to the controller for distribution among vaults were also smaller than they should have been.
Source: https://github.com/sherlock-audit/2023-01-derby-judging/issues/317
Found by
Jeiwan, KingNFT, hyh, psy4n0n
Summary
During rebalancing, Compound and Beta interest is not accrued until after gamer rewards are calculated leading to the rewards being calculated on the underlying amounts that are smaller than the real amounts. As a result, gamers receive reduced rewards. Also, since interest accrual doesn't happen when vaults push underlying amounts to XChainController, the underlying amounts to be distributed among vaults will be smaller that real amounts.
Vulnerability Detail
The Derby protocol allows users to deposit funds to third-party protocols to earn passive yield. Two of such protocols are Compound and Beta: funds deposited to Compound and Beta earn interest collected from borrowers who borrow the funds. Due to accruing of interest, the exchange rate of cTokens and BTokens increases over time: for example, 1 cToken can be exchange for 1.01 underlying token after some time.
During rebalancing, Derby vaults report their underlying token balances (the amount of funds deposited in third-party protocols) to XChainController, which re-allocates and re-distributes them according to the allocations set by gamers. However, when the underlying balances are calculated, interest is not accrued on Compound and Beta:
- setTotalUnderlying calls balanceUnderlying on each of the supported protocol;
- CompoundProvider.balanceUnderlying multiplies contract's cToken balance by the exchange rate of the cToken;
- CompoundProvider.exchangeRate calls ICToken.exchangeRateStored, which doesn't accrue interest and returns cached exchange rate:
This function does not accrue interest before calculating the exchange rate
- Likewise, BetaProvider.balanceUnderlying and BetaProvider.calcShares read
IBeta.totalLoanableandIBeta.totalLoanto convert BTokens to underlying tokens, however the interest is not accrued intotalLoanbeforehand (as can be seen in the code ofBToken, accruing interest increasestotalLoan).
Thus, the interest accrued by the funds deposited to Compound and Beta since the previous rebalancing won't be counted in the new rebalancing, and the underlying balance reported by the vault will be lower than the real balance.
Interest is accrued only at the end of rebalancing, in the Vault.rebalance function: first deposit or withdrawal to/from Compound and Beta will accrue interest. However, this will happen after gamer rewards have been calculated:
- gamer rewards are calculated in storePriceAndRewards, before funds are deposited/withdrawn to Compound or Beta;
- gamer rewards are calculated based on the underlying balance calculated in calcUnderlyingIncBalance, which reads the value of
savedTotalUnderlying–it was set in the beginning of rebalancing, and interest wasn't accrued before it was set.
Thus, gamer rewards will always lag behind actual underlying balances of Compound and Beta, and gamers will always earn reduced rewards.
Impact
Gamer receive reduced rewards due to delayed accruing of interest in Compound and Beta.
Code Snippet
setTotalUnderlyingis called when vaults report their balances:
setTotalUnderlyingcallsbalanceUnderlying, which callsbalanceUnderlyingon each protocol provider:
CompoundProvider.balanceUnderlyingcallsexchangeRateto get the exchange rate of the cToken:
- The exchange rate is read via the
exchangeRateStoredfunction, which returned cached rate: the interest earned since the previous accrual is not counted:
https://github.com/sherlock-audit/2023-01-derby/blob/main/derby-yield-optimiser/contracts/Providers/CompoundProvider.sol#L127 https://github.com/compound-finance/compound-protocol/blob/master/contracts/CToken.sol#L284-L293
BetaProvider.balanceUnderlyingandBetaProvider.calcSharesreadIBeta.totalLoanto convert BTokens to underlying tokens:
https://github.com/sherlock-audit/2023-01-derby/blob/main/derby-yield-optimiser/contracts/Providers/BetaProvider.sol#L89 https://github.com/sherlock-audit/2023-01-derby/blob/main/derby-yield-optimiser/contracts/Providers/BetaProvider.sol#L102
- In
BToken,totalLoanis increased by accumulated interest when interest is accrued:
https://github.com/beta-finance/beta/blob/master/contracts/BToken.sol#L92-L93
Tool used
Manual Review
Recommendation
In CompoundProvider.exchangeRate, consider calling exchangeRateCurrent instead of exchangeRateStored.
In BetaProvider.balanceUnderlying and BetaProvider.calcShares consider calling IBeta.accrue before calling IBeta.totalLoan.
Discussion
sjoerdsommen
This does only lead to a slightly lower yield. If we would fix it, this would also lead to a more expensive call. We would have to investigate this.
hrishibhat
Considering this issue a valid medium as there is a stale exchangeRateStored resulting in reduced rewards for depositors and affects allocator rewards.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The balance calculation used Compound's cached exchange rate and Beta's unaccrued total loan figure instead of triggering interest accrual first, so interest earned since the last accrual was omitted.
Impact
Gamers receive reduced rewards, and the underlying amounts distributed among vaults are smaller than the real amounts; the project noted the effect is a slightly lower yield and that a fix would make the call more expensive.
What the attacker needed
Funds deposited in Compound or Beta; rebalancing triggered
How it was found
- During rebalancing the vault reports its balances by calling setTotalUnderlying, which calls balanceUnderlying on each protocol provider.
- For Compound, the provider multiplies the cToken balance by the exchange rate read from exchangeRateStored, which returns a cached rate.
- For Beta, the provider reads totalLoan to convert BTokens without accruing interest first.
- Gamer rewards are then calculated from the saved underlying balance before any deposit or withdrawal triggers accrual.
- Interest is only accrued later, when funds are deposited to or withdrawn from Compound or Beta.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
When you put money into a lending pool, it slowly earns interest, so the pool's internal exchange rate should creep upward. This protocol read a saved, out-of-date version of that rate instead of asking the pool to update it first, so it thought the vault held less money than it really did. Rewards were then paid on that too-small number, meaning users got slightly less than they earned.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.