XRPLF / rippled

Decentralized cryptocurrency blockchain daemon implementing the XRP Ledger protocol in C++
https://xrpl.org
ISC License
4.48k stars 1.44k forks source link

Feature: API for AMM trade price #5007

Open mDuo13 opened 1 month ago

mDuo13 commented 1 month ago

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:

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: