Skip to content

Swap Input/Output Fields & Exact-Input/Output Mode Toggle #56

@0x-r4bbit

Description

@0x-r4bbit

The core input area of the swap card. The user can either enter how much they want to spend (SwapExactInput) or how much they want to receive (SwapExactOutput). The opposite field is always read-only and computed from the pool math.

Swap math

Exact input (selling amount_in of Token A for Token B):

amount_in_with_fee = amount_in × (10_000 − fees)
amount_out = (reserve_b × amount_in_with_fee) / (reserve_a × 10_000 + amount_in_with_fee)

Exact output (buying exact_out of Token B):

amount_in = (reserve_a × exact_out × 10_000) / ((reserve_b − exact_out) × (10_000 − fees)) + 1

Fee constant: fees = 30, FEE_BPS_DENOMINATOR = 10_000

Requirements

  • Two amount fields: "You pay" (top) and "You receive" (bottom)
  • Clicking into either field sets the mode to exact-input or exact-output respectively
  • The non-active field updates in real time as the user types
  • Input is validated: cannot exceed user balance; cannot exceed pool reserve on the output side
  • Display the input token's user balance; clicking "MAX" fills the input field with the full balance

Acceptance criteria

  • Typing in "You pay" triggers exact-input calculation and updates "You receive"
  • Typing in "You receive" triggers exact-output calculation and updates "You pay"
  • MAX button fills input and computes output correctly
  • Amounts above user balance or pool reserve are flagged with an error state
  • Fields recompute on swap direction flip (from token pair selector)

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions