Skip to content

logic: Scaffold the reverse exact-output swap math #75

@AlAfiz

Description

@AlAfiz

Description

Currently, our swap function only supports "Exact Input" (e.g., I want to sell exactly 100 XLM and get whatever USDC it's worth).
Advanced traders and smart contracts often need "Exact Output" routing (e.g., I need exactly 50 USDC, tell me how much XLM to charge me).
We need to scaffold the mathematical formula for this reverse calculation using the constant product formula.
This is purely a mathematical addition for this issue and won't be hooked into the transfer logic yet.

Requirements

  • Create a helper function calculate_amount_in(amount_out, reserve_in, reserve_out).
  • Implement the math: numerator = reserve_in * amount_out * 1000 and denominator = (reserve_out - amount_out) * 997.
  • Ensure the function rounds up to guarantee the pool is never shortchanged by precision loss.
  • Return the calculated amount_in required to execute the trade.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions