Builder can halve the interest paid to a community owner due to arithmetic rounding
The interest calculation in returnToLender() rounds the number of days down to a whole number, so a builder who repays a tiny amount just before the two-day mark only accrues one day of interest. This lets a builder halve the APR paid to a community owner while still advertising the higher rate.
Submitted by scaraven, also found by 0x52, auditor0517, Deivitto, hansfriese, Lambda, rbserver, simon135, smiling_heretic, sseefried, and TrungOre
Due to arithmetic rounding in returnToLender(), a builder can halve the APR paid to a community owner by paying every 1.9999 days. This allows a builder to drastically decrease the amount of interest paid to a community owner, which in turn allows them to advertise very high APR rates to secure funding, most of which they will not pay.
This issue occurs in the calculation of noOfDays in returnToLender() which calculates the number of days since interest has last been calculated. If a builder repays a very small amount of tokens every 1.9999 days, then the noOfDays will be rounded down to 1 days however lastTimestamp is updated to the current timestamp anyway, so the builder essentially accumulates only 1 day of interest after 2 days.
I believe this is high severity because a community owner can have a drastic decrease in interest gained from a loan which counts as lost rewards. Additionally, this problem does not require a malicious builder because if a builder pays at a wrong time, the loaner receives less interest anyway.
Proof of Concept
- A community owner provides a loan of 500_000 tokens to a builder with an APR of 10% (ignoring treasury fees)
- Therefore, the community owner will expect an interest of 136.9 tokens per day (273.9 per 2 days)
- A builder repays 0.000001 tokens at
lastTimestamp + 2*86400 - 1 noOfDaysrounds down to 1 thereby accumulating500_000 * 100 * 1 / 365000 = 136tokens for 2 days- Therefore, the community owner only receives 5% APR with negligible expenses for the builder
Tools Used
VS Code
Recommended Mitigation Steps
There are two possible mitigations:
- Add a scalar to
noOfDaysso that any rounding which occurs is negligible
i.e.
uint256 _noOfDays = (block.timestamp -
_communityProject.lastTimestamp) * SCALAR / 86400; // 24*60*60
/// Interest formula = (principal * APR * days) / (365 * 1000)
// prettier-ignore
uint256 _unclaimedInterest =
_lentAmount *
_communities[_communityID].projectDetails[_project].apr *
_noOfDays /
365000 /
SCALAR;
- Remove the
noOfDayscalculation and calculate interest in one equation which reduces arithmetic rounding
uint256 _unclaimedInterest =
_lentAmount *
_communities[_communityID].projectDetails[_project].apr *
(block.timestamp -
_communityProject.lastTimestamp) /
365000 /
86400;
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 interest calculation converts elapsed seconds into whole days with integer division, discarding the remainder, while still updating the last-timestamp to the current time.
Impact
A builder could pay roughly half the advertised interest to a community owner, and the loss also happens accidentally when repayments land at the wrong time.
What the attacker needed
An active loan between a builder and a community; builder controls repayment timing; no special privileges needed
The fix
The interest should be calculated from the exact elapsed seconds rather than a rounded-down day count, or a scaling factor should be added so rounding is negligible.
How it was found
- A community owner lends 500,000 tokens to a builder at 10% APR.
- The builder repays a very small amount just before two full days have elapsed since the last timestamp.
- The elapsed time is rounded down to one day, so only one day of interest is charged.
- The last timestamp is still updated, so the builder keeps repeating this to pay roughly half the expected interest.
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.
A smart contract lends tokens and charges interest based on how many whole days have passed. Because the code throws away the leftover fraction of a day, someone can pay back a tiny amount just before two full days have passed and only be charged for one day. Doing this repeatedly means the lender gets about half the interest they expected.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.