-
Notifications
You must be signed in to change notification settings - Fork 33
logic: Scaffold the reverse exact-output swap math #75
Copy link
Copy link
Open
Description
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 * 1000anddenominator = (reserve_out - amount_out) * 997. - Ensure the function rounds up to guarantee the pool is never shortchanged by precision loss.
- Return the calculated
amount_inrequired to execute the trade.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels