21#include <xrpld/app/misc/AMMHelpers.h>
22#include <xrpl/protocol/Quality.h>
24#include <boost/regex.hpp>
55 str = boost::regex_replace(str, boost::regex(
"^(A|O)[(]"),
"");
58 boost::regex rx(
"^([^(]+)[(]([^)]+)[)]([)])?$");
59 if (boost::regex_search(str,
match, rx))
62 *delimited = (
match[3] !=
"");
63 if (
match[1] ==
"XRP")
66 else if (
match[1] ==
"XRPA")
79 str = boost::regex_replace(str, boost::regex(
"^T[(]"),
"");
82 boost::regex rx(
"^([^(]+)[(]([^)]+)[)]([)])?$");
83 if (boost::regex_search(str,
match, rx))
89 return {{currency,
rate,
match[3] !=
"" ? true :
false}};
111 bool const amm = s[0] ==
'O' ? false :
true;
112 auto const a1 =
getAmt(p++);
113 if (!a1 || p ==
end_)
115 auto const a2 =
getAmt(p++);
118 return {{{*a1, *a2}, amm}};
135 auto const [currency, trate, delimited] = *
rate;
136 rates[currency] = trate;
152 auto isPair = [](
auto const& p) {
154 return s[0] ==
'A' || s[0] ==
'O';
160 if (!res || p ==
end_)
165 auto const swap =
getAmt(p++);
173 return {{pairs, *swap, *
rate,
fee}};
197 auto const vp = std::get<steps>(args);
198 STAmount sout = std::get<STAmount>(args);
199 auto const fee = std::get<std::uint32_t>(args);
200 auto const rates = std::get<trates>(args);
204 int limitingStep = vp.size();
206 auto trate = [&](
auto const& amt) {
207 auto const currency =
to_string(amt.issue().currency);
208 return rates.find(currency) != rates.end() ? rates.at(currency)
213 for (
auto it = vp.rbegin(); it != vp.rend(); ++it)
215 sout =
mulratio(sin, trate(sin), QUALITY_ONE,
true);
216 auto const [amts, amm] = *it;
222 else if (sout <= amts.out)
224 sin = Quality{amts}.ceil_out(amts, sout).in;
230 limitingStep = vp.rend() - it - 1;
232 if (it == vp.rbegin())
233 resultOut = amts.out;
239 for (
int i = limitingStep + 1; i < vp.size(); ++i)
241 auto const [amts, amm] = vp[i];
242 sin =
mulratio(sin, QUALITY_ONE, trate(sin),
false);
250 sout = Quality{amts}.ceil_in(amts, sin).out;
262 auto const vp = std::get<steps>(args);
263 STAmount sin = std::get<STAmount>(args);
264 auto const fee = std::get<std::uint32_t>(args);
265 auto const rates = std::get<trates>(args);
269 int limitingStep = 0;
271 auto trate = [&](
auto const& amt) {
272 auto const currency =
to_string(amt.issue().currency);
273 return rates.find(currency) != rates.end() ? rates.at(currency)
277 for (
auto it = vp.begin(); it != vp.end(); ++it)
279 auto const [amts, amm] = *it;
290 else if (sin <= amts.in)
292 sout = Quality{amts}.ceil_in(amts, sin).out;
299 limitingStep = it - vp.begin();
307 for (
int i = limitingStep - 1; i >= 0; --i)
309 sout =
mulratio(sin, trate(sin), QUALITY_ONE,
false);
310 auto const [amts, amm] = vp[i];
318 sin = Quality{amts}.ceil_out(amts, sout).in;
322 resultOut =
mulratio(resultOut, QUALITY_ONE, trate(resultOut),
true);
331 auto const a =
arg();
332 boost::regex re(
",");
350 auto const exec = [&]() ->
bool {
365 if (
auto const swap =
getSwap(++p); swap)
380 else if (*p ==
"swapout")
382 if (
auto const swap =
getSwap(++p); swap)
392 else if (*p ==
"lptokens")
397 auto const LPT = amm[
"LPT"];
414 else if (*p ==
"changespq")
424 Quality{offer->first},
430 <<
"amm offer: " <<
toString(ammOffer->in)
433 <<
toString(pool->first.in + ammOffer->in)
435 <<
toString(pool->first.out - ammOffer->out)
438 std::cout <<
"can't change the pool's SP quality"
459BEAST_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.
STAmount amountFromString(Asset const &issue, std::string const &amount)
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)
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.