fix(payout): reject malformed amounts and drop float conversion (#41) - #43
Open
devorun wants to merge 1 commit into
Open
fix(payout): reject malformed amounts and drop float conversion (#41)#43devorun wants to merge 1 commit into
devorun wants to merge 1 commit into
Conversation
The payout endpoint parsed amounts with parseFloat, which accepts a valid numeric prefix and ignores the rest, so "1USDC" became 1 and "1.5abc" became 1.5 -- passing validation and moving a value the request body never wrote. The parseFloat * 1_000_000 + Math.floor conversion also silently truncated anything beyond six decimals. Replace it with strict parsing: lift the existing (previously private) parseUsdcAmountToAtomicUnits / formatUsdcAtomicUnits helpers out of unified-balance-payout.ts into a shared lib/circle/usdc-amount.ts and use them in the payout route. Malformed, negative, empty, or over-precise amounts now return 400 Invalid amount, and every amount handed to the App Kit / Gateway SDK is derived from the validated atomic units instead of a re-parsed float. Fixes circlefin#41
devorun
force-pushed
the
fix/payout-strict-amount-parsing
branch
from
August 6, 2026 18:44
eaca7df to
164d2ab
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #41.
The payout endpoint parsed user-supplied amounts with
parseFloat, which accepts a valid numeric prefix and ignores the rest — so"1USDC"became1and"1.5abc"became1.5, passing validation and moving a value the request body never wrote. The sameparseFloat(...) * 1_000_000+Math.floorconversion also silently truncated anything beyond six decimals.Changes
lib/circle/usdc-amount.ts(new): strictparseUsdcAmountToAtomicUnits/formatUsdcAtomicUnitshelpers. This logic already existed privately inlib/circle/unified-balance-payout.ts; it is lifted into a shared module so there is a single implementation instead of two copies.app/api/payout/route.ts: replace theparseFloat-basedconvertToSmallestUnitand theparseFloatamount check with strict parsing. Malformed input ("1USDC","1.5abc","1e6", more than six decimals, empty/negative) now returns400 Invalid amountinstead of silently proceeding. Every amount passed downstream to the App Kit / Gateway SDK is derived from the validated atomic units, so no re-parsed float can drift from the validated value.lib/circle/unified-balance-payout.ts: consume the shared helpers (identical logic, behavior unchanged).Tests
tests/usdc-amount.test.tscovers the reported cases ("1USDC","1.5abc"), excess-decimal rejection, whitespace tolerance, signed/empty input, and format round-tripping.npx vitest runpasses except for a pre-existing, unrelated failure intests/spend-arc-gateway-usdc.test.ts(missing../scripts/spend-arc-gateway-usdc.mjs), which fails identically onmaster.tsc --noEmitreports no new errors.Note (out of scope)
app/api/bridge/rebalance/route.tsuses the sameparseFloat/amountNum.toString()pattern and would benefit from the shared helper too. Left out to keep this PR focused on #41.