20 #include <ripple/app/misc/AMMHelpers.h>
21 #include <ripple/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;
220 sin = swapAssetOut(amts, sout,
fee);
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);
245 sout = swapAssetIn(amts, sin,
fee);
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;
288 sout = swapAssetIn(amts, sin,
fee);
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];
313 sin = swapAssetOut(amts, sout,
fee);
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")
421 if (
auto const ammOffer = changeSpotPriceQuality(
422 pool->first, Quality{offer->first},
fee);
425 <<
"amm offer: " <<
toString(ammOffer->in)
428 <<
toString(pool->first.in + ammOffer->in)
430 <<
toString(pool->first.out - ammOffer->out)
433 std::cout <<
"can't change the pool's SP quality"