Skip to content

[Frontend] Consolidate duplicate amount helpers in lib/amount.ts and utils/amount.ts #520

Description

@ogazboiz

Telegram: https://t.me/+DOylgFv1jyJlNzM0

Why this matters

There are two parallel amount-formatting modules with overlapping function names but different signatures, and different parts of the app import from each. This is an active bug-magnet: a developer copying a call from one file to another can silently pass the wrong arguments.

Verified divergence

Both frontend/src/lib/amount.ts and frontend/src/utils/amount.ts export formatAmount, parseAmount, formatRate, hasValidPrecision, toStroops, fromStroops — but the signatures differ:

  • lib/amount.ts: fromStroops(stroops) (1 arg), toStroops(value) (1 arg, assumes 7 decimals).
  • utils/amount.ts: fromStroops(amount, decimals) (2 args), toStroops(amount, decimals) (2 args).

Import split (verified via grep):

  • Import from @/lib/amount: app/activity/page.tsx, components/IncomingStreams.tsx, components/stream-creation/{AmountStep,StreamCreationWizard,ScheduleStep}.tsx, components/dashboard/ActivityHistory.tsx.
  • Import from @/utils/amount: app/streams/[id]/page.tsx, app/streams/create/page.tsx, components/NotificationDropdown.tsx, components/stream-creation/TopUpModal.tsx.

utils/amount.ts is the richer one (token-decimals cache, async fetchTokenDecimals, validateAmountInput) and is the one covered by __tests__/utils.test.ts.

Acceptance criteria

  • Pick one canonical module (suggest utils/amount.ts, since it's tested and fuller) and migrate all imports to it.
  • Reconcile the differing fromStroops/toStroops arities at call sites.
  • Remove the duplicate module (or re-export from it for back-compat).
  • Tests and build pass.

Files to touch

  • frontend/src/lib/amount.ts
  • frontend/src/utils/amount.ts
  • All importers listed above.

Out of scope

  • Changing displayed precision/format behavior.

Metadata

Metadata

Labels

Stellar WaveIssues in the Stellar wave programfrontendFrontend related tasksrefactorRefactoring existing code

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions