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}};
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();
190 auto const currency =
to_string(amt.issue().currency);
191 return rates.find(currency) != rates.end() ? rates.at(currency) : QUALITY_ONE;
195 for (
auto it = vp.rbegin(); it != vp.rend(); ++it)
198 auto const [amts, amm] = *it;
204 else if (sout <= amts.out)
206 sin = Quality{amts}.ceil_out(amts, sout).in;
212 limitingStep = vp.rend() - it - 1;
214 if (it == vp.rbegin())
215 resultOut = amts.out;
221 for (
int i = limitingStep + 1; i < vp.size(); ++i)
223 auto const [amts, amm] = vp[i];
232 sout = Quality{amts}.ceil_in(amts, sin).out;
250 int limitingStep = 0;
253 auto const currency =
to_string(amt.issue().currency);
254 return rates.find(currency) != rates.end() ? rates.at(currency) : QUALITY_ONE;
257 for (
auto it = vp.begin(); it != vp.end(); ++it)
259 auto const [amts, amm] = *it;
267 else if (sin <= amts.in)
269 sout = Quality{amts}.ceil_in(amts, sin).out;
276 limitingStep = it - vp.begin();
284 for (
int i = limitingStep - 1; i >= 0; --i)
287 auto const [amts, amm] = vp[i];
295 sin = Quality{amts}.ceil_out(amts, sout).in;
307 auto const a =
arg();
308 boost::regex re(
",");
326 auto const exec = [&]() ->
bool {
341 if (
auto const swap =
getSwap(++p); swap)
356 else if (*p ==
"swapout")
358 if (
auto const swap =
getSwap(++p); swap)
368 else if (*p ==
"lptokens")
373 auto const LPT = amm[
"LPT"];
386 else if (*p ==
"changespq")
396 Quality{offer->first},
402 <<
"\nnew pool: " <<
toString(pool->first.in + ammOffer->in) <<
" "
425BEAST_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
STAmount mulratio(STAmount const &amt, std::uint32_t a, std::uint32_t b, bool round)
std::optional< std::tuple< std::string, std::uint32_t, bool > > getRate(token_iter const &p)
std::optional< std::pair< Amounts, bool > > getAmounts(token_iter &p)
boost::sregex_token_iterator token_iter
void swapIn(swapargs const &args)
std::optional< STAmount > getAmt(token_iter const &p, bool *delimited=nullptr)
std::string toString(STAmount const &a)
void run() override
Runs the suite.
std::optional< swapargs > getSwap(token_iter &p)
void swapOut(swapargs const &args)
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.