In this track: Smart contract security foundations
Rounding direction in financial code
Integer rounding can shift value between participants.
Cutting every fractional coin in the same direction can favor one side repeatedly.
1 ETH = 1000000000 Gwei = 1000000000000000000 Wei.
Identify the exact ratio
Write the numerator, denominator and units before evaluating an integer formula. Multiplying and dividing in a different order can change precision or overflow risk.
Choose who bears the remainder
Flooring minted shares and ceiling required payment can protect different invariants. The correct direction depends on the operation rather than one universal rule.
Test repeated small actions
A tiny one-step rounding difference can accumulate when an action is repeated or split. Check sequences, not only a large representative amount.
Document the invariant
Explain the quantity that must remain conserved or bounded. Tests should assert that property across deposit, withdrawal and conversion boundaries.
Reason about the scenario
integer floor: 5 / 2 = 2
integer ceiling for the same positive ratio: 3
Choose deliberately and test who receives the remainder.Pseudocode or a simplified scenario for local reasoning. This is not a production contract or a completed security review.
Is one rounding direction universally correct for every operation?
The intended accounting invariant determines the direction.
Original explanations by EtherTools. Examples are educational and source-linked; this site is independent of the Ethereum Foundation. Our approach.