Skip to content

Add "Trade" view for trading tokens #31

@0x-r4bbit

Description

@0x-r4bbit

Create a Swap view in the DEX app that allows users to trade one token for another through an existing AMM pool. Inspired by the
https://app.uniswap.org/swap.

The AMM program exposes a Swap instruction that executes a constant-product trade against an active pool:

Swap {
    swap_amount_in: u128,
    min_amount_out: u128,             // slippage guard
    token_definition_id_in: AccountId,
}

Required accounts: the AMM pool, vault A, vault B, the user's holding account for Token A, and the user's holding account for Token B (whichever is the input side must be authorized). The output amount is calculated against the pool's reserve_a / reserve_b using the
constant-product formula, minus the pool's fee tier.

UX Flow

  1. Select input token and amount — user picks the token they want to sell and enters an amount; available balance is shown beneath the
    field
  2. Select output token — user picks the token they want to receive; the expected output amount is calculated and displayed automatically
  3. Swap direction toggle — an arrow/swap button between the two fields reverses the direction without clearing the selected tokens
  4. Route / pool indicator — shows which pool will be used (token pair + fee tier), and that only direct single-hop swaps are supported for
    now
  5. Swap details panel — shown before confirmation, displaying:
  • Exchange rate (e.g. 1 TOKEN_A = X TOKEN_B)
  • Fee tier of the pool (e.g. 0.30%)
  • Price impact (estimated % change against current reserves)
  • Minimum received — output amount after applying the user's slippage tolerance
  1. Slippage tolerance setting — user-configurable (e.g. 0.1%, 0.5%, 1.0%, or custom); used to compute min_amount_out
  2. Submit — dispatches the Swap instruction; button disabled when input exceeds balance, pool does not exist/is inactive, or amounts are zero

Technical Notes

  • Output amount is estimated client-side using the constant-product formula, accounting for the pool fee:
    amount_in_with_fee = swap_amount_in * (FEE_BPS_DENOMINATOR - fees)
    amount_out = (reserve_out * amount_in_with_fee) / (reserve_in * FEE_BPS_DENOMINATOR + amount_in_with_fee)
  • min_amount_out passed to the instruction is amount_out * (1 - slippage_tolerance)
  • The pool is looked up by computing its PDA from the token pair using compute_pool_pda; if it doesn't exist or active (see active flag in PoolDefinition is redundant after MINIMUM_LIQUIDITY lock #25), the swap UI
    should show a "no liquidity" state
  • token_definition_id_in identifies which side of the pair is being sold; the program uses this to determine swap direction
  • Price impact should be surfaced as a warning when it exceeds a threshold (e.g. >1% warn, >5% error/block)

Acceptance Criteria

  • User can select an input token, output token, and input amount
  • Expected output amount updates in real time as the user types
  • Swap direction can be reversed with a single click
  • Fee tier, exchange rate, price impact, and minimum received are displayed before submission
  • Slippage tolerance is user-configurable and correctly sets min_amount_out
  • High price impact is surfaced as a visible warning
  • The submit button is disabled when the pool is missing/inactive, input exceeds balance, or amounts are zero
  • Submitting dispatches the correct Swap instruction with the right accounts and parameters

References

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions