xrpld
Loading...
Searching...
No Matches
PathMPT_test.cpp
1#include <test/jtx/Account.h>
2#include <test/jtx/Env.h>
3#include <test/jtx/TestHelpers.h>
4#include <test/jtx/amount.h>
5#include <test/jtx/balance.h>
6#include <test/jtx/domain.h>
7#include <test/jtx/envconfig.h>
8#include <test/jtx/mpt.h>
9#include <test/jtx/offer.h>
10#include <test/jtx/pay.h>
11#include <test/jtx/permissioned_dex.h>
12
13#include <xrpld/core/Config.h>
14#include <xrpld/rpc/Context.h>
15#include <xrpld/rpc/RPCHandler.h>
16#include <xrpld/rpc/Role.h>
17#include <xrpld/rpc/detail/AccountAssets.h>
18#include <xrpld/rpc/detail/AssetCache.h>
19#include <xrpld/rpc/detail/RPCHelpers.h>
20#include <xrpld/rpc/detail/Tuning.h>
21
22#include <xrpl/basics/base_uint.h>
23#include <xrpl/beast/unit_test/suite.h>
24#include <xrpl/core/Job.h>
25#include <xrpl/core/JobQueue.h>
26#include <xrpl/json/json_value.h>
27#include <xrpl/protocol/AccountID.h>
28#include <xrpl/protocol/ApiVersion.h>
29#include <xrpl/protocol/ErrorCodes.h>
30#include <xrpl/protocol/Indexes.h>
31#include <xrpl/protocol/PathAsset.h>
32#include <xrpl/protocol/SField.h>
33#include <xrpl/protocol/STAmount.h>
34#include <xrpl/protocol/STPathSet.h>
35#include <xrpl/protocol/UintTypes.h>
36#include <xrpl/protocol/jss.h>
37#include <xrpl/resource/Charge.h>
38#include <xrpl/resource/Consumer.h>
39#include <xrpl/resource/Fees.h>
40
41#include <algorithm>
42#include <cstdint>
43#include <memory>
44#include <optional>
45#include <tuple>
46#include <vector>
47
48namespace xrpl::test {
49namespace detail {
50
51static json::Value
52rpf(jtx::Account const& src,
53 jtx::Account const& dst,
54 xrpl::test::jtx::MPT const& usd,
55 std::vector<MPTID> const& numSrc)
56{
58 jv[jss::command] = "ripple_path_find";
59 jv[jss::source_account] = toBase58(src);
60
61 if (!numSrc.empty())
62 {
63 auto& sc = (jv[jss::source_currencies] = json::ValueType::Array);
65 for (auto const& id : numSrc)
66 {
67 j[jss::mpt_issuance_id] = to_string(id);
68 sc.append(j);
69 }
70 }
71
72 auto const d = toBase58(dst);
73 jv[jss::destination_account] = d;
74
75 json::Value& j = (jv[jss::destination_amount] = json::ValueType::Object);
76 j[jss::mpt_issuance_id] = to_string(usd.mpt());
77 j[jss::value] = "1";
78
79 return jv;
80}
81
82} // namespace detail
83
84//------------------------------------------------------------------------------
85
87{
90 {
91 // These tests were originally written with search parameters that are
92 // different from the current defaults. This function creates an env
93 // with the search parameters that the tests were written for.
94 using namespace jtx;
95 return Env(*this, envconfig([](std::unique_ptr<Config> cfg) {
96 cfg->pathSearchOld = 7;
97 cfg->pathSearch = 7;
98 cfg->pathSearchMax = 10;
99 return cfg;
100 }));
101 }
102
103public:
104 void
106 {
107 testcase("source currency limits");
108 using namespace std::chrono_literals;
109 using namespace jtx;
110 Env env = pathTestEnv();
111 auto const gw = Account("gateway");
112 auto const alice = Account("alice");
113 auto const bob = Account("bob");
114
115 env.fund(XRP(10'000), "alice", "bob", gw);
116
117 MPT const usd =
118 MPTTester({.env = env, .issuer = gw, .holders = {alice, bob}, .maxAmt = 100});
119
120 auto& app = env.app();
123
124 rpc::JsonContext context{
125 {
126 .j = env.journal,
127 .app = app,
128 .loadType = loadType,
129 .netOps = app.getOPs(),
130 .ledgerMaster = app.getLedgerMaster(),
131 .consumer = c,
132 .role = Role::USER,
133 .coro = {},
134 .infoSub = {},
135 .apiVersion = rpc::kApiVersionIfUnspecified,
136 },
137 {},
138 // NOLINTNEXTLINE(readability-trailing-comma) fix-it drops comma after `{}`
139 {},
140 };
141 json::Value result;
142 Gate g;
143 // Test rpc::tuning::max_src_cur source currencies.
144 std::vector<MPTID> numSrc;
146 for (std::uint8_t i = 0; i < rpc::tuning::kMaxSrcCur; ++i)
147 numSrc.push_back(makeMptID(i, bob));
148 app.getJobQueue().postCoro(JtClient, "RPC-Client", [&](auto const& coro) {
149 context.params = xrpl::test::detail::rpf(alice, bob, usd, numSrc);
150 context.coro = coro;
151 rpc::doCommand(context, result);
152 g.signal();
153 });
154 BEAST_EXPECT(g.waitFor(5s));
155 BEAST_EXPECT(!result.isMember(jss::error));
156
157 // Test more than rpc::tuning::max_src_cur source currencies.
159 app.getJobQueue().postCoro(JtClient, "RPC-Client", [&](auto const& coro) {
160 context.params = xrpl::test::detail::rpf(alice, bob, usd, numSrc);
161 context.coro = coro;
162 rpc::doCommand(context, result);
163 g.signal();
164 });
165 BEAST_EXPECT(g.waitFor(5s));
166 BEAST_EXPECT(result[jss::error] == "srcCurMalformed");
167 BEAST_EXPECT(result[jss::error_code] == RpcSrcCurMalformed);
168 BEAST_EXPECT(result[jss::error_message] == "Source currency is malformed.");
169
170 // Test rpc::tuning::max_auto_src_cur source currencies.
171 numSrc.clear();
172 for (auto i = 0; i < (rpc::tuning::kMaxAutoSrcCur - 1); ++i)
173 {
174 auto const curm = MPTTester({.env = env, .issuer = alice, .holders = {bob}});
175 numSrc.push_back(curm.issuanceID());
176 }
177 app.getJobQueue().postCoro(JtClient, "RPC-Client", [&](auto const& coro) {
178 context.params = xrpl::test::detail::rpf(alice, bob, usd, {});
179 context.coro = coro;
180 rpc::doCommand(context, result);
181 g.signal();
182 });
183 BEAST_EXPECT(g.waitFor(5s));
184 BEAST_EXPECT(!result.isMember(jss::error));
185
186 // Test more than rpc::tuning::max_auto_src_cur source currencies.
187 auto const curm = MPTTester({.env = env, .issuer = alice, .holders = {bob}});
188 app.getJobQueue().postCoro(JtClient, "RPC-Client", [&](auto const& coro) {
189 context.params = xrpl::test::detail::rpf(alice, bob, usd, {});
190 context.coro = coro;
191 rpc::doCommand(context, result);
192 g.signal();
193 });
194 BEAST_EXPECT(g.waitFor(5s));
195 BEAST_EXPECT(result[jss::error] == "internal");
196 BEAST_EXPECT(result[jss::error_code] == RpcInternal);
197 BEAST_EXPECT(result[jss::error_message] == "Internal error.");
198 }
199
200 void
202 {
203 testcase("no direct path no intermediary no alternatives");
204 using namespace jtx;
205
206 Env env = pathTestEnv();
207
208 env.fund(XRP(10'000), "alice", "bob");
209
210 auto const usdm = MPTTester({.env = env, .issuer = "bob"});
211
212 auto const result = findPaths(env, "alice", "bob", usdm(5));
213 BEAST_EXPECT(std::get<0>(result).empty());
214 }
215
216 void
218 {
219 testcase("direct path no intermediary");
220 using namespace jtx;
221 Env env = pathTestEnv();
222 env.fund(XRP(10'000), "alice", "bob");
223
224 MPT const usd = MPTTester({.env = env, .issuer = "alice", .holders = {"bob"}});
225
226 STPathSet st;
227 STAmount sa;
228 std::tie(st, sa, std::ignore) = findPaths(env, "alice", "bob", usd(5));
229 BEAST_EXPECT(st.empty());
230 BEAST_EXPECT(equal(sa, usd(5)));
231 }
232
233 void
235 {
236 testcase("payment auto path find");
237 using namespace jtx;
238 Env env = pathTestEnv();
239 auto const gw = Account("gateway");
240 env.fund(XRP(10'000), "alice", "bob", gw);
241 MPT const usd = MPTTester({.env = env, .issuer = gw, .holders = {"alice", "bob"}});
242 env(pay(gw, "alice", usd(70)));
243 env(pay("alice", "bob", usd(24)));
244 env.require(Balance("alice", usd(46)));
245 env.require(Balance("bob", usd(24)));
246 }
247
248 void
250 {
251 testcase("source currency with send_max");
252 using namespace jtx;
253
254 Env env = pathTestEnv();
255 auto const alice = Account("alice");
256 auto const bob = Account("bob");
257 auto const gw = Account("gateway");
258 env.fund(XRP(10'000), alice, bob, gw);
259
260 MPT const usd = MPTTester({.env = env, .issuer = gw, .holders = {alice, bob}});
261 env(pay(gw, alice, usd(25)));
262 env.close();
263
264 // MPT source_currencies entries do not carry an issuer. A matching
265 // send_max identifies the same issuance, so the request should not run
266 // the IOU issuer reconciliation path.
267 auto const result = findPathsRequest(
268 env,
269 alice,
270 bob,
271 usd(-1),
272 std::optional<STAmount>(usd(10).value()),
274 BEAST_EXPECTS(!result.isMember(jss::error), result.toStyledString());
275
276 auto const& alternatives = result[jss::alternatives];
277 if (BEAST_EXPECT(alternatives.size() == 1))
278 {
279 auto const sa = amountFromJson(sfGeneric, alternatives[0u][jss::source_amount]);
280 auto const da = amountFromJson(sfGeneric, alternatives[0u][jss::destination_amount]);
281 BEAST_EXPECTS(equal(sa, usd(10)), sa.getFullText());
282 BEAST_EXPECTS(equal(da, usd(10)), da.getFullText());
283 }
284 }
285
286 void
288 {
289 testcase("maxed-out MPT pathfinding");
290 using namespace jtx;
291
292 auto const hasMPT = [](auto const& assets, MPT const& mpt) {
293 return std::ranges::any_of(assets, [&](auto const& asset) {
294 return asset.template holds<MPTID>() && asset.template get<MPTID>() == mpt.mpt();
295 });
296 };
297
298 Env env = pathTestEnv();
299 auto const gw = Account("gateway");
300 auto const alice = Account("alice");
301 auto const bob = Account("bob");
302 auto const carol = Account("carol");
303
304 env.fund(XRP(10'000), gw, alice, bob, carol);
305 env.close();
306
307 MPT const usd =
308 MPTTester({.env = env, .issuer = gw, .holders = {alice, bob, carol}, .maxAmt = 100});
309 env(pay(gw, alice, usd(90)));
310 env(pay(gw, bob, usd(10)));
311 env.close();
312
313 auto const cache =
314 std::make_shared<AssetCache>(env.current(), env.app().getJournal("AssetCache"));
315
316 BEAST_EXPECT(hasMPT(accountSourceAssets(alice.id(), cache, false), usd));
317 BEAST_EXPECT(hasMPT(accountDestAssets(bob.id(), cache, false), usd));
318 BEAST_EXPECT(hasMPT(accountDestAssets(carol.id(), cache, false), usd));
319
320 // A fully minted issuance should not be advertised as issuer-side
321 // mintable source liquidity.
322 BEAST_EXPECT(!hasMPT(accountSourceAssets(gw.id(), cache, false), usd));
323
324 auto [st, sa, da] = findPaths(env, alice, bob, usd(5));
325 BEAST_EXPECT(st.empty());
326 BEAST_EXPECT(equal(sa, usd(5)));
327 BEAST_EXPECT(equal(da, usd(5)));
328
329 env(offer(carol, usd(5), XRP(5)));
330 env.close();
331
332 std::tie(st, sa, da) = findPaths(env, alice, bob, drops(-1), usd(100).value());
333 BEAST_EXPECT(sa == usd(5));
334 BEAST_EXPECT(equal(da, XRP(5)));
335 if (BEAST_EXPECT(st.size() == 1 && st[0].size() == 1))
336 {
337 auto const& pathElem = st[0][0];
338 BEAST_EXPECT(
339 pathElem.isOffer() && pathElem.getIssuerID() == xrpAccount() &&
340 pathElem.getCurrency() == xrpCurrency());
341 }
342 }
343
344 void
345 pathFind(bool const domainEnabled)
346 {
347 testcase(std::string("path find") + (domainEnabled ? " w/ " : " w/o ") + "domain");
348 using namespace jtx;
349 Env env = pathTestEnv();
350 auto const gw = Account("gateway");
351 env.fund(XRP(10'000), "alice", "bob", gw);
352 MPT const usd = MPTTester({.env = env, .issuer = gw, .holders = {"alice", "bob"}});
353 env(pay(gw, "alice", usd(70)));
354 env(pay(gw, "bob", usd(50)));
355
356 std::optional<UInt256> domainID;
357 if (domainEnabled)
358 domainID = setupDomain(env, {"alice", "bob", gw});
359
360 STPathSet st;
361 STAmount sa;
362 STAmount da;
363 std::tie(st, sa, da) = findPaths(
364 env, "alice", "bob", usd(5), std::nullopt, std::nullopt, std::nullopt, domainID);
365 // Note, a direct IOU payment will have "gateway" as alternative path
366 // since IOU supports rippling
367 BEAST_EXPECT(st.empty());
368 BEAST_EXPECT(equal(sa, usd(5)));
369 BEAST_EXPECT(equal(da, usd(5)));
370 }
371
372 void
373 pathFindConsumeAll(bool const domainEnabled)
374 {
375 testcase(
376 std::string("path find consume all") + (domainEnabled ? " w/ " : " w/o ") + "domain");
377 using namespace jtx;
378
379 {
380 Env env = pathTestEnv();
381 auto const gw = Account("gateway");
382 env.fund(XRP(10'000), "alice", "bob", "carol", gw);
383 MPT const usd = MPTTester({.env = env, .issuer = gw, .holders = {"bob", "carol"}});
384 MPT const aud(makeMptID(0, gw));
385 env(pay(gw, "carol", usd(100)));
386 std::optional<UInt256> domainID;
387 if (domainEnabled)
388 {
389 domainID = setupDomain(env, {"alice", "bob", "carol", "gateway"});
390 env(offer("carol", XRP(100), usd(100)), Domain(*domainID));
391 }
392 else
393 {
394 env(offer("carol", XRP(100), usd(100)));
395 }
396 env.close();
397
398 STPathSet st;
399 STAmount sa;
400 STAmount da;
401 std::tie(st, sa, da) = findPaths(
402 env,
403 "alice",
404 "bob",
405 aud(-1),
406 std::optional<STAmount>(XRP(100'000'000)),
407 std::nullopt,
408 std::nullopt,
409 domainID);
410 BEAST_EXPECT(st.empty());
411 std::tie(st, sa, da) = findPaths(
412 env,
413 "alice",
414 "bob",
415 usd(-1),
416 std::optional<STAmount>(XRP(100'000'000)),
417 std::nullopt,
418 std::nullopt,
419 domainID);
420 if (BEAST_EXPECT(st.size() == 1 && st[0].size() == 1))
421 {
422 auto const& pathElem = st[0][0];
423 BEAST_EXPECT(
424 pathElem.isOffer() && pathElem.getIssuerID() == gw.id() &&
425 pathElem.getMPTID() == usd.issuanceID);
426 }
427 BEAST_EXPECT(sa == XRP(100));
428 BEAST_EXPECT(equal(da, usd(100)));
429
430 // if domain is used, finding path in the open offerbook will return
431 // empty result
432 if (domainEnabled)
433 {
434 std::tie(st, sa, da) = findPaths(
435 env,
436 "alice",
437 "bob",
438 Account("bob")["USD"](-1),
440 std::nullopt,
441 std::nullopt); // not specifying a domain
442 BEAST_EXPECT(st.empty());
443 }
444 }
445 }
446
447 void
448 alternativePathsConsumeBestTransfer(bool const domainEnabled)
449 {
450 testcase(
451 std::string("alternative path consume best transfer") +
452 (domainEnabled ? " w/ " : " w/o ") + "domain");
453 using namespace jtx;
454 Env env = pathTestEnv();
455 auto const gw = Account("gateway");
456 auto const gw2 = Account("gateway2");
457 env.fund(XRP(10'000), "alice", "bob", gw, gw2);
458 MPT const usd = MPTTester({.env = env, .issuer = gw, .holders = {"alice", "bob"}});
459 MPT const gw2Usd = MPTTester(
460 {.env = env, .issuer = gw2, .holders = {"alice", "bob"}, .transferFee = 1'000});
461 std::optional<UInt256> domainID;
462 if (domainEnabled)
463 {
464 domainID = setupDomain(env, {"alice", "bob", "gateway", "gateway2"});
465 env(pay(gw, "alice", usd(70)), Domain(*domainID));
466 env(pay(gw2, "alice", gw2Usd(70)), Domain(*domainID));
467 env(pay("alice", "bob", usd(70)), Domain(*domainID));
468 }
469 else
470 {
471 env(pay(gw, "alice", usd(70)));
472 env(pay(gw2, "alice", gw2Usd(70)));
473 env(pay("alice", "bob", usd(70)));
474 }
475 env.require(Balance("alice", usd(0)));
476 env.require(Balance("alice", gw2Usd(70)));
477 env.require(Balance("bob", usd(70)));
478 env.require(Balance("bob", gw2Usd(0)));
479 }
480
481 void
482 receiveMax(bool const domainEnabled)
483 {
484 testcase(std::string("Receive max") + (domainEnabled ? " w/ " : " w/o ") + "domain");
485 using namespace jtx;
486 auto const alice = Account("alice");
487 auto const bob = Account("bob");
488 auto const charlie = Account("charlie");
489 auto const gw = Account("gw");
490 {
491 // XRP -> MPT receive max
492 Env env = pathTestEnv();
493 env.fund(XRP(10'000), alice, bob, charlie, gw);
494 env.close();
495 MPT const usd = MPTTester({.env = env, .issuer = gw, .holders = {alice, bob, charlie}});
496 env(pay(gw, charlie, usd(10)));
497 env.close();
498 std::optional<UInt256> domainID;
499 if (domainEnabled)
500 {
501 domainID = setupDomain(env, {alice, bob, charlie, gw});
502 env(offer(charlie, XRP(10), usd(10)), Domain(*domainID));
503 }
504 else
505 {
506 env(offer(charlie, XRP(10), usd(10)));
507 }
508 env.close();
509 auto [st, sa, da] = findPaths(
510 env, alice, bob, usd(-1), XRP(100).value(), std::nullopt, std::nullopt, domainID);
511 BEAST_EXPECT(sa == XRP(10));
512 BEAST_EXPECT(equal(da, usd(10)));
513 if (BEAST_EXPECT(st.size() == 1 && st[0].size() == 1))
514 {
515 auto const& pathElem = st[0][0];
516 BEAST_EXPECT(
517 pathElem.isOffer() && pathElem.getIssuerID() == gw.id() &&
518 pathElem.getMPTID() == usd.mpt());
519 }
520 }
521 {
522 // MPT -> XRP receive max
523 Env env = pathTestEnv();
524 env.fund(XRP(10'000), alice, bob, charlie, gw);
525 env.close();
526 MPT const usd = MPTTester({.env = env, .issuer = gw, .holders = {alice, bob, charlie}});
527 env(pay(gw, alice, usd(10)));
528 env.close();
529 std::optional<UInt256> domainID;
530 if (domainEnabled)
531 {
532 domainID = setupDomain(env, {alice, bob, charlie, gw});
533 env(offer(charlie, usd(10), XRP(10)), Domain(*domainID));
534 }
535 else
536 {
537 env(offer(charlie, usd(10), XRP(10)));
538 }
539 env.close();
540 auto [st, sa, da] = findPaths(
541 env, alice, bob, drops(-1), usd(100).value(), std::nullopt, std::nullopt, domainID);
542 BEAST_EXPECT(sa == usd(10));
543 BEAST_EXPECT(equal(da, XRP(10)));
544 if (BEAST_EXPECT(st.size() == 1 && st[0].size() == 1))
545 {
546 auto const& pathElem = st[0][0];
547 BEAST_EXPECT(
548 pathElem.isOffer() && pathElem.getIssuerID() == xrpAccount() &&
549 pathElem.getCurrency() == xrpCurrency());
550 }
551 }
552 }
553
554 // Regression test: the Pathfinder constructor must honor the
555 // caller-supplied srcAmount (= the user's send_max from PathRequest)
556 // when ranking candidate paths in convert_all mode.
557 //
558 // Background. The MPT-DEX refactor of `Pathfinder::Pathfinder`
559 // (src/xrpld/rpc/detail/Pathfinder.cpp) replaced the original
560 // `mSrcAmount(srcAmount.value_or(...))` initializer with an
561 // unconditional `amountFromPathAsset(...)` call. The latter always
562 // returns the negative "no limit" STAmount sentinel, so the
563 // `srcAmount` constructor parameter became dead code:
564 // `getPathLiquidity` and `computePathRanks` ran `rippleCalculate`
565 // with `saMaxAmountReq` = sentinel and recorded each path's
566 // saturated capacity instead of the capacity reachable inside
567 // send_max.
568 //
569 // In convert_all_ mode (the only mode that allows send_max),
570 // `Pathfinder::rankPaths` ignores quality and orders purely by
571 // liquidity, then `Pathfinder::getBestPaths` only fills the last
572 // (kMaxPaths-th = 4th) slot when `pathRank.liquidity >= remaining`.
573 // For convert_all_ `remaining = largestAmount(dstAmount_)`, so the
574 // last slot effectively never fills and the cut keeps the top 3
575 // ranked paths. With the wrong (unbounded-budget) ranking, a
576 // low-capacity / high-rate path that would actually deliver the
577 // most under the user's send_max can be excluded entirely.
578 //
579 // Topology. Four candidate paths from alice's XRP to bob's USD-MPT,
580 // each via a distinct IOU intermediary issued by a different market
581 // maker:
582 //
583 // charlie: XRP(1000) -> AUD(1000) -> USD(500) cap 1000 XRP, rate 0.5
584 // dave: XRP(1000) -> EUR(1000) -> USD(500) cap 1000 XRP, rate 0.5
585 // eve: XRP(1000) -> GBP(1000) -> USD(500) cap 1000 XRP, rate 0.5
586 // frank: XRP(50) -> JPY(50) -> USD(75) cap 50 XRP, rate 1.5
587 //
588 // Alice queries findPaths with destination = USD-MPT(-1) (convert_all)
589 // and send_max = XRP(100).
590 //
591 // Bug-free ranking (post-fix), with srcAmount = XRP(100):
592 // charlie/dave/eve liquidity = min(100, 1000) * 0.5 = 50 USD each
593 // frank liquidity = min(100, 50) * 1.5 = 75 USD
594 // -> frank ranks first; the flow uses frank's 50 XRP at 1.5 (=75 USD)
595 // plus 50 XRP via a 0.5-rate path (=25 USD), delivering USD(100).
596 //
597 // Pre-fix ranking, with srcAmount silently replaced by the sentinel:
598 // charlie/dave/eve liquidity = 1000 * 0.5 = 500 USD each
599 // frank liquidity = 50 * 1.5 = 75 USD
600 // -> frank ranks 4th; the last-slot rule excludes it from the
601 // surviving path set, the cut keeps the three 0.5-rate paths,
602 // and the flow delivers only 100 * 0.5 = USD(50).
603 //
604 // This test asserts the post-fix outcome (USD(100)). On the pre-fix
605 // tree the assertion fails with USD(50).
606 void
608 {
609 testcase("convert_all + send_max: srcAmount governs path ranking");
610 using namespace jtx;
611
612 Env env = pathTestEnv();
613 auto const alice = Account("alice");
614 auto const bob = Account("bob");
615 auto const gw = Account("gateway");
616 auto const charlie = Account("charlie");
617 auto const dave = Account("dave");
618 auto const eve = Account("eve");
619 auto const frank = Account("frank");
620
621 env.fund(XRP(10'000), alice, bob, gw, charlie, dave, eve, frank);
622 env.close();
623
624 // USD MPT issued by gw; the four market makers and bob are holders.
625 // alice is not a holder because she only pays XRP; USD only ever
626 // flows from gw / market-maker offers to bob.
627 MPT const usd =
628 MPTTester({.env = env, .issuer = gw, .holders = {charlie, dave, eve, frank, bob}});
629
630 // Capitalize each market maker with the USD-MPT they will sell.
631 env(pay(gw, charlie, usd(500)));
632 env(pay(gw, dave, usd(500)));
633 env(pay(gw, eve, usd(500)));
634 env(pay(gw, frank, usd(75)));
635 env.close();
636
637 // Each market maker issues their own intermediate IOU.
638 auto const aud = charlie["AUD"];
639 auto const eur = dave["EUR"];
640 auto const gbp = eve["GBP"];
641 auto const jpy = frank["JPY"];
642
643 // Three high-capacity, low-rate paths (1 XRP -> 0.5 USD-MPT,
644 // capacity 1000 XRP each).
645 env(offer(charlie, XRP(1'000), aud(1'000)));
646 env(offer(charlie, aud(1'000), usd(500)));
647 env(offer(dave, XRP(1'000), eur(1'000)));
648 env(offer(dave, eur(1'000), usd(500)));
649 env(offer(eve, XRP(1'000), gbp(1'000)));
650 env(offer(eve, gbp(1'000), usd(500)));
651
652 // One low-capacity, high-rate path (1 XRP -> 1.5 USD-MPT,
653 // capacity 50 XRP).
654 env(offer(frank, XRP(50), jpy(50)));
655 env(offer(frank, jpy(50), usd(75)));
656 env.close();
657
658 // ripple_path_find with convert_all (USD(-1)) and send_max XRP(100).
659 STPathSet st;
660 STAmount sa;
661 STAmount da;
662 std::tie(st, sa, da) =
663 findPaths(env, alice, bob, usd(-1), std::optional<STAmount>(XRP(100).value()));
664
665 // Post-fix: frank's high-rate path is included in the surviving
666 // path set, so the flow uses 50 XRP at 1.5 plus 50 XRP at 0.5,
667 // delivering exactly USD(100) on alice's 100-XRP budget.
668 BEAST_EXPECT(sa == XRP(100));
669 BEAST_EXPECT(equal(da, usd(100)));
670 }
671
672 void
673 run() override
674 {
682 for (auto const domainEnabled : {false, true})
683 {
684 pathFind(domainEnabled);
685 pathFindConsumeAll(domainEnabled);
687 receiveMax(domainEnabled);
688 }
689 }
690};
691
693
694} // namespace xrpl::test
T any_of(T... args)
A testsuite class.
Definition suite.h:52
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
Represents a JSON value.
Definition json_value.h:121
bool isMember(char const *key) const
Return true if the object has a member named key.
std::vector< STPath >::size_type size() const
Definition STPathSet.h:624
bool empty() const
Definition STPathSet.h:630
virtual beast::Journal getJournal(std::string const &name)=0
A consumption charge.
Definition Charge.h:13
An endpoint that consumes resources.
Definition Consumer.h:20
void pathFindConsumeAll(bool const domainEnabled)
void noDirectPathNoIntermediaryNoAlternatives()
void receiveMax(bool const domainEnabled)
void run() override
Runs the suite.
void alternativePathsConsumeBestTransfer(bool const domainEnabled)
void pathFind(bool const domainEnabled)
Immutable cryptographic account descriptor.
Definition jtx/Account.h:21
A balance matches.
Definition balance.h:24
A transaction testing environment.
Definition Env.h:161
Application & app()
Definition Env.h:300
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
Definition Env.cpp:133
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
Definition Env.cpp:323
beast::Journal const journal
Definition Env.h:204
void require(Args const &... args)
Check a set of requirements.
Definition Env.h:766
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Definition Env.h:377
bool waitFor(std::chrono::duration< Rep, Period > const &relTime)
Test helper for creating, mutating, and asserting MPT and confidential MPT ledger state.
Definition mpt.h:512
Converts to MPT Issue or STAmount.
xrpl::MPTID const & mpt() const
T clear(T... args)
T empty(T... args)
T make_shared(T... args)
@ Array
array value (ordered list)
Definition json_value.h:32
@ Object
object value (collection of name/value pairs).
Definition json_value.h:33
Charge const kFeeReferenceRpc
static constexpr int kMaxSrcCur
Maximum number of source currencies allowed in a path find request.
static constexpr int kMaxAutoSrcCur
Maximum number of auto source currencies in a path find request.
static constexpr auto kApiVersionIfUnspecified
Definition ApiVersion.h:34
::rpc::Status doCommand(rpc::JsonContext &context, json::Value &result)
Execute an RPC command and store the results in a json::Value.
static json::Value rpf(jtx::Account const &src, jtx::Account const &dst, xrpl::test::jtx::MPT const &usd, std::vector< MPTID > const &numSrc)
json::Value pay(AccountID const &account, AccountID const &to, AnyAmount amount)
Create a payment.
Definition pay.cpp:14
XrpT const XRP
Converts to XRP Issue or STAmount.
Definition amount.cpp:92
std::tuple< STPathSet, STAmount, STAmount > findPaths(jtx::Env &env, jtx::Account const &src, jtx::Account const &dst, STAmount const &saDstAmount, std::optional< STAmount > const &saSendMax, std::optional< PathAsset > const &srcAsset, std::optional< AccountID > const &srcIssuer, std::optional< UInt256 > const &domain)
json::Value findPathsRequest(jtx::Env &env, jtx::Account const &src, jtx::Account const &dst, STAmount const &saDstAmount, std::optional< STAmount > const &saSendMax, std::optional< PathAsset > const &srcAsset, std::optional< AccountID > const &srcIssuer, std::optional< UInt256 > const &domain)
bool equal(STAmount const &sa1, STAmount const &sa2)
json::Value offer(Account const &account, STAmount const &takerPays, STAmount const &takerGets, std::uint32_t flags)
Create an offer.
Definition offer.cpp:14
std::unique_ptr< Config > envconfig()
creates and initializes a default configuration for jtx::Env
Definition envconfig.h:38
UInt256 setupDomain(jtx::Env &env, std::vector< jtx::Account > const &accounts, jtx::Account const &domainOwner, std::string const &credType)
PrettyAmount drops(Integer i)
Returns an XRP PrettyAmount, which is trivially convertible to STAmount.
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ RpcInternal
Definition ErrorCodes.h:113
@ RpcSrcCurMalformed
Definition ErrorCodes.h:107
T get(Section const &section, std::string const &name, T const &defaultValue=T{})
Retrieve a key/value pair from a section.
BaseUInt< 256 > Domain
Domain is a 256-bit hash representing a specific domain.
Definition UintTypes.h:59
SField const sfGeneric
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:95
Currency const & xrpCurrency()
XRP currency.
Definition UintTypes.cpp:99
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:658
HashSet< PathAsset > accountDestAssets(AccountID const &account, std::shared_ptr< AssetCache > const &lrCache, bool includeXRP)
@ USER
Definition Role.h:27
@ JtClient
Definition Job.h:31
STAmount amountFromJson(SField const &name, json::Value const &v)
HashSet< PathAsset > accountSourceAssets(AccountID const &account, std::shared_ptr< AssetCache > const &lrCache, bool includeXRP)
MPTID makeMptID(std::uint32_t const sequence, AccountID const &account)
Definition Indexes.cpp:219
AccountID const & xrpAccount()
Compute AccountID from public key.
T push_back(T... args)
T reserve(T... args)
std::shared_ptr< JobQueue::Coro > coro
Definition Context.h:35
json::Value params
Definition Context.h:51
T tie(T... args)