Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: API for AMM trade price #5007

Open
mDuo13 opened this issue Apr 30, 2024 · 2 comments
Open

Feature: API for AMM trade price #5007

mDuo13 opened this issue Apr 30, 2024 · 2 comments
Labels
API Change Feature Request Used to indicate requests to add new features Will Need Documentation

Comments

@mDuo13
Copy link
Collaborator

mDuo13 commented Apr 30, 2024

Summary

The price of a trade that executes against an AMM is tricky to calculate and relies on the state of the ledger. It would be great to be able to quickly query the API to see how much a proposed trade might cost, or at least get the spot price for an AMM.

This could be a new API method or extend an existing, related method such as amm_info or path_find.

Motivation

Advanced trading, such as using an AMM's auction slot to save money, involves being able to predict the outcome of various possible trades, but the various AMM formulas are tricky to replicate especially with the exact precision that the XRPL uses. It would help integration to be able to query some of this directly from the XRPL server, which already has all the requisite information and formulas implemented.

Currently you can use path_find to estimate the cost of a circular cross-currency Payment that may use an AMM, but you can't use it to compare theoreticals like "what if I bought the auction slot first?" or "What if the trading fee were voted higher/lower?" If someone else currently holds the auction slot (non-expired) and has trust lines for the appropriate in/out currencies, you might be able to do a comparison with sending from their account, but that's not reliable.

Currently there is also no API or tool to estimate the proceeds of a single-asset withdrawal or the necessary input to receive a specific amount of LP Tokens.

Solution

Add new API method(s) or feature(s) to return:

  • The spot price an AMM is offering in either direction based on the state of a given ledger. (This could be added to amm_info)
  • The predicted cost or proceeds of using an AMM to execute a swap with and without the discount of the auction slot, including:
    • a swap "through" (in asset → out asset)
    • a single-asset withdrawal (LP Token → out asset)
    • a single-asset deposit (in asset → LP Token).

It could also be useful to add an option to show synthetic offers from AMM and autobridging to book_offers.

Paths Not Taken

These could be implemented in client libraries or Clio as well. However, it's especially helpful to have them in rippled for these reasons:

  • The calculations rely on data from the ledger on the state of the AMM, preferably as up-to-date as possible. Use cases like this with trading often want to use pending (not validated) ledgers, which Clio doesn't have access to
  • The precision and rounding are often relevant, and the server has the canonical implementations of those. Re-creating that logic in client libraries could lead to mismatches.
  • Required inputs, in the form of the current AMM state/balances, need to be queried from the API already. It would be efficient to provide some of these types of calculations in the same response.
@mDuo13 mDuo13 added API Change Feature Request Used to indicate requests to add new features Will Need Documentation labels Apr 30, 2024
@intelliot
Copy link
Collaborator

In addition to other solutions, the upcoming simulate method should also make it straightforward to calculate these prices.

@mvadari
Copy link
Collaborator

mvadari commented Aug 2, 2024

Ideally there would still be an API, though - simulate feels like a somewhat hacky way to get this value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Change Feature Request Used to indicate requests to add new features Will Need Documentation
Projects
None yet
Development

No branches or pull requests

3 participants