Bottom line: decimals() is attacker-controlled and not guaranteed to be 18. Code that hardcodes 1e18 over-sends by up to 10^12 on USDC and 10^6 on WBTC. Always read decimals per token and compute amounts from it.

The failure

1
2
3
// wrong
uint amount = 100 * 1e18; // sends $100 if decimals == 18
token.transferFrom(user, this, amount);

On USDC (6 decimals) that transfers 100,000,000,000,000 units — a hundred trillion dollars’ worth, which requires the user to have it or reverts. On a 6-decimal token where the balance happens to cover a chunk of it, it silently drains a large share.

Read it

1
2
uint8 d = IERC20(t).decimals();
uint amount = 100 * 10 ** d;

Cache d per token in storage; decimals() is a staticcall per use otherwise. Sanity-bound it: accept 0–36 and revert outside that — a token returning 255 is either broken or hostile.

Where else it bites

  • Price maths. price * amount / 1e18 assumes both sides share decimals. Normalise to a fixed internal precision (1e18) on entry, convert back only at the boundary.
  • Interface encoding. parseUnits(str, 6) versus parseEther(str) — parseEther is parseUnits(_, 18) and is wrong for anything else.
  • Display. formatUnits(v, d); a hardcoded formatEther shows 1e12-scale errors.
  • Rounding direction. Fee maths must round in the protocol’s favour, not truncate blindly: amount * fee / 1e4 truncates to zero for small amounts.

Non-standard tokens

Behaviour Handling
decimals() reverts try/catch, default 18, log
Returns > 36 clamp and warn
Fee-on-transfer (balance changes on transfer) measure balance before/after, credit the delta
Rebasing never cache balances across blocks

Fee-on-transfer is the one that silently breaks accounting, not just amounts. Any deposit path must compare balanceOf before and after and use the difference.

Test fixture

Fork a real mainnet block and run the deposit path against USDC, WBTC and DAI. Unit tests with a mock 18-decimal token pass while the real 6-decimal token reverts — that gap is exactly what mocks hide.

The rule

Treat decimals as external input on par with a user address: read it, bound it, derive every amount from it. A contract that mentions 1e18 more than it mentions decimals() has a bug waiting.

Operating notes

Centralise the conversion. One helper pair — toUnits(amount, token) and fromUnits(units, token) — and no raw 1e18 anywhere else. Grep for 1e18 in review; each hit outside the helper is a candidate bug.

1
2
3
4
5
function toUnits(uint human, address token) internal view returns (uint) {
uint8 d = IERC20(token).decimals();
require(d <= 36, "decimals");
return human * 10 ** d;
}

Bound the input too. A user-supplied amount scaled by 36 decimals overflows a uint256 above roughly 10^41; check the multiplication.

Fee-on-transfer accounting

Compare balanceOf before and after every deposit and credit the delta, not the argument. A 5% tax token silently breaks a ledger built on the argument value, and the discrepancy compounds with each deposit until a withdrawal underflows.

1
2
3
uint before = IERC20(t).balanceOf(address(this));
IERC20(t).safeTransferFrom(msg.sender, address(this), amount);
uint received = IERC20(t).balanceOf(address(this)) - before;

Rebasing tokens

Never cache a balance across blocks. Store shares against a stored totalShares and read the live balance on each interaction, or the accounting drifts every rebase.

Tests that catch it

Fork mainnet and run the deposit path against USDC, WBTC and a known tax token on three separate forks. A mock token with 18 decimals passes everything a real 6-decimal token reverts on — the mock is the trap, not the test.

FAQ

Why does hardcoding 1e18 lose funds?

decimals() is attacker-controlled and not guaranteed to be 18. Code that hardcodes 1e18 over-sends by up to 10^12 on USDC and 10^6 on WBTC. Always read decimals per token and compute amounts from it.

How do I compute a token amount correctly?

Read the token’s decimals and derive the amount from it: uint8 d = IERC20(t).decimals(); uint amount = 100 * 10 ** d;. Cache d per token in storage, since decimals() is a staticcall per use otherwise. Sanity-bound it to 0–36 and revert outside that range.

What should I do if decimals() reverts or returns a huge number?

Wrap the call in try/catch, default to 18, and log the failure. If it returns a value above 36, clamp and warn. A token returning 255 is either broken or hostile, so bound the input rather than trusting it.

Why do my unit tests pass but the real token reverts?

A mock token with 18 decimals passes everything a real 6-decimal token reverts on — the mock is the trap, not the test. Fork a real mainnet block and run the deposit path against USDC, WBTC and DAI. That gap is exactly what mocks hide.

How do I handle fee-on-transfer tokens in accounting?

Compare balanceOf before and after every deposit and credit the delta, not the argument. A 5% tax token silently breaks a ledger built on the argument value, and the discrepancy compounds with each deposit until a withdrawal underflows.