Repository navigation
fix(ui): treat an empty amount as zero when building quote parameters - #1158
devknight-ink wants to merge 1 commit into
Conversation
|
@peteobrien is attempting to deploy a commit to the Uneven Labs Team on Vercel. A member of the Team first needs to authorize it. |
[Medium impact] The PR appears safe to merge; the earlier request for parser tests remains non-blocking. Findings
|
| function parseUnitsOrZero( | ||
| value: string | number | null | undefined, | ||
| decimals: number | ||
| ): bigint { | ||
| const normalized = `${value ?? ''}`.trim() | ||
| if (/^[-.]*$/.test(normalized)) { | ||
| return 0n | ||
| } | ||
| return parseUnits(normalized, decimals) |
There was a problem hiding this comment.
New parsing behavior lacks tests
parseUnitsOrZero distinguishes empty and mid-edit values from malformed input, but the PR adds no automated tests for that boundary. Cover '', whitespace, '.', '-', null, and undefined returning 0n; valid amounts keeping their units; and 'abc' and '1e3' still throwing. This is non-blocking, but those checks would catch changes that restore the render crash or silently accept bad input.
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
10d7a70 to
acc0b35
Compare
Problem
SwapWidgetRenderer,DepositAddressModalRendererandOnrampModalbuild their quote parameters eagerly on every render and call viem'sparseUnits()on the raw (debounced) amount input. That value is''on first mount (when nodefaultAmountis set), after the user clears the field, and after every completed swap (setAmountInputValue('')).viem ≤ 2.55.2 returned
0nforparseUnits(''). From viem 2.55.4 (utils/unit/Value.ts,from()), any value without a digit —'','.','-'— throwsValue.InvalidDecimalNumberError. Nothing catches it in the render path, so React unmounts the whole tree:The repo's own lockfile resolves viem 2.52.2, which is why the demo does not show it. Integrators on a current viem (
>=2.26.0is the declared peer range) see the widget crash on load or immediately after a swap.Reproduction: mount
<SwapWidget />with viem 2.57.x installed, delete the0in the amount field (or complete a swap).Fix
parseUnitsOrZero(value, decimals)toutils/numbers.ts: returns0nwhen the trimmed value consists only of-/./nothing (empty or mid-edit input), otherwise defers toparseUnits, so genuinely malformed input still throws as before.Verification
pnpm run typecheck(packages/ui),eslinton the changed files andscripts/lint-changesets.mjspass.parseUnitsparseUnitsOrZero'','.','-',undefined0n'0.','0.01','12.5',5'abc','1e3'Changeset included (patch,
@relayprotocol/relay-kit-ui).