Skip to content

fix(amount): round USDC to whole base units so the on-chain amount is an integer - #45

Open
Bornoz wants to merge 1 commit into
circlefin:masterfrom
Bornoz:fix/usdc-amount-rounding
Open

fix(amount): round USDC to whole base units so the on-chain amount is an integer#45
Bornoz wants to merge 1 commit into
circlefin:masterfrom
Bornoz:fix/usdc-amount-rounding

Conversation

@Bornoz

@Bornoz Bornoz commented Sep 1, 2026

Copy link
Copy Markdown

convertUSDCToContractAmount turns a USDC amount into 6-decimal base units for the uint256 that pay(address,uint256,address) takes, but it scales in floating point: (amount * 1000000).toString(). Ordinary amounts land just under a whole unit — 2.01 becomes "2009999.9999999998", 2.05 "2049999.9999999998", 1.005 "1004999.9999999999" — and .toString() carries the fraction straight through. Around 3% of cent-precision amounts are affected.

Both deposit routes then do Number(convertUSDCToContractAmount(...)) and pass the result as the uint256 amount to the escrow pay() call (app/api/contracts/escrow/deposit/route.ts, .../deposit/approve/route.ts), so a plain $2.01 deposit goes on-chain as a non-integer amount — rejected by the encoder, or truncated a base unit short.

Rounding to the nearest base unit is the correct fixed-point conversion and restores the integer the function is meant to produce. Verified against the current values: 2.01, 2.05, 4.02, 1.005 return fractional strings before the change and the exact base-unit integers after; tsc --noEmit and prettier --check stay clean.

Separate from #44, which fixes parseAmount.

… an integer

convertUSDCToContractAmount scales a USDC amount to 6-decimal base units for the
uint256 that pay(address,uint256,address) takes, but it does it in floating
point: (amount * 1000000).toString(). Ordinary amounts land just under a whole
unit — 2.01 becomes "2009999.9999999998", 1.005 becomes "1004999.9999999999" —
and toString carries the fraction straight through. Both deposit routes pass the
result as the uint256 amount, so a plain 2.01 deposit goes on-chain as a
non-integer: rejected by the encoder, or truncated a base unit short.

Round to the nearest base unit, which is the integer the function is meant to
produce.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant