Skip to content
high DeFi Economic No SWC entry CWE-682

Interest / Rewards Not Accrued Before State Change

SAFE-0220 DeFi Economic Checked automatically by the scanner
1

What goes wrong

A deposit/withdraw/borrow changes balances without first accruing pending interest or rewards to the current index. The new balance retroactively earns (or dodges) interest for a period it wasn't present, mispricing every position.

2

The vulnerable pattern

proof of concept — how it is exploited
// user deposits right before a big accrual without _updateIndex()
// -> their fresh balance is credited interest for the whole past period,
// stealing yield from existing depositors.
3

How to fix it

the pattern that is safe
modifier accrue() { _updateIndex(); _; }
function deposit(uint256 a) external accrue { ... }   // accrue first
Accrue interest/rewards before any balance mutation.
Check your own contract for this

The SaferICO scanner runs 201 detectors over your Solidity source, SAFE-0220 among them. Paste an address or the source itself — a small per-scan fee, shown before you sign, or unlimited on any plan.

Run the scanner See how it is attacked Read the docs