Skip to content
Business logic Audit finding Intermediate

Incorrect Update of `calculateCumulativeRate` in `_withdraw` Results in Erroneous Interest Calculation

In the `_withdraw` function, `lastEventTime` is updated to the current block timestamp before calling `calculateCumulativeRate`, causing the time interval to be zero and interest not to accrue for that period. This results in missed interest for all users on each withdrawal.

0x37 Follow with 0xAadi, Abhan1041 and 14 others

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2024-11-autonomint-judging/issues/1003

Found by

0x37, 0xAadi, Abhan1041, Cybrid, EgisSecurity, LordAlive, ParthMandale, RampageAudit, Silvermist, futureHack, newspacexyz, nuthan2x, onthehunt, smbv-1923, super_jack, vatsal, volodya

Summary

In the Borrowing::_withdraw function, the calculateCumulativeRate method is invoked after the lastEventTime variable is updated to the current block.timestamp. Here's the relevant portion of the function:

Solidity
function _withdraw(
        address toAddress,
        uint64 index,
        bytes memory odosAssembledData,
        uint64 ethPrice,
        uint128 exchangeRate,
        uint64 withdrawTime
    ) internal {
        ...
            lastEventTime = uint128(block.timestamp);
        }

        // Call calculateCumulativeRate function to get the interest
        calculateCumulativeRate();
        ...
    }

The calculateCumulativeRate function internally calls a method from BorrowLib and passes the lastEventTime—which was updated just before this call—as a parameter. Here's the code for calculateCumulativeRate:

Solidity
function calculateCumulativeRate() public returns (uint256) {
        uint128 noOfBorrowers = treasury.noOfBorrowers();
        uint256 currentCumulativeRate = BorrowLib.calculateCumulativeRate(
            noOfBorrowers,
            ratePerSec,
            lastEventTime,
            lastCumulativeRate
        );
        lastCumulativeRate = currentCumulativeRate;
        return currentCumulativeRate;
    }

In BorrowLib.calculateCumulativeRate, the currentCumulativeRate is calculated using the difference between the current block.timestamp and the lastEventTime. This difference is referred to as timeInterval. However, since lastEventTime is updated to block.timestamp before the call to calculateCumulativeRate, the timeInterval becomes 0. This causes the interest to remain unchanged, as no meaningful time difference is accounted for in the calculations. This issue prevents the proper update of the cumulative rate and leaves it at its previous value.

Root Cause

https://github.com/sherlock-audit/2024-11-autonomint/blob/0d324e04d4c0ca306e1ae4d4c65f0cb9d681751b/Blockchain/Blockchian/contracts/Core_logic/borrowing.sol#L701C4-L704C35

Internal pre-conditions

No response

External pre-conditions

No response

Attack Path

The interest calculation relies on the timeInterval, which is the difference between the current time and lastEventTime. In the _withdraw function, lastEventTime is updated to block.timestamp before the interest is calculated, resulting in no actual update to the interest.

As a result, each time the _withdraw function is called, the interest for the period (current time - lastEventTime before the function call) is not properly accounted for.

Impact

The interest is not being updated correctly in the _withdraw function, causing the interest for that time interval to be missed for all users.

PoC

No response

Mitigation

The protocol should ensure that calculateCumulativeRate is updated before lastEventTime is set to block.timestamp, allowing the interest to be calculated accurately.

The original report as published on Sherlock, by 0x37. 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 order of operations is wrong: `lastEventTime` is set to the current timestamp before `calculateCumulativeRate` uses it to compute the elapsed time.

Impact

Interest for the period since the last update is not accounted for, causing all users to miss interest on each withdrawal.

What the attacker needed

None

The fix

The protocol should call `calculateCumulativeRate` before updating `lastEventTime` to `block.timestamp`.

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.

This is a smart contract bug where interest is calculated based on how much time has passed. The code updates the 'last event time' to now before calculating interest, so the time difference becomes zero and no interest is added. As a result, users lose out on interest they should have 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 0x37

View all

79 more disclosed reports, best first.

More from Autonomint Colored Dollar V1

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.