Skip to content
Audit finding Intermediate

Simple interest calculation is not exact

The borrow interest was calculated using a simple interest formula rather than compound interest, so the actual rate depended on how often the market was updated. In low-activity markets, this could lead to a lower borrow rate and suppliers losing out on interest.

cmichel Follow

Originally disclosed on Code4rena

Submitted by cmichel

https://github.com/code-423n4/2022-02-anchor/blob/7af353e3234837979a19ddc8093dc9ad3c63ab6b/contracts%2Fmoney-market-contracts%2Fcontracts%2Fmarket%2Fsrc%2Fborrow.rs#L304

The borrow rate uses a simple interest formula to compute the accrued debt, instead of a compounding formula.

pub fn compute_interest_raw(
    state: &mut State,
    block_height: u64,
    balance: Uint256,
    aterra_supply: Uint256,
    borrow_rate: Decimal256,
    target_deposit_rate: Decimal256,
) {
  // @audit simple interest
    let passed_blocks = Decimal256::from_uint256(block_height - state.last_interest_updated);

    let interest_factor = passed_blocks * borrow_rate;
    let interest_accrued = state.total_liabilities * interest_factor;
    // ...
}

This means the actual borrow rate and interest for suppliers depend on how often updates are made. This difference should be negligible in highly active markets, but it could lead to a lower borrow rate in low-activity markets, leading to suppliers losing out on interest.

Recommended Mitigation Steps

Ensure that the markets are accrued regularly, or switch to a compound interest formula (which has a higher computational cost due to exponentiation, but can be approximated, see Aave).

Alex the Entreprenerd (triage) commented:

Without Code and explanation, I’m skeptical of Med (loss of yield), as it could just be dust amount, provided that interest is compounded roughly 1 per month or similar (see compound interest math, and how e limits max autocompounds to dust variation)

Either way, the observation is correct.


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

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

Likely the code used a simple interest formula (interest = principal × rate × time) instead of a compound interest formula, making the accrued interest dependent on the frequency of updates.

Impact

In low-activity markets, the borrow rate could be lower than intended, causing suppliers to lose out on interest; the difference is negligible in highly active markets.

What the attacker needed

Low-activity market; infrequent interest updates

The fix

The markets should be accrued regularly, or the code should switch to a compound interest formula (which can be approximated to reduce computational cost).

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.

Interest on a loan can be calculated in two ways: simple interest only charges interest on the original amount, while compound interest also charges interest on previously accumulated interest. This code used simple interest, so if the market was not updated often, the total interest charged was lower than it should be. That meant people who supplied funds to the market earned less than expected.

More by cmichel

View all

196 more disclosed reports, best first.

More from Anchor contest

View all

The newest disclosed reports for this program.

Similar reports

Same bug class and techniques, clearest first.