Skip to content
Business logic Audit finding Advanced

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.

Jeiwan Follow with KingNFT, hyh and 1 other

Originally disclosed on Sherlock

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:

  1. setTotalUnderlying calls balanceUnderlying on each of the supported protocol;
  2. CompoundProvider.balanceUnderlying multiplies contract's cToken balance by the exchange rate of the cToken;
  3. 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

  1. Likewise, BetaProvider.balanceUnderlying and BetaProvider.calcShares read IBeta.totalLoanable and IBeta.totalLoan to convert BTokens to underlying tokens, however the interest is not accrued in totalLoan beforehand (as can be seen in the code of BToken, accruing interest increases totalLoan).

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:

  1. gamer rewards are calculated in storePriceAndRewards, before funds are deposited/withdrawn to Compound or Beta;
  2. 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

  1. setTotalUnderlying is called when vaults report their balances:

https://github.com/sherlock-audit/2023-01-derby/blob/main/derby-yield-optimiser/contracts/MainVault.sol#L252

  1. setTotalUnderlying calls balanceUnderlying, which calls balanceUnderlying on each protocol provider:

https://github.com/sherlock-audit/2023-01-derby/blob/main/derby-yield-optimiser/contracts/Vault.sol#L344-L361

  1. CompoundProvider.balanceUnderlying calls exchangeRate to get the exchange rate of the cToken:

https://github.com/sherlock-audit/2023-01-derby/blob/main/derby-yield-optimiser/contracts/Providers/CompoundProvider.sol#L96

  1. The exchange rate is read via the exchangeRateStored function, 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

  1. BetaProvider.balanceUnderlying and BetaProvider.calcShares read IBeta.totalLoan to 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

  1. In BToken, totalLoan is 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.

The original report as published on Sherlock, by Jeiwan. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.

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

  1. During rebalancing the vault reports its balances by calling setTotalUnderlying, which calls balanceUnderlying on each protocol provider.
  2. For Compound, the provider multiplies the cToken balance by the exchange rate read from exchangeRateStored, which returns a cached rate.
  3. For Beta, the provider reads totalLoan to convert BTokens without accruing interest first.
  4. Gamer rewards are then calculated from the saved underlying balance before any deposit or withdrawal triggers accrual.
  5. 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.

More by Jeiwan

View all

69 more disclosed reports, best first.

More from Derby

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.