Skip to content

fix(ui): treat an empty amount as zero when building quote parameters - #1158

Closed
devknight-ink wants to merge 1 commit into
relayprotocol:mainfrom
devknight-ink:fix/ui-empty-amount-parse-units
Closed

devknight-ink wants to merge 1 commit into
relayprotocol:mainfrom
devknight-ink:fix/ui-empty-amount-parse-units

Conversation

@devknight-ink

Copy link
Copy Markdown

Problem

SwapWidgetRenderer, DepositAddressModalRenderer and OnrampModal build their quote parameters eagerly on every render and call viem's parseUnits() on the raw (debounced) amount input. That value is '' on first mount (when no defaultAmount is set), after the user clears the field, and after every completed swap (setAmountInputValue('')).

viem ≤ 2.55.2 returned 0n for parseUnits(''). From viem 2.55.4 (utils/unit/Value.ts, from()), any value without a digit — '', '.', '-' — throws Value.InvalidDecimalNumberError. Nothing catches it in the render path, so React unmounts the whole tree:

Value.InvalidDecimalNumberError: Value '' is not a valid decimal number

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.0 is 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 the 0 in the amount field (or complete a swap).

Fix

  • Add parseUnitsOrZero(value, decimals) to utils/numbers.ts: returns 0n when the trimmed value consists only of -/./nothing (empty or mid-edit input), otherwise defers to parseUnits, so genuinely malformed input still throws as before.
  • Use it at the three quote-parameter call sites. The quote hooks were already disabled for a zero amount, so request behaviour is unchanged.

Verification

  • pnpm run typecheck (packages/ui), eslint on the changed files and scripts/lint-changesets.mjs pass.
  • Behaviour matrix against viem 2.57.10:
input parseUnits parseUnitsOrZero
'', '.', '-', undefined throws 0n
'0.', '0.01', '12.5', 5 ok same result
'abc', '1e3' throws still throws

Changeset included (patch, @relayprotocol/relay-kit-ui).

@vercel

vercel Bot commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

@peteobrien is attempting to deploy a commit to the Uneven Labs Team on Vercel.

A member of the Team first needs to authorize it.

@greptile-apps

greptile-apps Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; the earlier request for parser tests remains non-blocking.

Findings

  1. P2 New parsing behavior lacks tests ▶

Summary

Adds parseUnitsOrZero so empty amount fields no longer throw while quote parameters are built.

  • Empty amounts no longer break quote building across three flows.

Reviews (2) · Last reviewed commit: "fix(ui): treat an empty amount as zero w..." · Reviewed by Greptile

Comment on lines +375 to +383
function parseUnitsOrZero(
value: string | number | null | undefined,
decimals: number
): bigint {
const normalized = `${value ?? ''}`.trim()
if (/^[-.]*$/.test(normalized)) {
return 0n
}
return parseUnits(normalized, decimals)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 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!

@devknight-ink
devknight-ink force-pushed the fix/ui-empty-amount-parse-units branch from 10d7a70 to acc0b35 Compare October 23, 2026 13:25
@devknight-ink
devknight-ink deleted the fix/ui-empty-amount-parse-units branch October 23, 2026 13:25
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