3#include <xrpl/protocol/Quality.h>
4#include <xrpl/tx/transactors/dex/AMMHelpers.h>
6#include <boost/regex.hpp>
37 str = boost::regex_replace(str, boost::regex(
"^(A|O)[(]"),
"");
40 boost::regex
const rx(
"^([^(]+)[(]([^)]+)[)]([)])?$");
41 if (boost::regex_search(str,
match, rx))
43 if (delimited !=
nullptr)
44 *delimited = (
match[3] !=
"");
45 if (
match[1] ==
"XRP")
50 if (
match[1] ==
"XRPA")
65 str = boost::regex_replace(str, boost::regex(
"^T[(]"),
"");
68 boost::regex
const rx(
"^([^(]+)[(]([^)]+)[)]([)])?$");
69 if (boost::regex_search(str,
match, rx))
75 return {{currency,
rate,
match[3] !=
""}};
97 bool const amm = s[0] !=
'O';
98 auto const a1 =
getAmt(p++);
101 auto const a2 =
getAmt(p++);
104 return {{{*a1, *a2}, amm}};
140 auto isPair = [](
auto const& p) {
142 return s[0] ==
'A' || s[0] ==
'O';
148 if (!res || p ==
end_)
153 auto const swap =
getAmt(p++);
161 return {{pairs, *swap, *
rate,
fee}};
192 int limitingStep = vp.size();
195 auto const currency =
to_string(amt.issue().currency);
196 return rates.find(currency) != rates.end() ? rates.at(currency) : QUALITY_ONE;
200 for (
auto it = vp.rbegin(); it != vp.rend(); ++it)
203 auto const [amts, amm] = *it;
209 else if (sout <= amts.out)
211 sin = Quality{amts}.ceil_out(amts, sout).in;
217 limitingStep = vp.rend() - it - 1;
219 if (it == vp.rbegin())
220 resultOut = amts.out;
226 for (
int i = limitingStep + 1; i < vp.size(); ++i)
228 auto const [amts, amm] = vp[i];
237 sout = Quality{amts}.ceil_in(amts, sin).out;
255 int limitingStep = 0;
258 auto const currency =
to_string(amt.issue().currency);
259 return rates.find(currency) != rates.end() ? rates.at(currency) : QUALITY_ONE;
262 for (
auto it = vp.begin(); it != vp.end(); ++it)
264 auto const [amts, amm] = *it;
272 else if (sin <= amts.in)
274 sout = Quality{amts}.ceil_in(amts, sin).out;
281 limitingStep = it - vp.begin();
289 for (
int i = limitingStep - 1; i >= 0; --i)
292 auto const [amts, amm] = vp[i];
300 sin = Quality{amts}.ceil_out(amts, sout).in;
312 auto const a =
arg();
313 boost::regex
const re(
",");
331 auto const exec = [&]() ->
bool {
346 if (
auto const swap =
getSwap(++p); swap)
361 else if (*p ==
"swapout")
363 if (
auto const swap =
getSwap(++p); swap)
373 else if (*p ==
"lptokens")
378 auto const LPT = amm[
"LPT"];
392 else if (*p ==
"changespq")
394 Env const env(*
this);
402 Quality{offer->first},
410 <<
"\nnew pool: " <<
toString(pool->first.in + ammOffer->in)
411 <<
" " <<
toString(pool->first.out - ammOffer->out)
437BEAST_DEFINE_TESTSUITE_MANUAL(AMMCalc, app,
xrpl);
A generic endpoint for log messages.
static Sink & getNullSink()
Returns a Sink which does nothing.
std::string const & arg() const
Return the argument associated with the runner.
Issue const & issue() const
std::string getText() const override
bool native() const noexcept
std::optional< std::tuple< std::string, std::uint32_t, bool > > getRate(token_iter const &p)
static STAmount mulratio(STAmount const &amt, std::uint32_t a, std::uint32_t b, bool round)
std::optional< std::pair< Amounts, bool > > getAmounts(token_iter &p)
static void swapIn(swapargs const &args)
boost::sregex_token_iterator token_iter
static void swapOut(swapargs const &args)
static std::string toString(STAmount const &a)
std::optional< STAmount > getAmt(token_iter const &p, bool *delimited=nullptr)
void run() override
Runs the suite.
std::optional< swapargs > getSwap(token_iter &p)
std::uint32_t getFee(token_iter const &p)
std::optional< transfer_rates > getTransferRate(token_iter &p)
Immutable cryptographic account descriptor.
A transaction testing environment.
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Json::Value rate(Account const &account, double multiplier)
Set a transfer rate.
XRP_t const XRP
Converts to XRP Issue or STAmount.
Json::Value offer(Account const &account, STAmount const &takerPays, STAmount const &takerGets, std::uint32_t flags)
Create an offer.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
static void limitStepOut(Offer const &offer, TAmounts< TIn, TOut > &ofrAmt, TAmounts< TIn, TOut > &stpAmt, TOut &ownerGives, std::uint32_t transferRateIn, std::uint32_t transferRateOut, TOut const &limit)
std::string to_string(base_uint< Bits, Tag > const &a)
STAmount toSTAmount(IOUAmount const &iou, Issue const &iss)
STAmount amountFromString(Asset const &asset, std::string const &amount)
TOut swapAssetIn(TAmounts< TIn, TOut > const &pool, TIn const &assetIn, std::uint16_t tfee)
AMM pool invariant - the product (A * B) after swap in/out has to remain at least the same: (A + in) ...
static void limitStepIn(Offer const &offer, TAmounts< TIn, TOut > &ofrAmt, TAmounts< TIn, TOut > &stpAmt, TOut &ownerGives, std::uint32_t transferRateIn, std::uint32_t transferRateOut, TIn const &limit)
STAmount ammLPTokens(STAmount const &asset1, STAmount const &asset2, Issue const &lptIssue)
Calculate LP Tokens given AMM pool reserves.
IOUAmount mulRatio(IOUAmount const &amt, std::uint32_t num, std::uint32_t den, bool roundUp)
Rate transferRate(ReadView const &view, AccountID const &issuer)
Returns IOU issuer transfer fee as Rate.
std::optional< TAmounts< TIn, TOut > > changeSpotPriceQuality(TAmounts< TIn, TOut > const &pool, Quality const &quality, std::uint16_t tfee, Rules const &rules, beast::Journal j)
Generate AMM offer so that either updated Spot Price Quality (SPQ) is equal to LOB quality (in this c...
TIn swapAssetOut(TAmounts< TIn, TOut > const &pool, TOut const &assetOut, std::uint16_t tfee)
Swap assetOut out of the pool and swap in a proportional amount of the other asset.