3#include <xrpld/app/misc/AMMHelpers.h>
5#include <xrpl/protocol/Quality.h>
7#include <boost/regex.hpp>
38 str = boost::regex_replace(str, boost::regex(
"^(A|O)[(]"),
"");
41 boost::regex rx(
"^([^(]+)[(]([^)]+)[)]([)])?$");
42 if (boost::regex_search(str,
match, rx))
45 *delimited = (
match[3] !=
"");
46 if (
match[1] ==
"XRP")
49 else if (
match[1] ==
"XRPA")
62 str = boost::regex_replace(str, boost::regex(
"^T[(]"),
"");
65 boost::regex rx(
"^([^(]+)[(]([^)]+)[)]([)])?$");
66 if (boost::regex_search(str,
match, rx))
72 return {{currency,
rate,
match[3] !=
"" ? true :
false}};
94 bool const amm = s[0] ==
'O' ? false :
true;
95 auto const a1 =
getAmt(p++);
98 auto const a2 =
getAmt(p++);
101 return {{{*a1, *a2}, amm}};
118 auto const [currency, trate, delimited] = *
rate;
119 rates[currency] = trate;
135 auto isPair = [](
auto const& p) {
137 return s[0] ==
'A' || s[0] ==
'O';
143 if (!res || p ==
end_)
148 auto const swap =
getAmt(p++);
156 return {{pairs, *swap, *
rate,
fee}};
187 int limitingStep = vp.size();
189 auto trate = [&](
auto const& amt) {
190 auto const currency =
to_string(amt.issue().currency);
191 return rates.find(currency) != rates.end() ? rates.at(currency)
196 for (
auto it = vp.rbegin(); it != vp.rend(); ++it)
198 sout =
mulratio(sin, trate(sin), QUALITY_ONE,
true);
199 auto const [amts, amm] = *it;
205 else if (sout <= amts.out)
207 sin = Quality{amts}.ceil_out(amts, sout).in;
213 limitingStep = vp.rend() - it - 1;
215 if (it == vp.rbegin())
216 resultOut = amts.out;
222 for (
int i = limitingStep + 1; i < vp.size(); ++i)
224 auto const [amts, amm] = vp[i];
225 sin =
mulratio(sin, QUALITY_ONE, trate(sin),
false);
233 sout = Quality{amts}.ceil_in(amts, sin).out;
252 int limitingStep = 0;
254 auto trate = [&](
auto const& amt) {
255 auto const currency =
to_string(amt.issue().currency);
256 return rates.find(currency) != rates.end() ? rates.at(currency)
260 for (
auto it = vp.begin(); it != vp.end(); ++it)
262 auto const [amts, amm] = *it;
273 else if (sin <= amts.in)
275 sout = Quality{amts}.ceil_in(amts, sin).out;
282 limitingStep = it - vp.begin();
290 for (
int i = limitingStep - 1; i >= 0; --i)
292 sout =
mulratio(sin, trate(sin), QUALITY_ONE,
false);
293 auto const [amts, amm] = vp[i];
301 sin = Quality{amts}.ceil_out(amts, sout).in;
305 resultOut =
mulratio(resultOut, QUALITY_ONE, trate(resultOut),
true);
314 auto const a =
arg();
315 boost::regex re(
",");
333 auto const exec = [&]() ->
bool {
348 if (
auto const swap =
getSwap(++p); swap)
363 else if (*p ==
"swapout")
365 if (
auto const swap =
getSwap(++p); swap)
375 else if (*p ==
"lptokens")
380 auto const LPT = amm[
"LPT"];
397 else if (*p ==
"changespq")
407 Quality{offer->first},
413 <<
"amm offer: " <<
toString(ammOffer->in)
416 <<
toString(pool->first.in + ammOffer->in)
418 <<
toString(pool->first.out - ammOffer->out)
421 std::cout <<
"can't change the pool's SP quality"
442BEAST_DEFINE_TESTSUITE_MANUAL(AMMCalc, app,
ripple);
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.
std::string getText() const override
Issue const & issue() const
bool native() const noexcept
std::optional< std::pair< Amounts, bool > > getAmounts(token_iter &p)
void swapOut(swapargs const &args)
void swapIn(swapargs const &args)
std::optional< trates > getTransferRate(token_iter &p)
std::optional< std::tuple< std::string, std::uint32_t, bool > > getRate(token_iter const &p)
boost::sregex_token_iterator token_iter
STAmount mulratio(STAmount const &amt, std::uint32_t a, std::uint32_t b, bool round)
std::uint32_t getFee(token_iter const &p)
std::optional< STAmount > getAmt(token_iter const &p, bool *delimited=nullptr)
std::string toString(STAmount const &a)
std::optional< swapargs > getSwap(token_iter &p)
void run() override
Runs the suite.
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.
Json::Value offer(Account const &account, STAmount const &takerPays, STAmount const &takerGets, std::uint32_t flags)
Create an offer.
XRP_t const XRP
Converts to XRP Issue or STAmount.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
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)
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) ...
STAmount toSTAmount(IOUAmount const &iou, Issue const &iss)
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...
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)
IOUAmount mulRatio(IOUAmount const &amt, std::uint32_t num, std::uint32_t den, bool roundUp)
STAmount ammLPTokens(STAmount const &asset1, STAmount const &asset2, Issue const &lptIssue)
Calculate LP Tokens given AMM pool reserves.
std::string to_string(base_uint< Bits, Tag > const &a)
STAmount amountFromString(Asset const &asset, std::string const &amount)
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.