Page cover
githubEdit

Swap

A swap is the process of exchanging one Stellar asset for another using liquidity from Soroban-based Automated Market Makers (AMMs). Swaps can be executed directly through the Soroswap AMM arrow-up-rightor routed via the Aggregator,arrow-up-right which optimally resolves the operation by finding the best available price at the time through an intelligent distribution across supported AMMs. πŸ‘‰πŸ½ How to Swaparrow-up-right Follow the Swap Tutorialarrow-up-right

How does a swap work?

User selects two assets – one to sell and one to receive.

  1. Input amount is defined – the app estimates the output based on current pool conditions.

  2. Router SDK scans available pools – if the Aggregator is enabled, it calculates the best distribution DexDistribution.

  3. User confirms the swap – the smart contract receives the input asset and performs the swap.

  4. User receives the new asset – if it's the first time, a trustline must be signed to accept the asset.

πŸ“ Constant Product Formula

Each AMM in Soroswap follows this formula:

Where:

  • x = balance of asset A

  • y = balance of asset B

  • k = constant product (remains unchanged)

When someone swaps A for B:

  • A is deposited into the pool

  • B is withdrawn

  • The price updates to maintain the balance

This model ensures continuous liquidity.

πŸ”œ Next: Feesarrow-up-right πŸ”™ Previous: Liquidity Poolsarrow-up-right

Last updated