xrpld
Loading...
Searching...
No Matches
MPToken_test.cpp
1#include <test/jtx/AMM.h>
2#include <test/jtx/AMMTest.h>
3#include <test/jtx/Account.h>
4#include <test/jtx/Env.h>
5#include <test/jtx/TestHelpers.h>
6#include <test/jtx/amount.h>
7#include <test/jtx/balance.h> // IWYU pragma: keep
8#include <test/jtx/check.h>
9#include <test/jtx/credentials.h>
10#include <test/jtx/delivermin.h>
11#include <test/jtx/deposit.h>
12#include <test/jtx/domain.h>
13#include <test/jtx/envconfig.h>
14#include <test/jtx/escrow.h>
15#include <test/jtx/fee.h>
16#include <test/jtx/flags.h>
17#include <test/jtx/mpt.h>
18#include <test/jtx/offer.h>
19#include <test/jtx/paths.h>
20#include <test/jtx/pay.h>
21#include <test/jtx/permissioned_dex.h>
22#include <test/jtx/permissioned_domains.h>
23#include <test/jtx/sendmax.h>
24#include <test/jtx/ter.h>
25#include <test/jtx/trust.h>
26#include <test/jtx/txflags.h>
27#include <test/jtx/vault.h>
28
29#include <xrpl/basics/base_uint.h>
30#include <xrpl/basics/strHex.h>
31#include <xrpl/beast/unit_test/suite.h>
32#include <xrpl/beast/utility/Journal.h>
33#include <xrpl/json/json_value.h>
34#include <xrpl/json/to_string.h>
35#include <xrpl/ledger/ApplyView.h>
36#include <xrpl/ledger/ApplyViewImpl.h>
37#include <xrpl/ledger/helpers/MPTokenHelpers.h>
38#include <xrpl/ledger/helpers/TokenHelpers.h>
39#include <xrpl/protocol/Asset.h>
40#include <xrpl/protocol/ErrorCodes.h>
41#include <xrpl/protocol/Feature.h>
42#include <xrpl/protocol/IOUAmount.h>
43#include <xrpl/protocol/Indexes.h>
44#include <xrpl/protocol/Issue.h>
45#include <xrpl/protocol/LedgerFormats.h>
46#include <xrpl/protocol/MPTAmount.h>
47#include <xrpl/protocol/MPTIssue.h>
48#include <xrpl/protocol/Protocol.h>
49#include <xrpl/protocol/Quality.h>
50#include <xrpl/protocol/SField.h>
51#include <xrpl/protocol/SOTemplate.h>
52#include <xrpl/protocol/STAmount.h>
53#include <xrpl/protocol/STPathSet.h>
54#include <xrpl/protocol/STTx.h>
55#include <xrpl/protocol/SeqProxy.h>
56#include <xrpl/protocol/Serializer.h>
57#include <xrpl/protocol/TER.h>
58#include <xrpl/protocol/TxFlags.h>
59#include <xrpl/protocol/TxFormats.h>
60#include <xrpl/protocol/UintTypes.h>
61#include <xrpl/protocol/XRPAmount.h>
62#include <xrpl/protocol/jss.h>
63#include <xrpl/tx/transactors/token/MPTokenIssuanceSet.h>
64
65#include <array>
66#include <cstdint>
67#include <functional>
68#include <initializer_list>
69#include <limits>
70#include <memory>
71#include <optional>
72#include <set>
73#include <stdexcept>
74#include <string>
75#include <string_view>
76#include <tuple>
77#include <utility>
78#include <vector>
79
80namespace xrpl::test {
81
83{
84 void
86 {
87 testcase("Create Validate");
88 using namespace test::jtx;
89 Account const alice("alice");
90
91 // test preflight of MPTokenIssuanceCreate
92 {
93 // If the MPT amendment is not enabled, you should not be able to
94 // create MPTokenIssuance
95 Env env{*this, features - featureMPTokensV1};
96 MPTTester mptAlice(env, alice);
97
98 mptAlice.create({.ownerCount = 0, .err = temDISABLED});
99 }
100
101 // test preflight of MPTokenIssuanceCreate
102 {
103 Env env{*this, features};
104 MPTTester mptAlice(env, alice);
105
106 mptAlice.create({.flags = 0x00000001, .err = temINVALID_FLAG});
107
108 // tries to set a txFee while not enabling in the flag
109 mptAlice.create({
110 .maxAmt = 100,
111 .assetScale = 0,
112 .transferFee = 1,
113 .metadata = "test",
114 .err = temMALFORMED,
115 });
116
117 if (!features[featureSingleAssetVault] || !features[featurePermissionedDomains])
118 {
119 // tries to set DomainID when SAV or PD is disabled
120 mptAlice.create({
121 .maxAmt = 100,
122 .assetScale = 0,
123 .metadata = "test",
124 .flags = tfMPTRequireAuth,
125 .domainID = UInt256(42),
126 .err = temDISABLED,
127 });
128 }
129 else
130 {
131 // tries to set DomainID when RequireAuth is not set
132 mptAlice.create({
133 .maxAmt = 100,
134 .assetScale = 0,
135 .metadata = "test",
136 .domainID = UInt256(42),
137 .err = temMALFORMED,
138 });
139
140 // tries to set zero DomainID
141 mptAlice.create({
142 .maxAmt = 100,
143 .assetScale = 0,
144 .metadata = "test",
145 .flags = tfMPTRequireAuth,
146 .domainID = UInt256{},
147 .err = temMALFORMED,
148 });
149 }
150
151 // tries to set a txFee greater than max
152 mptAlice.create({
153 .maxAmt = 100,
154 .assetScale = 0,
155 .transferFee = kMaxTransferFee + 1,
156 .metadata = "test",
157 .flags = tfMPTCanTransfer,
158 .err = temBAD_TRANSFER_FEE,
159 });
160
161 // tries to set a txFee while not enabling transfer
162 mptAlice.create({
163 .maxAmt = 100,
164 .assetScale = 0,
165 .transferFee = kMaxTransferFee,
166 .metadata = "test",
167 .err = temMALFORMED,
168 });
169
170 // empty metadata returns error
171 mptAlice.create({
172 .maxAmt = 100,
173 .assetScale = 0,
174 .transferFee = 0,
175 .metadata = "",
176 .err = temMALFORMED,
177 });
178
179 // MaximumAmount of 0 returns error
180 mptAlice.create({
181 .maxAmt = 0,
182 .assetScale = 1,
183 .transferFee = 1,
184 .metadata = "test",
185 .err = temMALFORMED,
186 });
187
188 // MaximumAmount larger than 63 bit returns error
189 mptAlice.create({
190 .maxAmt = 0xFFFF'FFFF'FFFF'FFF0, // 18'446'744'073'709'551'600
191 .assetScale = 0,
192 .transferFee = 0,
193 .metadata = "test",
194 .err = temMALFORMED,
195 });
196 mptAlice.create({
197 .maxAmt = kMaxMpTokenAmount + 1, // 9'223'372'036'854'775'808
198 .assetScale = 0,
199 .transferFee = 0,
200 .metadata = "test",
201 .err = temMALFORMED,
202 });
203 }
204
205 // sfReferenceHolding is populated internally only by VaultCreate.
206 // A user-submitted MPTokenIssuanceCreate carrying the field must be
207 // rejected at preflight under fixCleanup3_2_0.
208 if (features[fixCleanup3_2_0])
209 {
210 Env env{*this, features};
211 env.fund(XRP(1'000), alice);
212 env.close();
213
214 json::Value jv;
215 jv[sfAccount] = alice.human();
216 jv[sfTransactionType] = jss::MPTokenIssuanceCreate;
217 jv[sfReferenceHolding] = to_string(UInt256{1});
218 env(jv, Ter(temMALFORMED));
219 }
220 }
221
222 void
224 {
225 testcase("Create Enabled");
226
227 using namespace test::jtx;
228 Account const alice("alice");
229
230 {
231 // If the MPT amendment IS enabled, you should be able to create
232 // MPTokenIssuances
233 Env env{*this, features};
234 MPTTester mptAlice(env, alice);
235 mptAlice.create({
236 .maxAmt = kMaxMpTokenAmount, // 9'223'372'036'854'775'807
237 .assetScale = 1,
238 .transferFee = 10,
239 .metadata = "123",
240 .ownerCount = 1,
241 .flags = tfMPTCanLock | tfMPTRequireAuth | tfMPTCanEscrow | tfMPTCanTrade |
242 tfMPTCanTransfer | tfMPTCanClawback,
243 });
244
245 // Get the hash for the most recent transaction.
246 std::string const txHash{
247 env.tx()->getJson(JsonOptions::Values::None)[jss::hash].asString(),
248 };
249
250 json::Value const result = env.rpc("tx", txHash)[jss::result];
251 BEAST_EXPECT(result[sfMaximumAmount.getJsonName()] == "9223372036854775807");
252 }
253
254 if (features[featureSingleAssetVault])
255 {
256 // Add permissioned domain
257 Account const credIssuer1{"credIssuer1"};
258 std::string const credType = "credential";
259
260 pdomain::Credentials const credentials1{{.issuer = credIssuer1, .credType = credType}};
261
262 {
263 Env env{*this, features};
264 env.fund(XRP(1000), credIssuer1);
265
266 env(pdomain::setTx(credIssuer1, credentials1));
267 auto const domainId1 = [&] {
268 auto const tx = env.tx()->getJson(JsonOptions::Values::None);
269 return pdomain::getNewDomain(env.meta());
270 }();
271
272 MPTTester mptAlice(env, alice);
273 mptAlice.create({
274 .maxAmt = kMaxMpTokenAmount, // 9'223'372'036'854'775'807
275 .assetScale = 1,
276 .transferFee = 10,
277 .metadata = "123",
278 .ownerCount = 1,
279 .flags = tfMPTCanLock | tfMPTRequireAuth | tfMPTCanEscrow | tfMPTCanTrade |
280 tfMPTCanTransfer | tfMPTCanClawback,
281 .domainID = domainId1,
282 });
283
284 // Get the hash for the most recent transaction.
285 std::string const txHash{
286 env.tx()->getJson(JsonOptions::Values::None)[jss::hash].asString(),
287 };
288
289 json::Value const result = env.rpc("tx", txHash)[jss::result];
290 BEAST_EXPECT(result[sfMaximumAmount.getJsonName()] == "9223372036854775807");
291 }
292 }
293 }
294
295 void
297 {
298 testcase("Destroy Validate");
299
300 using namespace test::jtx;
301 Account const alice("alice");
302 Account const bob("bob");
303 // MPTokenIssuanceDestroy (preflight)
304 {
305 Env env{*this, features - featureMPTokensV1};
306 MPTTester mptAlice(env, alice);
307 auto const id = makeMptID(env.seq(alice), alice);
308 mptAlice.destroy({.id = id, .ownerCount = 0, .err = temDISABLED});
309
310 env.enableFeature(featureMPTokensV1);
311
312 mptAlice.destroy({.id = id, .flags = 0x00000001, .err = temINVALID_FLAG});
313 }
314
315 // MPTokenIssuanceDestroy (preclaim)
316 {
317 Env env{*this, features};
318 MPTTester mptAlice(env, alice, {.holders = {bob}});
319
320 mptAlice.destroy({
321 .id = makeMptID(env.seq(alice), alice),
322 .ownerCount = 0,
323 .err = tecOBJECT_NOT_FOUND,
324 });
325
326 mptAlice.create({.ownerCount = 1});
327
328 // a non-issuer tries to destroy a mptissuance they didn't issue
329 mptAlice.destroy({.issuer = bob, .err = tecNO_PERMISSION});
330
331 // Make sure that issuer can't delete issuance when it still has
332 // outstanding balance
333 {
334 // bob now holds a mptoken object
335 mptAlice.authorize({.account = bob, .holderCount = 1});
336
337 // alice pays bob 100 tokens
338 mptAlice.pay(alice, bob, 100);
339
340 mptAlice.destroy({.err = tecHAS_OBLIGATIONS});
341 }
342 }
343 }
344
345 void
347 {
348 testcase("Destroy Enabled");
349
350 using namespace test::jtx;
351 Account const alice("alice");
352
353 // If the MPT amendment IS enabled, you should be able to destroy
354 // MPTokenIssuances
355 Env env{*this, features};
356 MPTTester mptAlice(env, alice);
357
358 mptAlice.create({.ownerCount = 1});
359
360 mptAlice.destroy({.ownerCount = 0});
361 }
362
363 void
365 {
366 testcase("Validate authorize transaction");
367
368 using namespace test::jtx;
369 Account const alice("alice");
370 Account const bob("bob");
371 Account const cindy("cindy");
372 // Validate amendment enable in MPTokenAuthorize (preflight)
373 {
374 Env env{*this, features - featureMPTokensV1};
375 MPTTester mptAlice(env, alice, {.holders = {bob}});
376
377 mptAlice.authorize(
378 {.account = bob, .id = makeMptID(env.seq(alice), alice), .err = temDISABLED});
379 }
380
381 // Validate fields in MPTokenAuthorize (preflight)
382 {
383 Env env{*this, features};
384 MPTTester mptAlice(env, alice, {.holders = {bob}});
385
386 mptAlice.create({.ownerCount = 1});
387
388 // The only valid MPTokenAuthorize flag is tfMPTUnauthorize, which
389 // has a value of 1
390 mptAlice.authorize({.account = bob, .flags = 0x00000002, .err = temINVALID_FLAG});
391
392 mptAlice.authorize({.account = bob, .holder = bob, .err = temMALFORMED});
393
394 mptAlice.authorize({.holder = alice, .err = temMALFORMED});
395 }
396
397 // Try authorizing when MPTokenIssuance doesn't exist in
398 // MPTokenAuthorize (preclaim)
399 {
400 Env env{*this, features};
401 MPTTester mptAlice(env, alice, {.holders = {bob}});
402 auto const id = makeMptID(env.seq(alice), alice);
403
404 mptAlice.authorize({.holder = bob, .id = id, .err = tecOBJECT_NOT_FOUND});
405
406 mptAlice.authorize({.account = bob, .id = id, .err = tecOBJECT_NOT_FOUND});
407 }
408
409 // Test bad scenarios without allowlisting in MPTokenAuthorize
410 // (preclaim)
411 {
412 Env env{*this, features};
413 MPTTester mptAlice(env, alice, {.holders = {bob}});
414
415 mptAlice.create({.ownerCount = 1});
416
417 // bob submits a tx with a holder field
418 mptAlice.authorize({.account = bob, .holder = alice, .err = tecNO_PERMISSION});
419
420 // alice tries to hold onto her own token
421 mptAlice.authorize({.account = alice, .err = tecNO_PERMISSION});
422
423 // the mpt does not enable allowlisting
424 mptAlice.authorize({.holder = bob, .err = tecNO_AUTH});
425
426 // bob now holds a mptoken object
427 mptAlice.authorize({.account = bob, .holderCount = 1});
428
429 // bob cannot create the mptoken the second time
430 mptAlice.authorize({.account = bob, .err = tecDUPLICATE});
431
432 // Check that bob cannot delete MPToken when his balance is
433 // non-zero
434 {
435 // alice pays bob 100 tokens
436 mptAlice.pay(alice, bob, 100);
437
438 // bob tries to delete his MPToken, but fails since he still
439 // holds tokens
440 mptAlice.authorize(
441 {.account = bob, .flags = tfMPTUnauthorize, .err = tecHAS_OBLIGATIONS});
442
443 // bob pays back alice 100 tokens
444 mptAlice.pay(bob, alice, 100);
445 }
446
447 // bob deletes/unauthorizes his MPToken
448 mptAlice.authorize({.account = bob, .flags = tfMPTUnauthorize});
449
450 // bob receives error when he tries to delete his MPToken that has
451 // already been deleted
452 mptAlice.authorize({
453 .account = bob,
454 .holderCount = 0,
455 .flags = tfMPTUnauthorize,
456 .err = tecOBJECT_NOT_FOUND,
457 });
458 }
459
460 // Test bad scenarios with allow-listing in MPTokenAuthorize (preclaim)
461 {
462 Env env{*this, features};
463 MPTTester mptAlice(env, alice, {.holders = {bob}});
464
465 mptAlice.create({.ownerCount = 1, .flags = tfMPTRequireAuth});
466
467 // alice submits a tx without specifying a holder's account
468 mptAlice.authorize({.err = tecNO_PERMISSION});
469
470 // alice submits a tx to authorize a holder that hasn't created
471 // a mptoken yet
472 mptAlice.authorize({.holder = bob, .err = tecOBJECT_NOT_FOUND});
473
474 // alice specifies a holder acct that doesn't exist
475 mptAlice.authorize({.holder = cindy, .err = tecNO_DST});
476
477 // bob now holds a mptoken object
478 mptAlice.authorize({.account = bob, .holderCount = 1});
479
480 // alice tries to unauthorize bob.
481 // although tx is successful,
482 // but nothing happens because bob hasn't been authorized yet
483 mptAlice.authorize({.holder = bob, .flags = tfMPTUnauthorize});
484
485 // alice authorizes bob
486 // make sure bob's mptoken has set lsfMPTAuthorized
487 mptAlice.authorize({.holder = bob});
488
489 // alice tries authorizes bob again.
490 // tx is successful, but bob is already authorized,
491 // so no changes
492 mptAlice.authorize({.holder = bob});
493
494 // bob deletes his mptoken
495 mptAlice.authorize({.account = bob, .holderCount = 0, .flags = tfMPTUnauthorize});
496 }
497
498 // Test mptoken reserve requirement - first two mpts free (doApply)
499 {
500 Env env{*this, features};
501 auto const acctReserve = env.current()->fees().reserve;
502 auto const incReserve = env.current()->fees().increment;
503
504 // 1 drop
505 BEAST_EXPECT(incReserve > XRPAmount(1));
506 MPTTester mptAlice1(
507 env, alice, {.holders = {bob}, .xrpHolders = acctReserve + (incReserve - 1)});
508 mptAlice1.create();
509
510 MPTTester mptAlice2(env, alice, {.fund = false});
511 mptAlice2.create();
512
513 MPTTester mptAlice3(env, alice, {.fund = false});
514 mptAlice3.create({.ownerCount = 3});
515
516 // first mpt for free
517 mptAlice1.authorize({.account = bob, .holderCount = 1});
518
519 // second mpt free
520 mptAlice2.authorize({.account = bob, .holderCount = 2});
521
522 mptAlice3.authorize({.account = bob, .err = tecINSUFFICIENT_RESERVE});
523
524 env(pay(env.master, bob, drops(incReserve + incReserve + incReserve)));
525 env.close();
526
527 mptAlice3.authorize({.account = bob, .holderCount = 3});
528 }
529 }
530
531 void
533 {
534 testcase("Authorize Enabled");
535
536 using namespace test::jtx;
537 Account const alice("alice");
538 Account const bob("bob");
539 // Basic authorization without allowlisting
540 {
541 Env env{*this, features};
542
543 // alice create mptissuance without allowisting
544 MPTTester mptAlice(env, alice, {.holders = {bob}});
545
546 mptAlice.create({.ownerCount = 1});
547
548 // bob creates a mptoken
549 mptAlice.authorize({.account = bob, .holderCount = 1});
550
551 mptAlice.authorize({.account = bob, .holderCount = 1, .err = tecDUPLICATE});
552
553 // bob deletes his mptoken
554 mptAlice.authorize({.account = bob, .holderCount = 0, .flags = tfMPTUnauthorize});
555 }
556
557 // With allowlisting
558 {
559 Env env{*this, features};
560
561 // alice creates a mptokenissuance that requires authorization
562 MPTTester mptAlice(env, alice, {.holders = {bob}});
563
564 mptAlice.create({.ownerCount = 1, .flags = tfMPTRequireAuth});
565
566 // bob creates a mptoken
567 mptAlice.authorize({.account = bob, .holderCount = 1});
568
569 // alice authorizes bob
570 mptAlice.authorize({.account = alice, .holder = bob});
571
572 // Unauthorize bob's mptoken
573 mptAlice.authorize(
574 {.account = alice, .holder = bob, .holderCount = 1, .flags = tfMPTUnauthorize});
575
576 mptAlice.authorize({.account = bob, .holderCount = 0, .flags = tfMPTUnauthorize});
577 }
578
579 // Holder can have dangling MPToken even if issuance has been destroyed.
580 // Make sure they can still delete/unauthorize the MPToken
581 {
582 Env env{*this, features};
583 MPTTester mptAlice(env, alice, {.holders = {bob}});
584
585 mptAlice.create({.ownerCount = 1});
586
587 // bob creates a mptoken
588 mptAlice.authorize({.account = bob, .holderCount = 1});
589
590 // alice deletes her issuance
591 mptAlice.destroy({.ownerCount = 0});
592
593 // bob can delete his mptoken even though issuance is no longer
594 // existent
595 mptAlice.authorize({.account = bob, .holderCount = 0, .flags = tfMPTUnauthorize});
596 }
597 }
598
599 void
601 {
602 testcase("Validate set transaction");
603
604 using namespace test::jtx;
605 Account const alice("alice"); // issuer
606 Account const bob("bob"); // holder
607 Account const cindy("cindy");
608 // Validate fields in MPTokenIssuanceSet (preflight)
609 {
610 Env env{*this, features - featureMPTokensV1};
611 MPTTester mptAlice(env, alice, {.holders = {bob}});
612
613 mptAlice.set(
614 {.account = bob, .id = makeMptID(env.seq(alice), alice), .err = temDISABLED});
615
616 env.enableFeature(featureMPTokensV1);
617
618 mptAlice.create({.ownerCount = 1, .holderCount = 0});
619
620 mptAlice.authorize({.account = bob, .holderCount = 1});
621
622 // test invalid flag - an unrecognized flag bit is always
623 // rejected, regardless of which amendments are enabled
624 mptAlice.set({.account = alice, .flags = 0x00001000, .err = temINVALID_FLAG});
625
626 if (!features[featureSingleAssetVault] && !features[featureDynamicMPT] &&
627 !features[featureConfidentialTransfer])
628 {
629 // test invalid flags - nothing is being changed
630 mptAlice.set({.account = alice, .flags = 0x00000000, .err = tecNO_PERMISSION});
631
632 mptAlice.set({
633 .account = alice,
634 .holder = bob,
635 .flags = 0x00000000,
636 .err = tecNO_PERMISSION,
637 });
638
639 // cannot set DomainID since SAV is not enabled
640 mptAlice.set({.account = alice, .domainID = UInt256(42), .err = temDISABLED});
641 }
642 else
643 {
644 // test invalid flags - nothing is being changed
645 mptAlice.set({.account = alice, .flags = 0x00000000, .err = temMALFORMED});
646
647 mptAlice.set(
648 {.account = alice, .holder = bob, .flags = 0x00000000, .err = temMALFORMED});
649
650 if (!features[featurePermissionedDomains] || !features[featureSingleAssetVault])
651 {
652 // cannot set DomainID since PD is not enabled
653 mptAlice.set({.account = alice, .domainID = UInt256(42), .err = temDISABLED});
654 }
655 else if (features[featureSingleAssetVault])
656 {
657 // cannot set DomainID since Holder is set
658 mptAlice.set({
659 .account = alice,
660 .holder = bob,
661 .domainID = UInt256(42),
662 .err = temMALFORMED,
663 });
664 }
665 }
666
667 // set both lock and unlock flags at the same time will fail
668 mptAlice.set(
669 {.account = alice, .flags = tfMPTLock | tfMPTUnlock, .err = temINVALID_FLAG});
670
671 // if the holder is the same as the acct that submitted the tx,
672 // tx fails
673 mptAlice.set(
674 {.account = alice, .holder = alice, .flags = tfMPTLock, .err = temMALFORMED});
675 }
676
677 // Validate fields in MPTokenIssuanceSet (preclaim)
678 // test when a mptokenissuance has disabled locking
679 {
680 Env env{*this, features};
681
682 MPTTester mptAlice(env, alice, {.holders = {bob}});
683
684 mptAlice.create({.ownerCount = 1});
685
686 // alice tries to lock a mptissuance that has disabled locking
687 mptAlice.set({.account = alice, .flags = tfMPTLock, .err = tecNO_PERMISSION});
688
689 // alice tries to unlock mptissuance that has disabled locking
690 mptAlice.set({.account = alice, .flags = tfMPTUnlock, .err = tecNO_PERMISSION});
691
692 // issuer tries to lock a bob's mptoken that has disabled
693 // locking
694 mptAlice.set(
695 {.account = alice, .holder = bob, .flags = tfMPTLock, .err = tecNO_PERMISSION});
696
697 // issuer tries to unlock a bob's mptoken that has disabled
698 // locking
699 mptAlice.set(
700 {.account = alice, .holder = bob, .flags = tfMPTUnlock, .err = tecNO_PERMISSION});
701 }
702
703 // Validate fields in MPTokenIssuanceSet (preclaim)
704 // test when mptokenissuance has enabled locking
705 {
706 Env env{*this, features};
707
708 MPTTester mptAlice(env, alice, {.holders = {bob}});
709
710 // alice trying to set when the mptissuance doesn't exist yet
711 mptAlice.set({
712 .id = makeMptID(env.seq(alice), alice),
713 .flags = tfMPTLock,
714 .err = tecOBJECT_NOT_FOUND,
715 });
716
717 // create a mptokenissuance with locking
718 mptAlice.create({.ownerCount = 1, .flags = tfMPTCanLock});
719
720 // a non-issuer acct tries to set the mptissuance
721 mptAlice.set({.account = bob, .flags = tfMPTLock, .err = tecNO_PERMISSION});
722
723 // trying to set a holder who doesn't have a mptoken
724 mptAlice.set({.holder = bob, .flags = tfMPTLock, .err = tecOBJECT_NOT_FOUND});
725
726 // trying to set a holder who doesn't exist
727 mptAlice.set({.holder = cindy, .flags = tfMPTLock, .err = tecNO_DST});
728 }
729
730 if (features[featureSingleAssetVault] && features[featurePermissionedDomains])
731 {
732 // Add permissioned domain
733 Account const credIssuer1{"credIssuer1"};
734 std::string const credType = "credential";
735
736 pdomain::Credentials const credentials1{{.issuer = credIssuer1, .credType = credType}};
737
738 {
739 Env env{*this, features};
740
741 MPTTester mptAlice(env, alice);
742 mptAlice.create({});
743
744 // Trying to set DomainID on a public MPTokenIssuance
745 mptAlice.set({.domainID = UInt256(42), .err = tecNO_PERMISSION});
746
747 mptAlice.set({.domainID = UInt256{}, .err = tecNO_PERMISSION});
748 }
749
750 {
751 Env env{*this, features};
752
753 MPTTester mptAlice(env, alice);
754 mptAlice.create({.flags = tfMPTRequireAuth});
755
756 // Trying to set non-existing DomainID
757 mptAlice.set({.domainID = UInt256(42), .err = tecOBJECT_NOT_FOUND});
758
759 // Trying to lock but locking is disabled
760 mptAlice.set(
761 {.flags = tfMPTUnlock, .domainID = UInt256(42), .err = tecNO_PERMISSION});
762
763 mptAlice.set(
764 {.flags = tfMPTUnlock, .domainID = UInt256{}, .err = tecNO_PERMISSION});
765 }
766 }
767 }
768
769 void
771 {
772 testcase("Enabled set transaction");
773
774 using namespace test::jtx;
775 Account const alice("alice"); // issuer
776 Account const bob("bob"); // holder
777
778 {
779 // Test locking and unlocking
780 Env env{*this, features};
781
782 MPTTester mptAlice(env, alice, {.holders = {bob}});
783
784 // create a mptokenissuance with locking
785 mptAlice.create({.ownerCount = 1, .holderCount = 0, .flags = tfMPTCanLock});
786
787 mptAlice.authorize({.account = bob, .holderCount = 1});
788
789 // locks bob's mptoken
790 mptAlice.set({.account = alice, .holder = bob, .flags = tfMPTLock});
791
792 // trying to lock bob's mptoken again will still succeed
793 // but no changes to the objects
794 mptAlice.set({.account = alice, .holder = bob, .flags = tfMPTLock});
795
796 // alice locks the mptissuance
797 mptAlice.set({.account = alice, .flags = tfMPTLock});
798
799 // alice tries to lock up both mptissuance and mptoken again
800 // it will not change the flags and both will remain locked.
801 mptAlice.set({.account = alice, .flags = tfMPTLock});
802 mptAlice.set({.account = alice, .holder = bob, .flags = tfMPTLock});
803
804 // alice unlocks bob's mptoken
805 mptAlice.set({.account = alice, .holder = bob, .flags = tfMPTUnlock});
806
807 // locks up bob's mptoken again
808 mptAlice.set({.account = alice, .holder = bob, .flags = tfMPTLock});
809 if (!features[featureSingleAssetVault] && !features[fixCleanup3_4_0])
810 {
811 // Delete bob's mptoken even though it is locked
812 mptAlice.authorize({.account = bob, .flags = tfMPTUnauthorize});
813
814 mptAlice.set({
815 .account = alice,
816 .holder = bob,
817 .flags = tfMPTUnlock,
818 .err = tecOBJECT_NOT_FOUND,
819 });
820
821 return;
822 }
823
824 // Cannot delete locked MPToken
825 mptAlice.authorize(
826 {.account = bob, .flags = tfMPTUnauthorize, .err = tecNO_PERMISSION});
827
828 // alice unlocks mptissuance
829 mptAlice.set({.account = alice, .flags = tfMPTUnlock});
830
831 // alice unlocks bob's mptoken
832 mptAlice.set({.account = alice, .holder = bob, .flags = tfMPTUnlock});
833
834 // alice unlocks mptissuance and bob's mptoken again despite that
835 // they are already unlocked. Make sure this will not change the
836 // flags
837 mptAlice.set({.account = alice, .holder = bob, .flags = tfMPTUnlock});
838 mptAlice.set({.account = alice, .flags = tfMPTUnlock});
839 }
840
841 if (features[featureSingleAssetVault])
842 {
843 // Add permissioned domain
844 std::string const credType = "credential";
845
846 // Test setting and resetting domain ID
847 Env env{*this, features};
848
849 auto const domainId1 = [&] {
850 Account const credIssuer1{"credIssuer1"};
851 env.fund(XRP(1000), credIssuer1);
852
853 pdomain::Credentials const credentials1{
854 {.issuer = credIssuer1, .credType = credType},
855 };
856
857 env(pdomain::setTx(credIssuer1, credentials1));
858 return [&] {
859 auto const tx = env.tx()->getJson(JsonOptions::Values::None);
860 return pdomain::getNewDomain(env.meta());
861 }();
862 }();
863
864 auto const domainId2 = [&] {
865 Account const credIssuer2{"credIssuer2"};
866 env.fund(XRP(1000), credIssuer2);
867
868 pdomain::Credentials const credentials2{
869 {.issuer = credIssuer2, .credType = credType},
870 };
871
872 env(pdomain::setTx(credIssuer2, credentials2));
873 return [&] {
874 auto const tx = env.tx()->getJson(JsonOptions::Values::None);
875 return pdomain::getNewDomain(env.meta());
876 }();
877 }();
878
879 MPTTester mptAlice(env, alice, {.holders = {bob}});
880
881 // create a mptokenissuance with auth.
882 mptAlice.create({.ownerCount = 1, .holderCount = 0, .flags = tfMPTRequireAuth});
883 BEAST_EXPECT(mptAlice.checkDomainID(std::nullopt));
884
885 // reset "domain not set" to "domain not set", i.e. no change
886 mptAlice.set({.domainID = UInt256{}});
887 BEAST_EXPECT(mptAlice.checkDomainID(std::nullopt));
888
889 // reset "domain not set" to domain1
890 mptAlice.set({.domainID = domainId1});
891 BEAST_EXPECT(mptAlice.checkDomainID(domainId1));
892
893 // reset domain1 to domain2
894 mptAlice.set({.domainID = domainId2});
895 BEAST_EXPECT(mptAlice.checkDomainID(domainId2));
896
897 // reset domain to "domain not set"
898 mptAlice.set({.domainID = UInt256{}});
899 BEAST_EXPECT(mptAlice.checkDomainID(std::nullopt));
900 }
901 }
902
903 void
905 {
906 testcase("Payment");
907
908 using namespace test::jtx;
909 Account const alice("alice"); // issuer
910 Account const bob("bob"); // holder
911 Account const carol("carol"); // holder
912 auto const mpTokensV2 = features[featureMPTokensV2];
913
914 // preflight validation
915
916 // MPT is disabled
917 {
918 Env env{*this, features - featureMPTokensV1}; // NOLINT TODO
919 Account const alice("alice");
920 Account const bob("bob");
921
922 env.fund(XRP(1'000), alice);
923 env.fund(XRP(1'000), bob);
924 STAmount const mpt{MPTIssue{makeMptID(1, alice)}, UINT64_C(100)};
925
926 env(pay(alice, bob, mpt), Ter(temDISABLED));
927 }
928
929 // MPT is disabled, unsigned request
930 {
931 Env env{*this, features - featureMPTokensV1};
932 Account const alice("alice"); // issuer
933 Account const carol("carol");
934 auto const usd = alice["USD"];
935
936 env.fund(XRP(1'000), alice);
937 env.fund(XRP(1'000), carol);
938 STAmount const mpt{MPTIssue{makeMptID(1, alice)}, UINT64_C(100)};
939
940 json::Value jv;
941 jv[jss::secret] = alice.name();
942 jv[jss::tx_json] = pay(alice, carol, mpt);
943 jv[jss::tx_json][jss::Fee] = to_string(env.current()->fees().base);
944 auto const jrr = env.rpc("json", "submit", to_string(jv));
945 BEAST_EXPECT(jrr[jss::result][jss::engine_result] == "temDISABLED");
946 }
947
948 // Invalid flag
949 {
950 Env env{*this, features};
951
952 MPTTester mptAlice(env, alice, {.holders = {bob}});
953
954 mptAlice.create({.ownerCount = 1, .holderCount = 0});
955 auto const mpt = mptAlice["MPT"];
956
957 mptAlice.authorize({.account = bob});
958
959 auto err = !features[featureMPTokensV2] ? Ter(temINVALID_FLAG) : Ter(temRIPPLE_EMPTY);
960 env(pay(alice, bob, mpt(10)), Txflags(tfNoRippleDirect), err);
961 err = !features[featureMPTokensV2] ? Ter(temINVALID_FLAG) : Ter(tesSUCCESS);
962 env(pay(alice, bob, mpt(10)), Txflags(tfLimitQuality), err);
963 }
964
965 // Invalid combination of send, sendMax, deliverMin, paths
966 {
967 Env env{*this, features};
968 Account const alice("alice");
969 Account const carol("carol");
970
971 MPTTester mptAlice(env, alice, {.holders = {carol}});
972
973 mptAlice.create(
974 {.ownerCount = 1, .holderCount = 0, .flags = tfMPTCanTransfer | tfMPTCanTrade});
975
976 mptAlice.authorize({.account = carol});
977
978 // sendMax and DeliverMin are valid XRP amount,
979 // but is invalid combination with MPT amount
980 auto const mpt = mptAlice["MPT"];
981 auto err = !mpTokensV2 ? Ter(temMALFORMED) : Ter(tecPATH_PARTIAL);
982 env(pay(alice, carol, mpt(100)), Sendmax(XRP(100)), err);
983 env(pay(alice, carol, mpt(100)), DeliverMin(XRP(100)), Ter(temBAD_AMOUNT));
984 // sendMax MPT is invalid with IOU or XRP
985 auto const usd = alice["USD"];
986 err = !mpTokensV2 ? Ter(temMALFORMED) : Ter(tecPATH_DRY);
987 env(pay(alice, carol, usd(100)), Sendmax(mpt(100)), err);
988 err = !mpTokensV2 ? Ter(temMALFORMED) : Ter(tecPATH_PARTIAL);
989 env(pay(alice, carol, XRP(100)), Sendmax(mpt(100)), err);
990 env(pay(alice, carol, usd(100)), DeliverMin(mpt(100)), Ter(temBAD_AMOUNT));
991 env(pay(alice, carol, XRP(100)), DeliverMin(mpt(100)), Ter(temBAD_AMOUNT));
992 // sendmax and amount are different MPT issue
993 test::jtx::MPT const mpT1("MPT", makeMptID(env.seq(alice) + 10, alice));
994 err = !mpTokensV2 ? Ter(temMALFORMED) : Ter(tecOBJECT_NOT_FOUND);
995 env(pay(alice, carol, mpT1(100)), Sendmax(mpt(100)), err);
996 // "paths" is invalid in V1. In V2 any path from the issuer back
997 // to its own MPT is a loop, like it is for a holder.
998 err = !mpTokensV2 ? Ter(temMALFORMED) : Ter(temBAD_PATH_LOOP);
999 env(pay(alice, carol, mpt(100)), Path(~usd), err);
1000 }
1001
1002 // build_path is invalid if MPT
1003 {
1004 Env env{*this, features - featureMPTokensV2};
1005 Account const alice("alice");
1006 Account const carol("carol");
1007
1008 MPTTester mptAlice(env, alice, {.holders = {bob, carol}});
1009
1010 mptAlice.create({.ownerCount = 1, .holderCount = 0});
1011 auto const mpt = mptAlice["MPT"];
1012
1013 mptAlice.authorize({.account = carol});
1014
1015 json::Value payment;
1016 payment[jss::secret] = alice.name();
1017 payment[jss::tx_json] = pay(alice, carol, mpt(100));
1018
1019 payment[jss::build_path] = true;
1020 auto jrr = env.rpc("json", "submit", to_string(payment));
1021 BEAST_EXPECT(jrr[jss::result][jss::error] == "invalidParams");
1022 BEAST_EXPECT(jrr[jss::result][jss::error_code] == RpcInvalidParams);
1023 BEAST_EXPECT(
1024 jrr[jss::result][jss::error_message] ==
1025 "Field 'build_path' not allowed in this context.");
1026 }
1027
1028 // Can't pay negative amount
1029 {
1030 Env env{*this, features};
1031
1032 MPTTester mptAlice(env, alice, {.holders = {bob, carol}});
1033
1034 mptAlice.create({.ownerCount = 1, .holderCount = 0});
1035 auto const mpt = mptAlice["MPT"];
1036
1037 mptAlice.authorize({.account = bob});
1038 mptAlice.authorize({.account = carol});
1039
1040 mptAlice.pay(alice, bob, -1, temBAD_AMOUNT);
1041
1042 mptAlice.pay(bob, carol, -1, temBAD_AMOUNT);
1043
1044 mptAlice.pay(bob, alice, -1, temBAD_AMOUNT);
1045
1046 env(pay(alice, bob, mpt(10)), Sendmax(mpt(-1)), Ter(temBAD_AMOUNT));
1047 }
1048
1049 // Pay to self
1050 {
1051 Env env{*this, features};
1052
1053 MPTTester mptAlice(env, alice, {.holders = {bob}});
1054
1055 mptAlice.create({.ownerCount = 1, .holderCount = 0});
1056
1057 mptAlice.authorize({.account = bob});
1058
1059 mptAlice.pay(bob, bob, 10, temREDUNDANT);
1060 }
1061
1062 // preclaim validation
1063
1064 // Destination doesn't exist
1065 {
1066 Env env{*this, features};
1067
1068 MPTTester mptAlice(env, alice, {.holders = {bob}});
1069
1070 mptAlice.create({.ownerCount = 1, .holderCount = 0});
1071
1072 mptAlice.authorize({.account = bob});
1073
1074 Account const bad{"bad"};
1075 env.memoize(bad);
1076
1077 mptAlice.pay(bob, bad, 10, tecNO_DST);
1078 }
1079
1080 // apply validation
1081
1082 // If RequireAuth is enabled, Payment fails if the receiver is not
1083 // authorized
1084 {
1085 Env env{*this, features};
1086
1087 MPTTester mptAlice(env, alice, {.holders = {bob}});
1088
1089 mptAlice.create(
1090 {.ownerCount = 1, .holderCount = 0, .flags = tfMPTRequireAuth | tfMPTCanTransfer});
1091
1092 mptAlice.authorize({.account = bob});
1093
1094 mptAlice.pay(alice, bob, 100, tecNO_AUTH);
1095 }
1096
1097 // If RequireAuth is enabled, Payment fails if the sender is not
1098 // authorized
1099 {
1100 Env env{*this, features};
1101
1102 MPTTester mptAlice(env, alice, {.holders = {bob}});
1103
1104 mptAlice.create(
1105 {.ownerCount = 1, .holderCount = 0, .flags = tfMPTRequireAuth | tfMPTCanTransfer});
1106
1107 // bob creates an empty MPToken
1108 mptAlice.authorize({.account = bob});
1109
1110 // alice authorizes bob to hold funds
1111 mptAlice.authorize({.account = alice, .holder = bob});
1112
1113 // alice sends 100 MPT to bob
1114 mptAlice.pay(alice, bob, 100);
1115
1116 // alice UNAUTHORIZES bob
1117 mptAlice.authorize({.account = alice, .holder = bob, .flags = tfMPTUnauthorize});
1118
1119 // bob fails to send back to alice because he is no longer
1120 // authorize to move his funds!
1121 mptAlice.pay(bob, alice, 100, tecNO_AUTH);
1122 }
1123
1124 if (features[featureSingleAssetVault] && features[featurePermissionedDomains])
1125 {
1126 // If RequireAuth is enabled and domain is a match, payment succeeds
1127 {
1128 Env env{*this, features};
1129 std::string const credType = "credential";
1130 Account const credIssuer1{"credIssuer1"};
1131 env.fund(XRP(1000), credIssuer1, bob);
1132
1133 auto const domainId1 = [&] {
1134 pdomain::Credentials const credentials1{
1135 {.issuer = credIssuer1, .credType = credType},
1136 };
1137
1138 env(pdomain::setTx(credIssuer1, credentials1));
1139 return [&] {
1140 auto const tx = env.tx()->getJson(JsonOptions::Values::None);
1141 return pdomain::getNewDomain(env.meta());
1142 }();
1143 }();
1144 // bob is authorized via domain
1145 env(credentials::create(bob, credIssuer1, credType));
1146 env(credentials::accept(bob, credIssuer1, credType));
1147 env.close();
1148
1149 MPTTester mptAlice(env, alice, MPTInit{});
1150 env.close();
1151
1152 mptAlice.create({
1153 .ownerCount = 1,
1154 .holderCount = 0,
1155 .flags = tfMPTRequireAuth | tfMPTCanTransfer,
1156 .domainID = domainId1,
1157 });
1158
1159 mptAlice.authorize({.account = bob});
1160 env.close();
1161
1162 // bob is authorized via domain
1163 mptAlice.pay(alice, bob, 100);
1164 mptAlice.set({.domainID = UInt256{}});
1165
1166 // bob is no longer authorized
1167 mptAlice.pay(alice, bob, 100, tecNO_AUTH);
1168 }
1169
1170 {
1171 Env env{*this, features};
1172 std::string const credType = "credential";
1173 Account const credIssuer1{"credIssuer1"};
1174 env.fund(XRP(1000), credIssuer1, bob);
1175
1176 auto const domainId1 = [&] {
1177 pdomain::Credentials const credentials1{
1178 {.issuer = credIssuer1, .credType = credType},
1179 };
1180
1181 env(pdomain::setTx(credIssuer1, credentials1));
1182 return [&] {
1183 auto const tx = env.tx()->getJson(JsonOptions::Values::None);
1184 return pdomain::getNewDomain(env.meta());
1185 }();
1186 }();
1187 // bob is authorized via domain
1188 env(credentials::create(bob, credIssuer1, credType));
1189 env(credentials::accept(bob, credIssuer1, credType));
1190 env.close();
1191
1192 MPTTester mptAlice(env, alice, MPTInit{});
1193 env.close();
1194
1195 mptAlice.create({
1196 .ownerCount = 1,
1197 .holderCount = 0,
1198 .flags = tfMPTRequireAuth | tfMPTCanTransfer,
1199 .domainID = domainId1,
1200 });
1201
1202 // bob creates an empty MPToken
1203 mptAlice.authorize({.account = bob});
1204
1205 // alice authorizes bob to hold funds
1206 mptAlice.authorize({.account = alice, .holder = bob});
1207
1208 // alice sends 100 MPT to bob
1209 mptAlice.pay(alice, bob, 100);
1210
1211 // alice UNAUTHORIZES bob
1212 mptAlice.authorize({.account = alice, .holder = bob, .flags = tfMPTUnauthorize});
1213
1214 // bob is still authorized, via domain
1215 mptAlice.pay(bob, alice, 10);
1216
1217 mptAlice.set({.domainID = UInt256{}});
1218
1219 // bob fails to send back to alice because he is no longer
1220 // authorize to move his funds!
1221 mptAlice.pay(bob, alice, 10, tecNO_AUTH);
1222 }
1223
1224 {
1225 Env env{*this, features};
1226 std::string const credType = "credential";
1227 // credIssuer1 is the owner of domainId1 and a credential issuer
1228 Account const credIssuer1{"credIssuer1"};
1229 // credIssuer2 is the owner of domainId2 and a credential issuer
1230 // Note, domainId2 also lists credentials issued by credIssuer1
1231 Account const credIssuer2{"credIssuer2"};
1232 env.fund(XRP(1000), credIssuer1, credIssuer2, bob, carol);
1233
1234 auto const domainId1 = [&] {
1236 {.issuer = credIssuer1, .credType = credType},
1237 };
1238
1239 env(pdomain::setTx(credIssuer1, credentials));
1240 return [&] {
1241 auto const tx = env.tx()->getJson(JsonOptions::Values::None);
1242 return pdomain::getNewDomain(env.meta());
1243 }();
1244 }();
1245
1246 auto const domainId2 = [&] {
1248 {.issuer = credIssuer1, .credType = credType},
1249 {.issuer = credIssuer2, .credType = credType},
1250 };
1251
1252 env(pdomain::setTx(credIssuer2, credentials));
1253 return [&] {
1254 auto const tx = env.tx()->getJson(JsonOptions::Values::None);
1255 return pdomain::getNewDomain(env.meta());
1256 }();
1257 }();
1258
1259 // bob is authorized via credIssuer1 which is recognized by both
1260 // domainId1 and domainId2
1261 env(credentials::create(bob, credIssuer1, credType));
1262 env(credentials::accept(bob, credIssuer1, credType));
1263 env.close();
1264
1265 // carol is authorized via credIssuer2, only recognized by
1266 // domainId2
1267 env(credentials::create(carol, credIssuer2, credType));
1268 env(credentials::accept(carol, credIssuer2, credType));
1269 env.close();
1270
1271 MPTTester mptAlice(env, alice, MPTInit{});
1272 env.close();
1273
1274 mptAlice.create({
1275 .ownerCount = 1,
1276 .holderCount = 0,
1277 .flags = tfMPTRequireAuth | tfMPTCanTransfer,
1278 .domainID = domainId1,
1279 });
1280
1281 // bob and carol create an empty MPToken
1282 mptAlice.authorize({.account = bob});
1283 mptAlice.authorize({.account = carol});
1284 env.close();
1285
1286 // alice sends 50 MPT to bob but cannot send to carol
1287 mptAlice.pay(alice, bob, 50);
1288 mptAlice.pay(alice, carol, 50, tecNO_AUTH);
1289 env.close();
1290
1291 // bob cannot send to carol because they are not on the same
1292 // domain (since credIssuer2 is not recognized by domainId1)
1293 mptAlice.pay(bob, carol, 10, tecNO_AUTH);
1294 env.close();
1295
1296 // alice updates domainID to domainId2 which recognizes both
1297 // credIssuer1 and credIssuer2
1298 mptAlice.set({.domainID = domainId2});
1299 // alice can now send to carol
1300 mptAlice.pay(alice, carol, 10);
1301 env.close();
1302
1303 // bob can now send to carol because both are in the same
1304 // domain
1305 mptAlice.pay(bob, carol, 10);
1306 env.close();
1307
1308 // bob loses his authorization and can no longer send MPT
1309 env(credentials::deleteCred(credIssuer1, bob, credIssuer1, credType));
1310 env.close();
1311
1312 mptAlice.pay(bob, carol, 10, tecNO_AUTH);
1313 mptAlice.pay(bob, alice, 10, tecNO_AUTH);
1314 }
1315 }
1316
1317 // Non-issuer cannot send to each other if MPTCanTransfer isn't set
1318 {
1319 Env env(*this, features);
1320 Account const alice{"alice"};
1321 Account const bob{"bob"};
1322 Account const cindy{"cindy"};
1323
1324 MPTTester mptAlice(env, alice, {.holders = {bob, cindy}});
1325
1326 // alice creates issuance without MPTCanTransfer
1327 mptAlice.create({.ownerCount = 1, .holderCount = 0});
1328
1329 // bob creates a MPToken
1330 mptAlice.authorize({.account = bob});
1331
1332 // cindy creates a MPToken
1333 mptAlice.authorize({.account = cindy});
1334
1335 // alice pays bob 100 tokens
1336 mptAlice.pay(alice, bob, 100);
1337
1338 // bob tries to send cindy 10 tokens, but fails because canTransfer
1339 // is off
1340 mptAlice.pay(bob, cindy, 10, tecNO_AUTH);
1341
1342 // bob can send back to alice(issuer) just fine
1343 mptAlice.pay(bob, alice, 10);
1344 }
1345
1346 // Holder is not authorized
1347 {
1348 Env env{*this, features};
1349
1350 MPTTester mptAlice(env, alice, {.holders = {bob, carol}});
1351
1352 mptAlice.create({.ownerCount = 1, .holderCount = 0, .flags = tfMPTCanTransfer});
1353
1354 // issuer to holder
1355 mptAlice.pay(alice, bob, 100, tecNO_AUTH);
1356
1357 // holder to issuer
1358 mptAlice.pay(bob, alice, 100, tecNO_AUTH);
1359
1360 // holder to holder
1361 mptAlice.pay(bob, carol, 50, tecNO_AUTH);
1362 }
1363
1364 // Payer doesn't have enough funds
1365 {
1366 Env env{*this, features};
1367
1368 MPTTester mptAlice(env, alice, {.holders = {bob, carol}});
1369
1370 mptAlice.create({.ownerCount = 1, .flags = tfMPTCanTransfer});
1371
1372 mptAlice.authorize({.account = bob});
1373 mptAlice.authorize({.account = carol});
1374
1375 mptAlice.pay(alice, bob, 100);
1376
1377 // Pay to another holder
1378 mptAlice.pay(bob, carol, 101, tecPATH_PARTIAL);
1379
1380 // Pay to the issuer
1381 mptAlice.pay(bob, alice, 101, tecPATH_PARTIAL);
1382 }
1383
1384 // MPT is locked
1385 {
1386 Env env{*this, features};
1387
1388 MPTTester mptAlice(env, alice, {.holders = {bob, carol}});
1389
1390 mptAlice.create({.ownerCount = 1, .flags = tfMPTCanLock | tfMPTCanTransfer});
1391
1392 mptAlice.authorize({.account = bob});
1393 mptAlice.authorize({.account = carol});
1394
1395 mptAlice.pay(alice, bob, 100);
1396 mptAlice.pay(alice, carol, 100);
1397
1398 auto const err =
1399 env.current()->rules().enabled(featureMPTokensV2) ? tecPATH_DRY : tecLOCKED;
1400
1401 // Global lock
1402 mptAlice.set({.account = alice, .flags = tfMPTLock});
1403 // Can't send between holders
1404 mptAlice.pay(bob, carol, 1, err);
1405 mptAlice.pay(carol, bob, 2, err);
1406 // Issuer can send
1407 mptAlice.pay(alice, bob, 3);
1408 // Holder can send back to issuer
1409 mptAlice.pay(bob, alice, 4);
1410
1411 // Global unlock
1412 mptAlice.set({.account = alice, .flags = tfMPTUnlock});
1413 // Individual lock
1414 mptAlice.set({.account = alice, .holder = bob, .flags = tfMPTLock});
1415 // Can't send between holders
1416 mptAlice.pay(bob, carol, 5, err);
1417 mptAlice.pay(carol, bob, 6, err);
1418 // Issuer can send
1419 mptAlice.pay(alice, bob, 7);
1420 // Holder can send back to issuer
1421 mptAlice.pay(bob, alice, 8);
1422 }
1423
1424 // Transfer fee
1425 {
1426 Env env{*this, features};
1427
1428 MPTTester mptAlice(env, alice, {.holders = {bob, carol}});
1429
1430 // Transfer fee is 10%
1431 mptAlice.create({
1432 .transferFee = 10'000,
1433 .ownerCount = 1,
1434 .holderCount = 0,
1435 .flags = tfMPTCanTransfer,
1436 });
1437
1438 // Holders create MPToken
1439 mptAlice.authorize({.account = bob});
1440 mptAlice.authorize({.account = carol});
1441
1442 // Payment between the issuer and the holder, no transfer fee.
1443 mptAlice.pay(alice, bob, 2'000);
1444
1445 // Payment between the holder and the issuer, no transfer fee.
1446 mptAlice.pay(bob, alice, 1'000);
1447 BEAST_EXPECT(mptAlice.checkMPTokenAmount(bob, 1'000));
1448
1449 // Payment between the holders. The sender doesn't have
1450 // enough funds to cover the transfer fee.
1451 mptAlice.pay(bob, carol, 1'000, tecPATH_PARTIAL);
1452
1453 // Payment between the holders. The sender has enough funds
1454 // but SendMax is not included.
1455 mptAlice.pay(bob, carol, 100, tecPATH_PARTIAL);
1456
1457 auto const mpt = mptAlice["MPT"];
1458 // SendMax doesn't cover the fee
1459 env(pay(bob, carol, mpt(100)), Sendmax(mpt(109)), Ter(tecPATH_PARTIAL));
1460
1461 // Payment succeeds if sufficient SendMax is included.
1462 // 100 to carol, 10 to issuer
1463 env(pay(bob, carol, mpt(100)), Sendmax(mpt(110)));
1464 // 100 to carol, 10 to issuer
1465 env(pay(bob, carol, mpt(100)), Sendmax(mpt(115)));
1466 BEAST_EXPECT(mptAlice.checkMPTokenAmount(bob, 780));
1467 BEAST_EXPECT(mptAlice.checkMPTokenAmount(carol, 200));
1468 // Payment succeeds if partial payment even if
1469 // SendMax is less than deliver amount
1470 env(pay(bob, carol, mpt(100)), Sendmax(mpt(90)), Txflags(tfPartialPayment));
1471 // 82 to carol, 8 to issuer (90 / 1.1 ~ 81.81 (rounded to nearest) =
1472 // 82)
1473 BEAST_EXPECT(mptAlice.checkMPTokenAmount(bob, 690));
1474 // In V2 the payments are executed via the payment engine and
1475 // the rounding results in a higher quality trade
1476 BEAST_EXPECT(
1477 mptAlice.checkMPTokenAmount(carol, !features[featureMPTokensV2] ? 282 : 281));
1478 }
1479
1480 // Insufficient SendMax with no transfer fee
1481 {
1482 Env env{*this, features};
1483
1484 MPTTester mptAlice(env, alice, {.holders = {bob, carol}});
1485
1486 mptAlice.create({.ownerCount = 1, .holderCount = 0, .flags = tfMPTCanTransfer});
1487
1488 // Holders create MPToken
1489 mptAlice.authorize({.account = bob});
1490 mptAlice.authorize({.account = carol});
1491 mptAlice.pay(alice, bob, 1'000);
1492
1493 auto const mpt = mptAlice["MPT"];
1494 // SendMax is less than the amount
1495 env(pay(bob, carol, mpt(100)), Sendmax(mpt(99)), Ter(tecPATH_PARTIAL));
1496 env(pay(bob, alice, mpt(100)), Sendmax(mpt(99)), Ter(tecPATH_PARTIAL));
1497
1498 // Payment succeeds if sufficient SendMax is included.
1499 env(pay(bob, carol, mpt(100)), Sendmax(mpt(100)));
1500 BEAST_EXPECT(mptAlice.checkMPTokenAmount(carol, 100));
1501 // Payment succeeds if partial payment
1502 env(pay(bob, carol, mpt(100)), Sendmax(mpt(99)), Txflags(tfPartialPayment));
1503 BEAST_EXPECT(mptAlice.checkMPTokenAmount(carol, 199));
1504 }
1505
1506 // DeliverMin
1507 {
1508 Env env{*this, features};
1509
1510 MPTTester mptAlice(env, alice, {.holders = {bob, carol}});
1511
1512 mptAlice.create({.ownerCount = 1, .holderCount = 0, .flags = tfMPTCanTransfer});
1513
1514 // Holders create MPToken
1515 mptAlice.authorize({.account = bob});
1516 mptAlice.authorize({.account = carol});
1517 mptAlice.pay(alice, bob, 1'000);
1518
1519 auto const mpt = mptAlice["MPT"];
1520 // Fails even with the partial payment because
1521 // deliver amount < deliverMin
1522 env(pay(bob, alice, mpt(100)),
1523 Sendmax(mpt(99)),
1524 DeliverMin(mpt(100)),
1525 Txflags(tfPartialPayment),
1527 // Payment succeeds if deliver amount >= deliverMin
1528 env(pay(bob, alice, mpt(100)),
1529 Sendmax(mpt(99)),
1530 DeliverMin(mpt(99)),
1531 Txflags(tfPartialPayment));
1532 }
1533
1534 // Issuer fails trying to send more than the maximum amount allowed
1535 {
1536 Env env{*this, features};
1537
1538 MPTTester mptAlice(env, alice, {.holders = {bob}});
1539
1540 mptAlice.create(
1541 {.maxAmt = 100, .ownerCount = 1, .holderCount = 0, .flags = tfMPTCanTransfer});
1542
1543 mptAlice.authorize({.account = bob});
1544
1545 // issuer sends holder the max amount allowed
1546 mptAlice.pay(alice, bob, 100);
1547
1548 // issuer tries to exceed max amount
1549 auto const err = mpTokensV2 ? tecPATH_DRY : tecPATH_PARTIAL;
1550 mptAlice.pay(alice, bob, 1, err);
1551 }
1552
1553 // Issuer fails trying to send more than the default maximum
1554 // amount allowed
1555 {
1556 Env env{*this, features};
1557
1558 MPTTester mptAlice(env, alice, {.holders = {bob}});
1559
1560 mptAlice.create({.ownerCount = 1, .holderCount = 0});
1561
1562 mptAlice.authorize({.account = bob});
1563
1564 // issuer sends holder the default max amount allowed
1565 mptAlice.pay(alice, bob, kMaxMpTokenAmount);
1566
1567 // issuer tries to exceed max amount
1568 auto const err = mpTokensV2 ? tecPATH_DRY : tecPATH_PARTIAL;
1569 mptAlice.pay(alice, bob, 1, err);
1570 }
1571
1572 // Pay more than max amount fails in the json parser before
1573 // transactor is called
1574 {
1575 Env env{*this, features};
1576 env.fund(XRP(1'000), alice, bob);
1577 STAmount const mpt{MPTIssue{makeMptID(1, alice)}, UINT64_C(100)};
1578 json::Value jv;
1579 jv[jss::secret] = alice.name();
1580 jv[jss::tx_json] = pay(alice, bob, mpt);
1581 jv[jss::tx_json][jss::Amount][jss::value] = std::to_string(kMaxMpTokenAmount + 1);
1582 auto const jrr = env.rpc("json", "submit", to_string(jv));
1583 BEAST_EXPECT(jrr[jss::result][jss::error] == "invalidParams");
1584 BEAST_EXPECT(jrr[jss::result][jss::error_code] == RpcInvalidParams);
1585 BEAST_EXPECT(jrr[jss::result][jss::error_message] == "Invalid field 'tx_json.Amount'.");
1586 }
1587
1588 // Pay maximum amount with the transfer fee, SendMax, and
1589 // partial payment
1590 {
1591 Env env{*this, features};
1592
1593 MPTTester mptAlice(env, alice, {.holders = {bob, carol}});
1594
1595 mptAlice.create({
1596 .maxAmt = 10'000,
1597 .transferFee = 100,
1598 .ownerCount = 1,
1599 .holderCount = 0,
1600 .flags = tfMPTCanTransfer,
1601 });
1602 auto const mpt = mptAlice["MPT"];
1603
1604 mptAlice.authorize({.account = bob});
1605 mptAlice.authorize({.account = carol});
1606
1607 // issuer sends holder the max amount allowed
1608 mptAlice.pay(alice, bob, 10'000);
1609
1610 // payment between the holders
1611 env(pay(bob, carol, mpt(10'000)), Sendmax(mpt(10'000)), Txflags(tfPartialPayment));
1612 // Verify the metadata
1613 auto const meta =
1614 env.meta()->getJson(JsonOptions::Values::None)[sfAffectedNodes.fieldName];
1615 // Issuer got 10 in the transfer fees
1616 BEAST_EXPECT(
1617 meta[0u][sfModifiedNode.fieldName][sfFinalFields.fieldName]
1618 [sfOutstandingAmount.fieldName] == "9990");
1619 // Destination account got 9'990
1620 BEAST_EXPECT(
1621 meta[1u][sfModifiedNode.fieldName][sfFinalFields.fieldName]
1622 [sfMPTAmount.fieldName] == "9990");
1623 // Source account spent 10'000
1624 BEAST_EXPECT(
1625 meta[2u][sfModifiedNode.fieldName][sfPreviousFields.fieldName]
1626 [sfMPTAmount.fieldName] == "10000");
1627 BEAST_EXPECT(!meta[2u][sfModifiedNode.fieldName][sfFinalFields.fieldName].isMember(
1628 sfMPTAmount.fieldName));
1629
1630 // payment between the holders fails without
1631 // partial payment
1632 auto const err = mpTokensV2 ? tecPATH_DRY : tecPATH_PARTIAL;
1633 env(pay(bob, carol, mpt(10'000)), Sendmax(mpt(10'000)), Ter(err));
1634 }
1635
1636 // Pay maximum allowed amount
1637 {
1638 Env env{*this, features};
1639
1640 MPTTester mptAlice(env, alice, {.holders = {bob, carol}});
1641
1642 mptAlice.create({
1643 .maxAmt = kMaxMpTokenAmount,
1644 .ownerCount = 1,
1645 .holderCount = 0,
1646 .flags = tfMPTCanTransfer,
1647 });
1648 auto const mpt = mptAlice["MPT"];
1649
1650 mptAlice.authorize({.account = bob});
1651 mptAlice.authorize({.account = carol});
1652
1653 // issuer sends holder the max amount allowed
1654 mptAlice.pay(alice, bob, kMaxMpTokenAmount);
1655 BEAST_EXPECT(mptAlice.checkMPTokenOutstandingAmount(kMaxMpTokenAmount));
1656
1657 // payment between the holders
1658 mptAlice.pay(bob, carol, kMaxMpTokenAmount);
1659 BEAST_EXPECT(mptAlice.checkMPTokenOutstandingAmount(kMaxMpTokenAmount));
1660 // holder pays back to the issuer
1661 mptAlice.pay(carol, alice, kMaxMpTokenAmount);
1662 BEAST_EXPECT(mptAlice.checkMPTokenOutstandingAmount(0));
1663 }
1664
1665 // Issuer fails trying to send fund after issuance was destroyed
1666 {
1667 Env env{*this, features};
1668
1669 MPTTester mptAlice(env, alice, {.holders = {bob}});
1670
1671 mptAlice.create({.ownerCount = 1, .holderCount = 0});
1672
1673 mptAlice.authorize({.account = bob});
1674
1675 // alice destroys issuance
1676 mptAlice.destroy({.ownerCount = 0});
1677
1678 // alice tries to send bob fund after issuance is destroyed, should
1679 // fail.
1680 mptAlice.pay(alice, bob, 100, tecOBJECT_NOT_FOUND);
1681 }
1682
1683 // Non-existent issuance
1684 {
1685 Env env{*this, features}; // NOLINT TODO
1686
1687 env.fund(XRP(1'000), alice, bob);
1688
1689 STAmount const mpt{MPTID{0}, 100};
1690 auto const err =
1691 !features[featureMPTokensV2] ? Ter(tecOBJECT_NOT_FOUND) : Ter(temBAD_CURRENCY);
1692 env(pay(alice, bob, mpt), err);
1693 }
1694
1695 // Issuer fails trying to send to an account, which doesn't own MPT for
1696 // an issuance that was destroyed
1697 {
1698 Env env{*this, features};
1699
1700 MPTTester mptAlice(env, alice, {.holders = {bob}});
1701
1702 mptAlice.create({.ownerCount = 1, .holderCount = 0});
1703
1704 // alice destroys issuance
1705 mptAlice.destroy({.ownerCount = 0});
1706
1707 // alice tries to send bob who doesn't own the MPT after issuance is
1708 // destroyed, it should fail
1709 mptAlice.pay(alice, bob, 100, tecOBJECT_NOT_FOUND);
1710 }
1711
1712 // Issuers issues maximum amount of MPT to a holder, the holder should
1713 // be able to transfer the max amount to someone else
1714 {
1715 Env env{*this, features};
1716 Account const alice("alice");
1717 Account const carol("bob");
1718 Account const bob("carol");
1719
1720 MPTTester mptAlice(env, alice, {.holders = {bob, carol}});
1721
1722 mptAlice.create({.maxAmt = 100, .ownerCount = 1, .flags = tfMPTCanTransfer});
1723
1724 mptAlice.authorize({.account = bob});
1725 mptAlice.authorize({.account = carol});
1726
1727 mptAlice.pay(alice, bob, 100);
1728
1729 // transfer max amount to another holder
1730 mptAlice.pay(bob, carol, 100);
1731 }
1732
1733 // Simple payment
1734 {
1735 Env env{*this, features};
1736
1737 MPTTester mptAlice(env, alice, {.holders = {bob, carol}});
1738
1739 mptAlice.create({.ownerCount = 1, .holderCount = 0, .flags = tfMPTCanTransfer});
1740
1741 mptAlice.authorize({.account = bob});
1742 mptAlice.authorize({.account = carol});
1743
1744 // issuer to holder
1745 mptAlice.pay(alice, bob, 100);
1746
1747 // holder to issuer
1748 mptAlice.pay(bob, alice, 100);
1749
1750 // holder to holder
1751 mptAlice.pay(alice, bob, 100);
1752 mptAlice.pay(bob, carol, 50);
1753 }
1754 }
1755
1756 void
1758 {
1759 using namespace test::jtx;
1760 Account const alice("alice"); // issuer
1761 Account const bob("bob"); // holder
1762 Account const diana("diana");
1763 Account const dpIssuer("dpIssuer"); // holder
1764
1765 char const credType[] = "abcde";
1766
1767 if (features[featureCredentials])
1768 {
1769 testcase("DepositPreauth");
1770
1771 Env env(*this, features);
1772
1773 env.fund(XRP(50000), diana, dpIssuer);
1774 env.close();
1775
1776 MPTTester mptAlice(env, alice, {.holders = {bob}});
1777 mptAlice.create(
1778 {.ownerCount = 1, .holderCount = 0, .flags = tfMPTRequireAuth | tfMPTCanTransfer});
1779
1780 env(pay(diana, bob, XRP(500)));
1781 env.close();
1782
1783 // bob creates an empty MPToken
1784 mptAlice.authorize({.account = bob});
1785 // alice authorizes bob to hold funds
1786 mptAlice.authorize({.account = alice, .holder = bob});
1787
1788 // Bob require pre-authorization
1789 env(fset(bob, asfDepositAuth));
1790 env.close();
1791
1792 // alice try to send 100 MPT to bob, not authorized
1793 mptAlice.pay(alice, bob, 100, tecNO_PERMISSION);
1794 env.close();
1795
1796 // Bob authorize alice
1797 env(deposit::auth(bob, alice));
1798 env.close();
1799
1800 // alice sends 100 MPT to bob
1801 mptAlice.pay(alice, bob, 100);
1802 env.close();
1803
1804 // Create credentials
1805 env(credentials::create(alice, dpIssuer, credType));
1806 env.close();
1807 env(credentials::accept(alice, dpIssuer, credType));
1808 env.close();
1809 auto const jv = credentials::ledgerEntry(env, alice, dpIssuer, credType);
1810 std::string const credIdx = jv[jss::result][jss::index].asString();
1811
1812 // alice sends 100 MPT to bob with credentials which aren't required
1813 mptAlice.pay(alice, bob, 100, tesSUCCESS, {{credIdx}});
1814 env.close();
1815
1816 // Bob revoke authorization
1817 env(deposit::unauth(bob, alice));
1818 env.close();
1819
1820 // alice try to send 100 MPT to bob, not authorized
1821 mptAlice.pay(alice, bob, 100, tecNO_PERMISSION);
1822 env.close();
1823
1824 // alice sends 100 MPT to bob with credentials, not authorized
1825 mptAlice.pay(alice, bob, 100, tecNO_PERMISSION, {{credIdx}});
1826 env.close();
1827
1828 // Bob authorize credentials
1829 env(deposit::authCredentials(bob, {{.issuer = dpIssuer, .credType = credType}}));
1830 env.close();
1831
1832 // alice try to send 100 MPT to bob, not authorized
1833 mptAlice.pay(alice, bob, 100, tecNO_PERMISSION);
1834 env.close();
1835
1836 // alice sends 100 MPT to bob with credentials
1837 mptAlice.pay(alice, bob, 100, tesSUCCESS, {{credIdx}});
1838 env.close();
1839 }
1840
1841 testcase("DepositPreauth disabled featureCredentials");
1842 {
1843 Env env(*this, testableAmendments() - featureCredentials);
1844
1845 std::string const credIdx =
1846 "D007AE4B6E1274B4AF872588267B810C2F82716726351D1C7D38D3E5499FC6"
1847 "E2";
1848
1849 env.fund(XRP(50000), diana, dpIssuer);
1850 env.close();
1851
1852 MPTTester mptAlice(env, alice, {.holders = {bob}});
1853 mptAlice.create(
1854 {.ownerCount = 1, .holderCount = 0, .flags = tfMPTRequireAuth | tfMPTCanTransfer});
1855
1856 env(pay(diana, bob, XRP(500)));
1857 env.close();
1858
1859 // bob creates an empty MPToken
1860 mptAlice.authorize({.account = bob});
1861 // alice authorizes bob to hold funds
1862 mptAlice.authorize({.account = alice, .holder = bob});
1863
1864 // Bob require pre-authorization
1865 env(fset(bob, asfDepositAuth));
1866 env.close();
1867
1868 // alice try to send 100 MPT to bob, not authorized
1869 mptAlice.pay(alice, bob, 100, tecNO_PERMISSION);
1870 env.close();
1871
1872 // alice try to send 100 MPT to bob with credentials, amendment
1873 // disabled
1874 mptAlice.pay(alice, bob, 100, temDISABLED, {{credIdx}});
1875 env.close();
1876
1877 // Bob authorize alice
1878 env(deposit::auth(bob, alice));
1879 env.close();
1880
1881 // alice sends 100 MPT to bob
1882 mptAlice.pay(alice, bob, 100);
1883 env.close();
1884
1885 // alice sends 100 MPT to bob with credentials, amendment disabled
1886 mptAlice.pay(alice, bob, 100, temDISABLED, {{credIdx}});
1887 env.close();
1888
1889 // Bob revoke authorization
1890 env(deposit::unauth(bob, alice));
1891 env.close();
1892
1893 // alice try to send 100 MPT to bob
1894 mptAlice.pay(alice, bob, 100, tecNO_PERMISSION);
1895 env.close();
1896
1897 // alice sends 100 MPT to bob with credentials, amendment disabled
1898 mptAlice.pay(alice, bob, 100, temDISABLED, {{credIdx}});
1899 env.close();
1900 }
1901 }
1902
1903 void
1905 {
1906 testcase("MPT Issue Invalid in Transaction");
1907 using namespace test::jtx;
1908
1909 // Validate that every transaction with an amount/issue field,
1910 // which doesn't support MPT, fails.
1911
1912 // keyed by transaction + amount/issue field
1913 std::set<std::string> txWithAmounts;
1914 for (auto const& format : TxFormats::getInstance())
1915 {
1916 for (auto const& e : format.getSOTemplate())
1917 {
1918 // Transaction has amount/issue fields.
1919 // Exclude pseudo-transaction SetFee. Don't consider
1920 // the Fee field since it's included in every transaction.
1921 if (e.supportMPT() == SoeMptNotSupported && e.sField().getName() != jss::Fee &&
1922 format.getName() != std::string("SetFee"))
1923 {
1924 txWithAmounts.insert(format.getName() + e.sField().fieldName);
1925 break;
1926 }
1927 }
1928 }
1929
1930 Account const alice("alice");
1931 auto const usd = alice["USD"];
1932 Account const carol("carol");
1933 MPTIssue const issue(makeMptID(1, alice));
1934 STAmount mpt{issue, UINT64_C(100)};
1935 for (auto const& feature : {features, features - featureMPTokensV1})
1936 {
1937 Env env{*this, feature};
1938 env.fund(XRP(1'000), alice);
1939 env.fund(XRP(1'000), carol);
1940 auto test = [&](json::Value const& jv, std::string const& mptField) {
1941 txWithAmounts.erase(jv[jss::TransactionType].asString() + mptField);
1942
1943 // tx is signed
1944 auto const jtx = env.jt(jv);
1945 Serializer s;
1946 jtx.stx->add(s);
1947 auto jrr = env.rpc("submit", strHex(s.slice()));
1948 BEAST_EXPECT(jrr[jss::result][jss::error] == "invalidTransaction");
1949 BEAST_EXPECT(!jrr[jss::result].isMember(jss::error_code));
1950 BEAST_EXPECT(jrr[jss::result][jss::error_message].isNull());
1951
1952 // tx is unsigned
1953 json::Value jv1;
1954 jv1[jss::secret] = alice.name();
1955 jv1[jss::tx_json] = jv;
1956 jrr = env.rpc("json", "submit", to_string(jv1));
1957 BEAST_EXPECT(jrr[jss::result][jss::error] == "invalidParams");
1958 BEAST_EXPECT(jrr[jss::result][jss::error_code] == RpcInvalidParams);
1959 BEAST_EXPECT(jrr[jss::result][jss::error_message] == "Amount can not be MPT.");
1960
1961 jrr = env.rpc("json", "sign", to_string(jv1));
1962 BEAST_EXPECT(jrr[jss::result][jss::error] == "invalidParams");
1963 BEAST_EXPECT(jrr[jss::result][jss::error_code] == RpcInvalidParams);
1964 BEAST_EXPECT(jrr[jss::result][jss::error_message] == "Amount can not be MPT.");
1965 };
1966 auto setMPTFields = [&](SField const& field, json::Value& jv, bool withAmount = true) {
1967 jv[jss::Asset] = toJson(xrpIssue());
1968 jv[jss::Asset2] = toJson(usd.issue());
1969 if (withAmount)
1970 jv[field.fieldName] = usd(10).value().getJson(JsonOptions::Values::None);
1971 if (field == sfAsset)
1972 {
1973 jv[jss::Asset] = toJson(mpt.get<MPTIssue>());
1974 }
1975 else if (field == sfAsset2)
1976 {
1977 jv[jss::Asset2] = toJson(mpt.get<MPTIssue>());
1978 }
1979 else
1980 {
1982 }
1983 };
1984 // All transactions with sfAmount, which don't support MPT.
1985 // Transactions with amount fields, which can't be MPT.
1986 // Transactions with issue fields, which can't be MPT.
1987
1988 // AMMDeposit
1989 auto const ammDeposit = [&](SField const& field) {
1990 json::Value jv;
1991 jv[jss::TransactionType] = jss::AMMDeposit;
1992 jv[jss::Account] = alice.human();
1993 jv[jss::Flags] = tfSingleAsset;
1994 setMPTFields(field, jv);
1995 test(jv, field.fieldName);
1996 };
1997 for (SField const& field : {std::ref(sfEPrice), std::ref(sfLPTokenOut)})
1998 ammDeposit(field);
1999 // AMMWithdraw
2000 auto const ammWithdraw = [&](SField const& field) {
2001 json::Value jv;
2002 jv[jss::TransactionType] = jss::AMMWithdraw;
2003 jv[jss::Account] = alice.human();
2004 jv[jss::Flags] = tfSingleAsset;
2005 setMPTFields(field, jv);
2006 test(jv, field.fieldName);
2007 };
2008 for (SField const& field : {std::ref(sfEPrice), std::ref(sfLPTokenIn)})
2009 ammWithdraw(field);
2010 // AMMBid
2011 auto const ammBid = [&](SField const& field) {
2012 json::Value jv;
2013 jv[jss::TransactionType] = jss::AMMBid;
2014 jv[jss::Account] = alice.human();
2015 setMPTFields(field, jv);
2016 test(jv, field.fieldName);
2017 };
2018 ammBid(sfBidMin);
2019 ammBid(sfBidMax);
2020 // PaymentChannelCreate
2021 {
2022 json::Value jv;
2023 jv[jss::TransactionType] = jss::PaymentChannelCreate;
2024 jv[jss::Account] = alice.human();
2025 jv[jss::Destination] = carol.human();
2026 jv[jss::SettleDelay] = 1;
2027 jv[sfPublicKey.fieldName] = strHex(alice.pk().slice());
2028 jv[jss::Amount] = mpt.getJson(JsonOptions::Values::None);
2029 test(jv, jss::Amount.cStr());
2030 }
2031 // PaymentChannelFund
2032 {
2033 json::Value jv;
2034 jv[jss::TransactionType] = jss::PaymentChannelFund;
2035 jv[jss::Account] = alice.human();
2036 jv[sfChannel.fieldName] = to_string(UInt256{1});
2037 jv[jss::Amount] = mpt.getJson(JsonOptions::Values::None);
2038 test(jv, jss::Amount.cStr());
2039 }
2040 // PaymentChannelClaim
2041 {
2042 json::Value jv;
2043 jv[jss::TransactionType] = jss::PaymentChannelClaim;
2044 jv[jss::Account] = alice.human();
2045 jv[sfChannel.fieldName] = to_string(UInt256{1});
2046 jv[jss::Amount] = mpt.getJson(JsonOptions::Values::None);
2047 test(jv, jss::Amount.cStr());
2048 }
2049 // NFTokenCreateOffer
2050 {
2051 json::Value jv;
2052 jv[jss::TransactionType] = jss::NFTokenCreateOffer;
2053 jv[jss::Account] = alice.human();
2054 jv[sfNFTokenID.fieldName] = to_string(UInt256{1});
2055 jv[jss::Amount] = mpt.getJson(JsonOptions::Values::None);
2056 test(jv, jss::Amount.cStr());
2057 }
2058 // NFTokenAcceptOffer
2059 {
2060 json::Value jv;
2061 jv[jss::TransactionType] = jss::NFTokenAcceptOffer;
2062 jv[jss::Account] = alice.human();
2063 jv[sfNFTokenBrokerFee.fieldName] = mpt.getJson(JsonOptions::Values::None);
2064 test(jv, sfNFTokenBrokerFee.fieldName);
2065 }
2066 // NFTokenMint
2067 {
2068 json::Value jv;
2069 jv[jss::TransactionType] = jss::NFTokenMint;
2070 jv[jss::Account] = alice.human();
2071 jv[sfNFTokenTaxon.fieldName] = 1;
2072 jv[jss::Amount] = mpt.getJson(JsonOptions::Values::None);
2073 test(jv, jss::Amount.cStr());
2074 }
2075 // TrustSet
2076 auto const trustSet = [&](SField const& field) {
2077 json::Value jv;
2078 jv[jss::TransactionType] = jss::TrustSet;
2079 jv[jss::Account] = alice.human();
2080 jv[jss::Flags] = 0;
2082 test(jv, field.fieldName);
2083 };
2084 trustSet(sfLimitAmount);
2085 trustSet(sfFee);
2086 // SponsorshipSet
2087 {
2088 json::Value jv;
2089 jv[jss::TransactionType] = jss::SponsorshipSet;
2090 jv[jss::Account] = alice.human();
2091 jv[sfSponsee.fieldName] = carol.human();
2092 jv[sfFeeAmountDelta.fieldName] = mpt.getJson(JsonOptions::Values::None);
2093 test(jv, sfFeeAmountDelta.fieldName);
2094 }
2095 }
2096 BEAST_EXPECT(txWithAmounts.empty());
2097 }
2098
2099 void
2101 {
2102 using namespace test::jtx;
2103 using namespace std::literals;
2104 FeatureBitset const withoutFix = features - fixCleanup3_2_0;
2105 FeatureBitset const withFix = features | fixCleanup3_2_0;
2106 FeatureBitset const withoutFixAndV2 = withoutFix - featureMPTokensV2;
2107 FeatureBitset const withFixAndWithoutV2 = withFix - featureMPTokensV2;
2108
2109 Account const alice{"alice"};
2110 Account const bob{"bob"};
2111 Account const gw{"gw"};
2112
2113 using MPTValue = MPTAmount::value_type;
2114 MPTValue const mptMin = std::numeric_limits<MPTValue>::min();
2115 MPTValue const mptMax = std::numeric_limits<MPTValue>::max();
2117 std::uint64_t const firstInvalidMPTMantissa = static_cast<std::uint64_t>(mptMax) + 1;
2118 MPTValue const alice0 = 10'000;
2119 MPTValue const gw0 = -20'000;
2120 TER const success = tesSUCCESS;
2121 TER const invariantFailed = tecINVARIANT_FAILED;
2122 TER const pathPartial = tecPATH_PARTIAL;
2123 TER const badAmountTer = temBAD_AMOUNT;
2124
2125 struct BadMPTAmount
2126 {
2127 std::string_view name;
2128 std::uint64_t mantissa;
2129 bool negative;
2130 MPTValue mptValue;
2131 TER issuerToHolderPreFixTer;
2132 TER holderSourcePreFixTer;
2133 MPTValue issuerToHolderAliceAfterPreFix;
2134 MPTValue issuerToHolderIssuerAfterPreFix;
2135 MPTValue issuerToHolderAliceAfterPostFix;
2136 MPTValue issuerToHolderIssuerAfterPostFix;
2137 };
2138 // clang-format off
2139 std::array<BadMPTAmount, 7> const badMPTAmounts = {{
2140 { .name="INT64_MAX + 1", .mantissa=firstInvalidMPTMantissa, .negative=false, .mptValue=mptMin, .issuerToHolderPreFixTer=invariantFailed, .holderSourcePreFixTer=pathPartial, .issuerToHolderAliceAfterPreFix=alice0, .issuerToHolderIssuerAfterPreFix=gw0, .issuerToHolderAliceAfterPostFix=alice0 - 1, .issuerToHolderIssuerAfterPostFix=gw0 + 1},
2141 { .name="INT64_MAX + 10", .mantissa=firstInvalidMPTMantissa + 9, .negative=false, .mptValue=mptMin + 9, .issuerToHolderPreFixTer=invariantFailed, .holderSourcePreFixTer=pathPartial, .issuerToHolderAliceAfterPreFix=alice0, .issuerToHolderIssuerAfterPreFix=gw0, .issuerToHolderAliceAfterPostFix=alice0 - 1, .issuerToHolderIssuerAfterPostFix=gw0 + 1},
2142 { .name="UINT64_MAX - 9998", .mantissa=u64Max - 9'998, .negative=false, .mptValue=MPTValue{-9'999}, .issuerToHolderPreFixTer=success, .holderSourcePreFixTer=pathPartial, .issuerToHolderAliceAfterPreFix=alice0 - 9'999, .issuerToHolderIssuerAfterPreFix=gw0 + 9'999, .issuerToHolderAliceAfterPostFix=alice0 - 10'000, .issuerToHolderIssuerAfterPostFix=gw0 + 10'000},
2143 { .name="UINT64_MAX - 9", .mantissa=u64Max - 9, .negative=false, .mptValue=MPTValue{-10}, .issuerToHolderPreFixTer=success, .holderSourcePreFixTer=pathPartial, .issuerToHolderAliceAfterPreFix=alice0 - 10, .issuerToHolderIssuerAfterPreFix=gw0 + 10, .issuerToHolderAliceAfterPostFix=alice0 - 11, .issuerToHolderIssuerAfterPostFix=gw0 + 11},
2144 { .name="UINT64_MAX - 1", .mantissa=u64Max - 1, .negative=false, .mptValue=MPTValue{-2}, .issuerToHolderPreFixTer=success, .holderSourcePreFixTer=pathPartial, .issuerToHolderAliceAfterPreFix=alice0 - 2, .issuerToHolderIssuerAfterPreFix=gw0 + 2, .issuerToHolderAliceAfterPostFix=alice0 - 3, .issuerToHolderIssuerAfterPostFix=gw0 + 3},
2145 { .name="UINT64_MAX", .mantissa=u64Max, .negative=false, .mptValue=MPTValue{-1}, .issuerToHolderPreFixTer=success, .holderSourcePreFixTer=pathPartial, .issuerToHolderAliceAfterPreFix=alice0 - 1, .issuerToHolderIssuerAfterPreFix=gw0 + 1, .issuerToHolderAliceAfterPostFix=alice0 - 2, .issuerToHolderIssuerAfterPostFix=gw0 + 2},
2146 { .name="-2", .mantissa=std::uint64_t{2}, .negative=true, .mptValue=MPTValue{-2}, .issuerToHolderPreFixTer=badAmountTer, .holderSourcePreFixTer=badAmountTer, .issuerToHolderAliceAfterPreFix=alice0, .issuerToHolderIssuerAfterPreFix=gw0, .issuerToHolderAliceAfterPostFix=alice0 - 1, .issuerToHolderIssuerAfterPostFix=gw0 + 1},
2147 },};
2148 // clang-format on
2149 auto const badMPTAmount = [&](MPTIssue const& issue, BadMPTAmount const& bad) {
2150 return STAmount{issue, bad.mantissa, 0, bad.negative, STAmount::Unchecked{}};
2151 };
2152 auto const makeIssue = [&](Env& env) {
2153 MPTTester const mpt{{
2154 .env = env,
2155 .issuer = gw,
2156 .holders = {alice, bob},
2157 .pay = 10'000,
2158 .flags = tfMPTCanTransfer | tfMPTCanTrade | tfMPTCanEscrow | tfMPTCanClawback,
2159 }};
2160 return MPTIssue{mpt.issuanceID()};
2161 };
2162 auto const withNonCanonicalMPTAmount =
2163 [](JTx jt, SField const& field, STAmount const& amount, Account const& signer) {
2164 STTx tx{*jt.stx};
2165 tx.setFieldAmount(field, amount);
2166 tx.sign(signer.pk(), signer.sk());
2168 return jt;
2169 };
2170 auto const roundTrip = [](STTx const& tx) {
2171 Serializer s;
2172 tx.add(s);
2173 SerialIter sit{s.slice()};
2174 return STTx{sit};
2175 };
2176 auto const expectRoundTripBadMPT =
2177 [&](JTx const& jt, SField const& field, BadMPTAmount const& bad) {
2178 auto const roundTripped = roundTrip(*jt.stx);
2179 auto const persisted = roundTripped.getFieldAmount(field);
2180 BEAST_EXPECT(persisted.holds<MPTIssue>());
2181 BEAST_EXPECT(persisted.mantissa() == bad.mantissa);
2182 BEAST_EXPECT(persisted.exponent() == 0);
2183 BEAST_EXPECT(persisted.negative() == bad.negative);
2184 BEAST_EXPECT(persisted.mpt().value() == bad.mptValue);
2185 if (!bad.negative)
2186 BEAST_EXPECT(persisted.mantissa() > kMaxMpTokenAmount);
2187 };
2188
2189 for (auto const& bad : badMPTAmounts)
2190 {
2191 testcase("fixCleanup3_2_0 rejects non-canonical MPT Payment amounts");
2192 {
2193 Env env{*this, withoutFixAndV2};
2194 env.fund(XRP(100'000), alice, bob, gw);
2195 env.close();
2196 auto const issue = makeIssue(env);
2197
2198 auto const badAmount = badMPTAmount(issue, bad);
2199 auto malformedHolderToHolder = withNonCanonicalMPTAmount(
2200 env.jt(pay(alice, bob, STAmount{issue, std::uint64_t{1}})),
2201 sfAmount,
2202 badAmount,
2203 alice);
2204 expectRoundTripBadMPT(malformedHolderToHolder, sfAmount, bad);
2205 malformedHolderToHolder.ter = bad.holderSourcePreFixTer;
2206 env.submit(malformedHolderToHolder);
2207 env.close();
2208 BEAST_EXPECT(
2209 (env.balance(alice, issue).value() == STAmount{MPTAmount{10'000}, issue}));
2210 BEAST_EXPECT(
2211 (env.balance(bob, issue).value() == STAmount{MPTAmount{10'000}, issue}));
2212 BEAST_EXPECT(
2213 (env.balance(gw, issue).value() == STAmount{MPTAmount{-20'000}, issue}));
2214
2215 env.enableFeature(fixCleanup3_2_0);
2216 env.close();
2217 env(env.jt(pay(bob, alice, STAmount{issue, std::uint64_t{1}})), Ter{tesSUCCESS});
2218 env.close();
2219 BEAST_EXPECT(
2220 (env.balance(alice, issue).value() == STAmount{MPTAmount{10'001}, issue}));
2221 BEAST_EXPECT(
2222 (env.balance(bob, issue).value() == STAmount{MPTAmount{9'999}, issue}));
2223 BEAST_EXPECT(
2224 (env.balance(gw, issue).value() == STAmount{MPTAmount{-20'000}, issue}));
2225 }
2226 {
2227 Env env{*this, envconfig(), withoutFixAndV2, nullptr, beast::Severity::Disabled};
2228 env.fund(XRP(100'000), alice, bob, gw);
2229 env.close();
2230 auto const issue = makeIssue(env);
2231
2232 auto const badAmount = badMPTAmount(issue, bad);
2233 auto malformedIssuerToHolder = withNonCanonicalMPTAmount(
2234 env.jt(pay(gw, alice, STAmount{issue, std::uint64_t{1}})),
2235 sfAmount,
2236 badAmount,
2237 gw);
2238 expectRoundTripBadMPT(malformedIssuerToHolder, sfAmount, bad);
2239 malformedIssuerToHolder.ter = bad.issuerToHolderPreFixTer;
2240 env.submit(malformedIssuerToHolder);
2241 env.close();
2242 BEAST_EXPECT(
2243 (env.balance(alice, issue).value() ==
2244 STAmount{MPTAmount{bad.issuerToHolderAliceAfterPreFix}, issue}));
2245 BEAST_EXPECT(
2246 (env.balance(bob, issue).value() == STAmount{MPTAmount{10'000}, issue}));
2247 BEAST_EXPECT(
2248 (env.balance(gw, issue).value() ==
2249 STAmount{MPTAmount{bad.issuerToHolderIssuerAfterPreFix}, issue}));
2250
2251 env.enableFeature(fixCleanup3_2_0);
2252 env.close();
2253 env(env.jt(pay(alice, gw, STAmount{issue, std::uint64_t{1}})), Ter{tesSUCCESS});
2254 env.close();
2255 BEAST_EXPECT(
2256 (env.balance(alice, issue).value() ==
2257 STAmount{MPTAmount{bad.issuerToHolderAliceAfterPostFix}, issue}));
2258 BEAST_EXPECT(
2259 (env.balance(bob, issue).value() == STAmount{MPTAmount{10'000}, issue}));
2260 BEAST_EXPECT(
2261 (env.balance(gw, issue).value() ==
2262 STAmount{MPTAmount{bad.issuerToHolderIssuerAfterPostFix}, issue}));
2263 }
2264 {
2265 Env env{*this, withoutFixAndV2};
2266 env.fund(XRP(100'000), alice, bob, gw);
2267 env.close();
2268 auto const issue = makeIssue(env);
2269
2270 auto const badAmount = badMPTAmount(issue, bad);
2271 auto malformedHolderToIssuer = withNonCanonicalMPTAmount(
2272 env.jt(pay(alice, gw, STAmount{issue, std::uint64_t{1}})),
2273 sfAmount,
2274 badAmount,
2275 alice);
2276 expectRoundTripBadMPT(malformedHolderToIssuer, sfAmount, bad);
2277 malformedHolderToIssuer.ter = bad.holderSourcePreFixTer;
2278 env.submit(malformedHolderToIssuer);
2279 env.close();
2280 BEAST_EXPECT(
2281 (env.balance(alice, issue).value() == STAmount{MPTAmount{10'000}, issue}));
2282 BEAST_EXPECT(
2283 (env.balance(bob, issue).value() == STAmount{MPTAmount{10'000}, issue}));
2284 BEAST_EXPECT(
2285 (env.balance(gw, issue).value() == STAmount{MPTAmount{-20'000}, issue}));
2286
2287 env.enableFeature(fixCleanup3_2_0);
2288 env.close();
2289 env(env.jt(pay(gw, alice, STAmount{issue, std::uint64_t{1}})), Ter{tesSUCCESS});
2290 env.close();
2291 BEAST_EXPECT(
2292 (env.balance(alice, issue).value() == STAmount{MPTAmount{10'001}, issue}));
2293 BEAST_EXPECT(
2294 (env.balance(bob, issue).value() == STAmount{MPTAmount{10'000}, issue}));
2295 BEAST_EXPECT(
2296 (env.balance(gw, issue).value() == STAmount{MPTAmount{-20'001}, issue}));
2297 }
2298 {
2299 Env env{*this, withFixAndWithoutV2};
2300 env.fund(XRP(100'000), alice, bob, gw);
2301 env.close();
2302 auto const issue = makeIssue(env);
2303
2304 auto const badAmount = badMPTAmount(issue, bad);
2305 auto tx = withNonCanonicalMPTAmount(
2306 env.jt(pay(alice, bob, STAmount{issue, std::uint64_t{1}})),
2307 sfAmount,
2308 badAmount,
2309 alice);
2310 tx.ter = temBAD_AMOUNT;
2311 env.submit(tx);
2312 }
2313 {
2314 Env env{*this, withFixAndWithoutV2};
2315 env.fund(XRP(100'000), alice, bob, gw);
2316 env.close();
2317 auto const issue = makeIssue(env);
2318
2319 auto const badAmount = badMPTAmount(issue, bad);
2320 auto tx = withNonCanonicalMPTAmount(
2321 env.jt(pay(gw, alice, STAmount{issue, std::uint64_t{1}})),
2322 sfAmount,
2323 badAmount,
2324 gw);
2325 tx.ter = temBAD_AMOUNT;
2326 env.submit(tx);
2327 }
2328 {
2329 Env env{*this, withFixAndWithoutV2};
2330 env.fund(XRP(100'000), alice, bob, gw);
2331 env.close();
2332 auto const issue = makeIssue(env);
2333
2334 auto const badAmount = badMPTAmount(issue, bad);
2335 auto tx = withNonCanonicalMPTAmount(
2336 env.jt(pay(alice, gw, STAmount{issue, std::uint64_t{1}})),
2337 sfAmount,
2338 badAmount,
2339 alice);
2340 tx.ter = temBAD_AMOUNT;
2341 env.submit(tx);
2342 }
2343
2344 testcase("fixCleanup3_2_0 rejects non-canonical MPT Check amounts");
2345 {
2346 Env env{*this, envconfig(), withoutFix, nullptr, beast::Severity::Disabled};
2347 env.fund(XRP(100'000), alice, bob, gw);
2348 env.close();
2349 auto const issue = makeIssue(env);
2350
2351 auto const badSendMax = badMPTAmount(issue, bad);
2352 auto const checkSeq = env.seq(alice);
2353 auto tx = withNonCanonicalMPTAmount(
2354 env.jt(check::create(alice, bob, STAmount{issue, std::uint64_t{10}})),
2355 sfSendMax,
2356 badSendMax,
2357 alice);
2358 tx.ter = bad.negative ? TER{temBAD_AMOUNT} : TER{tesSUCCESS};
2359 env.submit(tx);
2360 env.close();
2361
2362 auto const checkKeylet = keylet::check(alice.id(), SeqProxy::rawSequence(checkSeq));
2363 auto const sleCheck = env.le(checkKeylet);
2364 BEAST_EXPECT((sleCheck != nullptr) == !bad.negative);
2365 if (sleCheck && !bad.negative)
2366 {
2367 auto const persisted = sleCheck->getFieldAmount(sfSendMax);
2368 BEAST_EXPECT(persisted.holds<MPTIssue>());
2369 BEAST_EXPECT(persisted.mantissa() == bad.mantissa);
2370 BEAST_EXPECT(persisted.negative() == bad.negative);
2371 }
2372 }
2373 {
2374 Env env{*this, envconfig(), withoutFix, nullptr, beast::Severity::Disabled};
2375 env.fund(XRP(100'000), alice, bob, gw);
2376 env.close();
2377 auto const issue = makeIssue(env);
2378
2379 auto const badSendMax = badMPTAmount(issue, bad);
2380 auto const checkSeq = env.seq(alice);
2381 auto tx = withNonCanonicalMPTAmount(
2382 env.jt(check::create(alice, bob, STAmount{issue, std::uint64_t{10}})),
2383 sfSendMax,
2384 badSendMax,
2385 alice);
2386 tx.ter = bad.negative ? TER{temBAD_AMOUNT} : TER{tesSUCCESS};
2387 env.submit(tx);
2388 env.close();
2389
2390 auto const checkKeylet = keylet::check(alice.id(), SeqProxy::rawSequence(checkSeq));
2391 BEAST_EXPECT((env.le(checkKeylet) != nullptr) == !bad.negative);
2392 if (!bad.negative)
2393 {
2394 // CheckCancel has no amount fields, but it must be able to
2395 // remove a malformed legacy Check while the fix is disabled.
2396 env(env.jt(check::cancel(alice, checkKeylet.key)), Ter{tesSUCCESS});
2397 env.close();
2398 BEAST_EXPECT(env.le(checkKeylet) == nullptr);
2399 }
2400 }
2401 {
2402 Env env{*this, envconfig(), withoutFix, nullptr, beast::Severity::Disabled};
2403 env.fund(XRP(100'000), alice, bob, gw);
2404 env.close();
2405 auto const issue = makeIssue(env);
2406
2407 auto const badSendMax = badMPTAmount(issue, bad);
2408 auto const checkSeq = env.seq(alice);
2409 auto tx = withNonCanonicalMPTAmount(
2410 env.jt(check::create(alice, bob, STAmount{issue, std::uint64_t{10}})),
2411 sfSendMax,
2412 badSendMax,
2413 alice);
2414 tx.ter = bad.negative ? TER{temBAD_AMOUNT} : TER{tesSUCCESS};
2415 env.submit(tx);
2416 env.close();
2417
2418 auto const checkKeylet = keylet::check(alice.id(), SeqProxy::rawSequence(checkSeq));
2419 BEAST_EXPECT((env.le(checkKeylet) != nullptr) == !bad.negative);
2420 if (!bad.negative)
2421 {
2422 env.enableFeature(fixCleanup3_2_0);
2423 env.close();
2424
2425 // Once the fix is enabled, CheckCancel should still remove
2426 // a legacy Check because it does not consume the bad amount.
2427 env(env.jt(check::cancel(alice, checkKeylet.key)), Ter{tesSUCCESS});
2428 env.close();
2429 BEAST_EXPECT(env.le(checkKeylet) == nullptr);
2430 }
2431 }
2432 {
2433 Env env{*this, envconfig(), withoutFix, nullptr, beast::Severity::Disabled};
2434 env.fund(XRP(100'000), alice, bob, gw);
2435 env.close();
2436 auto const issue = makeIssue(env);
2437
2438 auto const badSendMax = badMPTAmount(issue, bad);
2439 auto const checkSeq = env.seq(alice);
2440 auto tx = withNonCanonicalMPTAmount(
2441 env.jt(check::create(alice, bob, STAmount{issue, std::uint64_t{10}})),
2442 sfSendMax,
2443 badSendMax,
2444 alice);
2445 tx.ter = bad.negative ? TER{temBAD_AMOUNT} : TER{tesSUCCESS};
2446 env.submit(tx);
2447 env.close();
2448
2449 auto const checkKeylet = keylet::check(alice.id(), SeqProxy::rawSequence(checkSeq));
2450 BEAST_EXPECT((env.le(checkKeylet) != nullptr) == !bad.negative);
2451 if (!bad.negative)
2452 {
2453 env.enableFeature(fixCleanup3_2_0);
2454 env.close();
2455
2456 auto const cashAmount = STAmount{sfAmount, issue, std::uint64_t{1}, 0, false};
2457 env(env.jt(check::cash(bob, checkKeylet.key, cashAmount)), Ter{tefBAD_LEDGER});
2458 env.close();
2459 BEAST_EXPECT(env.le(checkKeylet) != nullptr);
2460 }
2461 }
2462 {
2463 Env env{*this, withoutFix};
2464 env.fund(XRP(100'000), alice, bob, gw);
2465 env.close();
2466 auto const issue = makeIssue(env);
2467
2468 auto const sendMax = STAmount{sfSendMax, issue, std::uint64_t{10}, 0, false};
2469 auto const checkSeq = env.seq(alice);
2470 env(env.jt(check::create(alice, bob, sendMax)), Ter{tesSUCCESS});
2471 env.close();
2472
2473 auto const badCashAmount = badMPTAmount(issue, bad);
2474 auto tx = withNonCanonicalMPTAmount(
2475 env.jt(
2476 check::cash(
2477 bob,
2478 keylet::check(alice.id(), SeqProxy::rawSequence(checkSeq)).key,
2479 STAmount{issue, std::uint64_t{1}})),
2480 sfAmount,
2481 badCashAmount,
2482 bob);
2483 expectRoundTripBadMPT(tx, sfAmount, bad);
2484 tx.ter = bad.holderSourcePreFixTer;
2485 env.submit(tx);
2486 env.close();
2487 BEAST_EXPECT(
2488 env.le(keylet::check(alice.id(), SeqProxy::rawSequence(checkSeq))) != nullptr);
2489 BEAST_EXPECT(
2490 (env.balance(alice, issue).value() == STAmount{MPTAmount{10'000}, issue}));
2491 BEAST_EXPECT(
2492 (env.balance(bob, issue).value() == STAmount{MPTAmount{10'000}, issue}));
2493 BEAST_EXPECT(
2494 (env.balance(gw, issue).value() == STAmount{MPTAmount{-20'000}, issue}));
2495 }
2496 {
2497 Env env{*this, withFix};
2498 env.fund(XRP(100'000), alice, bob, gw);
2499 env.close();
2500 auto const issue = makeIssue(env);
2501
2502 auto const sendMax = STAmount{sfSendMax, issue, std::uint64_t{10}, 0, false};
2503 auto const checkSeq = env.seq(alice);
2504 env(env.jt(check::create(alice, bob, sendMax)), Ter{tesSUCCESS});
2505 env.close();
2506
2507 auto const badCashAmount = badMPTAmount(issue, bad);
2508 auto tx = withNonCanonicalMPTAmount(
2509 env.jt(
2510 check::cash(
2511 bob,
2512 keylet::check(alice.id(), SeqProxy::rawSequence(checkSeq)).key,
2513 STAmount{issue, std::uint64_t{1}})),
2514 sfAmount,
2515 badCashAmount,
2516 bob);
2517 tx.ter = temBAD_AMOUNT;
2518 env.submit(tx);
2519 }
2520
2521 testcase("fixCleanup3_2_0 rejects non-canonical MPT Escrow amounts");
2522 {
2523 Env env{*this, withoutFix};
2524 env.fund(XRP(100'000), alice, bob, gw);
2525 env.close();
2526 auto const issue = makeIssue(env);
2527
2528 auto const escrowSeq = env.seq(alice);
2529 auto const badAmount = badMPTAmount(issue, bad);
2530 auto tx = withNonCanonicalMPTAmount(
2531 env.jt(
2532 escrow::create(alice, bob, STAmount{issue, std::uint64_t{1}}),
2533 escrow::kFinishTime(env.now() + 1s)),
2534 sfAmount,
2535 badAmount,
2536 alice);
2537 tx.ter = bad.negative ? TER{temBAD_AMOUNT} : TER{tecINSUFFICIENT_FUNDS};
2538 env.submit(tx);
2539 env.close();
2540 BEAST_EXPECT(
2541 env.le(keylet::escrow(alice.id(), SeqProxy::rawSequence(escrowSeq))) ==
2542 nullptr);
2543 }
2544 {
2545 Env env{*this, withFix};
2546 env.fund(XRP(100'000), alice, bob, gw);
2547 env.close();
2548 auto const issue = makeIssue(env);
2549
2550 auto const badAmount = badMPTAmount(issue, bad);
2551 auto tx = withNonCanonicalMPTAmount(
2552 env.jt(
2553 escrow::create(alice, bob, STAmount{issue, std::uint64_t{1}}),
2554 escrow::kFinishTime(env.now() + 1s)),
2555 sfAmount,
2556 badAmount,
2557 alice);
2558 tx.ter = temBAD_AMOUNT;
2559 env.submit(tx);
2560 }
2561
2562 testcase("fixCleanup3_2_0 rejects non-canonical MPT Clawback amounts");
2563 {
2564 Env env{*this, withoutFix};
2565 env.fund(XRP(100'000), alice, bob, gw);
2566 env.close();
2567 auto const issue = makeIssue(env);
2568
2569 auto const badAmount = badMPTAmount(issue, bad);
2570 auto tx = withNonCanonicalMPTAmount(
2571 env.jt(claw(gw, STAmount{issue, std::uint64_t{1}}, bob)),
2572 sfAmount,
2573 badAmount,
2574 gw);
2575 expectRoundTripBadMPT(tx, sfAmount, bad);
2576 tx.ter = bad.negative ? TER{temBAD_AMOUNT} : TER{tesSUCCESS};
2577 env.submit(tx);
2578 env.close();
2579
2580 MPTValue const bobAfter = bad.negative ? MPTValue{10'000} : MPTValue{0};
2581 MPTValue const gwAfter = bad.negative ? MPTValue{-20'000} : MPTValue{-10'000};
2582 BEAST_EXPECT(
2583 (env.balance(alice, issue).value() == STAmount{MPTAmount{10'000}, issue}));
2584 BEAST_EXPECT(
2585 (env.balance(bob, issue).value() == STAmount{MPTAmount{bobAfter}, issue}));
2586 BEAST_EXPECT(
2587 (env.balance(gw, issue).value() == STAmount{MPTAmount{gwAfter}, issue}));
2588 }
2589 {
2590 Env env{*this, withFix};
2591 env.fund(XRP(100'000), alice, bob, gw);
2592 env.close();
2593 auto const issue = makeIssue(env);
2594
2595 auto const badAmount = badMPTAmount(issue, bad);
2596 auto tx = withNonCanonicalMPTAmount(
2597 env.jt(claw(gw, STAmount{issue, std::uint64_t{1}}, bob)),
2598 sfAmount,
2599 badAmount,
2600 gw);
2601 tx.ter = temBAD_AMOUNT;
2602 env.submit(tx);
2603 env.close();
2604
2605 BEAST_EXPECT(
2606 (env.balance(alice, issue).value() == STAmount{MPTAmount{10'000}, issue}));
2607 BEAST_EXPECT(
2608 (env.balance(bob, issue).value() == STAmount{MPTAmount{10'000}, issue}));
2609 BEAST_EXPECT(
2610 (env.balance(gw, issue).value() == STAmount{MPTAmount{-20'000}, issue}));
2611 }
2612
2613 testcase("featureMPTokensV2 disabled rejects MPT OfferCreate amounts");
2614 {
2615 Env env{*this, withoutFixAndV2};
2616 env.fund(XRP(100'000), alice, bob, gw);
2617 env.close();
2618 auto const issue = makeIssue(env);
2619
2620 auto const badTakerPays = badMPTAmount(issue, bad);
2621 auto tx = withNonCanonicalMPTAmount(
2622 env.jt(offer(alice, STAmount{issue, std::uint64_t{1}}, XRP(10))),
2623 sfTakerPays,
2624 badTakerPays,
2625 alice);
2626 expectRoundTripBadMPT(tx, sfTakerPays, bad);
2627 tx.ter = temDISABLED;
2628 env.submit(tx);
2629 }
2630 {
2631 Env env{*this, withFixAndWithoutV2};
2632 env.fund(XRP(100'000), alice, bob, gw);
2633 env.close();
2634 auto const issue = makeIssue(env);
2635
2636 auto const badTakerPays = badMPTAmount(issue, bad);
2637 auto tx = withNonCanonicalMPTAmount(
2638 env.jt(offer(alice, STAmount{issue, std::uint64_t{1}}, XRP(10))),
2639 sfTakerPays,
2640 badTakerPays,
2641 alice);
2642 tx.ter = temDISABLED;
2643 env.submit(tx);
2644 }
2645 {
2646 // sfTakerPays is MPT: both amendments active. Negative offers
2647 // fail in OfferCreate::preflight() before the universal check;
2648 // positive non-canonical amounts fail in the universal check.
2649 Env env{*this, withFix};
2650 env.fund(XRP(100'000), alice, bob, gw);
2651 env.close();
2652 auto const issue = makeIssue(env);
2653
2654 auto const badTakerPays = badMPTAmount(issue, bad);
2655 auto tx = withNonCanonicalMPTAmount(
2656 env.jt(offer(alice, STAmount{issue, std::uint64_t{1}}, XRP(10))),
2657 sfTakerPays,
2658 badTakerPays,
2659 alice);
2660 tx.ter = TER{temBAD_AMOUNT};
2661 env.submit(tx);
2662 }
2663 {
2664 // sfTakerGets is MPT: both amendments active. Negative offers
2665 // fail in OfferCreate::preflight() before the universal check;
2666 // positive non-canonical amounts fail in the universal check.
2667 Env env{*this, withFix};
2668 env.fund(XRP(100'000), alice, bob, gw);
2669 env.close();
2670 auto const issue = makeIssue(env);
2671
2672 auto const badTakerGets = badMPTAmount(issue, bad);
2673 auto tx = withNonCanonicalMPTAmount(
2674 env.jt(offer(alice, XRP(10), STAmount{issue, std::uint64_t{1}})),
2675 sfTakerGets,
2676 badTakerGets,
2677 alice);
2678 tx.ter = TER{temBAD_AMOUNT};
2679 env.submit(tx);
2680 }
2681
2682 testcase("featureMPTokensV2 disabled rejects MPT AMMCreate amounts");
2683 {
2684 Env env{*this, withoutFixAndV2};
2685 env.fund(XRP(100'000), alice, bob, gw);
2686 env.close();
2687 auto const issue = makeIssue(env);
2688
2689 auto const badAmount = badMPTAmount(issue, bad);
2690 auto tx = withNonCanonicalMPTAmount(
2691 env.jt(
2692 AMM::createJv(alice.id(), STAmount{issue, std::uint64_t{1}}, XRP(1), 0),
2693 Fee(static_cast<std::uint64_t>(env.current()->fees().increment.drops()))),
2694 sfAmount,
2695 badAmount,
2696 alice);
2697 expectRoundTripBadMPT(tx, sfAmount, bad);
2698 tx.ter = temDISABLED;
2699 env.submit(tx);
2700 }
2701 {
2702 Env env{*this, withFixAndWithoutV2};
2703 env.fund(XRP(100'000), alice, bob, gw);
2704 env.close();
2705 auto const issue = makeIssue(env);
2706
2707 auto const badAmount = badMPTAmount(issue, bad);
2708 auto tx = withNonCanonicalMPTAmount(
2709 env.jt(
2710 AMM::createJv(alice.id(), STAmount{issue, std::uint64_t{1}}, XRP(1), 0),
2711 Fee(static_cast<std::uint64_t>(env.current()->fees().increment.drops()))),
2712 sfAmount,
2713 badAmount,
2714 alice);
2715 tx.ter = temDISABLED;
2716 env.submit(tx);
2717 }
2718 {
2719 // sfAmount is MPT: both amendments active, expect temBAD_AMOUNT
2720 Env env{*this, withFix};
2721 env.fund(XRP(100'000), alice, bob, gw);
2722 env.close();
2723 auto const issue = makeIssue(env);
2724
2725 auto const badAmount = badMPTAmount(issue, bad);
2726 auto tx = withNonCanonicalMPTAmount(
2727 env.jt(
2728 AMM::createJv(alice.id(), STAmount{issue, std::uint64_t{1}}, XRP(1), 0),
2729 Fee(static_cast<std::uint64_t>(env.current()->fees().increment.drops()))),
2730 sfAmount,
2731 badAmount,
2732 alice);
2733 tx.ter = temBAD_AMOUNT;
2734 env.submit(tx);
2735 }
2736
2737 testcase("featureMPTokensV2 disabled rejects MPT AMMDeposit amounts");
2738 {
2739 Env env{*this, withoutFixAndV2};
2740 env.fund(XRP(100'000), alice, bob, gw);
2741 env.close();
2742 auto const issue = makeIssue(env);
2743
2744 auto const badAmount = badMPTAmount(issue, bad);
2745 auto tx = withNonCanonicalMPTAmount(
2746 env.jt(
2747 AMM::depositJv({
2748 .account = alice,
2749 .asset1In = STAmount{issue, std::uint64_t{1}},
2750 .assets = std::make_pair(Asset{issue}, Asset{xrpIssue()}),
2751 }),
2752 Fee(static_cast<std::uint64_t>(env.current()->fees().increment.drops()))),
2753 sfAmount,
2754 badAmount,
2755 alice);
2756 expectRoundTripBadMPT(tx, sfAmount, bad);
2757 tx.ter = temDISABLED;
2758 env.submit(tx);
2759 }
2760 {
2761 Env env{*this, withFixAndWithoutV2};
2762 env.fund(XRP(100'000), alice, bob, gw);
2763 env.close();
2764 auto const issue = makeIssue(env);
2765
2766 auto const badAmount = badMPTAmount(issue, bad);
2767 auto tx = withNonCanonicalMPTAmount(
2768 env.jt(
2769 AMM::depositJv({
2770 .account = alice,
2771 .asset1In = STAmount{issue, std::uint64_t{1}},
2772 .assets = std::make_pair(Asset{issue}, Asset{xrpIssue()}),
2773 }),
2774 Fee(static_cast<std::uint64_t>(env.current()->fees().increment.drops()))),
2775 sfAmount,
2776 badAmount,
2777 alice);
2778 tx.ter = temDISABLED;
2779 env.submit(tx);
2780 }
2781 {
2782 // sfAmount is MPT: both amendments active, expect temBAD_AMOUNT
2783 Env env{*this, withFix};
2784 env.fund(XRP(100'000), alice, bob, gw);
2785 env.close();
2786 auto const issue = makeIssue(env);
2787
2788 auto const badAmount = badMPTAmount(issue, bad);
2789 auto tx = withNonCanonicalMPTAmount(
2790 env.jt(
2791 AMM::depositJv({
2792 .account = alice,
2793 .asset1In = STAmount{issue, std::uint64_t{1}},
2794 .assets = std::make_pair(Asset{issue}, Asset{xrpIssue()}),
2795 }),
2796 Fee(static_cast<std::uint64_t>(env.current()->fees().increment.drops()))),
2797 sfAmount,
2798 badAmount,
2799 alice);
2800 tx.ter = temBAD_AMOUNT;
2801 env.submit(tx);
2802 }
2803
2804 testcase("featureMPTokensV2 disabled rejects MPT AMMWithdraw amounts");
2805 {
2806 Env env{*this, withoutFixAndV2};
2807 env.fund(XRP(100'000), alice, bob, gw);
2808 env.close();
2809 auto const issue = makeIssue(env);
2810
2811 auto const badAmount = badMPTAmount(issue, bad);
2812 auto tx = withNonCanonicalMPTAmount(
2813 env.jt(
2814 AMM::withdrawJv({
2815 .account = alice,
2816 .asset1Out = STAmount{issue, std::uint64_t{1}},
2817 .assets = std::make_pair(Asset{issue}, Asset{xrpIssue()}),
2818 }),
2819 Fee(static_cast<std::uint64_t>(env.current()->fees().increment.drops()))),
2820 sfAmount,
2821 badAmount,
2822 alice);
2823 expectRoundTripBadMPT(tx, sfAmount, bad);
2824 tx.ter = temDISABLED;
2825 env.submit(tx);
2826 }
2827 {
2828 Env env{*this, withFixAndWithoutV2};
2829 env.fund(XRP(100'000), alice, bob, gw);
2830 env.close();
2831 auto const issue = makeIssue(env);
2832
2833 auto const badAmount = badMPTAmount(issue, bad);
2834 auto tx = withNonCanonicalMPTAmount(
2835 env.jt(
2836 AMM::withdrawJv({
2837 .account = alice,
2838 .asset1Out = STAmount{issue, std::uint64_t{1}},
2839 .assets = std::make_pair(Asset{issue}, Asset{xrpIssue()}),
2840 }),
2841 Fee(static_cast<std::uint64_t>(env.current()->fees().increment.drops()))),
2842 sfAmount,
2843 badAmount,
2844 alice);
2845 tx.ter = temDISABLED;
2846 env.submit(tx);
2847 }
2848 {
2849 // sfAmount is MPT: both amendments active, expect temBAD_AMOUNT
2850 Env env{*this, withFix};
2851 env.fund(XRP(100'000), alice, bob, gw);
2852 env.close();
2853 auto const issue = makeIssue(env);
2854
2855 auto const badAmount = badMPTAmount(issue, bad);
2856 auto tx = withNonCanonicalMPTAmount(
2857 env.jt(
2858 AMM::withdrawJv({
2859 .account = alice,
2860 .asset1Out = STAmount{issue, std::uint64_t{1}},
2861 .assets = std::make_pair(Asset{issue}, Asset{xrpIssue()}),
2862 }),
2863 Fee(static_cast<std::uint64_t>(env.current()->fees().increment.drops()))),
2864 sfAmount,
2865 badAmount,
2866 alice);
2867 tx.ter = temBAD_AMOUNT;
2868 env.submit(tx);
2869 }
2870
2871 testcase("featureMPTokensV2 disabled rejects MPT AMMClawback amounts");
2872 {
2873 Env env{*this, withoutFixAndV2};
2874 env.fund(XRP(100'000), alice, bob, gw);
2875 env.close();
2876 auto const issue = makeIssue(env);
2877
2878 auto const badAmount = badMPTAmount(issue, bad);
2879 auto tx = withNonCanonicalMPTAmount(
2880 env.jt(
2881 amm::ammClawback(
2882 gw,
2883 alice,
2884 Asset{issue},
2885 Asset{xrpIssue()},
2886 std::make_optional(STAmount{issue, std::uint64_t{1}})),
2887 Fee(static_cast<std::uint64_t>(env.current()->fees().increment.drops()))),
2888 sfAmount,
2889 badAmount,
2890 gw);
2891 expectRoundTripBadMPT(tx, sfAmount, bad);
2892 tx.ter = temDISABLED;
2893 env.submit(tx);
2894 }
2895 {
2896 Env env{*this, withFixAndWithoutV2};
2897 env.fund(XRP(100'000), alice, bob, gw);
2898 env.close();
2899 auto const issue = makeIssue(env);
2900
2901 auto const badAmount = badMPTAmount(issue, bad);
2902 auto tx = withNonCanonicalMPTAmount(
2903 env.jt(
2904 amm::ammClawback(
2905 gw,
2906 alice,
2907 Asset{issue},
2908 Asset{xrpIssue()},
2909 std::make_optional(STAmount{issue, std::uint64_t{1}})),
2910 Fee(static_cast<std::uint64_t>(env.current()->fees().increment.drops()))),
2911 sfAmount,
2912 badAmount,
2913 gw);
2914 tx.ter = temDISABLED;
2915 env.submit(tx);
2916 }
2917 {
2918 // sfAmount is MPT: both amendments active, expect temBAD_AMOUNT
2919 Env env{*this, withFix};
2920 env.fund(XRP(100'000), alice, bob, gw);
2921 env.close();
2922 auto const issue = makeIssue(env);
2923
2924 auto const badAmount = badMPTAmount(issue, bad);
2925 auto tx = withNonCanonicalMPTAmount(
2926 env.jt(
2927 amm::ammClawback(
2928 gw,
2929 alice,
2930 Asset{issue},
2931 Asset{xrpIssue()},
2932 std::make_optional(STAmount{issue, std::uint64_t{1}})),
2933 Fee(static_cast<std::uint64_t>(env.current()->fees().increment.drops()))),
2934 sfAmount,
2935 badAmount,
2936 gw);
2937 tx.ter = temBAD_AMOUNT;
2938 env.submit(tx);
2939 }
2940
2941 testcase("fixCleanup3_2_0 rejects non-canonical MPT VaultClawback amounts");
2942 {
2943 Env env{*this, withFix};
2944 env.fund(XRP(100'000), alice, bob, gw);
2945 env.close();
2946 auto const issue = makeIssue(env);
2947
2948 auto const badAmount = badMPTAmount(issue, bad);
2949 UInt256 const fakeVaultId = keylet::vault(gw.id(), SeqProxy::rawSequence(1)).key;
2950 auto tx = withNonCanonicalMPTAmount(
2951 env.jt(
2952 Vault::clawback({
2953 .issuer = gw,
2954 .id = fakeVaultId,
2955 .holder = alice,
2956 .amount = STAmount{issue, std::uint64_t{1}},
2957 }),
2958 Fee(static_cast<std::uint64_t>(env.current()->fees().increment.drops()))),
2959 sfAmount,
2960 badAmount,
2961 gw);
2962 tx.ter = temBAD_AMOUNT;
2963 env.submit(tx);
2964 }
2965 }
2966 }
2967
2968 void
2970 {
2971 // checks synthetically injected mptissuanceid from `tx` response
2972 testcase("Test synthetic fields from tx response");
2973
2974 using namespace test::jtx;
2975
2976 Account const alice{"alice"};
2977
2978 auto cfg = envconfig();
2979 cfg->fees.referenceFee = 10;
2980 Env env{*this, std::move(cfg), features};
2981 MPTTester mptAlice(env, alice);
2982
2983 mptAlice.create();
2984
2985 std::string const txHash{
2986 env.tx()->getJson(JsonOptions::Values::None)[jss::hash].asString(),
2987 };
2988 BEAST_EXPECTS(
2989 txHash ==
2990 "E11F0E0CA14219922B7881F060B9CEE67CFBC87E4049A441ED2AE348FF8FAC"
2991 "0E",
2992 txHash);
2993 json::Value const meta = env.rpc("tx", txHash)[jss::result][jss::meta];
2994 auto const id = meta[jss::mpt_issuance_id].asString();
2995 // Expect mpt_issuance_id field
2996 BEAST_EXPECT(meta.isMember(jss::mpt_issuance_id));
2997 BEAST_EXPECT(id == to_string(mptAlice.issuanceID()));
2998 BEAST_EXPECTS(id == "00000004AE123A8556F3CF91154711376AFB0F894F832B3D", id);
2999 }
3000
3001 void
3003 {
3004 testcase("MPT clawback validations");
3005 using namespace test::jtx;
3006
3007 // Make sure clawback cannot work when featureMPTokensV1 is disabled
3008 {
3009 Env env(*this, features - featureMPTokensV1);
3010 Account const alice{"alice"};
3011 Account const bob{"bob"};
3012
3013 env.fund(XRP(1000), alice, bob);
3014 env.close();
3015
3016 auto const usd = alice["USD"];
3017 auto const mpt = xrpl::test::jtx::MPT(alice.name(), makeMptID(env.seq(alice), alice));
3018
3019 env(claw(alice, bob["USD"](5), bob), Ter(temMALFORMED));
3020 env.close();
3021
3022 env(claw(alice, mpt(5)), Ter(temDISABLED));
3023 env.close();
3024
3025 env(claw(alice, mpt(5), bob), Ter(temDISABLED));
3026 env.close();
3027 }
3028
3029 // Test preflight
3030 {
3031 Env env(*this, features);
3032 Account const alice{"alice"};
3033 Account const bob{"bob"};
3034
3035 env.fund(XRP(1000), alice, bob);
3036 env.close();
3037
3038 auto const usd = alice["USD"];
3039 auto const mpt = xrpl::test::jtx::MPT(alice.name(), makeMptID(env.seq(alice), alice));
3040
3041 // clawing back IOU from a MPT holder fails
3042 env(claw(alice, bob["USD"](5), bob), Ter(temMALFORMED));
3043 env.close();
3044
3045 // clawing back MPT without specifying a holder fails
3046 env(claw(alice, mpt(5)), Ter(temMALFORMED));
3047 env.close();
3048
3049 // clawing back zero amount fails
3050 env(claw(alice, mpt(0), bob), Ter(temBAD_AMOUNT));
3051 env.close();
3052
3053 // alice can't claw back from herself
3054 env(claw(alice, mpt(5), alice), Ter(temMALFORMED));
3055 env.close();
3056
3057 // can't clawback negative amount
3058 env(claw(alice, mpt(-1), bob), Ter(temBAD_AMOUNT));
3059 env.close();
3060 }
3061
3062 // Preclaim - clawback fails when MPTCanClawback is disabled on issuance
3063 {
3064 Env env(*this, features);
3065 Account const alice{"alice"};
3066 Account const bob{"bob"};
3067
3068 MPTTester mptAlice(env, alice, {.holders = {bob}});
3069
3070 // enable asfAllowTrustLineClawback for alice
3071 env(fset(alice, asfAllowTrustLineClawback));
3072 env.close();
3073 env.require(Flags(alice, asfAllowTrustLineClawback));
3074
3075 // Create issuance without enabling clawback
3076 mptAlice.create({.ownerCount = 1, .holderCount = 0});
3077
3078 mptAlice.authorize({.account = bob});
3079
3080 mptAlice.pay(alice, bob, 100);
3081
3082 // alice cannot clawback before she didn't enable MPTCanClawback
3083 // asfAllowTrustLineClawback has no effect
3084 mptAlice.claw(alice, bob, 1, tecNO_PERMISSION);
3085 }
3086
3087 // Preclaim - test various scenarios
3088 {
3089 Env env(*this, features);
3090 Account const alice{"alice"};
3091 Account const bob{"bob"};
3092 Account const carol{"carol"};
3093 env.fund(XRP(1000), carol);
3094 env.close();
3095 MPTTester mptAlice(env, alice, {.holders = {bob}});
3096
3097 auto const fakeMpt =
3098 xrpl::test::jtx::MPT(alice.name(), makeMptID(env.seq(alice), alice));
3099
3100 // issuer tries to clawback MPT where issuance doesn't exist
3101 env(claw(alice, fakeMpt(5), bob), Ter(tecOBJECT_NOT_FOUND));
3102 env.close();
3103
3104 // alice creates issuance
3105 mptAlice.create({.ownerCount = 1, .holderCount = 0, .flags = tfMPTCanClawback});
3106
3107 // alice tries to clawback from someone who doesn't have MPToken
3108 mptAlice.claw(alice, bob, 1, tecOBJECT_NOT_FOUND);
3109
3110 // bob creates a MPToken
3111 mptAlice.authorize({.account = bob});
3112
3113 // clawback fails because bob currently has a balance of zero
3114 mptAlice.claw(alice, bob, 1, tecINSUFFICIENT_FUNDS);
3115
3116 // alice pays bob 100 tokens
3117 mptAlice.pay(alice, bob, 100);
3118
3119 // carol fails tries to clawback from bob because he is not the
3120 // issuer
3121 mptAlice.claw(carol, bob, 1, tecNO_PERMISSION);
3122 }
3123
3124 // clawback more than max amount
3125 // fails in the json parser before
3126 // transactor is called
3127 {
3128 Env env(*this, features);
3129 Account const alice{"alice"};
3130 Account const bob{"bob"};
3131
3132 env.fund(XRP(1000), alice, bob);
3133 env.close();
3134
3135 auto const mpt = xrpl::test::jtx::MPT(alice.name(), makeMptID(env.seq(alice), alice));
3136
3137 json::Value jv = claw(alice, mpt(1), bob);
3138 jv[jss::Amount][jss::value] = std::to_string(kMaxMpTokenAmount + 1);
3139 json::Value jv1;
3140 jv1[jss::secret] = alice.name();
3141 jv1[jss::tx_json] = jv;
3142 auto const jrr = env.rpc("json", "submit", to_string(jv1));
3143 BEAST_EXPECT(jrr[jss::result][jss::error] == "invalidParams");
3144 BEAST_EXPECT(jrr[jss::result][jss::error_code] == RpcInvalidParams);
3145 BEAST_EXPECT(
3146 jrr[jss::result][jss::error_message] == "Field 'tx_json.Amount' has invalid data.");
3147 }
3148 }
3149
3150 void
3152 {
3153 testcase("MPT Clawback");
3154 using namespace test::jtx;
3155
3156 {
3157 Env env(*this, features);
3158 Account const alice{"alice"};
3159 Account const bob{"bob"};
3160
3161 MPTTester mptAlice(env, alice, {.holders = {bob}});
3162
3163 // alice creates issuance
3164 mptAlice.create({.ownerCount = 1, .holderCount = 0, .flags = tfMPTCanClawback});
3165
3166 // bob creates a MPToken
3167 mptAlice.authorize({.account = bob});
3168
3169 // alice pays bob 100 tokens
3170 mptAlice.pay(alice, bob, 100);
3171
3172 mptAlice.claw(alice, bob, 1);
3173
3174 mptAlice.claw(alice, bob, 1000);
3175
3176 // clawback fails because bob currently has a balance of zero
3177 mptAlice.claw(alice, bob, 1, tecINSUFFICIENT_FUNDS);
3178 }
3179
3180 // Test that globally locked funds can be clawed
3181 {
3182 Env env(*this, features);
3183 Account const alice{"alice"};
3184 Account const bob{"bob"};
3185
3186 MPTTester mptAlice(env, alice, {.holders = {bob}});
3187
3188 // alice creates issuance
3189 mptAlice.create(
3190 {.ownerCount = 1, .holderCount = 0, .flags = tfMPTCanLock | tfMPTCanClawback});
3191
3192 // bob creates a MPToken
3193 mptAlice.authorize({.account = bob});
3194
3195 // alice pays bob 100 tokens
3196 mptAlice.pay(alice, bob, 100);
3197
3198 mptAlice.set({.account = alice, .flags = tfMPTLock});
3199
3200 mptAlice.claw(alice, bob, 100);
3201 }
3202
3203 // Test that individually locked funds can be clawed
3204 {
3205 Env env(*this, features);
3206 Account const alice{"alice"};
3207 Account const bob{"bob"};
3208
3209 MPTTester mptAlice(env, alice, {.holders = {bob}});
3210
3211 // alice creates issuance
3212 mptAlice.create(
3213 {.ownerCount = 1, .holderCount = 0, .flags = tfMPTCanLock | tfMPTCanClawback});
3214
3215 // bob creates a MPToken
3216 mptAlice.authorize({.account = bob});
3217
3218 // alice pays bob 100 tokens
3219 mptAlice.pay(alice, bob, 100);
3220
3221 mptAlice.set({.account = alice, .holder = bob, .flags = tfMPTLock});
3222
3223 mptAlice.claw(alice, bob, 100);
3224 }
3225
3226 // Test that unauthorized funds can be clawed back
3227 {
3228 Env env(*this, features);
3229 Account const alice{"alice"};
3230 Account const bob{"bob"};
3231
3232 MPTTester mptAlice(env, alice, {.holders = {bob}});
3233
3234 // alice creates issuance
3235 mptAlice.create(
3236 {.ownerCount = 1, .holderCount = 0, .flags = tfMPTCanClawback | tfMPTRequireAuth});
3237
3238 // bob creates a MPToken
3239 mptAlice.authorize({.account = bob});
3240
3241 // alice authorizes bob
3242 mptAlice.authorize({.account = alice, .holder = bob});
3243
3244 // alice pays bob 100 tokens
3245 mptAlice.pay(alice, bob, 100);
3246
3247 // alice unauthorizes bob
3248 mptAlice.authorize({.account = alice, .holder = bob, .flags = tfMPTUnauthorize});
3249
3250 mptAlice.claw(alice, bob, 100);
3251 }
3252 }
3253
3254 void
3256 {
3257 using namespace test::jtx;
3258 testcase("Tokens Equality");
3259 Currency const cur1{toCurrency("CU1")};
3260 Currency const cur2{toCurrency("CU2")};
3261 Account const gw1{"gw1"};
3262 Account const gw2{"gw2"};
3263 MPTID const mpt1 = makeMptID(1, gw1);
3264 MPTID const mpt1a = makeMptID(1, gw1);
3265 MPTID const mpt2 = makeMptID(1, gw2);
3266 MPTID const mpt3 = makeMptID(2, gw2);
3267 Asset const assetCur1Gw1{Issue{cur1, gw1}};
3268 Asset const assetCur1Gw1a{Issue{cur1, gw1}};
3269 Asset const assetCur2Gw1{Issue{cur2, gw1}};
3270 Asset const assetCur2Gw2{Issue{cur2, gw2}};
3271 Asset const assetMpt1Gw1{mpt1};
3272 Asset const assetMpt1Gw1a{mpt1a};
3273 Asset const assetMpt1Gw2{mpt2};
3274 Asset const assetMpt2Gw2{mpt3};
3275
3276 // Assets holding Issue
3277 // Currencies are equal regardless of the issuer
3278 BEAST_EXPECT(equalTokens(assetCur1Gw1, assetCur1Gw1a));
3279 BEAST_EXPECT(equalTokens(assetCur2Gw1, assetCur2Gw2));
3280 // Currencies are different regardless of whether the issuers
3281 // are the same or not
3282 BEAST_EXPECT(!equalTokens(assetCur1Gw1, assetCur2Gw1));
3283 BEAST_EXPECT(!equalTokens(assetCur1Gw1, assetCur2Gw2));
3284
3285 // Assets holding MPTIssue
3286 // MPTIDs are the same if the sequence and the issuer are the same
3287 BEAST_EXPECT(equalTokens(assetMpt1Gw1, assetMpt1Gw1a));
3288 // MPTIDs are different if sequence and the issuer don't match
3289 BEAST_EXPECT(!equalTokens(assetMpt1Gw1, assetMpt1Gw2));
3290 BEAST_EXPECT(!equalTokens(assetMpt1Gw2, assetMpt2Gw2));
3291
3292 // Assets holding Issue and MPTIssue
3293 BEAST_EXPECT(!equalTokens(assetCur1Gw1, assetMpt1Gw1));
3294 BEAST_EXPECT(!equalTokens(assetMpt2Gw2, assetCur2Gw2));
3295 }
3296
3297 void
3299 {
3300 using namespace test::jtx;
3301 Account const gw{"gw"};
3302 Asset const asset1{makeMptID(1, gw)};
3303 Asset const asset2{makeMptID(2, gw)};
3304 Asset const asset3{makeMptID(3, gw)};
3305 STAmount const amt1{asset1, 100};
3306 STAmount const amt2{asset2, 100};
3307 STAmount const amt3{asset3, 10'000};
3308
3309 {
3310 testcase("Test STAmount MPT arithmetic");
3311 using namespace std::string_literals;
3312 STAmount res = multiply(amt1, amt2, asset3);
3313 BEAST_EXPECT(res == amt3);
3314
3315 res = mulRound(amt1, amt2, asset3, true);
3316 BEAST_EXPECT(res == amt3);
3317
3318 res = mulRoundStrict(amt1, amt2, asset3, true);
3319 BEAST_EXPECT(res == amt3);
3320
3321 // overflow, any value > 3037000499ull
3322 STAmount mptOverflow{asset2, UINT64_C(3037000500)};
3323 try
3324 {
3325 res = multiply(mptOverflow, mptOverflow, asset3);
3326 fail("should throw runtime exception 1");
3327 }
3328 catch (std::runtime_error const& e)
3329 {
3330 BEAST_EXPECTS(e.what() == "MPT value overflow"s, e.what());
3331 }
3332 // overflow, (v1 >> 32) * v2 > 2147483648ull
3333 mptOverflow = STAmount{asset2, UINT64_C(2147483648)};
3334 uint64_t const mantissa = (2ull << 32) + 2;
3335 try
3336 {
3337 res = multiply(STAmount{asset1, mantissa}, mptOverflow, asset3);
3338 fail("should throw runtime exception 2");
3339 }
3340 catch (std::runtime_error const& e)
3341 {
3342 BEAST_EXPECTS(e.what() == "MPT value overflow"s, e.what());
3343 }
3344 }
3345
3346 {
3347 testcase("Test MPTAmount arithmetic");
3348 MPTAmount mptAmt1{100};
3349 MPTAmount const mptAmt2{100};
3350 BEAST_EXPECT((mptAmt1 += mptAmt2) == MPTAmount{200});
3351 BEAST_EXPECT(mptAmt1 == 200);
3352 BEAST_EXPECT((mptAmt1 -= mptAmt2) == mptAmt1);
3353 BEAST_EXPECT(mptAmt1 == mptAmt2);
3354 BEAST_EXPECT(mptAmt1 == 100);
3355 BEAST_EXPECT(MPTAmount::minPositiveAmount() == MPTAmount{1});
3356 }
3357
3358 {
3359 testcase("Test MPTIssue from/to Json");
3360 MPTIssue const issue1{asset1.get<MPTIssue>()};
3361 json::Value const jv = toJson(issue1);
3362 BEAST_EXPECT(jv[jss::mpt_issuance_id] == to_string(asset1.get<MPTIssue>()));
3363 BEAST_EXPECT(issue1 == mptIssueFromJson(jv));
3364 }
3365
3366 {
3367 testcase("Test Asset from/to Json");
3368 json::Value const jv = toJson(asset1);
3369 BEAST_EXPECT(jv[jss::mpt_issuance_id] == to_string(asset1.get<MPTIssue>()));
3370 BEAST_EXPECT(
3371 to_string(jv) ==
3372 "{\"mpt_issuance_id\":"
3373 "\"00000001A407AF5856CCF3C42619DAA925813FC955C72983\"}");
3374 BEAST_EXPECT(asset1 == assetFromJson(jv));
3375 }
3376 }
3377
3378 void
3380 {
3381 testcase("invalid MPTokenIssuanceCreate for DynamicMPT");
3382
3383 using namespace test::jtx;
3384 Account const alice("alice");
3385
3386 // Can not provide ImmutableFlags when DynamicMPT amendment is not enabled
3387 {
3388 Env env{*this, features - featureDynamicMPT};
3389 MPTTester mptAlice(env, alice);
3390 mptAlice.create({.ownerCount = 0, .immutableFlags = 2, .err = temDISABLED});
3391 mptAlice.create({.ownerCount = 0, .immutableFlags = 0, .err = temDISABLED});
3392 }
3393
3394 // ImmutableFlags contains invalid values
3395 {
3396 Env env{*this, features};
3397 MPTTester mptAlice(env, alice);
3398
3399 // Value 1 is reserved for MPT lock.
3400 mptAlice.create({.ownerCount = 0, .immutableFlags = 1, .err = temINVALID_FLAG});
3401 mptAlice.create({.ownerCount = 0, .immutableFlags = 17, .err = temINVALID_FLAG});
3402 mptAlice.create({.ownerCount = 0, .immutableFlags = 65535, .err = temINVALID_FLAG});
3403
3404 // ImmutableFlags can not be 0
3405 mptAlice.create({.ownerCount = 0, .immutableFlags = 0, .err = temINVALID_FLAG});
3406 }
3407 }
3408
3409 void
3411 {
3412 testcase("invalid MPTokenIssuanceSet for DynamicMPT");
3413
3414 using namespace test::jtx;
3415 Account const alice("alice");
3416 Account const bob("bob");
3417
3418 // Can not provide mutate related flags, MPTokenMetadata or TransferFee when
3419 // DynamicMPT amendment is not enabled
3420 {
3421 Env env{*this, features - featureDynamicMPT};
3422 MPTTester mptAlice(env, alice, {.holders = {bob}});
3423 auto const mptID = makeMptID(env.seq(alice), alice);
3424
3425 // Mutate related flags is not allowed when DynamicMPT is not enabled
3426 mptAlice.set(
3427 {.account = alice, .id = mptID, .flags = tfMPTSetCanLock, .err = temDISABLED});
3428
3429 // MPTokenMetadata is not allowed when DynamicMPT is not enabled
3430 mptAlice.set({.account = alice, .id = mptID, .metadata = "test", .err = temDISABLED});
3431 mptAlice.set({.account = alice, .id = mptID, .metadata = "", .err = temDISABLED});
3432
3433 // TransferFee is not allowed when DynamicMPT is not enabled
3434 mptAlice.set({.account = alice, .id = mptID, .transferFee = 100, .err = temDISABLED});
3435 mptAlice.set({.account = alice, .id = mptID, .transferFee = 0, .err = temDISABLED});
3436 }
3437
3438 // Can not provide holder when mutate related flags, MPTokenMetadata or
3439 // TransferFee is present
3440 {
3441 Env env{*this, features};
3442 MPTTester mptAlice(env, alice, {.holders = {bob}});
3443 auto const mptID = makeMptID(env.seq(alice), alice);
3444
3445 // Holder is not allowed when mutate related flags is present
3446 mptAlice.set({
3447 .account = alice,
3448 .holder = bob,
3449 .id = mptID,
3450 .flags = tfMPTSetCanLock,
3451 .err = temMALFORMED,
3452 });
3453
3454 // Holder is not allowed when MPTokenMetadata is present
3455 mptAlice.set({
3456 .account = alice,
3457 .holder = bob,
3458 .id = mptID,
3459 .metadata = "test",
3460 .err = temMALFORMED,
3461 });
3462
3463 // Holder is not allowed when TransferFee is present
3464 mptAlice.set({
3465 .account = alice,
3466 .holder = bob,
3467 .id = mptID,
3468 .transferFee = 100,
3469 .err = temMALFORMED,
3470 });
3471 }
3472
3473 // Can not lock when mutate related flags, MPTokenMetadata or
3474 // TransferFee is present
3475 {
3476 Env env{*this, features};
3477 MPTTester mptAlice(env, alice, {.holders = {bob}});
3478 mptAlice.create({.ownerCount = 1});
3479
3480 // Lock is not allowed when mutate related flags is present
3481 mptAlice.set(
3482 {.account = alice, .flags = tfMPTLock | tfMPTSetCanLock, .err = temMALFORMED});
3483
3484 // Lock is not allowed when MPTokenMetadata is present
3485 mptAlice.set(
3486 {.account = alice, .flags = tfMPTLock, .metadata = "test", .err = temMALFORMED});
3487
3488 // Lock is not allowed when TransferFee is present
3489 mptAlice.set(
3490 {.account = alice, .flags = tfMPTLock, .transferFee = 100, .err = temMALFORMED});
3491 }
3492
3493 // Flags being 0 or tfFullyCanonicalSig is fine
3494 {
3495 Env env{*this, features};
3496 MPTTester mptAlice(env, alice, {.holders = {bob}});
3497
3498 mptAlice.create({
3499 .transferFee = 10,
3500 .ownerCount = 1,
3501 .flags = tfMPTCanTransfer,
3502 .immutableFlags = tifMPTTransferFee,
3503 });
3504
3505 mptAlice.set({.account = alice, .flags = 0, .metadata = "test"});
3506 mptAlice.set({.account = alice, .flags = tfFullyCanonicalSig, .metadata = "test2"});
3507 }
3508
3509 // Invalid flags
3510 {
3511 Env env{*this, features};
3512 MPTTester mptAlice(env, alice, {.holders = {bob}});
3513 auto const mptID = makeMptID(env.seq(alice), alice);
3514
3515 for (auto const flags : {0x0200u, 0x0800u, 0x2000u, 0x0201u})
3516 {
3517 mptAlice.set(
3518 {.account = alice, .id = mptID, .flags = flags, .err = temINVALID_FLAG});
3519 }
3520 }
3521
3522 // Can not set flag which is immutable
3523 {
3524 Env env{*this, features};
3525 MPTTester mptAlice(env, alice, {.holders = {bob}});
3526
3527 mptAlice.create({
3528 .ownerCount = 1,
3529 .immutableFlags = tifMPTCanLock | tifMPTCanTrade | tifMPTCanTransfer |
3532 });
3533
3534 for (auto const& f : MPTokenIssuanceSet::flagMapping)
3535 {
3536 mptAlice.set({.account = alice, .flags = f.setFlag, .err = tecNO_PERMISSION});
3537 }
3538 }
3539
3540 // Metadata exceeding max length
3541 {
3542 Env env{*this, features};
3543 MPTTester mptAlice(env, alice, {.holders = {bob}});
3544
3545 mptAlice.create({.ownerCount = 1});
3546
3547 std::string const metadata(kMaxMpTokenMetadataLength + 1, 'a');
3548 mptAlice.set({.account = alice, .metadata = metadata, .err = temMALFORMED});
3549 }
3550
3551 // Can not set metadata when it is immutable
3552 {
3553 Env env{*this, features};
3554 MPTTester mptAlice(env, alice, {.holders = {bob}});
3555
3556 mptAlice.create({.ownerCount = 1, .immutableFlags = tifMPTMetadata});
3557 mptAlice.set({.account = alice, .metadata = "test", .err = tecNO_PERMISSION});
3558 }
3559
3560 // Transfer fee exceeding the max value
3561 {
3562 Env env{*this, features};
3563 MPTTester mptAlice(env, alice, {.holders = {bob}});
3564 auto const mptID = makeMptID(env.seq(alice), alice);
3565
3566 mptAlice.create({.ownerCount = 1});
3567
3568 mptAlice.set({
3569 .account = alice,
3570 .id = mptID,
3571 .transferFee = kMaxTransferFee + 1,
3572 .err = temBAD_TRANSFER_FEE,
3573 });
3574 }
3575
3576 // Can not set non-zero transfer fee when MPTCanTransfer is not set
3577 {
3578 Env env{*this, features};
3579 MPTTester mptAlice(env, alice, {.holders = {bob}});
3580
3581 mptAlice.create({.ownerCount = 1});
3582
3583 // MPTCanTransfer is not set, return tecNO_PERMISSION
3584 mptAlice.set({.account = alice, .transferFee = 100, .err = tecNO_PERMISSION});
3585
3586 // Setting a non-zero transfer fee is fine if MPTCanTransfer is
3587 // being enabled in the same transaction
3588 mptAlice.set({.account = alice, .flags = tfMPTSetCanTransfer, .transferFee = 100});
3589 BEAST_EXPECT(mptAlice.checkFlags(lsfMPTCanTransfer));
3590 BEAST_EXPECT(mptAlice.checkTransferFee(100));
3591 }
3592
3593 // Can not set transfer fee when it is immutable
3594 {
3595 Env env{*this, features};
3596 MPTTester mptAlice(env, alice, {.holders = {bob}});
3597
3598 mptAlice.create({
3599 .transferFee = 10,
3600 .ownerCount = 1,
3601 .flags = tfMPTCanTransfer,
3602 .immutableFlags = tifMPTTransferFee,
3603 });
3604
3605 mptAlice.set({.account = alice, .transferFee = 100, .err = tecNO_PERMISSION});
3606 mptAlice.set({.account = alice, .transferFee = 0, .err = tecNO_PERMISSION});
3607 }
3608
3609 // Set some flags immutable. Others can still be set.
3610 {
3611 Env env{*this, features};
3612 MPTTester mptAlice(env, alice, {.holders = {bob}});
3613
3614 mptAlice.create({
3615 .ownerCount = 1,
3616 .immutableFlags = tifMPTCanTrade | tifMPTCanTransfer | tifMPTMetadata,
3617 });
3618
3619 auto const canEnableFlags = {
3620 tfMPTSetCanLock,
3621 tfMPTSetRequireAuth,
3622 tfMPTSetCanEscrow,
3623 tfMPTSetCanClawback,
3624 };
3625
3626 // Can not enable immutable flags
3627 mptAlice.set({.account = alice, .flags = tfMPTSetCanTrade, .err = tecNO_PERMISSION});
3628 mptAlice.set({.account = alice, .flags = tfMPTSetCanTransfer, .err = tecNO_PERMISSION});
3629
3630 // Can enable flags which are not immutable
3631 for (auto const& mutableFlag : canEnableFlags)
3632 {
3633 mptAlice.set({.account = alice, .flags = mutableFlag});
3634 }
3635 }
3636 }
3637
3638 void
3640 {
3641 testcase("Set MPT");
3642 using namespace test::jtx;
3643
3644 Account const alice("alice");
3645
3646 // Set metadata
3647 {
3648 Env env{*this, features};
3649 MPTTester mptAlice(env, alice);
3650 mptAlice.create({.metadata = "test", .ownerCount = 1});
3651
3652 std::vector<std::string> const metadatas = {
3653 "mutate metadata",
3654 "mutate metadata 2",
3655 "mutate metadata 3",
3656 "mutate metadata 3",
3657 "test",
3658 "mutate metadata",
3659 };
3660
3661 for (auto const& metadata : metadatas)
3662 {
3663 mptAlice.set({.account = alice, .metadata = metadata});
3664 BEAST_EXPECT(mptAlice.checkMetadata(metadata));
3665 }
3666
3667 // Metadata being empty will remove the field
3668 mptAlice.set({.account = alice, .metadata = ""});
3669 BEAST_EXPECT(!mptAlice.isMetadataPresent());
3670 }
3671
3672 // Set transfer fee
3673 {
3674 Env env{*this, features};
3675 MPTTester mptAlice(env, alice);
3676 mptAlice.create({
3677 .transferFee = 100,
3678 .metadata = "test",
3679 .ownerCount = 1,
3680 .flags = tfMPTCanTransfer,
3681 });
3682
3683 for (std::uint16_t const fee :
3684 std::initializer_list<std::uint16_t>{1, 10, 100, 200, 500, 1000, kMaxTransferFee})
3685 {
3686 mptAlice.set({.account = alice, .transferFee = fee});
3687 BEAST_EXPECT(mptAlice.checkTransferFee(fee));
3688 }
3689
3690 // Setting TransferFee to zero will remove the field
3691 mptAlice.set({.account = alice, .transferFee = 0});
3692 BEAST_EXPECT(!mptAlice.isTransferFeePresent());
3693
3694 // Set transfer fee again
3695 mptAlice.set({.account = alice, .transferFee = 10});
3696 BEAST_EXPECT(mptAlice.checkTransferFee(10));
3697 }
3698
3699 // Test setting flags
3700 {
3701 auto const testFlagSet = [&](std::uint32_t setFlags) {
3702 Env env{*this, features};
3703 MPTTester mptAlice(env, alice);
3704
3705 // Create issuance and the flags can be enabled once by default.
3706 mptAlice.create({.metadata = "test", .ownerCount = 1});
3707
3708 // Setting the same immutable flag more than once is harmless.
3709 mptAlice.set({.account = alice, .flags = setFlags});
3710 mptAlice.set({.account = alice, .flags = setFlags});
3711 };
3712
3713 for (auto const& f : MPTokenIssuanceSet::flagMapping)
3714 testFlagSet(f.setFlag);
3715 }
3716 }
3717
3718 void
3720 {
3721 testcase("Set MPTCanLock");
3722 using namespace test::jtx;
3723
3724 Account const alice("alice");
3725 Account const bob("bob");
3726
3727 // Individual lock
3728 {
3729 Env env{*this, features};
3730 MPTTester mptAlice(env, alice, {.holders = {bob}});
3731 mptAlice.create({.ownerCount = 1, .holderCount = 0});
3732 mptAlice.authorize({.account = bob, .holderCount = 1});
3733
3734 // Lock bob's mptoken fails because alice has not enabled MPTCanLock
3735 mptAlice.set(
3736 {.account = alice, .holder = bob, .flags = tfMPTLock, .err = tecNO_PERMISSION});
3737
3738 // set CanLock
3739 mptAlice.set({.account = alice, .flags = tfMPTSetCanLock});
3740
3741 // Now can lock
3742 mptAlice.set({.account = alice, .holder = bob, .flags = tfMPTLock});
3743 }
3744
3745 // Global lock
3746 {
3747 Env env{*this, features};
3748 MPTTester mptAlice(env, alice, {.holders = {bob}});
3749 mptAlice.create({.ownerCount = 1, .holderCount = 0});
3750 mptAlice.authorize({.account = bob, .holderCount = 1});
3751
3752 // Lock issuance fails because alice has not enabled MPTCanLock
3753 mptAlice.set({.account = alice, .flags = tfMPTLock, .err = tecNO_PERMISSION});
3754
3755 // Set CanLock
3756 mptAlice.set({.account = alice, .flags = tfMPTSetCanLock});
3757 // Now can lock
3758 mptAlice.set({.account = alice, .flags = tfMPTLock});
3759 }
3760 }
3761
3762 void
3764 {
3765 testcase("Set MPTRequireAuth");
3766 using namespace test::jtx;
3767
3768 // test enabling RequireAuth flag on the issuance and its effect on payment
3769 // authorization
3770 Env env{*this, features};
3771 Account const alice("alice");
3772 Account const bob("bob");
3773
3774 MPTTester mptAlice(env, alice, {.holders = {bob}});
3775 mptAlice.create({.ownerCount = 1, .flags = tfMPTCanTransfer});
3776
3777 mptAlice.authorize({.account = bob});
3778 mptAlice.pay(alice, bob, 1000);
3779
3780 // Set RequireAuth
3781 mptAlice.set({.account = alice, .flags = tfMPTSetRequireAuth});
3782
3783 // This should fail because bob is not authorized yet.
3784 mptAlice.pay(alice, bob, 1000, tecNO_AUTH);
3785
3786 // Issuer authorizes bob and pay should succeed.
3787 mptAlice.authorize({.account = alice, .holder = bob});
3788 mptAlice.pay(alice, bob, 1000);
3789 }
3790
3791 void
3793 {
3794 testcase("Set MPTCanEscrow");
3795 using namespace test::jtx;
3796 using namespace std::literals;
3797
3798 Env env{*this, features};
3799 auto const baseFee = env.current()->fees().base;
3800 auto const alice = Account("alice");
3801 auto const bob = Account("bob");
3802 auto const carol = Account("carol");
3803
3804 MPTTester mptAlice(env, alice, {.holders = {carol, bob}});
3805 mptAlice.create({.ownerCount = 1, .flags = tfMPTCanTransfer});
3806 mptAlice.authorize({.account = carol});
3807 mptAlice.authorize({.account = bob});
3808
3809 auto const mpt = mptAlice["MPT"];
3810 env(pay(alice, carol, mpt(10'000)));
3811 env(pay(alice, bob, mpt(10'000)));
3812 env.close();
3813
3814 // MPTCanEscrow is not enabled
3815 env(escrow::create(carol, bob, mpt(3)),
3817 escrow::kFinishTime(env.now() + 1s),
3818 Fee(baseFee * 150),
3820
3821 // Set MPTCanEscrow
3822 mptAlice.set({.account = alice, .flags = tfMPTSetCanEscrow});
3823 env(escrow::create(carol, bob, mpt(3)),
3825 escrow::kFinishTime(env.now() + 1s),
3826 Fee(baseFee * 150));
3827 }
3828
3829 void
3831 {
3832 testcase("Set MPTCanTransfer");
3833
3834 using namespace test::jtx;
3835 Account const alice("alice");
3836 Account const bob("bob");
3837 Account const carol("carol");
3838
3839 {
3840 Env env{*this, features};
3841
3842 MPTTester mptAlice(env, alice, {.holders = {bob, carol}});
3843 mptAlice.create({.ownerCount = 1});
3844
3845 mptAlice.authorize({.account = bob});
3846 mptAlice.authorize({.account = carol});
3847
3848 // Pay to bob
3849 mptAlice.pay(alice, bob, 1000);
3850
3851 // Bob can not pay carol since MPTCanTransfer is not set
3852 mptAlice.pay(bob, carol, 50, tecNO_AUTH);
3853
3854 // Can not set non-zero transfer fee when MPTCanTransfer is not set
3855 mptAlice.set({.account = alice, .transferFee = 100, .err = tecNO_PERMISSION});
3856
3857 // Set MPTCanTransfer
3858 BEAST_EXPECT(!mptAlice.isTransferFeePresent());
3859 mptAlice.set({.account = alice, .flags = tfMPTSetCanTransfer, .transferFee = 100});
3860 BEAST_EXPECT(mptAlice.checkFlags(lsfMPTCanTransfer));
3861 BEAST_EXPECT(mptAlice.isTransferFeePresent());
3862
3863 // Bob can pay carol
3864 MPT const mptc = mptAlice;
3865 if (!features[featureMPTokensV2] && !features[fixCleanup3_5_0])
3866 {
3867 mptAlice.pay(bob, carol, 50);
3868 BEAST_EXPECT(env.balance(carol, mptc) == mptc(50));
3869 }
3870 else
3871 {
3872 // The transfer fee (0.05) is rounded up, so delivering 50
3873 // costs 51, which exceeds the implicit SendMax.
3874 mptAlice.pay(bob, carol, 50, tecPATH_PARTIAL);
3875 // 1 MPTC is the transfer fee paid by bob to the issuer.
3876 env(pay(bob, carol, mptAlice(50)), Txflags(tfPartialPayment));
3877 BEAST_EXPECT(env.balance(carol, mptc) == mptc(49));
3878 }
3879 }
3880
3881 // Can set transfer fee to zero when transfer fee is mutable (i.e.
3882 // tifMPTTransferFee is not set).
3883 {
3884 Env env{*this, features};
3885
3886 MPTTester mptAlice(env, alice, {.holders = {bob, carol}});
3887 mptAlice.create({.transferFee = 100, .ownerCount = 1, .flags = tfMPTCanTransfer});
3888
3889 BEAST_EXPECT(mptAlice.checkTransferFee(100));
3890
3891 // Setting transfer fee to zero removes the field.
3892 mptAlice.set({.account = alice, .transferFee = 0});
3893 BEAST_EXPECT(!mptAlice.isTransferFeePresent());
3894 }
3895 }
3896
3897 void
3899 {
3900 testcase("Set MPTCanClawback");
3901
3902 using namespace test::jtx;
3903 Env env(*this, features);
3904 Account const alice{"alice"};
3905 Account const bob{"bob"};
3906
3907 MPTTester mptAlice(env, alice, {.holders = {bob}});
3908
3909 mptAlice.create({.ownerCount = 1, .holderCount = 0});
3910
3911 // Bob creates an MPToken
3912 mptAlice.authorize({.account = bob});
3913
3914 // Alice pays bob 100 tokens
3915 mptAlice.pay(alice, bob, 100);
3916
3917 // MPTCanClawback is not enabled
3918 mptAlice.claw(alice, bob, 1, tecNO_PERMISSION);
3919
3920 // Set MPTCanClawback
3921 mptAlice.set({.account = alice, .flags = tfMPTSetCanClawback});
3922
3923 // Can clawback now
3924 mptAlice.claw(alice, bob, 1);
3925 }
3926
3927 void
3929 {
3930 testcase("Set MPT ImmutableFlags via MPTokenIssuanceSet");
3931
3932 using namespace test::jtx;
3933 Account const alice{"alice"};
3934 Account const bob{"bob"};
3935
3936 // ImmutableFlags requires featureDynamicMPT.
3937 {
3938 Env env(*this, features - featureDynamicMPT);
3939 MPTTester mptAlice(env, alice);
3940 mptAlice.create({.ownerCount = 1});
3941
3942 mptAlice.set(
3943 {.account = alice, .immutableFlags = tifMPTCanClawback, .err = temDISABLED});
3944 }
3945
3946 // ImmutableFlags containing tifMPTCanHoldConfidentialBalance requires
3947 // featureConfidentialTransfer.
3948 {
3949 Env env(*this, features - featureConfidentialTransfer);
3950 MPTTester mptAlice(env, alice);
3951 mptAlice.create({.ownerCount = 1});
3952
3953 mptAlice.set({
3954 .account = alice,
3955 .immutableFlags = tifMPTCanHoldConfidentialBalance,
3956 .err = temDISABLED,
3957 });
3958 }
3959
3960 // ImmutableFlags of 0, or containing unknown bits, is rejected.
3961 {
3962 Env env(*this, features);
3963 MPTTester mptAlice(env, alice);
3964 mptAlice.create({.ownerCount = 1});
3965
3966 mptAlice.set({.account = alice, .immutableFlags = 0, .err = temINVALID_FLAG});
3967 mptAlice.set({.account = alice, .immutableFlags = 1, .err = temINVALID_FLAG});
3968 }
3969
3970 // Holder is not allowed alongside ImmutableFlags, and ImmutableFlags
3971 // can not be combined with Lock/Unlock in the same transaction.
3972 {
3973 Env env(*this, features);
3974 MPTTester mptAlice(env, alice, {.holders = {bob}});
3975 mptAlice.create({.ownerCount = 1});
3976
3977 mptAlice.set({
3978 .account = alice,
3979 .holder = bob,
3980 .immutableFlags = tifMPTCanClawback,
3981 .err = temMALFORMED,
3982 });
3983
3984 mptAlice.set({
3985 .account = alice,
3986 .flags = tfMPTLock,
3987 .immutableFlags = tifMPTCanClawback,
3988 .err = temMALFORMED,
3989 });
3990 }
3991
3992 // Can sets ImmutableFlags and the capability flags in the same transaction
3993 {
3994 Env env(*this, features);
3995 MPTTester mptAlice(env, alice);
3996 mptAlice.create({.ownerCount = 1});
3997
3998 mptAlice.set({
3999 .account = alice,
4000 .flags = tfMPTSetCanClawback,
4001 .immutableFlags = tifMPTCanClawback,
4002 });
4003
4004 mptAlice.set({
4005 .account = alice,
4006 .flags = tfMPTSetCanTransfer | tfMPTSetRequireAuth,
4007 .immutableFlags = tifMPTCanTrade,
4008 });
4009 }
4010
4011 // Setting ImmutableFlags persists to the ledger, permanently blocks
4012 // enabling the corresponding capability, and merges (rather than
4013 // overwrites) across multiple transactions.
4014 {
4015 Env env(*this, features);
4016 MPTTester mptAlice(env, alice);
4017 mptAlice.create({.ownerCount = 1});
4018
4019 mptAlice.set({.account = alice, .immutableFlags = tifMPTCanClawback});
4020 BEAST_EXPECT(mptAlice.checkImmutableFlags(tifMPTCanClawback));
4021
4022 // The CanClawback can no longer be enabled.
4023 mptAlice.set({.account = alice, .flags = tfMPTSetCanClawback, .err = tecNO_PERMISSION});
4024
4025 // A distinct bit merges with the first rather than overwriting it.
4026 // Both CanClawback and CanTrade are now immutable.
4027 mptAlice.set({.account = alice, .immutableFlags = tifMPTCanTrade});
4028 BEAST_EXPECT(mptAlice.checkImmutableFlags(tifMPTCanClawback | tifMPTCanTrade));
4029
4030 // Setting the same bit again is a harmless no-op.
4031 mptAlice.set({.account = alice, .immutableFlags = tifMPTCanClawback});
4032 BEAST_EXPECT(mptAlice.checkImmutableFlags(tifMPTCanClawback | tifMPTCanTrade));
4033 }
4034 }
4035
4036 void
4038 {
4039 // Verify that directSendNoLimitMultiMPT correctly enforces MaximumAmount
4040 // when the issuer sends to multiple receivers. Pre-fixCleanup3_1_3,
4041 // a stale view.read() snapshot caused per-iteration checks to miss
4042 // aggregate overflows. Post-fix, a running total is used instead.
4043 testcase("Multi-send MaximumAmount enforcement");
4044
4045 using namespace test::jtx;
4046
4047 Account const issuer("issuer");
4048 Account const alice("alice");
4049 Account const bob("bob");
4050
4051 static constexpr std::uint64_t kMaxAmt = 150;
4052 Env env{*this, features};
4053
4054 MPTTester mptTester(env, issuer, {.holders = {alice, bob}});
4055 mptTester.create({.maxAmt = kMaxAmt, .ownerCount = 1, .flags = tfMPTCanTransfer});
4056 mptTester.authorize({.account = alice});
4057 mptTester.authorize({.account = bob});
4058
4059 Asset const asset{MPTIssue{mptTester.issuanceID()}};
4060
4061 // Each test case creates a fresh ApplyView and calls
4062 // accountSendMulti from the issuer to the given receivers.
4063 auto const runTest = [&](MultiplePaymentDestinations const& receivers,
4064 TER expectedTer,
4065 std::optional<std::uint64_t> expectedOutstanding,
4066 std::string const& label) {
4067 ApplyViewImpl av(&*env.current(), TapNone);
4068 auto const ter =
4069 accountSendMulti(av, issuer.id(), asset, receivers, env.app().getJournal("View"));
4070 BEAST_EXPECTS(ter == expectedTer, label);
4071
4072 // Only verify OutstandingAmount on success — on error the
4073 // view may contain partial state and must be discarded.
4074 if (expectedOutstanding)
4075 {
4076 auto const sle = av.peek(keylet::mptokenIssuance(mptTester.issuanceID()));
4077 if (!BEAST_EXPECT(sle))
4078 return;
4079 BEAST_EXPECTS(sle->getFieldU64(sfOutstandingAmount) == *expectedOutstanding, label);
4080 }
4081 };
4082
4084
4085 // Post-amendment: aggregate check with running total
4086 runTest(
4087 R{{alice.id(), 100}, {bob.id(), 100}},
4089 std::nullopt,
4090 "aggregate exceeds max");
4091
4092 runTest(R{{alice.id(), 75}, {bob.id(), 75}}, tesSUCCESS, kMaxAmt, "aggregate at boundary");
4093
4094 runTest(R{{alice.id(), 50}, {bob.id(), 50}}, tesSUCCESS, 100, "aggregate within limit");
4095
4096 runTest(
4097 R{{alice.id(), 150}, {bob.id(), 0}},
4098 tesSUCCESS,
4099 kMaxAmt,
4100 "one receiver at max, other zero");
4101
4102 runTest(
4103 R{{alice.id(), 151}, {bob.id(), 0}},
4105 std::nullopt,
4106 "one receiver exceeds max, other zero");
4107
4108 // Issue 50 tokens so outstandingAmount is nonzero, then verify
4109 // the third condition: outstandingAmount > maximumAmount - sendAmount - totalSendAmount
4110 mptTester.pay(issuer, alice, 50);
4111 env.close();
4112
4113 // maxAmt=150, outstanding=50, so 100 more available
4114 runTest(
4115 R{{alice.id(), 50}, {bob.id(), 50}},
4116 tesSUCCESS,
4117 kMaxAmt,
4118 "nonzero outstanding, aggregate at boundary");
4119
4120 runTest(
4121 R{{alice.id(), 50}, {bob.id(), 51}},
4123 std::nullopt,
4124 "nonzero outstanding, aggregate exceeds max");
4125
4126 runTest(
4127 R{{alice.id(), 100}, {bob.id(), 0}},
4128 tesSUCCESS,
4129 kMaxAmt,
4130 "nonzero outstanding, single send at remaining capacity");
4131
4132 runTest(
4133 R{{alice.id(), 101}, {bob.id(), 0}},
4135 std::nullopt,
4136 "nonzero outstanding, single send exceeds remaining capacity");
4137
4138 // Pre-amendment: the stale per-iteration check allows each
4139 // individual send (100 <= 150) even though the aggregate (200)
4140 // exceeds MaximumAmount. Preserved for ledger replay.
4141 {
4142 // KNOWN BUG (pre-fixCleanup3_1_3): preserved for ledger replay only
4143 env.disableFeature(fixCleanup3_1_3);
4144 runTest(
4145 R{{alice.id(), 100}, {bob.id(), 100}},
4146 tesSUCCESS,
4147 250,
4148 "pre-amendment allows over-send");
4149 env.enableFeature(fixCleanup3_1_3);
4150 }
4151 }
4152
4153 void
4155 {
4156 testcase("Offer Crossing");
4157 using namespace test::jtx;
4158 Account const gw = Account("gw");
4159 Account const alice = Account("alice");
4160 Account const carol = Account("carol");
4161 auto const usd = gw["USD"];
4162
4163 // Blocking flags
4164 auto const blockingFlags = {
4165 tfMPTCanTrade | tfMPTCanLock | tfMPTCanClawback, // global lock - holder, issuer fail
4166 tfMPTCanTrade | tfMPTRequireAuth, // not authorized - holder fails
4167 tfMPTCanTrade, // holder, issuer succeed
4168 tfMPTCanTrade | tfMPTCanLock, // local lock - holder fails
4169 tfMPTCanTransfer, // can't trade - holder, issuer fail
4170 };
4171 for (auto flags : blockingFlags)
4172 {
4173 Env env{*this, features};
4174 env.fund(XRP(1'000), gw, alice);
4175 env.close();
4176
4177 // Use CanClawback flag to distinguish global from local lock
4178 bool const lockMPToken = (flags & (tfMPTCanLock | tfMPTCanClawback)) == tfMPTCanLock;
4179 bool const lockMPTIssue =
4180 (flags & (tfMPTCanLock | tfMPTCanClawback)) == (tfMPTCanLock | tfMPTCanClawback);
4181 bool const requireAuth = (flags & tfMPTRequireAuth) != 0u;
4182
4183 auto mptTester = MPTTester({
4184 .env = env,
4185 .issuer = gw,
4186 .holders = {alice},
4187 .pay = 1'000,
4188 .flags = flags,
4189 .authHolder = true,
4190 });
4191 MPT const btc = mptTester;
4192
4193 if (requireAuth)
4194 mptTester.authorize({.account = gw, .holder = alice, .flags = tfMPTUnauthorize});
4195 if (lockMPToken)
4196 {
4197 mptTester.set({.holder = alice, .flags = tfMPTLock});
4198 }
4199 else if (lockMPTIssue)
4200 {
4201 mptTester.set({.flags = tfMPTLock});
4202 }
4203
4204 auto testOffer =
4205 [&](Account const& account, auto const& buy, auto const& sell, bool buyUSD) {
4206 auto error = [&](auto const err) -> TER {
4207 if (account == gw)
4208 return tesSUCCESS;
4209 return err;
4210 };
4211 auto const [errBuy, errSell] = [&] -> std::pair<TER, TER> {
4212 // Global lock
4213 if (lockMPTIssue)
4215 // Local lock
4216 if (lockMPToken)
4217 return std::make_pair(error(tecLOCKED), error(tecUNFUNDED_OFFER));
4218 // MPToken doesn't exist
4219 if (requireAuth)
4220 return std::make_pair(error(tecNO_AUTH), error(tecUNFUNDED_OFFER));
4221 if (flags & tfMPTCanTransfer)
4224 }();
4225
4226 auto const err = buyUSD ? errBuy : errSell;
4227
4228 auto seq(env.seq(account));
4229 env(offer(account, buy(10), sell(10)), Ter(err));
4230 env(offerCancel(account, seq));
4231 env.close();
4232 };
4233
4234 auto const testOffers = [&](Account const& account) {
4235 testOffer(account, XRP, btc, false);
4236 testOffer(account, btc, XRP, true);
4237 };
4238 testOffers(alice);
4239 testOffers(gw);
4240 }
4241
4242 // MPTokenV2 is disabled
4243 {
4244 Env env{*this, features - featureMPTokensV2};
4245
4246 MPTTester mptTester(env, gw, {.holders = {alice}});
4247
4248 mptTester.create({.ownerCount = 1, .holderCount = 0, .flags = tfMPTCanTransfer});
4249
4250 mptTester.authorize({.account = alice});
4251 mptTester.pay(gw, alice, 200);
4252
4253 env(offer(alice, XRP(100), mptTester.mpt(101)), Ter(temDISABLED));
4254 env.close();
4255 }
4256
4257 // MPTokenIssuance object doesn't exist
4258 {
4259 Env env(*this);
4260 env.fund(XRP(1'000), gw, alice);
4261 env.close();
4262 MPT const btc = MPTTester({.env = env, .issuer = gw, .holders = {alice}, .pay = 100});
4263 MPT const eth = MPT(gw, 1);
4264
4265 env(offer(alice, eth(10), btc(10)), Ter(tecOBJECT_NOT_FOUND));
4266 env(offer(alice, btc(10), eth(10)), Ter(tecUNFUNDED_OFFER));
4267 }
4268
4269 // MPToken object doesn't exist and the account is not the issuer of MPT
4270 {
4271 Env env(*this);
4272 env.fund(XRP(1'000), gw, alice);
4273 MPTTester const btc({.env = env, .issuer = gw, .holders = {alice}, .pay = 100});
4274 MPTTester const eth({.env = env, .issuer = gw});
4275
4276 env(offer(alice, eth(10), btc(10)));
4277 env(offer(alice, btc(10), eth(10)), Ter(tecUNFUNDED_OFFER));
4278 }
4279
4280 // MPTLock flag is set: MPT/MPT offer crossing with independent issuers.
4281 // gw2 issues BTC and gw issues ETH so each asset can be frozen independently.
4282 // Passive setup: bob sells BTC (offer(bob, ETH, BTC)); dan sells ETH (offer(dan, BTC,
4283 // ETH)).
4284 {
4285 Account const gw2 = Account("gw2");
4286 Account const bob = Account("bob");
4287 Account const dan = Account("dan");
4288 Env env(*this);
4289 env.fund(XRP(1'000), gw, gw2, alice, carol, bob, dan);
4290 MPTTester btc({
4291 .env = env,
4292 .issuer = gw2,
4293 .holders = {alice, carol, bob, dan, gw},
4294 .pay = 100,
4295 .flags = tfMPTCanLock | kMptDexFlags,
4296 });
4297 MPTTester eth({
4298 .env = env,
4299 .issuer = gw,
4300 .holders = {alice, carol, bob, dan, gw2},
4301 .pay = 100,
4302 .flags = tfMPTCanLock | kMptDexFlags,
4303 });
4304
4305 // bob sells BTC (takerGets=BTC); dan sells ETH (takerGets=ETH)
4306 env(offer(bob, eth(10), btc(10)), Txflags(tfPassive));
4307 env(offer(dan, btc(10), eth(10)), Txflags(tfPassive));
4308 env.close();
4309
4310 // --- Individual lock on BTC ---
4311 // alice sells locked BTC (takerGets): balance zeroed, offer unfunded
4312 btc.set({.holder = alice, .flags = tfMPTLock});
4313 env(offer(alice, eth(1), btc(1)), Ter(tecUNFUNDED_OFFER));
4314 btc.set({.holder = alice, .flags = tfMPTUnlock});
4315 // carol buys locked BTC (takerPays): locked MPToken cannot receive
4316 btc.set({.holder = carol, .flags = tfMPTLock});
4317 env(offer(carol, btc(1), eth(1)), Ter(tecLOCKED));
4318 btc.set({.holder = carol, .flags = tfMPTUnlock});
4319 // gw2 is BTC issuer: individual lock on holders does not affect issuer
4320 env(offer(gw2, eth(1), btc(1)));
4321
4322 // --- Individual lock on ETH ---
4323 // alice sells locked ETH (takerGets): balance zeroed, offer unfunded
4324 eth.set({.holder = alice, .flags = tfMPTLock});
4325 env(offer(alice, btc(1), eth(1)), Ter(tecUNFUNDED_OFFER));
4326 eth.set({.holder = alice, .flags = tfMPTUnlock});
4327 // carol buys locked ETH (takerPays): locked MPToken cannot receive
4328 eth.set({.holder = carol, .flags = tfMPTLock});
4329 env(offer(carol, eth(1), btc(1)), Ter(tecLOCKED));
4330 eth.set({.holder = carol, .flags = tfMPTUnlock});
4331 // gw is ETH issuer: individual lock on holders does not affect issuer
4332 env(offer(gw, btc(1), eth(1)));
4333
4334 // --- Global lock on BTC ---
4335 // All accounts fail regardless of role: global lock is checked in OfferCreate
4336 // before offer crossing, so it applies even to the issuer.
4337 btc.set({.flags = tfMPTLock});
4338 env(offer(alice, eth(1), btc(1)), Ter(tecLOCKED)); // alice sells BTC
4339 env(offer(alice, btc(1), eth(1)), Ter(tecLOCKED)); // alice buys BTC
4340 env(offer(gw2, eth(1), btc(1)), Ter(tecLOCKED)); // gw2 is BTC issuer, still fails
4341 btc.set({.flags = tfMPTUnlock});
4342
4343 // --- Global lock on ETH ---
4344 eth.set({.flags = tfMPTLock});
4345 env(offer(alice, btc(1), eth(1)), Ter(tecLOCKED)); // alice sells ETH
4346 env(offer(alice, eth(1), btc(1)), Ter(tecLOCKED)); // alice buys ETH
4347 env(offer(gw, btc(1), eth(1)), Ter(tecLOCKED)); // gw is ETH issuer, still fails
4348 eth.set({.flags = tfMPTUnlock});
4349
4350 // --- After all locks cleared: normal crossing succeeds ---
4351 env(offer(alice, eth(1), btc(1)));
4352 env(offer(carol, btc(1), eth(1)));
4353 }
4354
4355 // MPTRequireAuth flag is set and the account is not authorized
4356 {
4357 Env env(*this);
4358 env.fund(XRP(1'000), gw, alice);
4359 MPTTester btc({
4360 .env = env,
4361 .issuer = gw,
4362 .holders = {alice},
4363 .pay = 100,
4364 .flags = tfMPTRequireAuth | kMptDexFlags,
4365 .authHolder = true,
4366 });
4367 MPTTester const eth({
4368 .env = env,
4369 .issuer = gw,
4370 .holders = {alice},
4371 .pay = 100,
4372 .flags = tfMPTRequireAuth | kMptDexFlags,
4373 .authHolder = true,
4374 });
4375
4376 btc.authorize({.account = gw, .holder = alice, .flags = tfMPTUnauthorize});
4377
4378 env(offer(alice, eth(10), btc(10)), Ter(tecUNFUNDED_OFFER));
4379
4380 // issuer can create
4381
4382 env(offer(gw, eth(10), btc(10)));
4383 env.close();
4384 }
4385
4386 // MPTCanTransfer is not set and the account is not the issuer of MPT
4387 {
4388 Env env(*this);
4389 env.fund(XRP(1'000), gw, alice, carol);
4390 MPTTester const btc({
4391 .env = env,
4392 .issuer = gw,
4393 .holders = {alice, carol},
4394 .pay = 100,
4395 .flags = tfMPTCanTrade | tfMPTCanTransfer,
4396 });
4397 MPTTester const eth({
4398 .env = env,
4399 .issuer = gw,
4400 .holders = {alice, carol},
4401 .pay = 100,
4402 .flags = tfMPTCanTrade,
4403 });
4404
4405 // Can create
4406 env(offer(alice, eth(10), btc(10)), Txflags(tfPassive));
4407 BEAST_EXPECT(getAccountOffers(env, alice)[jss::offers].size() == 1);
4408
4409 // issuer can create
4410 env(offer(gw, eth(10), btc(10)), Txflags(tfPassive));
4411 env.close();
4412
4413 // can cross issuer's offer, other offers are removed
4414 env(offer(carol, btc(10), eth(10)));
4415 BEAST_EXPECT(expectOffers(env, alice, 0));
4416 BEAST_EXPECT(expectOffers(env, gw, 0));
4417 BEAST_EXPECT(expectOffers(env, carol, 0));
4418 // can't cross holder's offer, holder's offer is removed
4419 env(offer(alice, eth(10), btc(10)), Txflags(tfPassive));
4420 env(offer(carol, btc(10), eth(10)));
4421 BEAST_EXPECT(expectOffers(env, alice, 0));
4422 BEAST_EXPECT(expectOffers(env, carol, 1));
4423 }
4424
4425 // MPTCanTrade is disabled
4426 {
4427 Env env(*this);
4428 env.fund(XRP(1'000), gw, alice, carol);
4429 MPTTester const btc({
4430 .env = env,
4431 .issuer = gw,
4432 .holders = {alice, carol},
4433 .pay = 100,
4434 .flags = tfMPTCanTransfer,
4435 .immutableFlags = tifMPTCanTrade,
4436 });
4437 MPTTester const eth({
4438 .env = env,
4439 .issuer = gw,
4440 .holders = {alice, carol},
4441 .pay = 100,
4442 .flags = tfMPTCanTrade,
4443 .immutableFlags = tifMPTCanTrade,
4444 });
4445
4446 // Can't create
4447 env(offer(gw, eth(10), btc(10)), Ter(tecNO_PERMISSION));
4448 env.close();
4449 }
4450
4451 // XRP/MPT
4452 {
4453 Env env{*this, features};
4454
4455 MPTTester mptTester(env, gw, {.holders = {alice, carol}});
4456
4457 mptTester.create(
4458 {.ownerCount = 1, .holderCount = 0, .flags = tfMPTCanTransfer | tfMPTCanTrade});
4459 auto const mpt = mptTester["MPT"];
4460
4461 mptTester.authorize({.account = alice});
4462 mptTester.pay(gw, alice, 200);
4463
4464 mptTester.authorize({.account = carol});
4465 mptTester.pay(gw, carol, 200);
4466
4467 env(offer(alice, XRP(100), mpt(101)));
4468 env.close();
4469 BEAST_EXPECT(expectOffers(env, alice, 1, {{Amounts{XRP(100), mpt(101)}}}));
4470
4471 env(offer(carol, mpt(101), XRP(100)));
4472 env.close();
4473 BEAST_EXPECT(expectOffers(env, alice, 0));
4474 BEAST_EXPECT(expectOffers(env, carol, 0));
4475 BEAST_EXPECT(mptTester.checkMPTokenOutstandingAmount(400));
4476 BEAST_EXPECT(mptTester.checkMPTokenAmount(alice, 99));
4477 BEAST_EXPECT(mptTester.checkMPTokenAmount(carol, 301));
4478 }
4479
4480 // IOU/MPT
4481 {
4482 Env env{*this, features};
4483
4484 MPTTester mptTester(env, gw, {.holders = {alice, carol}});
4485
4486 mptTester.create(
4487 {.ownerCount = 1, .holderCount = 0, .flags = tfMPTCanTransfer | tfMPTCanTrade});
4488 auto const mpt = mptTester["MPT"];
4489
4490 env(trust(alice, usd(2'000)));
4491 env(pay(gw, alice, usd(1'000)));
4492 env.close();
4493
4494 env(trust(carol, usd(2'000)));
4495 env(pay(gw, carol, usd(1'000)));
4496 env.close();
4497
4498 mptTester.authorize({.account = alice});
4499 mptTester.pay(gw, alice, 200);
4500
4501 mptTester.authorize({.account = carol});
4502 mptTester.pay(gw, carol, 200);
4503
4504 env(offer(alice, usd(100), mpt(101)));
4505 env.close();
4506 BEAST_EXPECT(expectOffers(env, alice, 1, {{Amounts{usd(100), mpt(101)}}}));
4507
4508 env(offer(carol, mpt(101), usd(100)));
4509 env.close();
4510
4511 BEAST_EXPECT(env.balance(alice, usd) == usd(1'100));
4512 BEAST_EXPECT(env.balance(carol, usd) == usd(900));
4513 BEAST_EXPECT(expectOffers(env, alice, 0));
4514 BEAST_EXPECT(expectOffers(env, carol, 0));
4515 BEAST_EXPECT(mptTester.checkMPTokenOutstandingAmount(400));
4516 BEAST_EXPECT(mptTester.checkMPTokenAmount(alice, 99));
4517 BEAST_EXPECT(mptTester.checkMPTokenAmount(carol, 301));
4518 }
4519
4520 // MPT/MPT
4521 {
4522 Env env{*this, features};
4523
4524 MPTTester mptTester1(env, gw, {.holders = {alice, carol}});
4525 mptTester1.create(
4526 {.ownerCount = 1, .holderCount = 0, .flags = tfMPTCanTransfer | tfMPTCanTrade});
4527 auto const mpt1 = mptTester1["MPT1"];
4528
4529 MPTTester mptTester2(env, gw, {.holders = {alice, carol}, .fund = false});
4530 mptTester2.create(
4531 {.ownerCount = 2, .holderCount = 0, .flags = tfMPTCanTransfer | tfMPTCanTrade});
4532 auto const mpt2 = mptTester2["MPT2"];
4533
4534 mptTester1.authorize({.account = alice});
4535 mptTester1.authorize({.account = carol});
4536 mptTester1.pay(gw, alice, 200);
4537 mptTester1.pay(gw, carol, 200);
4538
4539 mptTester2.authorize({.account = alice});
4540 mptTester2.authorize({.account = carol});
4541 mptTester2.pay(gw, alice, 200);
4542 mptTester2.pay(gw, carol, 200);
4543
4544 env(offer(alice, mpt2(100), mpt1(101)));
4545 env.close();
4546 BEAST_EXPECT(expectOffers(env, alice, 1, {{Amounts{mpt2(100), mpt1(101)}}}));
4547
4548 env(offer(carol, mpt1(101), mpt2(100)));
4549 env.close();
4550
4551 BEAST_EXPECT(expectOffers(env, alice, 0));
4552 BEAST_EXPECT(expectOffers(env, carol, 0));
4553 BEAST_EXPECT(mptTester1.checkMPTokenOutstandingAmount(400));
4554 BEAST_EXPECT(mptTester1.checkMPTokenAmount(alice, 99));
4555 BEAST_EXPECT(mptTester1.checkMPTokenAmount(carol, 301));
4556 BEAST_EXPECT(mptTester2.checkMPTokenOutstandingAmount(400));
4557 BEAST_EXPECT(mptTester2.checkMPTokenAmount(alice, 300));
4558 BEAST_EXPECT(mptTester2.checkMPTokenAmount(carol, 100));
4559 }
4560 }
4561
4562 void
4564 {
4565 testcase("Cross Asset Payment");
4566 using namespace test::jtx;
4567 Account const gw = Account("gw");
4568 Account const gw2 = Account("gw2");
4569 Account const alice = Account("alice");
4570 Account const carol = Account("carol");
4571 Account const bob = Account("bob");
4572 auto const usd = gw["USD"];
4573
4574 // Loop
4575 {
4576 Env env{*this, features};
4577 MPTTester mptTester(env, gw, {.holders = {carol, bob}});
4578
4579 mptTester.create(
4580 {.ownerCount = 1, .holderCount = 0, .flags = tfMPTCanTransfer | tfMPTCanTrade});
4581 auto const mpt = mptTester["MPT"];
4582
4583 mptTester.authorize({.account = carol});
4584 mptTester.pay(gw, carol, 200);
4585
4586 mptTester.authorize({.account = bob});
4587
4588 // holder to holder
4589 env(pay(carol, bob, mpt(1)),
4590 test::jtx::Path(~mpt, ~usd, ~mpt),
4591 Sendmax(XRP(1)),
4592 Txflags(tfPartialPayment),
4594 env.close();
4595
4596 // issuer to holder
4597 env(pay(gw, bob, mpt(1)),
4598 test::jtx::Path(~mpt, ~usd, ~mpt),
4599 Sendmax(XRP(1)),
4600 Txflags(tfPartialPayment),
4602 env.close();
4603
4604 // holder to issuer
4605 env(pay(bob, gw, mpt(1)),
4606 test::jtx::Path(~mpt, ~usd, ~mpt),
4607 Sendmax(XRP(1)),
4608 Txflags(tfPartialPayment),
4610 env.close();
4611 }
4612
4613 // Rippling
4614 {
4615 Env env{*this, features};
4616 MPTTester mptTester(env, gw, {.holders = {carol, bob}});
4617
4618 mptTester.create(
4619 {.ownerCount = 1, .holderCount = 0, .flags = tfMPTCanTransfer | tfMPTCanTrade});
4620 auto const mpt = mptTester["MPT"];
4621
4622 mptTester.authorize({.account = carol});
4623 mptTester.pay(gw, carol, 200);
4624
4625 mptTester.authorize({.account = bob});
4626
4627 // holder to holder
4628 env(pay(carol, bob, mpt(1)),
4629 test::jtx::Path(~mpt, gw),
4630 Sendmax(XRP(1)),
4631 Txflags(tfPartialPayment),
4632 Ter(temBAD_PATH));
4633 env.close();
4634
4635 // issuer to holder
4636 env(pay(gw, bob, mpt(1)),
4637 test::jtx::Path(~mpt, carol),
4638 Sendmax(XRP(1)),
4639 Txflags(tfPartialPayment),
4640 Ter(temBAD_PATH));
4641 env.close();
4642
4643 // holder to issuer
4644 env(pay(bob, gw, mpt(1)),
4645 test::jtx::Path(~mpt, carol),
4646 Sendmax(XRP(1)),
4647 Txflags(tfPartialPayment),
4648 Ter(temBAD_PATH));
4649 env.close();
4650 }
4651
4652 // MPTokenV2 is disabled
4653 {
4654 Env env{*this, features - featureMPTokensV2};
4655
4656 MPTTester mptTester(env, gw, {.holders = {alice}});
4657
4658 mptTester.create(
4659 {.ownerCount = 1, .holderCount = 0, .flags = tfMPTCanTransfer | tfMPTCanTrade});
4660 auto const mpt = mptTester["MPT"];
4661
4662 mptTester.authorize({.account = alice});
4663
4664 env(pay(gw, alice, mpt(101)),
4665 test::jtx::Path(~mpt),
4666 Sendmax(XRP(100)),
4667 Txflags(tfPartialPayment),
4668 Ter(temMALFORMED));
4669 }
4670
4671 {
4672 auto const ed = Account{"ed"};
4673 Env env{*this, features};
4674 env.fund(XRP(1'000), gw, alice, carol, bob, ed);
4675 MPTTester btc({
4676 .env = env,
4677 .issuer = gw,
4678 .holders = {alice, carol, bob},
4679 .pay = 1'000,
4680 .flags = tfMPTCanLock | kMptDexFlags,
4681 });
4682 MPTTester eth({
4683 .env = env,
4684 .issuer = gw,
4685 .holders = {alice, carol, bob},
4686 .pay = 1'000,
4687 .flags = tfMPTCanLock | kMptDexFlags,
4688 });
4689 MPTTester const usd({
4690 .env = env,
4691 .issuer = gw,
4692 .holders = {alice, carol, bob},
4693 .pay = 1'000,
4694 .flags = kMptDexFlags | tfMPTCanLock,
4695 });
4696 MPTTester const cad({
4697 .env = env,
4698 .issuer = gw,
4699 .holders = {alice, carol, bob},
4700 .pay = 1'000,
4701 .flags = kMptDexFlags | tfMPTCanLock,
4702 });
4703
4704 env(offer(bob, eth(1'000), btc(1'000)), Txflags(tfPassive));
4705 env.close();
4706 env(offer(bob, btc(1'000), eth(1'000)), Txflags(tfPassive));
4707 env.close();
4708
4709 // MPTokenIssuance doesn't exist
4710
4711 env(pay(alice, carol, MPT(gw, 1'000)(10)), Sendmax(eth(10)), Ter(tecOBJECT_NOT_FOUND));
4712 env.close();
4713 env(pay(alice, carol, eth(10)), Sendmax(MPT(gw)(10)), Ter(tecOBJECT_NOT_FOUND));
4714 env.close();
4715
4716 // MPToken object doesn't exist
4717
4718 // holder and issuer fail
4719 env(pay(ed, carol, btc(10)), Sendmax(eth(10)), Ter(tecNO_AUTH));
4720 env(pay(carol, ed, btc(10)), Sendmax(eth(10)), Ter(tecNO_AUTH));
4721 env(pay(ed, gw, btc(10)), Sendmax(eth(10)), Ter(tecNO_AUTH));
4722 env(pay(gw, ed, btc(10)), Sendmax(eth(10)), Ter(tecNO_AUTH));
4723 env.close();
4724
4725 // MPTRequireAuth is set
4726
4727 btc.authorize({.account = ed});
4728 eth.authorize({.account = ed});
4729 env(pay(gw, ed, eth(100)));
4730 env(pay(gw, ed, btc(100)));
4731 env.close();
4732 btc.set({.flags = tfMPTSetRequireAuth});
4733 // authorize bob to enable the offers trading
4734 btc.authorize({.account = gw, .holder = bob});
4735 env.close();
4736 env(pay(ed, carol, btc(10)), Path(~btc), Sendmax(eth(10)), Ter(tecNO_AUTH));
4737 env(pay(carol, ed, btc(10)), Path(~btc), Sendmax(eth(10)), Ter(tecNO_AUTH));
4738 // BTC is transferred from bob to ed, ed is not authorized
4739 env(pay(gw, ed, btc(10)), Path(~btc), Sendmax(eth(10)), Ter(tecNO_AUTH));
4740 // BTC is transferred from bob to issuer
4741 env(pay(ed, gw, btc(10)), Path(~btc), Sendmax(eth(10)));
4742 // BTC is transferred from issuer to bob
4743 env(pay(gw, ed, eth(10)), Path(~eth), Sendmax(btc(10)));
4744 //
4745 // BTC is transferred from ed to bob, ed is not authorized
4746 env(pay(ed, gw, eth(10)), Path(~eth), Sendmax(btc(10)), Ter(tecNO_AUTH));
4747 env.close();
4748 }
4749
4750 // MPTCanTransfer is not set.
4751 {
4752 auto const ed = Account{"ed"};
4753 Env env{*this, features};
4754 env.fund(XRP(1'000), gw, alice, carol, bob, ed);
4755 MPTTester const btc({
4756 .env = env,
4757 .issuer = gw,
4758 .holders = {alice, carol, bob, ed},
4759 .pay = 1'000,
4760 .flags = tfMPTCanTrade,
4761 });
4762 MPTTester const eth({
4763 .env = env,
4764 .issuer = gw,
4765 .holders = {alice, carol, bob, ed},
4766 .pay = 1'000,
4767 .flags = kMptDexFlags,
4768 });
4769
4770 env(offer(bob, eth(1'000), btc(1'000)), Txflags(tfPassive));
4771 env.close();
4772 env(offer(bob, btc(1'000), eth(1'000)), Txflags(tfPassive));
4773 env.close();
4774
4775 // Fail regardless if source/destination is the issuer or
4776 // not since the offer is owned by a holder.
4777 env(pay(ed, carol, btc(10)), Path(~btc), Sendmax(eth(10)), Ter(tecPATH_PARTIAL));
4778 env(pay(carol, ed, btc(10)), Path(~btc), Sendmax(eth(10)), Ter(tecPATH_PARTIAL));
4779 env(pay(ed, carol, eth(10)), Path(~eth), Sendmax(btc(10)), Ter(tecPATH_PARTIAL));
4780 env(pay(carol, ed, eth(10)), Path(~eth), Sendmax(btc(10)), Ter(tecPATH_PARTIAL));
4781 // Fail because BTC, which has CanTransfer disabled, is sent to bob
4782 env(pay(ed, gw, eth(10)), Path(~eth), Sendmax(btc(10)), Ter(tecPATH_PARTIAL));
4783 env(pay(ed, gw, btc(10)), Path(~btc), Sendmax(eth(10)), Ter(tesSUCCESS));
4784 env(pay(gw, ed, eth(10)), Path(~eth), Sendmax(btc(10)), Ter(tesSUCCESS));
4785 // Fail because BTC, which has CanTransfer disabled, is sent to ed
4786 env(pay(gw, ed, btc(10)), Path(~btc), Sendmax(eth(10)), Ter(tecPATH_PARTIAL));
4787 env.close();
4788 env(offer(gw, eth(100), btc(100)), Txflags(tfPassive));
4789 env.close();
4790 env(offer(gw, btc(100), eth(100)), Txflags(tfPassive));
4791 env.close();
4792 BEAST_EXPECT(expectOffers(env, bob, 2));
4793 env(pay(ed, carol, btc(10)), Path(~btc), Sendmax(eth(10)), Ter(tesSUCCESS));
4794 env(pay(ed, carol, eth(10)), Path(~eth), Sendmax(btc(10)), Ter(tesSUCCESS));
4795 env(pay(gw, carol, btc(10)), Path(~btc), Sendmax(eth(10)), Ter(tesSUCCESS));
4796 env.close();
4797 env(pay(ed, gw, btc(10)), Path(~btc), Sendmax(eth(10)));
4798 env.close();
4799 }
4800
4801 // Multiple steps: CAD/USD, USD/BTC, BTC/ETH.
4802 // takerGets can transfer if:
4803 // - CanTransfer is set
4804 // - The offer's owner is the issuer
4805 // - BookStep is the last step, which means strand's destination is
4806 // the issuer
4807 // takerPays can transfer if
4808 // - BookStep is the first step, which means strand's source is
4809 // the issuer
4810 // - The offer's owner is the issuer
4811 // - Previous step is BookStep, which transfers per above
4812 // - CanTransfer is set
4813 {
4814 // enum to indicate which MPT doesn't set CanTransfer flag.
4815 enum class NoTransferMPT { BTC, ETH, USD, CAD };
4816
4817 // Lambda to test multi-step payment with one of the MPTs not setting CanTransfer flag.
4818 auto const testMultiStepMPTCanTransfer = [&](NoTransferMPT const noTransferMPT,
4819 auto const& test) {
4820 auto const getFlags = [&](NoTransferMPT const mpt) {
4821 return mpt == noTransferMPT ? tfMPTCanTrade : kMptDexFlags;
4822 };
4823
4824 Env env{*this, features};
4825 env.fund(XRP(1'000), gw, alice, carol, bob);
4826 env.close();
4827 MPTTester const btc({
4828 .env = env,
4829 .issuer = gw,
4830 .holders = {alice, carol, bob},
4831 .pay = 1'000,
4832 .flags = getFlags(NoTransferMPT::BTC),
4833 });
4834 MPTTester const eth({
4835 .env = env,
4836 .issuer = gw,
4837 .holders = {alice, carol, bob},
4838 .pay = 1'000,
4839 .flags = getFlags(NoTransferMPT::ETH),
4840 });
4841 MPTTester const usd({
4842 .env = env,
4843 .issuer = gw,
4844 .holders = {alice, carol, bob},
4845 .pay = 1'000,
4846 .flags = getFlags(NoTransferMPT::USD),
4847 });
4848 MPTTester const cad({
4849 .env = env,
4850 .issuer = gw,
4851 .holders = {alice, carol, bob},
4852 .pay = 1'000,
4853 .flags = getFlags(NoTransferMPT::CAD),
4854 });
4855
4856 env(offer(bob, cad(100), usd(100)), Txflags(tfPassive));
4857 env(offer(bob, usd(100), btc(100)), Txflags(tfPassive));
4858 env(offer(bob, btc(100), eth(100)), Txflags(tfPassive));
4859 env.close();
4860
4861 test(env, btc, eth, usd, cad);
4862 };
4863
4864 // USD starts without MPTCanTransfer.
4865 testMultiStepMPTCanTransfer(
4866 NoTransferMPT::USD,
4867 [&](Env& env,
4868 MPTTester const& btc,
4869 MPTTester const& eth,
4870 MPTTester const& usd,
4871 MPTTester const& cad) {
4872 BEAST_EXPECT(expectOffers(env, bob, 3));
4873
4874 // fail - CAD/USD is owned by bob
4875 env(pay(alice, carol, eth(1)),
4876 Path(~usd, ~btc, ~eth),
4877 Sendmax(cad(1)),
4879
4880 auto seq(env.seq(gw));
4881 env(offer(gw, usd(1), btc(1)), Txflags(tfPassive));
4882 env.close();
4883 // fail - CAD/USD is owned by bob
4884 env(pay(alice, carol, eth(1)),
4885 Path(~usd, ~btc, ~eth),
4886 Sendmax(cad(1)),
4888 env.close();
4889 env(offerCancel(gw, seq));
4890 env(offer(gw, cad(1), usd(1)), Txflags(tfPassive));
4891 env.close();
4892 BEAST_EXPECT(expectOffers(env, bob, 3));
4893 // succeed - CAD/USD is owned by issuer
4894 env(pay(alice, carol, eth(1)), Path(~usd, ~btc, ~eth), Sendmax(cad(1)));
4895 env.close();
4896 // bob's CAD/USD is deleted.
4897 BEAST_EXPECT(expectOffers(env, bob, 2));
4898 env(offer(bob, cad(100), usd(100)), Txflags(tfPassive));
4899 BEAST_EXPECT(expectOffers(env, gw, 0));
4900 });
4901
4902 // ETH starts without MPTCanTransfer.
4903 testMultiStepMPTCanTransfer(
4904 NoTransferMPT::ETH,
4905 [&](Env& env,
4906 MPTTester const& btc,
4907 MPTTester const& eth,
4908 MPTTester const& usd,
4909 MPTTester const& cad) {
4910 // fail - BTC/ETH is owned by bob, destination is carol
4911 env(pay(alice, carol, eth(1)),
4912 Path(~usd, ~btc, ~eth),
4913 Sendmax(cad(1)),
4915 env.close();
4916 BEAST_EXPECT(expectOffers(env, bob, 3));
4917
4918 // succeed - destination is an issuer
4919 env(pay(alice, gw, eth(1)), Path(~usd, ~btc, ~eth), Sendmax(cad(1)));
4920 env.close();
4921 BEAST_EXPECT(expectOffers(env, bob, 3));
4922 });
4923
4924 // CAD starts without MPTCanTransfer.
4925 testMultiStepMPTCanTransfer(
4926 NoTransferMPT::CAD,
4927 [&](Env& env,
4928 MPTTester const& btc,
4929 MPTTester const& eth,
4930 MPTTester const& usd,
4931 MPTTester const& cad) {
4932 // fail - CAD/USD is owned by bob, source is alice
4933 env(pay(alice, carol, eth(1)),
4934 Path(~usd, ~btc, ~eth),
4935 Sendmax(cad(1)),
4937 // succeed - source is the issuer
4938 env(pay(gw, carol, eth(1)), Path(~usd, ~btc, ~eth), Sendmax(cad(1)));
4939 env.close();
4940 env(offer(gw, cad(1), usd(1)), Txflags(tfPassive));
4941 env.close();
4942 // succeed - CAD/USD is owned by issuer
4943 env(pay(alice, carol, eth(1)), Path(~usd, ~btc, ~eth), Sendmax(cad(1)));
4944 env.close();
4945 BEAST_EXPECT(expectOffers(env, gw, 0));
4946 BEAST_EXPECT(expectOffers(env, bob, 2));
4947 });
4948
4949 // BTC starts without MPTCanTransfer.
4950 testMultiStepMPTCanTransfer(
4951 NoTransferMPT::BTC,
4952 [&](Env& env,
4953 MPTTester const& btc,
4954 MPTTester const& eth,
4955 MPTTester const& usd,
4956 MPTTester const& cad) {
4957 env(offer(gw, usd(1), btc(1)), Txflags(tfPassive));
4958 env.close();
4959 // succeed - USD/BTC is owned by issuer
4960 env(pay(alice, carol, eth(1)), Path(~usd, ~btc, ~eth), Sendmax(cad(1)));
4961 env.close();
4962 BEAST_EXPECT(expectOffers(env, gw, 0));
4963 });
4964 }
4965
4966 // MPTCanTrade is not set
4967 {
4968 Env env{*this, features};
4969 env.fund(XRP(1'000), gw, alice, carol, bob);
4970 env.close();
4971 MPTTester btc({
4972 .env = env,
4973 .issuer = gw,
4974 .holders = {alice, carol, bob},
4975 .pay = 1'000,
4976 .flags = tfMPTCanTransfer,
4977 });
4978 MPTTester const eth({
4979 .env = env,
4980 .issuer = gw,
4981 .holders = {alice, carol, bob},
4982 .pay = 1'000,
4983 .flags = kMptDexFlags,
4984 });
4985 MPTTester const usd({
4986 .env = env,
4987 .issuer = gw,
4988 .holders = {alice, carol, bob},
4989 .pay = 1'000,
4990 .flags = kMptDexFlags,
4991 });
4992
4993 env(pay(alice, carol, eth(1)), Path(~eth), Sendmax(btc(1)), Ter(tecNO_PERMISSION));
4994 env(pay(alice, carol, btc(1)), Path(~btc), Sendmax(eth(1)), Ter(tecNO_PERMISSION));
4995 env.close();
4996
4997 // Enable MPTCanTrade so BTC can be crossed through offers.
4998 btc.set({.flags = tfMPTSetCanTrade});
4999 env(offer(bob, XRP(1), btc(1)));
5000 env(offer(bob, btc(1), eth(1)));
5001 env(offer(bob, eth(1), usd(1)));
5002 env.close();
5003 BEAST_EXPECT(expectOffers(env, bob, 3));
5004
5005 env(pay(gw, carol, usd(1)),
5006 Path(~btc, ~eth, ~usd),
5007 Sendmax(XRP(1)),
5008 Txflags(tfPartialPayment | tfNoRippleDirect));
5009 env.close();
5010 BEAST_EXPECT(expectOffers(env, bob, 0));
5011 }
5012
5013 // Holders are locked
5014 {
5015 enum class LockType { Global, Individual, None };
5016 struct TestArg
5017 {
5018 Account src;
5019 Account dst;
5020 Account offerOwner;
5021 LockType srcFlag = LockType::None;
5022 LockType dstFlag = LockType::None;
5023 LockType offerFlagBuy = LockType::None;
5024 LockType offerFlagSell = LockType::None;
5025 LockType globalFlagBuy = LockType::None;
5026 LockType globalFlagSell = LockType::None;
5027 TER err = tesSUCCESS;
5028 std::optional<TER> errIOU = std::nullopt;
5029 };
5030 auto getErr = [&]<typename Token>(Token const&, TestArg const& arg) {
5031 if constexpr (std::is_same_v<Token, IOU>)
5032 {
5033 return arg.errIOU.value_or(arg.err);
5034 }
5035 else if constexpr (std::is_same_v<Token, MPTTester>)
5036 {
5037 return arg.err;
5038 }
5039 };
5040 auto const getMPT = [&](Env& env) {
5041 MPTTester const btc({
5042 .env = env,
5043 .issuer = gw2,
5044 .holders = {alice, carol, bob, gw},
5045 .pay = 100,
5046 .flags = tfMPTCanLock | kMptDexFlags,
5047 });
5048 MPTTester const eth({
5049 .env = env,
5050 .issuer = gw,
5051 .holders = {alice, carol, bob, gw2},
5052 .pay = 100,
5053 .flags = tfMPTCanLock | kMptDexFlags,
5054 });
5055 return std::make_pair(btc, eth);
5056 };
5057 auto const getIOU = [&](Env& env) {
5058 for (auto const& a : {alice, carol, bob})
5059 {
5060 env(fset(a, asfDefaultRipple));
5061 env.close();
5062 env(trust(a, gw["ETH"](200)));
5063 env(pay(gw, a, gw["ETH"](100)));
5064 env(trust(a, gw2["BTC"](200)));
5065 env(pay(gw2, a, gw2["BTC"](100)));
5066 env.close();
5067 }
5068 // gw2 needs an ETH trust line to receive ETH when its offers fill
5069 // gw needs BTC to sell BTC
5070 env(trust(gw2, gw["ETH"](200)));
5071 env(trust(gw, gw2["BTC"](200)));
5072 env(pay(gw2, gw, gw2["BTC"](100)));
5073 env.close();
5074 return std::make_pair(gw2["BTC"], gw["ETH"]);
5075 };
5076 auto lock = [&]<typename Token>(
5077 Env& env, Account const& account, Token& token, LockType lock) {
5078 if (lock == LockType::None)
5079 return;
5080 if constexpr (std::is_same_v<Token, IOU>)
5081 {
5082 if (lock == LockType::Global)
5083 {
5084 env(fset(token.account, asfGlobalFreeze));
5085 }
5086 else
5087 {
5088 IOU const iou{account, token.currency};
5089 env(trust(token.account, iou(0), tfSetFreeze));
5090 }
5091 }
5092 else if constexpr (std::is_same_v<Token, MPTTester>)
5093 {
5094 if (lock == LockType::Global)
5095 {
5096 token.set({.flags = tfMPTLock});
5097 }
5098 else if (token.issuer() != account)
5099 {
5100 token.set({.holder = account, .flags = tfMPTLock});
5101 }
5102 }
5103 };
5104 auto const test = [&](auto&& getTokens, TestArg const& arg) {
5105 Env env(*this);
5106 env.fund(XRP(1'000), gw, gw2, alice, carol, bob);
5107
5108 auto [btc, eth] = getTokens(env);
5109
5110 env(offer(arg.offerOwner, eth(10), btc(10)), Txflags(tfPassive));
5111 env.close();
5112
5113 if (arg.globalFlagBuy != LockType::None)
5114 {
5115 lock(env, gw, eth, LockType::Global);
5116 }
5117 else
5118 {
5119 lock(env, arg.offerOwner, eth, arg.offerFlagBuy);
5120 lock(env, arg.src, eth, arg.srcFlag);
5121 }
5122 if (arg.globalFlagSell != LockType::None)
5123 {
5124 lock(env, gw2, btc, LockType::Global);
5125 }
5126 else
5127 {
5128 lock(env, arg.offerOwner, btc, arg.offerFlagSell);
5129 lock(env, arg.dst, btc, arg.dstFlag);
5130 }
5131
5132 auto const err = getErr(eth, arg);
5133 env(pay(arg.src, arg.dst, btc(1)),
5134 Path(~btc),
5135 Txflags(tfNoRippleDirect),
5136 Sendmax(eth(1)),
5137 Ter(err));
5138 env.close();
5139 };
5140 // clang-format off
5141 std::vector<TestArg> const tests = {
5142 // ----- src=alice (holder), dst=carol (holder), offerOwner=bob (holder) -----
5143 // alice's ETH locked: caught in check()
5144 {.src = alice, .dst = carol, .offerOwner = bob, .srcFlag = LockType::Individual, .err = tecPATH_DRY},
5145 // carol's BTC locked: caught in MPT check(); IOU dst can still receive when frozen
5146 {.src = alice, .dst = carol, .offerOwner = bob, .dstFlag = LockType::Individual, .err = tecPATH_DRY, .errIOU = tesSUCCESS},
5147 // ETH globally locked: caught in check()
5148 {.src = alice, .dst = carol, .offerOwner = bob, .globalFlagBuy = LockType::Global, .err = tecPATH_DRY},
5149 // BTC globally locked: bob's offer unfunded in OfferStream
5150 {.src = alice, .dst = carol, .offerOwner = bob, .globalFlagSell = LockType::Global, .err = tecPATH_PARTIAL},
5151 // bob's ETH (takerPays) locked: MPT offer unfunded in OfferStream (locked holder cannot receive); IOU can still receive
5152 {.src = alice, .dst = carol, .offerOwner = bob, .offerFlagBuy = LockType::Individual, .err = tecPATH_PARTIAL, .errIOU = tesSUCCESS},
5153 // bob's BTC (takerGets) locked: offer unfunded in OfferStream
5154 {.src = alice, .dst = carol, .offerOwner = bob, .offerFlagSell = LockType::Individual, .err = tecPATH_PARTIAL},
5155 // ----- src=alice (holder), dst=carol (holder), offerOwner=gw2 (BTC issuer) -----
5156 // alice's ETH locked: caught in check()
5157 {.src = alice, .dst = carol, .offerOwner = gw2, .srcFlag = LockType::Individual, .err = tecPATH_DRY},
5158 // carol's BTC locked: caught in MPT check(); IOU dst can still receive when frozen
5159 {.src = alice, .dst = carol, .offerOwner = gw2, .dstFlag = LockType::Individual, .err = tecPATH_DRY, .errIOU = tesSUCCESS},
5160 // ETH globally locked: caught in check()
5161 {.src = alice, .dst = carol, .offerOwner = gw2, .globalFlagBuy = LockType::Global, .err = tecPATH_DRY},
5162 // BTC globally locked: gw2 is the BTC issuer, offer always permitted regardless of global freeze
5163 {.src = alice, .dst = carol, .offerOwner = gw2, .globalFlagSell = LockType::Global, .err = tesSUCCESS},
5164 // ----- src=alice (holder), dst=carol (holder), offerOwner=gw (ETH issuer, BTC holder) -----
5165 // alice's ETH locked: caught in check()
5166 {.src = alice, .dst = carol, .offerOwner = gw, .srcFlag = LockType::Individual, .err = tecPATH_DRY},
5167 // carol's BTC locked: caught in MPT check(); IOU dst can still receive when frozen
5168 {.src = alice, .dst = carol, .offerOwner = gw, .dstFlag = LockType::Individual, .err = tecPATH_DRY, .errIOU = tesSUCCESS},
5169 // ETH globally locked: caught in check()
5170 {.src = alice, .dst = carol, .offerOwner = gw, .globalFlagBuy = LockType::Global, .err = tecPATH_DRY},
5171 // BTC globally locked: gw holds BTC as a holder (not BTC issuer), offer unfunded in OfferStream
5172 {.src = alice, .dst = carol, .offerOwner = gw, .globalFlagSell = LockType::Global, .err = tecPATH_PARTIAL},
5173 // ----- src=gw (ETH issuer), dst=carol (holder), offerOwner=bob (holder) -----
5174 // ETH globally locked, src is ETH issuer: no first MPTEndpointStep so check() passes;
5175 // MPT offer unfunded in OfferStream (globally-locked ETH cannot flow to holder via DEX); IOU issuer can still issue
5176 {.src = gw, .dst = carol, .offerOwner = bob, .srcFlag = LockType::Global, .err = tecPATH_PARTIAL, .errIOU = tesSUCCESS},
5177 // BTC globally locked: last MPTEndpointStep only checks individual freeze, check() passes; offer unfunded in OfferStream
5178 {.src = gw, .dst = carol, .offerOwner = bob, .dstFlag = LockType::Global, .err = tecPATH_PARTIAL},
5179 // carol's BTC locked: caught in MPT check(); IOU dst can still receive when frozen
5180 {.src = gw, .dst = carol, .offerOwner = bob, .dstFlag = LockType::Individual, .err = tecPATH_DRY, .errIOU = tesSUCCESS},
5181 // bob's ETH (takerPays) locked: MPT offer unfunded in OfferStream (locked holder cannot receive); IOU can still receive
5182 {.src = gw, .dst = carol, .offerOwner = bob, .offerFlagBuy = LockType::Individual, .err = tecPATH_PARTIAL, .errIOU = tesSUCCESS},
5183 // bob's BTC (takerGets) locked: offer unfunded in OfferStream
5184 {.src = gw, .dst = carol, .offerOwner = bob, .offerFlagSell = LockType::Individual, .err = tecPATH_PARTIAL},
5185 // ----- src=alice (holder), dst=gw2 (BTC issuer), offerOwner=bob (holder) -----
5186 // alice's ETH locked: caught in check()
5187 {.src = alice, .dst = gw2, .offerOwner = bob, .srcFlag = LockType::Individual, .err = tecPATH_DRY},
5188 // BTC globally locked, dst is BTC issuer: no last MPTEndpointStep so check() passes; offer unfunded in OfferStream
5189 {.src = alice, .dst = gw2, .offerOwner = bob, .dstFlag = LockType::Global, .err = tecPATH_PARTIAL},
5190 // bob's ETH (takerPays) locked: MPT offer unfunded in OfferStream (locked holder cannot receive); IOU can still receive
5191 {.src = alice, .dst = gw2, .offerOwner = bob, .offerFlagBuy = LockType::Individual, .err = tecPATH_PARTIAL, .errIOU = tesSUCCESS},
5192 // bob's BTC (takerGets) locked: offer unfunded in OfferStream
5193 {.src = alice, .dst = gw2, .offerOwner = bob, .offerFlagSell = LockType::Individual, .err = tecPATH_PARTIAL},
5194 };
5195 // clang-format on
5196
5197 for (auto const& t : tests)
5198 {
5199 test(getMPT, t);
5200 test(getIOU, t);
5201 }
5202 }
5203 {
5204 Env env(*this);
5205 auto const usd = gw["USD"];
5206 env.fund(XRP(1'000), gw, alice, carol, bob);
5207 MPTTester btc({
5208 .env = env,
5209 .issuer = gw,
5210 .holders = {alice, carol, bob},
5211 .pay = 100,
5212 .flags = tfMPTCanLock | kMptDexFlags,
5213 });
5214 MPTTester eth({
5215 .env = env,
5216 .issuer = gw,
5217 .holders = {alice, carol, bob},
5218 .pay = 100,
5219 .flags = tfMPTCanLock | kMptDexFlags,
5220 });
5221
5222 env(trust(alice, usd(100)));
5223 env(pay(gw, alice, usd(100)));
5224 env(trust(carol, usd(100)));
5225
5226 env(offer(alice, XRP(10), eth(10)));
5227 env(offer(bob, eth(10), btc(10)));
5228 env(offer(alice, btc(10), usd(10)));
5229 env.close();
5230
5231 btc.set({.holder = bob, .flags = tfMPTLock});
5232
5233 // Bob's offer is unfunded
5234 env(pay(alice, carol, usd(1)),
5235 Path(~(MPT)eth, ~(MPT)btc, ~usd),
5236 Txflags(tfNoRippleDirect | tfPartialPayment),
5237 Sendmax(XRP(1)),
5238 Ter(tecPATH_DRY));
5239 env.close();
5240
5241 btc.set({.holder = bob, .flags = tfMPTUnlock});
5242 eth.set({.holder = bob, .flags = tfMPTLock});
5243
5244 env(pay(alice, carol, usd(1)),
5245 Path(~(MPT)eth, ~(MPT)btc, ~usd),
5246 Txflags(tfNoRippleDirect | tfPartialPayment),
5247 Sendmax(XRP(1)),
5248 Ter(tecPATH_DRY));
5249 }
5250
5251 // A domain payment should only consume a USD/MPT offer with a domain.
5252 // It must not consume a regular USD/MPT offer.
5253 {
5254 Env env(*this, features);
5255 Account const domainOwner("DomainOwner");
5256 env.fund(XRP(1'000), gw, alice, carol, bob);
5257 auto const domainID =
5258 setupDomain(env, {alice, bob, carol, gw}, domainOwner, "permdex-cred");
5259
5260 MPTTester btc({.env = env, .issuer = gw, .holders = {alice, carol, bob}, .pay = 100});
5261 MPTTester eth({.env = env, .issuer = gw, .holders = {alice, carol, bob}, .pay = 100});
5262
5263 auto const test = [&](bool withDomain) {
5264 if (withDomain)
5265 {
5266 env(offer(bob, eth(1), btc(1)), Domain(domainID));
5267 }
5268 else
5269 {
5270 env(offer(bob, eth(1), btc(1)));
5271 }
5272
5273 auto const err = withDomain ? Ter(tesSUCCESS) : Ter(tecPATH_DRY);
5274 env(pay(alice, carol, btc(1)),
5275 Path(~(MPT)btc),
5276 Txflags(tfPartialPayment),
5277 Sendmax(eth(1)),
5278 Domain(domainID),
5279 err);
5280 };
5281 test(true);
5282 test(false);
5283 }
5284
5285 // A hybrid USD/MPT domain offer should still be consumable by
5286 // a regular payment.
5287 {
5288 Env env(*this, features);
5289 Account const domainOwner("DomainOwner");
5290 env.fund(XRP(1'000), gw, alice, carol, bob);
5291 auto const domainID =
5292 setupDomain(env, {alice, bob, carol, gw}, domainOwner, "permdex-cred");
5293
5294 MPTTester btc({.env = env, .issuer = gw, .holders = {alice, carol, bob}, .pay = 100});
5295 MPTTester eth({.env = env, .issuer = gw, .holders = {alice, carol, bob}, .pay = 100});
5296
5297 auto const test = [&](bool isHybrid) {
5298 auto const flags = isHybrid ? tfHybrid : 0;
5299 env(offer(bob, eth(1), btc(1)), Txflags(flags), Domain(domainID));
5300
5301 auto const err = isHybrid ? Ter(tesSUCCESS) : Ter(tecPATH_DRY);
5302 env(pay(alice, carol, btc(1)),
5303 Path(~(MPT)btc),
5304 Txflags(tfPartialPayment),
5305 Sendmax(eth(1)),
5306 err);
5307 };
5308 test(true);
5309 test(false);
5310 }
5311
5312 // MPT/XRP
5313 {
5314 Env env{*this, features};
5315 MPTTester mptTester(env, gw, {.holders = {alice, carol, bob}});
5316
5317 mptTester.create(
5318 {.ownerCount = 1, .holderCount = 0, .flags = tfMPTCanTransfer | tfMPTCanTrade});
5319 auto const mpt = mptTester["MPT"];
5320
5321 mptTester.authorize({.account = alice});
5322 mptTester.pay(gw, alice, 200);
5323
5324 mptTester.authorize({.account = carol});
5325 mptTester.pay(gw, carol, 200);
5326
5327 mptTester.authorize({.account = bob});
5328
5329 env(offer(alice, XRP(100), mpt(101)));
5330 env.close();
5331 BEAST_EXPECT(expectOffers(env, alice, 1, {{Amounts{XRP(100), mpt(101)}}}));
5332
5333 env(pay(carol, bob, mpt(101)),
5334 test::jtx::Path(~mpt),
5335 Sendmax(XRP(100)),
5336 Txflags(tfPartialPayment));
5337 env.close();
5338
5339 BEAST_EXPECT(expectOffers(env, alice, 0));
5340 BEAST_EXPECT(mptTester.checkMPTokenOutstandingAmount(400));
5341 BEAST_EXPECT(mptTester.checkMPTokenAmount(alice, 99));
5342 BEAST_EXPECT(mptTester.checkMPTokenAmount(bob, 101));
5343 }
5344
5345 // MPT/IOU
5346 {
5347 Env env{*this, features};
5348
5349 MPTTester mptTester(env, gw, {.holders = {alice, carol, bob}});
5350
5351 mptTester.create(
5352 {.ownerCount = 1, .holderCount = 0, .flags = tfMPTCanTransfer | tfMPTCanTrade});
5353 auto const mpt = mptTester["MPT"];
5354
5355 env(trust(alice, usd(2'000)));
5356 env(pay(gw, alice, usd(1'000)));
5357 env(trust(bob, usd(2'000)));
5358 env(pay(gw, bob, usd(1'000)));
5359 env(trust(carol, usd(2'000)));
5360 env(pay(gw, carol, usd(1'000)));
5361 env.close();
5362
5363 mptTester.authorize({.account = alice});
5364 mptTester.pay(gw, alice, 200);
5365
5366 mptTester.authorize({.account = carol});
5367 mptTester.pay(gw, carol, 200);
5368
5369 mptTester.authorize({.account = bob});
5370
5371 env(offer(alice, usd(100), mpt(101)));
5372 env.close();
5373 BEAST_EXPECT(expectOffers(env, alice, 1, {{Amounts{usd(100), mpt(101)}}}));
5374
5375 env(pay(carol, bob, mpt(101)),
5376 test::jtx::Path(~mpt),
5377 Sendmax(usd(100)),
5378 Txflags(tfPartialPayment));
5379 env.close();
5380
5381 BEAST_EXPECT(expectOffers(env, alice, 0));
5382 BEAST_EXPECT(env.balance(carol, usd) == usd(900));
5383 BEAST_EXPECT(mptTester.checkMPTokenOutstandingAmount(400));
5384 BEAST_EXPECT(mptTester.checkMPTokenAmount(alice, 99));
5385 BEAST_EXPECT(mptTester.checkMPTokenAmount(bob, 101));
5386 }
5387
5388 // IOU/MPT
5389 {
5390 Env env{*this, features};
5391
5392 MPTTester mptTester(env, gw, {.holders = {alice, carol, bob}});
5393
5394 mptTester.create(
5395 {.ownerCount = 1, .holderCount = 0, .flags = tfMPTCanTransfer | tfMPTCanTrade});
5396 auto const mpt = mptTester["MPT"];
5397
5398 env(trust(alice, usd(2'000)), Txflags(tfClearNoRipple));
5399 env(pay(gw, alice, usd(1'000)));
5400 env(trust(bob, usd(2'000)), Txflags(tfClearNoRipple));
5401 env.close();
5402
5403 mptTester.authorize({.account = alice});
5404 env(pay(gw, alice, mpt(200)));
5405
5406 mptTester.authorize({.account = carol});
5407 env(pay(gw, carol, mpt(200)));
5408
5409 env(offer(alice, mpt(101), usd(100)));
5410 env.close();
5411 BEAST_EXPECT(expectOffers(env, alice, 1, {{Amounts{mpt(101), usd(100)}}}));
5412
5413 env(pay(carol, bob, usd(100)),
5414 test::jtx::Path(~usd),
5415 Sendmax(mpt(101)),
5416 Txflags(tfPartialPayment | tfNoRippleDirect));
5417 env.close();
5418
5419 BEAST_EXPECT(expectOffers(env, alice, 0));
5420 BEAST_EXPECT(env.balance(alice, usd) == usd(900));
5421 BEAST_EXPECT(mptTester.checkMPTokenAmount(alice, 301));
5422 BEAST_EXPECT(mptTester.checkMPTokenOutstandingAmount(400));
5423 BEAST_EXPECT(mptTester.checkMPTokenAmount(carol, 99));
5424 BEAST_EXPECT(env.balance(bob, usd) == usd(100));
5425 }
5426
5427 // MPT/MPT
5428 {
5429 Env env{*this, features};
5430
5431 MPTTester mptTester1(env, gw, {.holders = {alice, carol, bob}});
5432 mptTester1.create(
5433 {.ownerCount = 1, .holderCount = 0, .flags = tfMPTCanTransfer | tfMPTCanTrade});
5434 auto const mpt1 = mptTester1["MPT1"];
5435
5436 MPTTester mptTester2(env, gw, {.holders = {alice, carol, bob}, .fund = false});
5437 mptTester2.create(
5438 {.ownerCount = 2, .holderCount = 0, .flags = tfMPTCanTransfer | tfMPTCanTrade});
5439 auto const mpt2 = mptTester2["MPT2"];
5440
5441 mptTester1.authorize({.account = alice});
5442 mptTester1.pay(gw, alice, 200);
5443 mptTester2.authorize({.account = alice});
5444
5445 mptTester2.authorize({.account = carol});
5446 mptTester2.pay(gw, carol, 200);
5447
5448 mptTester1.authorize({.account = bob});
5449 mptTester2.authorize({.account = bob});
5450 mptTester2.pay(gw, bob, 200);
5451
5452 env(offer(alice, mpt2(100), mpt1(100)));
5453 env.close();
5454 BEAST_EXPECT(
5455 expectOffers(env, alice, 1, {{Amounts{mptTester2(100), mptTester1(100)}}}));
5456
5457 // holder to holder
5458 env(pay(carol, bob, mpt1(10)),
5459 test::jtx::Path(~mpt1),
5460 Sendmax(mpt2(10)),
5461 Txflags(tfPartialPayment));
5462 env.close();
5463
5464 BEAST_EXPECT(expectOffers(env, alice, 1));
5465 BEAST_EXPECT(mptTester1.checkMPTokenAmount(alice, 190));
5466 BEAST_EXPECT(mptTester2.checkMPTokenAmount(alice, 10));
5467 BEAST_EXPECT(mptTester1.checkMPTokenOutstandingAmount(200));
5468 BEAST_EXPECT(mptTester2.checkMPTokenOutstandingAmount(400));
5469 BEAST_EXPECT(mptTester2.checkMPTokenAmount(carol, 190));
5470 BEAST_EXPECT(mptTester1.checkMPTokenAmount(bob, 10));
5471
5472 // issuer to holder
5473 env(pay(gw, bob, mpt1(20)),
5474 test::jtx::Path(~mpt1),
5475 Sendmax(mpt2(20)),
5476 Txflags(tfPartialPayment));
5477 env.close();
5478
5479 BEAST_EXPECT(expectOffers(env, alice, 1));
5480 BEAST_EXPECT(mptTester1.checkMPTokenAmount(alice, 170));
5481 BEAST_EXPECT(mptTester2.checkMPTokenAmount(alice, 30));
5482 BEAST_EXPECT(mptTester1.checkMPTokenOutstandingAmount(200));
5483 BEAST_EXPECT(mptTester2.checkMPTokenOutstandingAmount(420));
5484 BEAST_EXPECT(mptTester2.checkMPTokenAmount(carol, 190));
5485 BEAST_EXPECT(mptTester1.checkMPTokenAmount(bob, 30));
5486
5487 // holder to issuer
5488 env(pay(bob, gw, mpt1(70)),
5489 test::jtx::Path(~mpt1),
5490 Sendmax(mpt2(70)),
5491 Txflags(tfPartialPayment));
5492 env.close();
5493
5494 BEAST_EXPECT(expectOffers(env, alice, 0));
5495 BEAST_EXPECT(mptTester1.checkMPTokenAmount(alice, 100));
5496 BEAST_EXPECT(mptTester2.checkMPTokenAmount(alice, 100));
5497 BEAST_EXPECT(mptTester1.checkMPTokenOutstandingAmount(130));
5498 BEAST_EXPECT(mptTester2.checkMPTokenOutstandingAmount(420));
5499 BEAST_EXPECT(mptTester2.checkMPTokenAmount(carol, 190));
5500 BEAST_EXPECT(mptTester1.checkMPTokenAmount(bob, 30));
5501 BEAST_EXPECT(mptTester2.checkMPTokenAmount(bob, 130));
5502 }
5503
5504 // MPT/MPT, issuer owns the offer
5505 {
5506 Env env{*this, features};
5507
5508 MPTTester mptTester1(env, gw, {.holders = {carol, bob}});
5509 mptTester1.create(
5510 {.ownerCount = 1, .holderCount = 0, .flags = tfMPTCanTransfer | tfMPTCanTrade});
5511 auto const mpt1 = mptTester1["MPT1"];
5512
5513 MPTTester mptTester2(env, gw, {.holders = {carol, bob}, .fund = false});
5514 mptTester2.create(
5515 {.ownerCount = 2, .holderCount = 0, .flags = tfMPTCanTransfer | tfMPTCanTrade});
5516 auto const mpt2 = mptTester2["MPT2"];
5517
5518 mptTester2.authorize({.account = carol});
5519 mptTester2.pay(gw, carol, 200);
5520
5521 mptTester1.authorize({.account = bob});
5522 mptTester2.authorize({.account = bob});
5523 mptTester2.pay(gw, bob, 200);
5524
5525 env(offer(gw, mpt2(100), mpt1(100)));
5526 env.close();
5527 BEAST_EXPECT(expectOffers(env, gw, 1, {{Amounts{mpt2(100), mpt1(100)}}}));
5528
5529 // holder to holder
5530 env(pay(carol, bob, mpt1(10)),
5531 test::jtx::Path(~mpt1),
5532 Sendmax(mpt2(10)),
5533 Txflags(tfPartialPayment));
5534 env.close();
5535
5536 BEAST_EXPECT(expectOffers(env, gw, 1));
5537 BEAST_EXPECT(mptTester1.checkMPTokenOutstandingAmount(10));
5538 BEAST_EXPECT(mptTester2.checkMPTokenOutstandingAmount(390));
5539 BEAST_EXPECT(mptTester2.checkMPTokenAmount(carol, 190));
5540 BEAST_EXPECT(mptTester1.checkMPTokenAmount(bob, 10));
5541
5542 // issuer to holder
5543 env(pay(gw, bob, mpt1(20)),
5544 test::jtx::Path(~mpt1),
5545 Sendmax(mpt2(20)),
5546 Txflags(tfPartialPayment));
5547 env.close();
5548
5549 BEAST_EXPECT(expectOffers(env, gw, 1));
5550 BEAST_EXPECT(mptTester1.checkMPTokenOutstandingAmount(30));
5551 BEAST_EXPECT(mptTester2.checkMPTokenOutstandingAmount(390));
5552 BEAST_EXPECT(mptTester2.checkMPTokenAmount(carol, 190));
5553 BEAST_EXPECT(mptTester1.checkMPTokenAmount(bob, 30));
5554
5555 // holder to issuer
5556 env(pay(bob, gw, mpt1(70)),
5557 test::jtx::Path(~mpt1),
5558 Sendmax(mpt2(70)),
5559 Txflags(tfPartialPayment));
5560 env.close();
5561
5562 BEAST_EXPECT(expectOffers(env, gw, 0));
5563 BEAST_EXPECT(mptTester1.checkMPTokenOutstandingAmount(30));
5564 BEAST_EXPECT(mptTester2.checkMPTokenOutstandingAmount(320));
5565 BEAST_EXPECT(mptTester2.checkMPTokenAmount(carol, 190));
5566 BEAST_EXPECT(mptTester1.checkMPTokenAmount(bob, 30));
5567 BEAST_EXPECT(mptTester2.checkMPTokenAmount(bob, 130));
5568 }
5569
5570 // MPT/MPT, different issuer
5571 {
5572 Env env{*this, features};
5573 Account const gw1{"gw1"};
5574
5575 MPTTester mptTester1(env, gw, {.holders = {alice, carol, bob}});
5576 mptTester1.create(
5577 {.ownerCount = 1, .holderCount = 0, .flags = tfMPTCanTransfer | tfMPTCanTrade});
5578 auto const mpt1 = mptTester1["MPT1"];
5579
5580 env.fund(XRP(1'000), gw1);
5581 MPTTester mptTester2(env, gw1, {.holders = {alice, carol, bob}, .fund = false});
5582 mptTester2.create(
5583 {.ownerCount = 1, .holderCount = 0, .flags = tfMPTCanTransfer | tfMPTCanTrade});
5584 auto const mpt2 = mptTester2["MPT2"];
5585
5586 mptTester1.authorize({.account = alice});
5587 mptTester1.pay(gw, alice, 200);
5588 mptTester2.authorize({.account = alice});
5589
5590 mptTester2.authorize({.account = carol});
5591 mptTester2.pay(gw1, carol, 200);
5592
5593 mptTester1.authorize({.account = bob});
5594 mptTester1.pay(gw, bob, 200);
5595 mptTester2.authorize({.account = bob});
5596 mptTester2.pay(gw1, bob, 200);
5597
5598 mptTester1.authorize({.account = gw1});
5599 mptTester1.pay(gw, gw1, 200);
5600
5601 mptTester2.authorize({.account = gw});
5602 mptTester2.pay(gw1, gw, 200);
5603
5604 env(offer(alice, mpt2(100), mpt1(100)));
5605 env.close();
5606 BEAST_EXPECT(expectOffers(env, alice, 1, {{Amounts{mpt2(100), mpt1(100)}}}));
5607
5608 env(pay(carol, bob, mpt1(10)),
5609 test::jtx::Path(~mpt1),
5610 Sendmax(mpt2(10)),
5611 Txflags(tfPartialPayment));
5612 env.close();
5613 BEAST_EXPECT(expectOffers(env, alice, 1));
5614 BEAST_EXPECT(mptTester1.checkMPTokenOutstandingAmount(600));
5615 BEAST_EXPECT(mptTester2.checkMPTokenOutstandingAmount(600));
5616 BEAST_EXPECT(mptTester1.checkMPTokenAmount(gw1, 200));
5617 BEAST_EXPECT(mptTester2.checkMPTokenAmount(gw, 200));
5618 BEAST_EXPECT(mptTester2.checkMPTokenAmount(carol, 190));
5619 BEAST_EXPECT(mptTester1.checkMPTokenAmount(bob, 210));
5620 BEAST_EXPECT(mptTester2.checkMPTokenAmount(bob, 200));
5621 BEAST_EXPECT(mptTester1.checkMPTokenAmount(alice, 190));
5622 BEAST_EXPECT(mptTester2.checkMPTokenAmount(alice, 10));
5623
5624 env(pay(bob, gw, mpt1(10)),
5625 test::jtx::Path(~mpt1),
5626 Sendmax(mpt2(10)),
5627 Txflags(tfPartialPayment));
5628 env.close();
5629 BEAST_EXPECT(expectOffers(env, alice, 1));
5630 BEAST_EXPECT(mptTester1.checkMPTokenOutstandingAmount(590));
5631 BEAST_EXPECT(mptTester2.checkMPTokenOutstandingAmount(600));
5632 BEAST_EXPECT(mptTester1.checkMPTokenAmount(gw1, 200));
5633 BEAST_EXPECT(mptTester2.checkMPTokenAmount(gw, 200));
5634 BEAST_EXPECT(mptTester1.checkMPTokenAmount(bob, 210));
5635 BEAST_EXPECT(mptTester2.checkMPTokenAmount(bob, 190));
5636 BEAST_EXPECT(mptTester1.checkMPTokenAmount(alice, 180));
5637 BEAST_EXPECT(mptTester2.checkMPTokenAmount(alice, 20));
5638
5639 env(pay(gw, bob, mpt1(10)),
5640 test::jtx::Path(~mpt1),
5641 Sendmax(mpt2(10)),
5642 Txflags(tfPartialPayment));
5643 env.close();
5644 BEAST_EXPECT(expectOffers(env, alice, 1));
5645 BEAST_EXPECT(mptTester1.checkMPTokenOutstandingAmount(590));
5646 BEAST_EXPECT(mptTester2.checkMPTokenOutstandingAmount(600));
5647 BEAST_EXPECT(mptTester1.checkMPTokenAmount(gw1, 200));
5648 BEAST_EXPECT(mptTester2.checkMPTokenAmount(gw, 190));
5649 BEAST_EXPECT(mptTester1.checkMPTokenAmount(bob, 220));
5650 BEAST_EXPECT(mptTester2.checkMPTokenAmount(bob, 190));
5651 BEAST_EXPECT(mptTester1.checkMPTokenAmount(alice, 170));
5652 BEAST_EXPECT(mptTester2.checkMPTokenAmount(alice, 30));
5653
5654 env(pay(bob, gw1, mpt1(10)),
5655 test::jtx::Path(~mpt1),
5656 Sendmax(mpt2(10)),
5657 Txflags(tfPartialPayment));
5658 env.close();
5659 BEAST_EXPECT(expectOffers(env, alice, 1));
5660 BEAST_EXPECT(mptTester1.checkMPTokenOutstandingAmount(590));
5661 BEAST_EXPECT(mptTester2.checkMPTokenOutstandingAmount(600));
5662 BEAST_EXPECT(mptTester1.checkMPTokenAmount(gw1, 210));
5663 BEAST_EXPECT(mptTester2.checkMPTokenAmount(gw, 190));
5664 BEAST_EXPECT(mptTester1.checkMPTokenAmount(bob, 220));
5665 BEAST_EXPECT(mptTester2.checkMPTokenAmount(bob, 180));
5666 BEAST_EXPECT(mptTester1.checkMPTokenAmount(alice, 160));
5667 BEAST_EXPECT(mptTester2.checkMPTokenAmount(alice, 40));
5668
5669 env(pay(gw1, bob, mpt1(10)),
5670 test::jtx::Path(~mpt1),
5671 Sendmax(mpt2(10)),
5672 Txflags(tfPartialPayment));
5673 env.close();
5674 BEAST_EXPECT(expectOffers(env, alice, 1));
5675 BEAST_EXPECT(mptTester1.checkMPTokenOutstandingAmount(590));
5676 BEAST_EXPECT(mptTester2.checkMPTokenOutstandingAmount(610));
5677 BEAST_EXPECT(mptTester1.checkMPTokenAmount(gw1, 210));
5678 BEAST_EXPECT(mptTester2.checkMPTokenAmount(gw, 190));
5679 BEAST_EXPECT(mptTester1.checkMPTokenAmount(bob, 230));
5680 BEAST_EXPECT(mptTester2.checkMPTokenAmount(bob, 180));
5681 BEAST_EXPECT(mptTester1.checkMPTokenAmount(alice, 150));
5682 BEAST_EXPECT(mptTester2.checkMPTokenAmount(alice, 50));
5683
5684 env(pay(gw, gw1, mpt1(10)),
5685 test::jtx::Path(~mpt1),
5686 Sendmax(mpt2(10)),
5687 Txflags(tfPartialPayment));
5688 env.close();
5689 BEAST_EXPECT(expectOffers(env, alice, 1));
5690 BEAST_EXPECT(mptTester1.checkMPTokenOutstandingAmount(590));
5691 BEAST_EXPECT(mptTester2.checkMPTokenOutstandingAmount(610));
5692 BEAST_EXPECT(mptTester1.checkMPTokenAmount(gw1, 220));
5693 BEAST_EXPECT(mptTester2.checkMPTokenAmount(gw, 180));
5694 BEAST_EXPECT(mptTester1.checkMPTokenAmount(alice, 140));
5695 BEAST_EXPECT(mptTester2.checkMPTokenAmount(alice, 60));
5696
5697 env(pay(gw1, gw, mpt1(40)),
5698 test::jtx::Path(~mpt1),
5699 Sendmax(mpt2(40)),
5700 Txflags(tfPartialPayment));
5701 env.close();
5702 BEAST_EXPECT(expectOffers(env, alice, 0));
5703 BEAST_EXPECT(mptTester1.checkMPTokenOutstandingAmount(550));
5704 BEAST_EXPECT(mptTester2.checkMPTokenOutstandingAmount(650));
5705 BEAST_EXPECT(mptTester1.checkMPTokenAmount(gw1, 220));
5706 BEAST_EXPECT(mptTester2.checkMPTokenAmount(gw, 180));
5707 BEAST_EXPECT(mptTester1.checkMPTokenAmount(alice, 100));
5708 BEAST_EXPECT(mptTester2.checkMPTokenAmount(alice, 100));
5709 }
5710
5711 // MPT/IOU IOU/mpt1
5712 {
5713 Env env = pathTestEnv(*this);
5714 Account const gw1{"gw1"};
5715 Account const gw2{"gw2"};
5716 Account const dan{"dan"};
5717 env.fund(XRP(1'000), gw2);
5718 auto const usd = gw2["USD"];
5719
5720 MPTTester mptTester(env, gw, {.holders = {alice, carol}});
5721 mptTester.create(
5722 {.ownerCount = 1, .holderCount = 0, .flags = tfMPTCanTransfer | tfMPTCanTrade});
5723 auto const mpt = mptTester["MPT"];
5724 mptTester.authorize({.account = alice});
5725 mptTester.authorize({.account = carol});
5726 mptTester.pay(gw, carol, 200);
5727
5728 MPTTester mptTester1(env, gw1, {.holders = {bob, dan}});
5729 mptTester1.create(
5730 {.ownerCount = 1, .holderCount = 0, .flags = tfMPTCanTransfer | tfMPTCanTrade});
5731 auto const mpt1 = mptTester1["MPT1"];
5732 mptTester1.authorize({.account = bob});
5733 mptTester1.pay(gw1, bob, 200);
5734 mptTester1.authorize({.account = dan});
5735
5736 env(trust(alice, usd(400)));
5737 env(pay(gw2, alice, usd(200)));
5738 env(trust(bob, usd(400)));
5739
5740 env(offer(alice, mpt(100), usd(100)));
5741 env(offer(bob, usd(100), mpt1(100)));
5742 env.close();
5743
5744 env(pay(carol, dan, mpt1(100)),
5745 Sendmax(mpt(100)),
5746 Path(~usd, ~mpt1),
5747 Txflags(tfPartialPayment | tfNoRippleDirect));
5748 env.close();
5749 BEAST_EXPECT(expectOffers(env, alice, 0));
5750 BEAST_EXPECT(expectOffers(env, bob, 0));
5751 BEAST_EXPECT(mptTester.checkMPTokenAmount(carol, 100));
5752 BEAST_EXPECT(mptTester1.checkMPTokenAmount(dan, 100));
5753 }
5754
5755 // XRP/MPT AMM
5756 {
5757 Env env{*this, features};
5758
5759 fund(env, gw, {alice, carol, bob}, XRP(11'000), {usd(20'000)});
5760
5761 MPTTester mptTester(env, gw, {.fund = false});
5762
5763 mptTester.create(
5764 {.ownerCount = 1, .holderCount = 0, .flags = tfMPTCanTransfer | tfMPTCanTrade});
5765 auto const mpt = mptTester["MPT"];
5766
5767 mptTester.authorize({.account = alice});
5768 mptTester.authorize({.account = bob});
5769 mptTester.pay(gw, alice, 10'100);
5770
5771 AMM const amm(env, alice, XRP(10'000), mpt(10'100));
5772
5773 env(pay(carol, bob, mpt(100)),
5774 test::jtx::Path(~mpt),
5775 Sendmax(XRP(100)),
5776 Txflags(tfPartialPayment | tfNoRippleDirect));
5777 env.close();
5778
5779 BEAST_EXPECT(amm.expectBalances(XRP(10'100), mpt(10'000), amm.tokens()));
5780 BEAST_EXPECT(mptTester.checkMPTokenAmount(bob, 100));
5781 }
5782
5783 // IOU/MPT AMM
5784 {
5785 Env env{*this, features};
5786
5787 fund(env, gw, {alice, carol, bob}, XRP(11'000), {usd(20'000)});
5788
5789 MPTTester mptTester(env, gw, {.fund = false});
5790
5791 mptTester.create(
5792 {.ownerCount = 1, .holderCount = 0, .flags = tfMPTCanTransfer | tfMPTCanTrade});
5793 auto const mpt = mptTester["MPT"];
5794
5795 mptTester.authorize({.account = alice});
5796 mptTester.authorize({.account = bob});
5797 mptTester.pay(gw, alice, 10'100);
5798
5799 AMM const amm(env, alice, usd(10'000), mpt(10'100));
5800
5801 env(pay(carol, bob, mpt(100)),
5802 test::jtx::Path(~mpt),
5803 Sendmax(usd(100)),
5804 Txflags(tfPartialPayment | tfNoRippleDirect));
5805 env.close();
5806
5807 BEAST_EXPECT(amm.expectBalances(usd(10'100), mpt(10'000), amm.tokens()));
5808 BEAST_EXPECT(mptTester.checkMPTokenAmount(bob, 100));
5809 }
5810
5811 // MPT/MPT AMM cross-asset payment
5812 {
5813 Env env{*this, features};
5814 env.fund(XRP(20'000), gw, alice, carol, bob);
5815 env.close();
5816
5817 MPTTester mptTester1(env, gw, {.fund = false});
5818 mptTester1.create({.flags = tfMPTCanTransfer | tfMPTCanTrade});
5819 auto const mpt1 = mptTester1["MPT1"];
5820 mptTester1.authorize({.account = alice});
5821 mptTester1.authorize({.account = bob});
5822 mptTester1.pay(gw, alice, 10'100);
5823
5824 MPTTester mptTester2(env, gw, {.fund = false});
5825 mptTester2.create({.flags = tfMPTCanTransfer | tfMPTCanTrade});
5826 auto const mpt2 = mptTester2["MPT1"];
5827 mptTester2.authorize({.account = alice});
5828 mptTester2.authorize({.account = bob});
5829 mptTester2.authorize({.account = carol});
5830 mptTester2.pay(gw, alice, 10'100);
5831 mptTester2.pay(gw, carol, 100);
5832
5833 AMM const amm(env, alice, mpt2(10'000), mpt1(10'100));
5834
5835 env(pay(carol, bob, mpt1(100)),
5836 test::jtx::Path(~mpt1),
5837 Sendmax(mpt2(100)),
5838 Txflags(tfPartialPayment | tfNoRippleDirect));
5839 env.close();
5840
5841 BEAST_EXPECT(amm.expectBalances(mpt2(10'100), mpt1(10'000), amm.tokens()));
5842 BEAST_EXPECT(mptTester1.checkMPTokenAmount(bob, 100));
5843 }
5844
5845 // Multi-steps with AMM
5846 // EUR/MPT1 MPT1/MPT2 MPT2/USD USD/CRN AMM:CRN/MPT MPT/YAN
5847 {
5848 Env env{*this, features};
5849 auto const usd = gw["USD"];
5850 auto const eur = gw["EUR"];
5851 auto const crn = gw["CRN"];
5852 auto const yan = gw["YAN"];
5853
5854 fund(
5855 env,
5856 gw,
5857 {alice, carol, bob},
5858 XRP(1'000),
5859 {usd(1'000), eur(1'000), crn(2'000), yan(1'000)});
5860
5861 auto const createMPT = [&] -> std::pair<MPTTester, MPT> {
5862 MPTTester mptTester(env, gw, {.fund = false});
5863 mptTester.create({.flags = tfMPTCanTransfer | tfMPTCanTrade});
5864 mptTester.authorize({.account = alice});
5865 mptTester.pay(gw, alice, 2'000);
5866 return {mptTester, mptTester["MPT"]};
5867 };
5868
5869 auto const [mptTester1, mpt1] = createMPT();
5870 auto const [mptTester2, mpt2] = createMPT();
5871 auto const [mptTester3, mpt3] = createMPT();
5872
5873 env(offer(alice, eur(100), mpt1(101)));
5874 env(offer(alice, mpt1(101), mpt2(102)));
5875 env(offer(alice, mpt2(102), usd(103)));
5876 env(offer(alice, usd(103), crn(104)));
5877 env.close();
5878 AMM const amm(env, alice, crn(1'000), mpt3(1'104));
5879 env(offer(alice, mpt3(104), yan(100)));
5880
5881 env(pay(carol, bob, yan(100)),
5882 test::jtx::Path(~mpt1, ~mpt2, ~usd, ~crn, ~mpt3, ~yan),
5883 Sendmax(eur(100)),
5884 Txflags(tfPartialPayment | tfNoRippleDirect));
5885 env.close();
5886
5887 BEAST_EXPECT(env.balance(carol, eur) == eur(900));
5888 BEAST_EXPECT(env.balance(bob, yan) == yan(1'100));
5889 BEAST_EXPECT(amm.expectBalances(crn(1'104), mpt3(1'000), amm.tokens()));
5890 BEAST_EXPECT(expectOffers(env, alice, 0));
5891 }
5892
5893 // Multi-steps with AMM and MPT endpoints
5894 // mpt1/EUR EUR/mpt2 mpt2/USD USD/CRN AMM:CRN/MPT3 MPT3/MPT4
5895 {
5896 Env env{*this, features};
5897 auto const usd = gw["USD"];
5898 auto const eur = gw["EUR"];
5899 auto const crn = gw["CRN"];
5900
5901 fund(env, gw, {alice, carol, bob}, XRP(1'000), {usd(1'000), eur(1'000), crn(2'000)});
5902
5903 auto const createMPT = [&] -> std::pair<MPTTester, MPT> {
5904 MPTTester mptTester(env, gw, {.fund = false});
5905 mptTester.create({.flags = tfMPTCanTransfer | tfMPTCanTrade});
5906 mptTester.authorize({.account = alice});
5907 mptTester.pay(gw, alice, 2'000);
5908 return {mptTester, mptTester["MPT"]};
5909 };
5910
5911 auto const [mptTester1, mpt1] = createMPT();
5912 auto const [mptTester2, mpt2] = createMPT();
5913 auto const [mptTester3, mpt3] = createMPT();
5914 auto [mptTester4, mpt4] = createMPT();
5915 mptTester4.authorize({.account = bob});
5916
5917 env(offer(alice, eur(100), mpt1(101)));
5918 env(offer(alice, mpt1(101), mpt2(102)));
5919 env(offer(alice, mpt2(102), usd(103)));
5920 env(offer(alice, usd(103), crn(104)));
5921 env.close();
5922 AMM const amm(env, alice, crn(1'000), mpt3(1'104));
5923 env(offer(alice, mpt3(104), mpt4(100)));
5924
5925 env(pay(carol, bob, mpt4(100)),
5926 test::jtx::Path(~mpt1, ~mpt2, ~usd, ~crn, ~mpt3, ~mpt4),
5927 Sendmax(eur(100)),
5928 Txflags(tfPartialPayment | tfNoRippleDirect));
5929 env.close();
5930
5931 BEAST_EXPECT(env.balance(carol, eur) == eur(900));
5932 BEAST_EXPECT(mptTester4.checkMPTokenAmount(bob, 100));
5933 BEAST_EXPECT(amm.expectBalances(crn(1'104), mpt3(1'000), amm.tokens()));
5934 BEAST_EXPECT(expectOffers(env, alice, 0));
5935 }
5936
5937 // Check that limiting step reduces maximumAmount returned by
5938 // MPTEndpointStep::maxPaymentFlow()
5939 {
5940 Env env(*this, features);
5941
5942 env.fund(XRP(1'000), gw, alice, carol, bob);
5943
5944 MPTTester usdTester(env, gw, {.holders = {alice, carol, bob}, .fund = false});
5945 usdTester.create({
5946 .maxAmt = 1'000,
5947 .authorize = MPTCreate::allHolders,
5948 .pay = {{{alice}, 1'000}},
5949 .flags = tfMPTCanTransfer | tfMPTCanTrade,
5950 });
5951 auto const usd = usdTester["USD"];
5952
5953 MPTTester eurTester(env, gw, {.holders = {alice, carol, bob}, .fund = false});
5954 eurTester.create({
5955 .maxAmt = 1'000,
5956 .authorize = {{alice, carol}},
5957 .pay = {{{carol}, 100}},
5958 .flags = tfMPTCanTransfer | tfMPTCanTrade,
5959 });
5960 auto const eur = eurTester["EUR"];
5961
5962 env(offer(alice, eur(10), usd(10)));
5963
5964 env(pay(carol, bob, usd(10)),
5965 Sendmax(eur(10)),
5966 Path(~usd),
5967 Txflags(tfNoRippleDirect | tfPartialPayment));
5968 }
5969 {
5970 Env env(*this, features); // NOLINT TODO
5971 env.fund(XRP(1'000), gw, alice, carol, bob);
5972
5973 auto const musd = MPTTester(
5974 {.env = env, .issuer = gw, .holders = {alice, carol, bob}, .maxAmt = 1'000});
5975 MPT const usd = musd;
5976 env(pay(gw, alice, usd(800)));
5977 env(offer(gw, XRP(300), usd(300)));
5978 env(pay(carol, bob, usd(300)),
5979 Sendmax(XRP(300)),
5980 Path(~usd),
5981 Txflags(tfPartialPayment));
5982 BEAST_EXPECT(musd.checkMPTokenAmount(bob, 200));
5983 BEAST_EXPECT(musd.checkMPTokenOutstandingAmount(1'000));
5984 // initial + offer - fees
5985 BEAST_EXPECT(env.balance(gw) == (XRP(1'000) + XRP(200) - txFee(env, 3)));
5986 }
5987 {
5988 Env env(*this, features);
5989 auto const eur = gw["EUR"];
5990 env.fund(XRP(1'000), gw, alice, carol, bob);
5991 env.close();
5992
5993 env(trust(alice, eur(1'000)));
5994 env(pay(gw, alice, eur(300)));
5995 env(trust(bob, eur(1'000)));
5996
5997 auto const musd = MPTTester(
5998 {.env = env, .issuer = gw, .holders = {alice, carol, bob}, .maxAmt = 1'000});
5999 MPT const usd = musd;
6000
6001 env(pay(gw, alice, usd(800)));
6002 env(offer(gw, XRP(300), usd(300)));
6003 env(offer(alice, usd(300), eur(300)));
6004 env(pay(carol, bob, eur(300)),
6005 Sendmax(XRP(300)),
6006 Path(~usd, ~eur),
6007 Txflags(tfPartialPayment));
6008 BEAST_EXPECT(musd.checkMPTokenAmount(alice, 1'000));
6009 BEAST_EXPECT(musd.checkMPTokenOutstandingAmount(1'000));
6010 // initial + offer - fees
6011 BEAST_EXPECT(env.balance(gw) == (XRP(1'000) + XRP(200) - txFee(env, 4)));
6012 BEAST_EXPECT(env.balance(bob, eur) == eur(200));
6013 }
6014 }
6015
6016 void
6018 {
6019 testcase("Path");
6020 using namespace test::jtx;
6021 Account const gw{"gw"};
6022 Account const gw1{"gw1"};
6023 Account const alice{"alice"};
6024 Account const carol{"carol"};
6025 Account const bob{"bob"};
6026 Account const dan{"dan"};
6027 auto const usd = gw["USD"];
6028 auto const eur = gw1["EUR"];
6029
6030 // MPT can be a mpt end point step or a book-step
6031
6032 // Direct MPT payment
6033 {
6034 Env env = pathTestEnv(*this);
6035
6036 MPTTester mptTester(env, gw, {.holders = {dan, carol}});
6037 mptTester.create(
6038 {.ownerCount = 1, .holderCount = 0, .flags = tfMPTCanTransfer | tfMPTCanTrade});
6039 auto const mpt = mptTester["MPT"];
6040 mptTester.authorize({.account = dan});
6041 mptTester.authorize({.account = carol});
6042 mptTester.pay(gw, carol, 200);
6043
6044 auto const [pathSet, srcAmt, dstAmt] = findPaths(env, carol, dan, mpt(-1));
6045 BEAST_EXPECT(srcAmt == mpt(200));
6046 BEAST_EXPECT(dstAmt == mpt(200));
6047 // Direct payment, no path
6048 BEAST_EXPECT(pathSet.empty());
6049 }
6050
6051 // Cross-asset payment via XRP/MPT offer (one step)
6052 {
6053 Env env = pathTestEnv(*this);
6054
6055 env.fund(XRP(1'000), carol);
6056
6057 MPTTester mptTester(env, gw, {.holders = {alice, dan}});
6058
6059 mptTester.create(
6060 {.ownerCount = 1, .holderCount = 0, .flags = tfMPTCanTransfer | tfMPTCanTrade});
6061 auto const mpt = mptTester["MPT"];
6062
6063 mptTester.authorize({.account = alice});
6064 mptTester.authorize({.account = dan});
6065 mptTester.pay(gw, alice, 200);
6066
6067 env(offer(alice, XRP(100), mpt(100)));
6068 env.close();
6069
6070 auto const [pathSet, srcAmt, dstAmt] = findPaths(env, carol, dan, mpt(-1));
6071 BEAST_EXPECT(srcAmt == XRP(100));
6072 BEAST_EXPECT(dstAmt == mpt(100));
6073 if (BEAST_EXPECT(same(pathSet, stpath(ipe(mptTester.issuanceID())))))
6074 {
6075 // validate a payment works with the path
6076 env(pay(carol, dan, mpt(10)),
6077 Path(~mpt),
6078 Sendmax(XRP(10)),
6079 Txflags(tfNoRippleDirect | tfPartialPayment));
6080 }
6081 }
6082
6083 // Cross-asset payment via IOU/MPT offer (one step)
6084 {
6085 Env env = pathTestEnv(*this);
6086
6087 env.fund(XRP(1'000), carol);
6088 env.fund(XRP(1'000), gw);
6089
6090 MPTTester mptTester(env, gw1, {.holders = {alice, dan}});
6091
6092 mptTester.create(
6093 {.ownerCount = 1, .holderCount = 0, .flags = tfMPTCanTransfer | tfMPTCanTrade});
6094 auto const mpt = mptTester["MPT"];
6095
6096 mptTester.authorize({.account = alice});
6097 mptTester.authorize({.account = dan});
6098 mptTester.pay(gw1, alice, 200);
6099
6100 env(trust(alice, usd(400)));
6101 env(trust(carol, usd(400)));
6102 env(pay(gw, carol, usd(200)));
6103
6104 env(offer(alice, usd(100), mpt(100)));
6105 env.close();
6106
6107 // No sendMax
6108 STPathSet pathSet;
6109 STAmount srcAmt;
6110 STAmount dstAmt;
6111 std::tie(pathSet, srcAmt, dstAmt) = findPaths(env, carol, dan, mpt(-1));
6112 BEAST_EXPECT(srcAmt == usd(100));
6113 BEAST_EXPECT(dstAmt == mpt(100));
6114 if (BEAST_EXPECT(
6115 pathSet.size() == 1 && same(pathSet, stpath(gw, ipe(mptTester.issuanceID())))))
6116 {
6117 // Validate the payment works with the path
6118 env(pay(carol, dan, mpt(10)),
6119 Path(pathSet[0]),
6120 Sendmax(usd(10)),
6121 Txflags(tfNoRippleDirect | tfPartialPayment));
6122 }
6123
6124 // Include sendMax
6125 std::tie(pathSet, srcAmt, dstAmt) = findPaths(env, carol, dan, mpt(-1), usd(-1));
6126 BEAST_EXPECT(srcAmt == usd(90));
6127 BEAST_EXPECT(dstAmt == mpt(90));
6128 if (BEAST_EXPECT(
6129 pathSet.size() == 1 && same(pathSet, stpath(ipe(mptTester.issuanceID())))))
6130 {
6131 // validate a payment works with the path
6132 env(pay(carol, dan, mpt(10)),
6133 Path(pathSet[0]),
6134 Sendmax(usd(10)),
6135 Txflags(tfNoRippleDirect | tfPartialPayment));
6136 }
6137
6138 // Include source token
6139 std::tie(pathSet, srcAmt, dstAmt) =
6140 findPaths(env, carol, dan, mpt(-1), std::nullopt, usd.currency);
6141 BEAST_EXPECT(srcAmt == usd(80));
6142 BEAST_EXPECT(dstAmt == mpt(80));
6143 if (BEAST_EXPECT(
6144 pathSet.size() == 1 && same(pathSet, stpath(gw, ipe(mptTester.issuanceID())))))
6145 {
6146 // validate a payment works with the path
6147 env(pay(carol, dan, mpt(10)),
6148 Path(pathSet[0]),
6149 Sendmax(usd(10)),
6150 Txflags(tfNoRippleDirect | tfPartialPayment));
6151 }
6152 }
6153
6154 // Cross-asset payment via MPT/IOU offer (one step)
6155 {
6156 Env env = pathTestEnv(*this);
6157
6158 env.fund(XRP(1'000), dan);
6159 env.fund(XRP(1'000), gw);
6160
6161 MPTTester mptTester(env, gw1, {.holders = {carol, alice}});
6162
6163 mptTester.create(
6164 {.ownerCount = 1, .holderCount = 0, .flags = tfMPTCanTransfer | tfMPTCanTrade});
6165 auto const mpt = mptTester["MPT"];
6166
6167 mptTester.authorize({.account = carol});
6168 mptTester.authorize({.account = alice});
6169 mptTester.pay(gw1, carol, 200);
6170
6171 env(trust(dan, usd(400)));
6172 env(trust(alice, usd(400)));
6173 env(pay(gw, alice, usd(200)));
6174
6175 env(offer(alice, mpt(100), usd(100)));
6176 env.close();
6177
6178 // No sendMax
6179 STPathSet pathSet;
6180 STAmount srcAmt;
6181 STAmount dstAmt;
6182 std::tie(pathSet, srcAmt, dstAmt) = findPaths(env, carol, dan, usd(-1));
6183 BEAST_EXPECT(srcAmt == mpt(100));
6184 BEAST_EXPECT(dstAmt == usd(100));
6185 if (BEAST_EXPECT(pathSet.size() == 1 && same(pathSet, stpath(ipe(usd)))))
6186 {
6187 // Validate the payment works with the path
6188 env(pay(carol, dan, usd(10)),
6189 Path(pathSet[0]),
6190 Sendmax(mpt(10)),
6191 Txflags(tfNoRippleDirect | tfPartialPayment));
6192 }
6193
6194 // Include sendMax
6195 std::tie(pathSet, srcAmt, dstAmt) = findPaths(env, carol, dan, usd(-1), mpt(-1));
6196 BEAST_EXPECT(srcAmt == mpt(90));
6197 BEAST_EXPECT(dstAmt == usd(90));
6198 if (BEAST_EXPECT(pathSet.size() == 1 && same(pathSet, stpath(ipe(usd)))))
6199 {
6200 // validate a payment works with the path
6201 env(pay(carol, dan, usd(10)),
6202 Path(pathSet[0]),
6203 Sendmax(mpt(10)),
6204 Txflags(tfNoRippleDirect | tfPartialPayment));
6205 }
6206
6207 // Include source token
6208 std::tie(pathSet, srcAmt, dstAmt) =
6209 findPaths(env, carol, dan, usd(-1), std::nullopt, mpt.mpt());
6210 BEAST_EXPECT(srcAmt == mpt(80));
6211 BEAST_EXPECT(dstAmt == usd(80));
6212 if (BEAST_EXPECT(pathSet.size() == 1 && same(pathSet, stpath(ipe(usd)))))
6213 {
6214 // validate a payment works with the path
6215 env(pay(carol, dan, usd(10)),
6216 Path(pathSet[0]),
6217 Sendmax(mpt(10)),
6218 Txflags(tfNoRippleDirect | tfPartialPayment));
6219 }
6220 }
6221
6222 // Cross-asset payment via mpt1/MPT offer (one step)
6223 {
6224 Env env = pathTestEnv(*this);
6225
6226 MPTTester mptTester(env, gw, {.holders = {alice, dan}});
6227 MPTTester mptTester1(env, gw1, {.holders = {carol}});
6228
6229 mptTester.create(
6230 {.ownerCount = 1, .holderCount = 0, .flags = tfMPTCanTransfer | tfMPTCanTrade});
6231 auto const mpt = mptTester["MPT"];
6232 mptTester1.create(
6233 {.ownerCount = 1, .holderCount = 0, .flags = tfMPTCanTransfer | tfMPTCanTrade});
6234 auto const mpt1 = mptTester1["MPT1"];
6235
6236 mptTester.authorize({.account = alice});
6237 mptTester.authorize({.account = dan});
6238 mptTester.pay(gw, alice, 200);
6239
6240 mptTester1.authorize({.account = carol});
6241 mptTester1.authorize({.account = alice});
6242 mptTester1.pay(gw1, carol, 200);
6243
6244 env(offer(alice, mpt1(100), mpt(100)));
6245 env.close();
6246
6247 // No sendMax
6248 STPathSet pathSet;
6249 STAmount srcAmt;
6250 STAmount dstAmt;
6251 std::tie(pathSet, srcAmt, dstAmt) = findPaths(env, carol, dan, mpt(-1));
6252 BEAST_EXPECT(srcAmt == mpt1(100));
6253 BEAST_EXPECT(dstAmt == mpt(100));
6254 if (BEAST_EXPECT(
6255 pathSet.size() == 1 && same(pathSet, stpath(ipe(mptTester.issuanceID())))))
6256 {
6257 // validate a payment works with the path
6258 env(pay(carol, dan, mpt(10)),
6259 Path(pathSet[0]),
6260 Sendmax(mpt1(10)),
6261 Txflags(tfNoRippleDirect | tfPartialPayment));
6262 }
6263
6264 // Include sendMax
6265 std::tie(pathSet, srcAmt, dstAmt) = findPaths(env, carol, dan, mpt(-1), mpt1(-1));
6266 BEAST_EXPECT(srcAmt == mpt1(90));
6267 BEAST_EXPECT(dstAmt == mpt(90));
6268 if (BEAST_EXPECT(
6269 pathSet.size() == 1 && same(pathSet, stpath(ipe(mptTester.issuanceID())))))
6270 {
6271 // validate a payment works with the path
6272 env(pay(carol, dan, mpt(10)),
6273 Path(pathSet[0]),
6274 Sendmax(mpt1(10)),
6275 Txflags(tfNoRippleDirect | tfPartialPayment));
6276 }
6277
6278 // Include source token
6279 std::tie(pathSet, srcAmt, dstAmt) =
6280 findPaths(env, carol, dan, mpt(-1), std::nullopt, mpt1.mpt());
6281 BEAST_EXPECT(srcAmt == mpt1(80));
6282 BEAST_EXPECT(dstAmt == mpt(80));
6283 if (BEAST_EXPECT(
6284 pathSet.size() == 1 && same(pathSet, stpath(ipe(mptTester.issuanceID())))))
6285 {
6286 // validate a payment works with the path
6287 env(pay(carol, dan, mpt(10)),
6288 Path(pathSet[0]),
6289 Sendmax(mpt1(10)),
6290 Txflags(tfNoRippleDirect | tfPartialPayment));
6291 }
6292 }
6293
6294 // Cross-asset payment via offers (two steps)
6295 {
6296 Env env = pathTestEnv(*this);
6297
6298 env.fund(XRP(1'000), carol);
6299 env.fund(XRP(1'000), dan);
6300
6301 MPTTester mptTester(env, gw, {.holders = {alice, bob}});
6302
6303 mptTester.create(
6304 {.ownerCount = 1, .holderCount = 0, .flags = tfMPTCanTransfer | tfMPTCanTrade});
6305 auto const mpt = mptTester["MPT"];
6306
6307 mptTester.authorize({.account = alice});
6308 mptTester.authorize({.account = bob});
6309 mptTester.pay(gw, alice, 200);
6310 mptTester.pay(gw, bob, 200);
6311
6312 env(trust(bob, usd(200)));
6313 env(pay(gw, bob, usd(100)));
6314 env(trust(dan, usd(200)));
6315 env(trust(alice, usd(200)));
6316
6317 env(offer(alice, XRP(100), mpt(100)));
6318 env(offer(bob, mpt(100), usd(100)));
6319 env.close();
6320
6321 // No sendMax
6322 STPathSet pathSet;
6323 STAmount srcAmt;
6324 STAmount dstAmt;
6325 std::tie(pathSet, srcAmt, dstAmt) = findPaths(env, carol, dan, usd(-1));
6326 BEAST_EXPECT(srcAmt == XRP(100));
6327 BEAST_EXPECT(dstAmt == usd(100));
6328 if (BEAST_EXPECT(
6329 pathSet.size() == 1 &&
6330 same(pathSet, stpath(ipe(mptTester.issuanceID()), ipe(usd)))))
6331 {
6332 // validate a payment works with the path
6333 env(pay(carol, dan, usd(10)),
6334 Path(pathSet[0]),
6335 Sendmax(XRP(10)),
6336 Txflags(tfNoRippleDirect | tfPartialPayment));
6337 }
6338
6339 // Include sendMax
6340 std::tie(pathSet, srcAmt, dstAmt) = findPaths(env, carol, dan, usd(-1), XRP(100));
6341 BEAST_EXPECT(srcAmt == XRP(90));
6342 BEAST_EXPECT(dstAmt == usd(90));
6343 if (BEAST_EXPECT(
6344 pathSet.size() == 1 &&
6345 same(pathSet, stpath(ipe(mptTester.issuanceID()), ipe(usd)))))
6346 {
6347 // validate a payment works with the path
6348 env(pay(carol, dan, usd(10)),
6349 Path(pathSet[0]),
6350 Sendmax(XRP(10)),
6351 Txflags(tfNoRippleDirect | tfPartialPayment));
6352 }
6353 }
6354
6355 // Cross-asset payment via offers (two steps)
6356 // Start/End with mpt/mp1 and book steps in the middle
6357 {
6358 Env env = pathTestEnv(*this);
6359 Account const gw2{"gw2"};
6360 env.fund(XRP(1'000), gw2);
6361 auto const usd2 = gw2["USD"];
6362
6363 MPTTester mptTester(env, gw, {.holders = {alice, carol}});
6364 mptTester.create(
6365 {.ownerCount = 1, .holderCount = 0, .flags = tfMPTCanTransfer | tfMPTCanTrade});
6366 auto const mpt = mptTester["MPT"];
6367 mptTester.authorize({.account = alice});
6368 mptTester.authorize({.account = carol});
6369 mptTester.pay(gw, carol, 200);
6370
6371 MPTTester mptTester1(env, gw1, {.holders = {bob, dan}});
6372 mptTester1.create(
6373 {.ownerCount = 1, .holderCount = 0, .flags = tfMPTCanTransfer | tfMPTCanTrade});
6374 auto const mpt1 = mptTester1["MPT1"];
6375 mptTester1.authorize({.account = bob});
6376 mptTester1.pay(gw1, bob, 200);
6377 mptTester1.authorize({.account = dan});
6378
6379 env(trust(alice, usd2(400)));
6380 env(pay(gw2, alice, usd2(200)));
6381 env(trust(bob, usd2(400)));
6382
6383 env(offer(alice, mpt(100), usd2(100)));
6384 env(offer(bob, usd2(100), mpt1(100)));
6385 env.close();
6386
6387 // No sendMax
6388 STPathSet pathSet;
6389 STAmount srcAmt;
6390 STAmount dstAmt;
6391 std::tie(pathSet, srcAmt, dstAmt) = findPaths(env, carol, dan, mpt1(-1));
6392 BEAST_EXPECT(srcAmt == mpt(100));
6393 BEAST_EXPECT(dstAmt == mpt1(100));
6394 if (BEAST_EXPECT(
6395 pathSet.size() == 1 &&
6396 same(pathSet, stpath(ipe(usd2), ipe(mptTester1.issuanceID())))))
6397 {
6398 // validate a payment works with the path
6399 env(pay(carol, dan, mpt1(10)),
6400 Path(pathSet[0]),
6401 Sendmax(mpt(10)),
6402 Txflags(tfNoRippleDirect | tfPartialPayment));
6403 }
6404
6405 // Include sendMax
6406 std::tie(pathSet, srcAmt, dstAmt) = findPaths(env, carol, dan, mpt1(-1), mpt(-1));
6407 BEAST_EXPECT(srcAmt == mpt(90));
6408 BEAST_EXPECT(dstAmt == mpt1(90));
6409 if (BEAST_EXPECT(
6410 pathSet.size() == 1 &&
6411 same(pathSet, stpath(ipe(usd2), ipe(mptTester1.issuanceID())))))
6412 {
6413 // validate a payment works with the path
6414 env(pay(carol, dan, mpt1(10)),
6415 Path(pathSet[0]),
6416 Sendmax(mpt(10)),
6417 Txflags(tfNoRippleDirect | tfPartialPayment));
6418 }
6419
6420 // Include source token
6421 std::tie(pathSet, srcAmt, dstAmt) =
6422 findPaths(env, carol, dan, mpt1(-1), std::nullopt, mpt.mpt());
6423 BEAST_EXPECT(srcAmt == mpt(80));
6424 BEAST_EXPECT(dstAmt == mpt1(80));
6425 if (BEAST_EXPECT(
6426 pathSet.size() == 1 &&
6427 same(pathSet, stpath(ipe(usd2), ipe(mptTester1.issuanceID())))))
6428 {
6429 // validate a payment works with the path
6430 env(pay(carol, dan, mpt1(10)),
6431 Path(pathSet[0]),
6432 Sendmax(mpt(10)),
6433 Txflags(tfNoRippleDirect | tfPartialPayment));
6434 }
6435 }
6436
6437 // Cross-asset payment via offers (two steps)
6438 // Start/End with mpt/mp2 and book steps in the middle
6439 // offers are MPT/MPT
6440 {
6441 Env env = pathTestEnv(*this);
6442 Account const gw2{"gw2"};
6443 env.fund(XRP(1'000), gw, gw1, gw2, alice, bob, carol, dan);
6444
6445 MPTTester mptTester(env, gw, {.holders = {alice, carol}, .fund = false});
6446 mptTester.create(
6447 {.ownerCount = 1, .holderCount = 0, .flags = tfMPTCanTransfer | tfMPTCanTrade});
6448 auto const mpt = mptTester["MPT"];
6449 mptTester.authorize({.account = alice});
6450 mptTester.authorize({.account = carol});
6451 mptTester.pay(gw, carol, 200);
6452
6453 MPTTester mptTester1(env, gw1, {.holders = {bob, alice}, .fund = false});
6454 mptTester1.create({.ownerCount = 1, .flags = tfMPTCanTransfer | tfMPTCanTrade});
6455 auto const mpt1 = mptTester1["MPT1"];
6456 mptTester1.authorize({.account = alice});
6457 mptTester1.pay(gw1, alice, 200);
6458 mptTester1.authorize({.account = bob});
6459
6460 MPTTester mptTester2(env, gw2, {.holders = {bob, dan}, .fund = false});
6461 mptTester2.create({.ownerCount = 1, .flags = tfMPTCanTransfer | tfMPTCanTrade});
6462 auto const mpt2 = mptTester2["MPT2"];
6463 mptTester2.authorize({.account = bob});
6464 mptTester2.pay(gw2, bob, 200);
6465 mptTester2.authorize({.account = dan});
6466
6467 env(offer(alice, mpt(100), mpt1(100)));
6468 env(offer(bob, mpt1(100), mpt2(100)));
6469 env.close();
6470
6471 // No sendMax
6472 STPathSet pathSet;
6473 STAmount srcAmt;
6474 STAmount dstAmt;
6475 std::tie(pathSet, srcAmt, dstAmt) = findPaths(env, carol, dan, mpt2(-1));
6476 BEAST_EXPECT(srcAmt == mpt(100));
6477 BEAST_EXPECT(dstAmt == mpt2(100));
6478 if (BEAST_EXPECT(
6479 pathSet.size() == 1 &&
6480 same(
6481 pathSet,
6482 stpath(ipe(mptTester1.issuanceID()), ipe(mptTester2.issuanceID())))))
6483 {
6484 // validate a payment works with the path
6485 env(pay(carol, dan, mpt2(10)),
6486 Path(pathSet[0]),
6487 Sendmax(mpt(10)),
6488 Txflags(tfNoRippleDirect | tfPartialPayment));
6489 }
6490
6491 // Include sendMax
6492 std::tie(pathSet, srcAmt, dstAmt) = findPaths(env, carol, dan, mpt2(-1), mpt(-1));
6493 BEAST_EXPECT(srcAmt == mpt(90));
6494 BEAST_EXPECT(dstAmt == mpt2(90));
6495 if (BEAST_EXPECT(
6496 pathSet.size() == 1 &&
6497 same(
6498 pathSet,
6499 stpath(ipe(mptTester1.issuanceID()), ipe(mptTester2.issuanceID())))))
6500 {
6501 // validate a payment works with the path
6502 env(pay(carol, dan, mpt2(10)),
6503 Path(pathSet[0]),
6504 Sendmax(mpt(10)),
6505 Txflags(tfNoRippleDirect | tfPartialPayment));
6506 }
6507
6508 // Include source token
6509 std::tie(pathSet, srcAmt, dstAmt) =
6510 findPaths(env, carol, dan, mpt2(-1), std::nullopt, mpt.mpt());
6511 BEAST_EXPECT(srcAmt == mpt(80));
6512 BEAST_EXPECT(dstAmt == mpt2(80));
6513 if (BEAST_EXPECT(
6514 pathSet.size() == 1 &&
6515 same(
6516 pathSet,
6517 stpath(ipe(mptTester1.issuanceID()), ipe(mptTester2.issuanceID())))))
6518 {
6519 // validate a payment works with the path
6520 env(pay(carol, dan, mpt2(10)),
6521 Path(pathSet[0]),
6522 Sendmax(mpt(10)),
6523 Txflags(tfNoRippleDirect | tfPartialPayment));
6524 }
6525 }
6526
6527 // verify no MPT rippling
6528 {
6529 Env env = pathTestEnv(*this);
6530 Account const gw{"gw"};
6531 Account const gw1{"gw1"};
6532 Account const carol{"carol"};
6533 Account const bob{"bob"};
6534 Account const dan{"dan"};
6535 Account const john{"john"};
6536 Account const sean{"sean"};
6537
6538 env.fund(XRP(1'000'000), gw);
6539 env.fund(XRP(1'000'000), gw1);
6540 env.fund(XRP(1'000'000), carol);
6541 env.fund(XRP(1'000'000), dan);
6542 env.fund(XRP(1'000'000), bob);
6543 env.fund(XRP(1'000'000), john);
6544 env.fund(XRP(1'000'000), sean);
6545 env.close();
6546
6547 MPTTester usdTester(env, gw, {.holders = {carol, dan}, .fund = false});
6548 usdTester.create({
6549 .authorize = MPTCreate::allHolders,
6550 .pay = {{MPTCreate::allHolders, 100}},
6551 .flags = tfMPTCanTransfer | tfMPTCanTrade,
6552 });
6553 auto const usd = usdTester["USD"];
6554 env(offer(carol, XRP(100), usd(100)));
6555
6556 MPTTester gbpTester(env, gw, {.holders = {bob, sean}, .fund = false});
6557 gbpTester.create({
6558 .authorize = MPTCreate::allHolders,
6559 .pay = {{{bob}, 100}},
6560 .flags = tfMPTCanTransfer | tfMPTCanTrade,
6561 });
6562 auto const gbp = gbpTester["GBP"];
6563
6564 MPTTester usd1(env, gw1, {.holders = {bob, dan}, .fund = false});
6565 usd1.create({
6566 .authorize = MPTCreate::allHolders,
6567 .pay = {{{dan}, 100}},
6568 .flags = tfMPTCanTransfer | tfMPTCanTrade,
6569 });
6570 auto const usD1 = usd1["USD1"];
6571 env(offer(bob, usd1(100), gbp(100)));
6572
6573 // dan has USD/gw and USD1/gw. Had USD been IOU, it would have
6574 // been able to ripple through dan's account.
6575 auto const [pathSet, srcAmt, dstAmt] = findPaths(env, john, sean, gbp(-1), XRP(-1));
6576 BEAST_EXPECT(pathSet.empty());
6577
6578 env(pay(john, sean, gbp(10)),
6579 Sendmax(XRP(20)),
6580 Path(~usd, dan, gw1, ~gbp),
6581 Txflags(tfNoRippleDirect | tfPartialPayment),
6582 Ter(temBAD_PATH));
6583 }
6584 }
6585
6586 void
6588 {
6589 testcase("Check Create/Cash");
6590
6591 using namespace test::jtx;
6592 Account const gw{"gw"};
6593 Account const alice{"alice"};
6594 Account const carol{"carol"};
6595
6596 // MPTokensV2 is disabled
6597 {
6598 Env env{*this, features - featureMPTokensV2};
6599
6600 MPTTester mptTester(env, gw, {.holders = {alice}});
6601 mptTester.create(
6602 {.ownerCount = 1, .holderCount = 0, .flags = tfMPTCanTransfer | tfMPTCanTrade});
6603 auto const mpt = mptTester["MPT"];
6604 mptTester.authorize({.account = alice});
6605
6606 UInt256 const checkId{keylet::check(gw, SeqProxy::rawSequence(env.seq(gw))).key};
6607
6608 env(check::create(gw, alice, mpt(100)), Ter(temDISABLED));
6609 env.close();
6610
6611 env(check::cash(alice, checkId, mpt(100)), Ter(temDISABLED));
6612 env.close();
6613 }
6614
6615 // Insufficient funds
6616 {
6617 Env env{*this, features};
6618 Account const carol{"carol"};
6619
6620 MPTTester mptTester(env, gw, {.holders = {alice, carol}});
6621 mptTester.create(
6622 {.ownerCount = 1, .holderCount = 0, .flags = tfMPTCanTransfer | tfMPTCanTrade});
6623 auto const mpt = mptTester["MPT"];
6624 mptTester.authorize({.account = alice});
6625 mptTester.pay(gw, alice, 50);
6626
6627 UInt256 const checkId{keylet::check(alice, SeqProxy::rawSequence(env.seq(alice))).key};
6628
6629 // can create
6630 env(check::create(alice, carol, mpt(100)));
6631 env.close();
6632
6633 // can't cash since alice only has 50 of MPT
6634 env(check::cash(carol, checkId, mpt(100)), Ter(tecPATH_PARTIAL));
6635 env.close();
6636
6637 // can cash if DeliverMin is set
6638 // carol is not authorized, MPToken is authorized by CheckCash
6639 env(check::cash(carol, checkId, check::DeliverMin(mpt(50))));
6640 env.close();
6641 BEAST_EXPECT(mptTester.checkMPTokenAmount(carol, 50));
6642 BEAST_EXPECT(mptTester.checkMPTokenOutstandingAmount(50));
6643 }
6644
6645 // Exceed max amount
6646 {
6647 Env env{*this, features};
6648
6649 MPTTester mptTester(env, gw, {.holders = {alice}});
6650 mptTester.create({
6651 .maxAmt = 100,
6652 .ownerCount = 1,
6653 .holderCount = 0,
6654 .flags = tfMPTCanTransfer | tfMPTCanTrade,
6655 });
6656 auto const mpt = mptTester["MPT"];
6657
6658 UInt256 const checkId{keylet::check(gw, SeqProxy::rawSequence(env.seq(gw))).key};
6659
6660 // can create
6661 env(check::create(gw, alice, mpt(200)));
6662 env.close();
6663
6664 // can't cash since the outstanding amount exceeds max amount
6665 env(check::cash(alice, checkId, mpt(200)), Ter(tecPATH_PARTIAL));
6666 env.close();
6667
6668 // can cash if DeliverMin is set
6669 env(check::cash(alice, checkId, check::DeliverMin(mpt(100))));
6670 env.close();
6671 BEAST_EXPECT(mptTester.checkMPTokenAmount(alice, 100));
6672 BEAST_EXPECT(mptTester.checkMPTokenOutstandingAmount(100));
6673 }
6674
6675 // MPTokenIssuance object doesn't exist
6676 {
6677 Env env{*this, features};
6678 env.fund(XRP(1'000), gw, alice, carol);
6679 env(check::create(alice, carol, MPT(gw)(50)), Ter(tecOBJECT_NOT_FOUND));
6680 env.close();
6681 auto const btc = MPTTester({.env = env, .issuer = gw});
6682 UInt256 const chkId{getCheckIndex(gw, env.seq(gw))};
6683 env(check::cash(carol, chkId, MPT(gw)(1)), Ter(tecNO_ENTRY));
6684 env.close();
6685 }
6686
6687 // MPToken doesn't exist - can create check since MPToken will be
6688 // automatically created on cash check
6689 {
6690 Env env{*this, features};
6691 env.fund(XRP(1'000), gw, alice, carol);
6692 auto const btc = MPTTester({.env = env, .issuer = gw});
6693 UInt256 const chkId{getCheckIndex(alice, env.seq(alice))};
6694 env(check::create(alice, carol, btc(50)));
6695 env.close();
6696
6697 // But cashing fails if alice doesn't have MPToken
6698 env(check::cash(carol, chkId, btc(1)), Ter(tecPATH_PARTIAL));
6699 env.close();
6700 }
6701
6702 // MPTLock is set
6703 {
6704 Env env{*this, features};
6705 env.fund(XRP(1'000), gw, alice, carol);
6706 env.close();
6707 auto mpt = MPTTester({
6708 .env = env,
6709 .issuer = gw,
6710 .holders = {alice, carol},
6711 .pay = 100,
6712 .flags = kMptDexFlags | tfMPTCanLock,
6713 });
6714
6715 mpt.set({.flags = tfMPTLock});
6716
6717 // Create Check fails, holder or issuer as destination
6718 env(check::create(alice, carol, mpt(10)), Ter(tecLOCKED));
6719 env.close();
6720 env(check::create(gw, carol, mpt(10)), Ter(tecLOCKED));
6721 env.close();
6722
6723 mpt.set({.flags = tfMPTUnlock});
6724
6725 // Create Check succeeds, holder or issuer as destination
6726 UInt256 const chkIdAlice{getCheckIndex(alice, env.seq(alice))};
6727 env(check::create(alice, carol, mpt(10)));
6728 env.close();
6729 UInt256 const chkIdGw{getCheckIndex(gw, env.seq(gw))};
6730 env(check::create(gw, carol, mpt(10)));
6731 env.close();
6732
6733 mpt.set({.flags = tfMPTLock});
6734
6735 // Cash Check fails, holder and issuer env(check::cash(carol,
6736 // chkIdAlice, mpt(1)), ter(tecPATH_PARTIAL)); // tec is different
6737 // if the source is the issuer (this is consistent with IOU)
6738 env(check::cash(carol, chkIdGw, mpt(2)), Ter(tecLOCKED));
6739 env.close();
6740
6741 mpt.set({.flags = tfMPTUnlock});
6742
6743 // Cash Check succeeds, holder and issuer.
6744 env(check::cash(carol, chkIdAlice, mpt(1)));
6745 env(check::cash(carol, chkIdGw, mpt(2)));
6746
6747 // Individual lock
6748 mpt.set({.holder = alice, .flags = tfMPTLock});
6749 env(check::create(alice, carol, mpt(10)), Ter(tecLOCKED));
6750 env.close();
6751 env(check::create(carol, alice, mpt(10)), Ter(tecLOCKED));
6752 env.close();
6753
6754 mpt.set({.holder = alice, .flags = tfMPTUnlock});
6755 UInt256 const chkId1{getCheckIndex(alice, env.seq(alice))};
6756 env(check::create(alice, carol, mpt(10)));
6757 env.close();
6758 UInt256 const chkId2{getCheckIndex(gw, env.seq(gw))};
6759 env(check::create(gw, alice, mpt(10)));
6760 env.close();
6761 UInt256 const chkId3{getCheckIndex(alice, env.seq(alice))};
6762 env(check::create(alice, gw, mpt(10)));
6763 env.close();
6764 UInt256 const chkId4{getCheckIndex(gw, env.seq(gw))};
6765 env(check::create(gw, alice, mpt(10)));
6766 env.close();
6767 mpt.set({.holder = alice, .flags = tfMPTLock});
6768 env(check::cash(carol, chkId1, mpt(1)), Ter(tecPATH_PARTIAL));
6769 env(check::cash(alice, chkId2, mpt(1)), Ter(tecLOCKED));
6770 env(check::cash(gw, chkId3, mpt(1)), Ter(tecPATH_PARTIAL));
6771 env(check::cash(alice, chkId4, mpt(1)), Ter(tecLOCKED));
6772 }
6773
6774 // MPTRequireAuth flag is set and the account is not authorized.
6775 // Can create check, which is consistent with the trustlines.
6776 // It should fail on cash check.
6777 {
6778 Env env{*this, features};
6779 env.fund(XRP(1'000), gw, alice, carol);
6780 auto btc = MPTTester({
6781 .env = env,
6782 .issuer = gw,
6783 .holders = {alice, carol},
6784 .flags = tfMPTRequireAuth | kMptDexFlags,
6785 });
6786 UInt256 const chkId{getCheckIndex(alice, env.seq(alice))};
6787 env(check::create(alice, carol, btc(50)));
6788 env.close();
6789
6790 // Authorize alice
6791 btc.authorize({.account = gw, .holder = alice});
6792 env(pay(gw, alice, btc(100)));
6793
6794 // carol is still not authorized
6795 env(check::cash(carol, chkId, btc(10)), Ter(tecNO_AUTH));
6796 env.close();
6797
6798 // authorize carol, can cash now
6799 btc.authorize({.account = gw, .holder = carol});
6800 env(check::cash(carol, chkId, btc(10)));
6801 env.close();
6802 }
6803
6804 // MPTCanTransfer is disabled
6805 {
6806 Env env{*this, features};
6807 env.fund(XRP(1'000), gw, alice, carol);
6808 env.close();
6809
6810 MPTTester mpt(
6811 {.env = env, .issuer = gw, .holders = {alice, carol}, .flags = tfMPTCanTrade});
6812
6813 // src is issuer
6814 UInt256 checkId{keylet::check(gw, SeqProxy::rawSequence(env.seq(gw))).key};
6815
6816 // can create
6817 env(check::create(gw, alice, mpt(100)));
6818 env.close();
6819
6820 // can cash since source is issuer
6821 env(check::cash(alice, checkId, mpt(100)));
6822 env.close();
6823
6824 BEAST_EXPECT(env.balance(alice, mpt) == mpt(100));
6825 BEAST_EXPECT(env.balance(gw, mpt) == mpt(-100));
6826
6827 // dst is issuer
6828 checkId = keylet::check(alice, SeqProxy::rawSequence(env.seq(alice))).key;
6829
6830 // can create
6831 env(check::create(alice, gw, mpt(100)));
6832 env.close();
6833
6834 // can cash since source is issuer
6835 env(check::cash(gw, checkId, mpt(100)));
6836 env.close();
6837
6838 BEAST_EXPECT(env.balance(alice, mpt) == mpt(0));
6839 BEAST_EXPECT(env.balance(gw, mpt) == mpt(0));
6840
6841 // neither src nor dst is issuer, can't create
6842 checkId = keylet::check(alice, SeqProxy::rawSequence(env.seq(alice))).key;
6843 env(check::create(alice, carol, mpt(100)), Ter(tecNO_AUTH));
6844 env.close();
6845
6846 // can create now
6847 mpt.set({.account = gw, .flags = tfMPTSetCanTransfer});
6848 checkId = keylet::check(alice, SeqProxy::rawSequence(env.seq(alice))).key;
6849 env(check::create(alice, carol, mpt(100)));
6850 env.close();
6851 env(pay(gw, alice, mpt(10)));
6852 env.close();
6853
6854 // can cash since MPTCanTransfer is enabled
6855 env(check::cash(carol, checkId, mpt(10)));
6856 env.close();
6857 }
6858
6859 // MPTCanTrade is disabled
6860 {
6861 Env env{*this, features};
6862 env.fund(XRP(1'000), gw, alice, carol);
6863 env.close();
6864
6865 MPT const mpt = MPTTester({
6866 .env = env,
6867 .issuer = gw,
6868 .holders = {alice, carol},
6869 .pay = 10,
6870 .flags = tfMPTCanTransfer,
6871 });
6872
6873 UInt256 const checkId{keylet::check(alice, SeqProxy::rawSequence(env.seq(alice))).key};
6874
6875 // can create
6876 env(check::create(alice, carol, mpt(100)));
6877 env.close();
6878
6879 // can cash
6880 env(check::cash(carol, checkId, mpt(10)));
6881 env.close();
6882 }
6883
6884 // MPTokenIssuance object doesn't exist
6885 {
6886 Env env{*this, features};
6887 env.fund(XRP(1'000), gw, alice, carol);
6888 auto const usd = MPTTester({.env = env, .issuer = gw, .holders = {alice}});
6889 UInt256 const chkId{getCheckIndex(alice, env.seq(alice))};
6890 env(check::create(alice, carol, usd(1)));
6891 env.close();
6892
6893 // temMALFORMED because MPT is not USD. It doesn't matter if it
6894 // exists or not
6895 env(check::cash(carol, chkId, MPT(alice)(1)), Ter(temMALFORMED));
6896 env.close();
6897 }
6898
6899 // MPToken object doesn't exist and the account is not the issuer of MPT
6900 {
6901 Env env{*this, features};
6902 env.fund(XRP(1'000), gw, alice, carol);
6903
6904 auto const btc =
6905 MPTTester({.env = env, .issuer = gw, .holders = {alice}, .pay = 1'000});
6906
6907 UInt256 const chkId{getCheckIndex(alice, env.seq(alice))};
6908
6909 env(check::create(alice, carol, btc(1)));
6910 env.close();
6911
6912 // MPToken is automatically created
6913 env(check::cash(carol, chkId, btc(1)));
6914 env.close();
6915 }
6916
6917 // MPTRequireAuth flag is set and the account is not authorized.
6918 {
6919 Env env{*this, features};
6920 env.fund(XRP(1'000), gw, alice, carol);
6921
6922 auto const btc = MPTTester({
6923 .env = env,
6924 .issuer = gw,
6925 .holders = {alice},
6926 .flags = tfMPTRequireAuth | kMptDexFlags,
6927 .authHolder = true,
6928 });
6929 UInt256 const chkId{getCheckIndex(alice, env.seq(alice))};
6930 env(check::create(alice, carol, btc(1)));
6931 env.close();
6932
6933 env(check::cash(carol, chkId, btc(1)), Ter(tecPATH_PARTIAL));
6934 env.close();
6935 }
6936
6937 // Can create check if src/dst don't own MPT
6938 {
6939 Env env{*this, features};
6940
6941 MPTTester mptTester(env, gw);
6942 mptTester.create(
6943 {.ownerCount = 1, .holderCount = 0, .flags = tfMPTCanTransfer | tfMPTCanTrade});
6944 auto const mpt = mptTester["MPT"];
6945
6946 env.fund(XRP(1'000), alice, carol);
6947
6948 // src is issuer
6949 UInt256 const checkId{keylet::check(alice, SeqProxy::rawSequence(env.seq(alice))).key};
6950
6951 // can create
6952 env(check::create(alice, carol, mpt(100)));
6953 env.close();
6954
6955 // authorize/fund alice
6956 mptTester.authorize({.account = alice});
6957 mptTester.pay(gw, alice, 100);
6958
6959 // carol can cash the check. MPToken is created automatically
6960 env(check::cash(carol, checkId, mpt(100)));
6961 env.close();
6962
6963 BEAST_EXPECT(mptTester.checkMPTokenAmount(carol, 100));
6964 BEAST_EXPECT(mptTester.checkMPTokenOutstandingAmount(100));
6965 }
6966
6967 // Normal create/cash
6968 {
6969 Env env{*this, features};
6970
6971 MPTTester mptTester(env, gw, {.holders = {alice}});
6972 mptTester.create(
6973 {.ownerCount = 1, .holderCount = 0, .flags = tfMPTCanTransfer | tfMPTCanTrade});
6974 auto const mpt = mptTester["MPT"];
6975 mptTester.authorize({.account = alice});
6976
6977 UInt256 const checkId{keylet::check(gw, SeqProxy::rawSequence(env.seq(gw))).key};
6978
6979 env(check::create(gw, alice, mpt(100)));
6980 env.close();
6981
6982 env(check::cash(alice, checkId, mpt(100)));
6983 env.close();
6984
6985 BEAST_EXPECT(mptTester.checkMPTokenAmount(alice, 100));
6986 BEAST_EXPECT(mptTester.checkMPTokenOutstandingAmount(100));
6987 }
6988 }
6989
6990 void
6992 {
6993 using namespace jtx;
6994 testcase("AMMClawback");
6995 Account const gw{"gw"};
6996 Account const alice{"alice"};
6997 auto const usd = gw["USD"];
6998
6999 // MPTokenIssuance object doesn't exist
7000 {
7001 Env env(*this, features);
7002 env.fund(XRP(1'000), gw, alice);
7003 MPTTester const btc({.env = env, .issuer = gw});
7004 AMM const amm(env, gw, btc(100), usd(100));
7005 env(amm::ammClawback(gw, alice, usd, MPT(alice), std::nullopt), Ter(terNO_AMM));
7006 env(amm::ammClawback(gw, alice, usd, btc, MPT(alice)(100)), Ter(temBAD_AMOUNT));
7007 }
7008
7009 // MPTLock flag is set and the account is not the issuer of MPT -
7010 // can still clawback since the issuer clawbacks
7011 {
7012 Env env(*this, features);
7013 env.fund(XRP(100'000), gw, alice);
7014 env.close();
7015
7016 env(fset(gw, asfAllowTrustLineClawback));
7017 env.close();
7018
7019 auto btc = MPTTester({
7020 .env = env,
7021 .issuer = gw,
7022 .holders = {alice},
7023 .pay = 40'000,
7024 .flags = tfMPTCanLock | tfMPTCanClawback | kMptDexFlags,
7025 });
7026
7027 env.trust(usd(10'000), alice);
7028 env(pay(gw, alice, usd(10'000)));
7029 env.close();
7030
7031 AMM amm(env, gw, btc(100), usd(100));
7032 env.close();
7033 amm.deposit(alice, 1'000);
7034 env.close();
7035
7036 btc.set({.flags = tfMPTLock});
7037
7038 env(amm::ammClawback(gw, alice, btc, usd, std::nullopt));
7039 }
7040
7041 // MPTRequireAuth flag is set and the account is not authorized -
7042 // can still clawback since the issuer clawbacks
7043 {
7044 Env env(*this, features);
7045 env.fund(XRP(100'000), gw, alice);
7046 env.close();
7047
7048 env(fset(gw, asfAllowTrustLineClawback));
7049 env.close();
7050
7051 auto btc = MPTTester({
7052 .env = env,
7053 .issuer = gw,
7054 .holders = {alice},
7055 .pay = 40'000,
7056 .flags = tfMPTRequireAuth | tfMPTCanClawback | kMptDexFlags,
7057 .authHolder = true,
7058 });
7059
7060 env.trust(usd(10'000), alice);
7061 env(pay(gw, alice, usd(10'000)));
7062 env.close();
7063
7064 AMM amm(env, gw, btc(100), usd(100));
7065 env.close();
7066 amm.deposit(alice, 1'000);
7067 env.close();
7068
7069 btc.authorize({.account = gw, .holder = alice, .flags = tfMPTUnauthorize});
7070
7071 env(amm::ammClawback(gw, alice, btc, usd, std::nullopt));
7072 }
7073
7074 // MPTCanTransfer is not set and the account is not the issuer of MPT -
7075 // can't clawback since a holder can't deposit
7076 {
7077 Env env(*this, features);
7078 env.fund(XRP(100'000), gw, alice);
7079 env.close();
7080
7081 env(fset(gw, asfAllowTrustLineClawback));
7082 env.close();
7083
7084 auto const btc = MPTTester({
7085 .env = env,
7086 .issuer = gw,
7087 .holders = {alice},
7088 .pay = 40'000,
7089 .flags = tfMPTCanClawback | tfMPTCanTrade,
7090 .authHolder = true,
7091 });
7092
7093 env.trust(usd(10'000), alice);
7094 env(pay(gw, alice, usd(10'000)));
7095 env.close();
7096
7097 AMM amm(env, gw, btc(100), usd(100));
7098 env.close();
7099 // alice can't deposit since MPTCanTransfer is not set
7100 amm.deposit(DepositArg{.account = alice, .tokens = 1'000, .err = Ter(tecNO_AUTH)});
7101 env.close();
7102
7103 // can't clawback since alice is not an LP
7104 env(amm::ammClawback(gw, alice, btc, usd, std::nullopt), Ter(tecAMM_BALANCE));
7105 }
7106
7107 {
7108 Env env(*this, features);
7109 fund(env, gw, {alice}, XRP(1'000), {usd(1'000)});
7110 MPTTester mptTester(env, gw, {.fund = false});
7111 mptTester.create({.flags = tfMPTCanTransfer | tfMPTCanTrade});
7112 auto const mpt = mptTester["MPT"];
7113 AMM amm(env, gw, mpt(100), XRP(100));
7114 amm.deposit(DepositArg{.account = alice, .asset1In = XRP(10)});
7115 amm::ammClawback(gw, alice, MPTIssue(mptTester.issuanceID()), xrpIssue(), mpt(10));
7116 }
7117
7118 {
7119 Env env(*this, features);
7120 fund(env, gw, {alice}, XRP(1'000), {usd(1'000)});
7121 MPTTester mptTester(env, gw, {.fund = false});
7122 mptTester.create({.flags = tfMPTCanTransfer | tfMPTCanTrade});
7123 mptTester.authorize({.account = alice});
7124 mptTester.pay(gw, alice, 1'000);
7125 auto const mpt = mptTester["MPT"];
7126 AMM amm(env, gw, mpt(100), XRP(100));
7127 amm.deposit(DepositArg{.account = alice, .tokens = 10'000});
7128 amm::ammClawback(gw, alice, MPTIssue(mptTester.issuanceID()), xrpIssue(), mpt(10));
7129 }
7130
7131 // clawback one asset from MPT/MPT AMM. MPToken for another asset
7132 // is created for the Liquidity Provider
7133 {
7134 Env env(*this, features);
7135 env.fund(XRP(1'000), gw, alice);
7136 auto const usd = MPTTester({
7137 .env = env,
7138 .issuer = gw,
7139 .holders = {alice},
7140 .pay = 10'000,
7141 .flags = tfMPTCanClawback | kMptDexFlags,
7142 });
7143 auto const eur =
7144 MPTTester({.env = env, .issuer = gw, .flags = tfMPTCanClawback | kMptDexFlags});
7145 AMM amm(env, gw, usd(1'000), eur(1'000));
7146 amm.deposit({.account = alice, .asset1In = usd(1'000)});
7147 // MPToken doesn't exist
7148 BEAST_EXPECT(env.le(keylet::mptoken(eur.issuanceID(), alice)) == nullptr);
7149 env(amm::ammClawback(gw, alice, usd, eur, usd(100)));
7150 // MPToken is created
7151 BEAST_EXPECT(env.le(keylet::mptoken(eur.issuanceID(), alice)));
7152 }
7153 }
7154
7155 void
7157 {
7158 testcase("Basic AMM");
7159 using namespace jtx;
7160 Account const gw{"gw"};
7161 Account const alice{"alice"};
7162 Account const carol{"carol"};
7163 Account const bob{"bob"};
7164 auto const usd = gw["USD"];
7165
7166 // Create/deposit/withdraw
7167 {
7168 Env env{*this};
7169
7170 fund(env, gw, {alice, carol, bob}, XRP(1'000), {usd(1'000)});
7171
7172 MPTTester mptTester(env, gw, {.fund = false});
7173 mptTester.create({.flags = tfMPTCanTransfer | tfMPTCanTrade});
7174 auto const mpt = mptTester["MPT"];
7175 mptTester.authorize({.account = alice});
7176 mptTester.authorize({.account = carol});
7177 mptTester.pay(gw, alice, 1'000);
7178 mptTester.pay(gw, carol, 1'000);
7179
7180 MPTTester mptTester1(env, gw, {.fund = false});
7181 mptTester1.create({.flags = tfMPTCanTransfer | tfMPTCanTrade});
7182 auto const mpt1 = mptTester1["MPT1"];
7183 mptTester1.authorize({.account = alice});
7184 mptTester1.authorize({.account = carol});
7185 mptTester1.pay(gw, alice, 1'000);
7186 mptTester1.pay(gw, carol, 1'000);
7187
7189 {XRP(100), mpt(100), IOUAmount{100'000}},
7190 {usd(100), mpt(100), IOUAmount{100}},
7191 {mpt(100), mpt1(100), IOUAmount{100}},
7192 };
7193 for (auto& pool : pools)
7194 {
7195 AMM amm(env, gw, std::get<0>(pool), std::get<1>(pool));
7196 amm.deposit(alice, std::get<2>(pool));
7197 amm.deposit(carol, std::get<2>(pool));
7198 // bob doesn't own MPT
7199 amm.deposit(
7200 DepositArg{
7201 .account = bob,
7202 .tokens = std::get<2>(pool),
7203 .err = Ter(tecNO_AUTH),
7204 });
7205 amm.withdrawAll(alice);
7206 amm.withdrawAll(carol);
7207 amm.withdrawAll(gw);
7208 BEAST_EXPECT(!amm.ammExists());
7209 }
7210 }
7211
7212 // Payment, one step
7213 {
7214 Env env(*this);
7215
7216 env.fund(XRP(1'000), gw, alice, carol);
7217 env.close();
7218
7219 MPT const usd = MPTTester({.env = env, .issuer = gw, .holders = {alice, carol}});
7220 MPT const eur = MPTTester({.env = env, .issuer = gw, .holders = {alice, carol}});
7221
7222 env(pay(gw, alice, eur(100)));
7223
7224 AMM const amm(env, gw, usd(1'100), eur(1'000));
7225
7226 env(pay(alice, carol, usd(100)), Sendmax(eur(100)));
7227
7228 BEAST_EXPECT(amm.expectBalances(usd(1'000), eur(1'100), amm.tokens()));
7229 BEAST_EXPECT(env.balance(carol, usd) == usd(100));
7230 BEAST_EXPECT(env.balance(alice, eur) == eur(0));
7231 }
7232
7233 // Payment, two steps
7234 {
7235 Env env(*this);
7236
7237 env.fund(XRP(1'000), gw, alice, carol);
7238 env.close();
7239
7240 MPT const usd = MPTTester({.env = env, .issuer = gw, .holders = {alice, carol}});
7241 MPT const eur = MPTTester({.env = env, .issuer = gw, .holders = {alice, carol}});
7242 MPT const btc = MPTTester({.env = env, .issuer = gw, .holders = {alice, carol}});
7243 env(pay(gw, alice, eur(100)));
7244
7245 AMM const ammEurUsd(env, gw, eur(1'000), usd(1'100));
7246 AMM const ammUsdBtc(env, gw, usd(1'000), btc(1'100));
7247
7248 env(pay(alice, carol, btc(100)),
7249 Sendmax(eur(100)),
7250 Path(~usd, ~btc),
7251 Txflags(tfNoRippleDirect));
7252
7253 BEAST_EXPECT(ammEurUsd.expectBalances(usd(1'000), eur(1'100), ammEurUsd.tokens()));
7254 BEAST_EXPECT(ammUsdBtc.expectBalances(usd(1'100), btc(1'000), ammUsdBtc.tokens()));
7255 BEAST_EXPECT(env.balance(carol, btc) == btc(100));
7256 BEAST_EXPECT(env.balance(alice, eur) == eur(0));
7257 }
7258
7259 // Offer crossing
7260 {
7261 Env env(*this);
7262
7263 env.fund(XRP(1'000), gw, alice);
7264 env.close();
7265
7266 MPT const usd = MPTTester({.env = env, .issuer = gw, .holders = {alice}});
7267 MPT const eur = MPTTester({.env = env, .issuer = gw, .holders = {alice}});
7268
7269 env(pay(gw, alice, eur(1'000)));
7270
7271 AMM const amm(env, gw, eur(1'000'000), usd(1'001'000));
7272
7273 env(offer(alice, usd(1'000), eur(1'000)));
7274
7275 BEAST_EXPECT(amm.expectBalances(usd(1'000'000), eur(1'001'000), amm.tokens()));
7276 BEAST_EXPECT(env.balance(alice, usd) == usd(1'000));
7277 BEAST_EXPECT(env.balance(alice, eur) == eur(0));
7278 }
7279
7280 {
7281 Env env(*this);
7282 env.fund(XRP(1'000'000), gw, alice, carol);
7283
7284 auto const increment = env.current()->fees().increment;
7285 auto const txfee = Fee(drops(increment));
7286 auto const badMPT = MPT(gw, 1'000);
7287
7288 auto const makeMPT =
7289 [&](std::uint32_t const flags, Holders holders = {}, std::uint64_t const pay = 0) {
7290 return MPTTester({
7291 .env = env,
7292 .issuer = gw,
7293 .holders = holders,
7294 .pay = pay ? std::optional<std::uint64_t>{pay} : std::nullopt,
7295 .flags = flags,
7296 });
7297 };
7298
7299 auto const makeDexMPT = [&](Holders holders = {}, std::uint64_t const pay = 0) {
7300 return makeMPT(tfMPTCanLock | kMptDexFlags, holders, pay);
7301 };
7302
7303 auto const makeNoTransferMPT = [&](Holders holders = {}, std::uint64_t const pay = 0) {
7304 return makeMPT(tfMPTCanLock | tfMPTCanTrade, holders, pay);
7305 };
7306
7307 auto const makeNoTradeMPT = [&](Holders holders = {}, std::uint64_t const pay = 0) {
7308 return makeMPT(tfMPTCanLock | tfMPTCanTransfer, holders, pay);
7309 };
7310
7311 // AMMCreate
7312 {
7313 auto usd = makeDexMPT();
7314 auto eur = makeDexMPT({alice}, 1'000'000);
7315
7316 auto const createDeleteAMM = [&](auto const& asset, Account const& lp) {
7317 AMM amm(
7318 env,
7319 lp,
7320 asset(1'000),
7321 eur(1'000),
7322 CreateArg{.fee = static_cast<std::uint32_t>(increment.value())});
7323 amm.withdrawAll(lp);
7324 BEAST_EXPECT(!amm.ammExists());
7325 };
7326
7327 auto const createFail =
7328 [&](auto const& asset, Account const& account, auto const& err) {
7329 auto const createJv = AMM::createJv(account, asset(1'000), eur(1'000), 0);
7330 env(createJv, txfee, Ter(err));
7331 env.close();
7332 };
7333
7334 // MPTokenIssuance doesn't exist
7335 createFail(badMPT, alice, tecOBJECT_NOT_FOUND);
7336
7337 // MPToken doesn't exist
7338 createFail(usd, alice, tecNO_AUTH);
7339
7340 // alice authorizes MPToken, can create
7341 usd.authorize({.account = alice});
7342 env(pay(gw, alice, usd(1'000'000)), txfee);
7343 env.close();
7344 createDeleteAMM(usd, alice);
7345
7346 // MPTLock is set
7347 // alice and issuer can't create
7348 usd.set({.flags = tfMPTLock});
7349 createFail(usd, alice, tecLOCKED);
7350 createFail(usd, gw, tecLOCKED);
7351
7352 // MPTRequireAuth is set
7353 // alice is not authorized
7354 usd.set({.flags = tfMPTUnlock});
7355 usd.set({.flags = tfMPTSetRequireAuth});
7356 createFail(usd, alice, tecNO_AUTH);
7357 // issuer can create
7358 createDeleteAMM(usd, gw);
7359
7360 // alice is authorized, can create
7361 usd.authorize({.account = gw, .holder = alice});
7362 createDeleteAMM(usd, alice);
7363
7364 // MPTCanTransfer is not set
7365 {
7366 auto usd2 = makeNoTransferMPT({alice}, 1'000'000);
7367
7368 // alice can't create
7369 createFail(usd2, alice, tecNO_AUTH);
7370 // issuer can create
7371 createDeleteAMM(usd2, gw);
7372 usd2.set({.flags = tfMPTSetCanTransfer});
7373 // alice can create
7374 createDeleteAMM(usd2, alice);
7375 }
7376
7377 // MPTCanTrade is not set
7378 {
7379 auto usd3 = makeNoTradeMPT({alice}, 1'000'000);
7380
7381 // alice and issuer can't create
7382 createFail(usd3, alice, tecNO_PERMISSION);
7383 createFail(usd3, gw, tecNO_PERMISSION);
7384 usd3.set({.flags = tfMPTSetCanTrade});
7385 // alice can create
7386 createDeleteAMM(usd3, alice);
7387 }
7388 }
7389
7390 // AMMDeposit
7391 {
7392 auto usd = makeDexMPT();
7393 auto eur = makeDexMPT({alice}, 1'000'000);
7394 AMM amm(env, gw, usd(1'000), eur(1'000));
7395
7396 // MPTokenIssuance doesn't exist
7397 amm.deposit({
7398 .account = alice,
7399 .asset1In = badMPT(1),
7400 .asset2In = eur(1),
7401 .assets = std::make_pair(badMPT, eur),
7402 .err = Ter(terNO_AMM),
7403 });
7404
7405 // MPToken doesn't exist
7406 amm.deposit({
7407 .account = carol,
7408 .asset1In = usd(1),
7409 .asset2In = eur(1),
7410 .err = Ter(tecNO_AUTH),
7411 });
7412
7413 // Fund carol for the AMMDeposit checks.
7414 usd.authorize({.account = carol});
7415 env(pay(gw, carol, usd(1'000'000)));
7416 eur.authorize({.account = carol});
7417 env(pay(gw, carol, eur(1'000'000)));
7418 env.close();
7419
7420 // MPTLock is set
7421 usd.set({.flags = tfMPTLock});
7422
7423 // alice and issuer can't deposit
7424 for (auto const& account : {carol, gw})
7425 {
7426 amm.deposit({
7427 .account = account,
7428 .asset1In = usd(1),
7429 .asset2In = eur(1),
7430 .err = Ter(tecLOCKED),
7431 });
7432 amm.deposit({
7433 .account = account,
7434 .asset1In = eur(1),
7435 .assets = std::make_pair(eur, usd),
7436 .err = Ter(tecLOCKED),
7437 });
7438 }
7439 usd.set({.flags = tfMPTUnlock});
7440
7441 // MPTRequireAuth is set
7442 // carol is not authorized by the issuer
7443 usd.set({.flags = tfMPTSetRequireAuth});
7444 env.close();
7445 amm.deposit({
7446 .account = carol,
7447 .asset1In = usd(1),
7448 .asset2In = eur(1),
7449 .err = Ter(tecNO_AUTH),
7450 });
7451 amm.deposit({
7452 .account = carol,
7453 .asset1In = eur(1),
7454 .assets = std::make_pair(eur, usd),
7455 .err = Ter(tecNO_AUTH),
7456 });
7457 // issuer can deposit
7458 amm.deposit({.account = gw, .tokens = 1'000});
7459 // carol is authorized, can deposit
7460 usd.authorize({.account = gw, .holder = carol});
7461 amm.deposit({.account = carol, .tokens = 1'000});
7462 // Can't authorize or unauthorize AMM pseudo-account
7463 usd.authorize({
7464 .account = gw,
7465 .holder = Account{"amm", amm.ammAccount()},
7466 .err = tecNO_PERMISSION,
7467 });
7468 usd.authorize({
7469 .account = gw,
7470 .holder = Account{"amm", amm.ammAccount()},
7471 .flags = tfMPTUnauthorize,
7472 .err = tecNO_PERMISSION,
7473 });
7474
7475 // MPTCanTransfer is not set
7476 {
7477 auto usd2 = makeNoTransferMPT({carol}, 1'000'000);
7478 AMM amm2(env, gw, usd2(1'000), eur(1'000));
7479
7480 // carol can't deposit
7481 amm2.deposit({
7482 .account = carol,
7483 .asset1In = usd2(1),
7484 .asset2In = eur(1),
7485 .err = Ter(tecNO_AUTH),
7486 });
7487 amm2.deposit({
7488 .account = carol,
7489 .asset1In = eur(1),
7490 .assets = std::make_pair(eur, usd2),
7491 .err = Ter(tecNO_AUTH),
7492 });
7493 // issuer can deposit
7494 amm2.deposit({.account = gw, .tokens = 1'000});
7495 usd2.set({.flags = tfMPTSetCanTransfer});
7496 // carol can deposit
7497 amm2.deposit({.account = carol, .tokens = 1'000});
7498 }
7499 }
7500
7501 // AMMWithdraw
7502 {
7503 auto usd = makeDexMPT();
7504 auto const eur = makeDexMPT({carol}, 1'000'000);
7505 AMM amm(env, gw, usd(1'000), eur(1'000));
7506
7507 usd.authorize({.account = carol});
7508 env(pay(gw, carol, usd(1'000'000)));
7509 env.close();
7510 amm.deposit({.account = carol, .tokens = 1'000});
7511
7512 // MPTokenIssuance doesn't exist
7513 amm.withdraw(
7515 .account = carol,
7516 .asset1Out = badMPT(1),
7517 .asset2Out = eur(1),
7518 .assets = std::make_pair(badMPT, eur),
7519 .err = Ter(terNO_AMM),
7520 });
7521
7522 // MPToken doesn't exist - doesn't apply since MPToken is created
7523 // on withdraw in this case
7524
7525 // MPTLock is set
7526 usd.set({.flags = tfMPTLock});
7527 auto const fix330 = env.current()->rules().enabled(fixCleanup3_3_0);
7528
7529 // carol can't withdraw the locked token (any withdrawal type)
7530 amm.withdraw({
7531 .account = carol,
7532 .asset1Out = usd(1),
7533 .asset2Out = eur(1),
7534 .err = Ter(tecLOCKED),
7535 });
7536 amm.withdraw({.account = carol, .tokens = 1'000, .err = Ter(tecLOCKED)});
7537 // can single withdraw the non-locked asset
7538 amm.withdraw(
7539 {.account = carol, .asset1Out = eur(1), .assets = std::make_pair(eur, usd)});
7540
7541 // post-fixCleanup3_3_0 the issuer can redeem even when locked.
7542 // Each successful withdrawal burns LP tokens, so replenish between
7543 // each type to keep gw's LP balance stable.
7544 auto const gwLockErr = fix330 ? Ter(tesSUCCESS) : Ter(tecLOCKED);
7545 auto const replenish = [&](IOUAmount tokens) {
7546 usd.set({.flags = tfMPTUnlock});
7547 amm.deposit({.account = gw, .tokens = tokens});
7548 usd.set({.flags = tfMPTLock});
7549 };
7550
7551 amm.withdraw(
7552 {.account = gw, .asset1Out = usd(1), .asset2Out = eur(1), .err = gwLockErr});
7553 if (fix330)
7554 replenish(IOUAmount{1});
7555
7556 amm.withdraw({.account = gw, .tokens = 1'000, .err = gwLockErr});
7557 if (fix330)
7558 replenish(IOUAmount{1'000});
7559
7560 // can single withdraw the non-locked asset
7561 amm.withdraw(
7562 {.account = gw, .asset1Out = eur(1), .assets = std::make_pair(eur, usd)});
7563
7564 usd.set({.flags = tfMPTUnlock});
7565
7566 // MPTRequireAuth is set
7567 usd.set({.flags = tfMPTSetRequireAuth});
7568 usd.authorize({.account = gw, .holder = carol, .flags = tfMPTUnauthorize});
7569 // carol can't withdraw
7570 amm.withdraw({
7571 .account = carol,
7572 .asset1Out = usd(1),
7573 .asset2Out = eur(1),
7574 .err = Ter(tecNO_AUTH),
7575 });
7576 // can withdraw another asset
7577 amm.withdraw(
7578 {.account = carol, .asset1Out = eur(1), .assets = std::make_pair(eur, usd)});
7579 // issuer can withdraw
7580 amm.withdraw({.account = gw, .asset1Out = usd(1), .asset2Out = eur(1)});
7581 // carol is authorized, can withdraw
7582 usd.authorize({.account = gw, .holder = carol});
7583 amm.withdraw({.account = carol, .asset1Out = usd(1), .asset2Out = eur(1)});
7584
7585 // MPTCanTransfer is not set, allow to withdraw
7586 {
7587 auto usd2 = makeNoTransferMPT({carol}, 1'000'000);
7588 AMM amm2(env, gw, usd2(1'000), eur(1'000));
7589
7590 // carol cannot deposit usd2 without MPTCanTransfer, so give her
7591 // LP tokens directly to test the withdraw path.
7592 env.trust(STAmount{amm2.lptIssue(), 1'000}, carol);
7593 env(pay(gw, carol, STAmount{amm2.lptIssue(), 100}));
7594 env.close();
7595
7596 // carol can withdraw
7597 amm2.withdraw({.account = carol, .asset1Out = usd2(1), .asset2Out = eur(1)});
7598 // can withdraw another asset
7599 amm2.withdraw({
7600 .account = carol,
7601 .asset1Out = eur(1),
7602 .assets = std::make_pair(eur, usd2),
7603 });
7604 // issuer can withdraw
7605 amm2.withdraw({.account = gw, .asset1Out = usd2(1), .asset2Out = eur(1)});
7606 // Holder can't transfer to another holder
7607 env.fund(XRP(1'000), bob);
7608 usd2.authorize({.account = bob});
7609 env(pay(carol, bob, usd2(1)), Ter(tecNO_AUTH));
7610 usd2.authorize({.account = bob, .flags = tfMPTUnauthorize});
7611 // Can redeem
7612 env(pay(carol, gw, usd2(1)));
7613 usd2.set({.flags = tfMPTSetCanTransfer});
7614 // carol can withdraw
7615 amm2.withdraw({.account = carol, .asset1Out = usd2(1), .asset2Out = eur(1)});
7616 }
7617
7618 // MPToken created on withdraw
7619 {
7620 auto const usd3 = makeDexMPT();
7621 auto const eur3 = makeDexMPT({carol}, 1'000'000);
7622 AMM amm3(env, gw, usd3(1'000), eur3(1'000));
7623
7624 BEAST_EXPECT(env.le(keylet::mptoken(usd3.issuanceID(), carol)) == nullptr);
7625 // single-deposit EUR
7626 amm3.deposit({
7627 .account = carol,
7628 .asset1In = eur3(1'000),
7629 .assets = std::make_pair(eur3, usd3),
7630 });
7631 BEAST_EXPECT(env.le(keylet::mptoken(usd3.issuanceID(), carol)) == nullptr);
7632 // withdraw in USD to create MPToken
7633 amm3.withdraw({.account = carol, .asset1Out = usd3(100)});
7634 BEAST_EXPECT(env.le(keylet::mptoken(usd3.issuanceID(), carol)));
7635 }
7636 }
7637 }
7638 }
7639
7640 void
7642 {
7643 testcase("Fix Double OwnerCount in AMMWithdraw");
7644
7645 using namespace jtx;
7646
7647 // Carol deposits XRP into an XRP/MPT pool, then withdraws MPT.
7648 // Carol has no MPToken before the withdrawal. If the bug exists,
7649 // her ownerCount will be inflated by +1 extra.
7650 Account const gw{"gw"};
7651 Account const alice{"alice"};
7652 Account const carol{"carol"};
7653 Env env(*this, all);
7654 env.fund(XRP(30'000), gw, alice, carol);
7655 env.close();
7656
7657 // Create MPT with DEX flags. Only alice is a holder initially.
7658 MPT const btc = MPTTester(
7659 {.env = env, .issuer = gw, .holders = {alice}, .pay = 20'000, .flags = kMptDexFlags});
7660
7661 // Alice creates XRP/MPT AMM pool
7662 AMM amm(env, alice, XRP(10'000), btc(10'000));
7663
7664 // Carol deposits XRP (single asset) into the pool.
7665 // Carol gets LP tokens but does NOT have an MPToken yet.
7666 auto const carolOwnersBefore = ownerCount(env, carol);
7667 amm.deposit(carol, XRP(1'000), std::nullopt, std::nullopt, tfSingleAsset);
7668 auto const carolOwnersAfterDeposit = ownerCount(env, carol);
7669 // Carol should have +1 for LP token trustline
7670 BEAST_EXPECT(carolOwnersAfterDeposit == carolOwnersBefore + 1);
7671
7672 auto const carolOwnersBeforeWithdraw = ownerCount(env, carol);
7673 // Carol withdraws single MPT asset. She doesn't have an MPToken,
7674 // so one must be created. Bug: ownerCount incremented twice.
7675 amm.withdraw({.account = carol, .asset1Out = btc(100), .flags = tfSingleAsset});
7676 auto const carolOwnersAfterWithdraw = ownerCount(env, carol);
7677
7678 // Expected: +1 for the new MPToken (so total increase = 1)
7679 BEAST_EXPECT(carolOwnersAfterWithdraw == carolOwnersBeforeWithdraw + 1);
7680 }
7681
7682 void
7684 {
7685 using namespace jtx;
7686 testcase("Trade and Transfer");
7687
7688 // Verify canMPTTradeAndTransfer validates the flags when from == to and from != to
7689
7690 Account const gw{"gw"};
7691 Account const alice{"alice"};
7692 Account const carol{"carol"};
7693 Env env(*this);
7694 env.fund(XRP(1'000), gw, alice, carol);
7695
7696 auto const checkCanTradeCanTransfer = [&](std::uint32_t const flags,
7697 TER const gwToGw,
7698 TER const gwToAlice,
7699 TER const aliceToAlice,
7700 TER const aliceToCarol) {
7701 MPTTester const mpt(
7702 {.env = env, .issuer = gw, .holders = {alice, carol}, .pay = 100, .flags = flags});
7703
7704 BEAST_EXPECT(canMPTTradeAndTransfer(*env.current(), mpt, gw, gw) == gwToGw);
7705 BEAST_EXPECT(canMPTTradeAndTransfer(*env.current(), mpt, gw, alice) == gwToAlice);
7706 BEAST_EXPECT(canMPTTradeAndTransfer(*env.current(), mpt, alice, alice) == aliceToAlice);
7707 BEAST_EXPECT(canMPTTradeAndTransfer(*env.current(), mpt, alice, carol) == aliceToCarol);
7708 };
7709
7710 // Both flags are enabled
7711 checkCanTradeCanTransfer(kMptDexFlags, tesSUCCESS, tesSUCCESS, tesSUCCESS, tesSUCCESS);
7712
7713 // MPTCanTrade is disabled
7714 checkCanTradeCanTransfer(
7715 tfMPTCanTransfer,
7720
7721 // MPTCanTransfer is disabled
7722 checkCanTradeCanTransfer(tfMPTCanTrade, tesSUCCESS, tesSUCCESS, tecNO_AUTH, tecNO_AUTH);
7723
7724 // Both flags are disabled
7725 checkCanTradeCanTransfer(
7727 }
7728
7729 void
7731 {
7732 testcase("Locked MPToken with destroyed issuance");
7733
7734 using namespace test::jtx;
7735 Account const alice("alice"); // issuer
7736 Account const bob("bob"); // holder
7737
7738 Env env{*this, features};
7739 env.fund(XRP(1'000), alice, bob);
7740 env.close();
7741 MPTTester mptAlice(
7742 {.env = env, .issuer = alice, .holders = {bob}, .flags = kMptDexFlags | tfMPTCanLock});
7743
7744 // alice locks bob's mptoken individually
7745 mptAlice.set({.account = alice, .holder = bob, .flags = tfMPTLock});
7746
7747 // alice destroys her issuance. This succeeds: MPTokenIssuanceDestroy
7748 // only requires that the issuance has no outstanding balance; it does
7749 // not require that all holder MPTokens have been deleted first.
7750 mptAlice.destroy({.ownerCount = 0});
7751
7752 if (!features[featureSingleAssetVault] || features[fixCleanup3_4_0])
7753 {
7754 // pre SAV or post Cleanup340 amendment: bob deletes the dangling locked MPToken
7755 mptAlice.authorize({.account = bob, .holderCount = 0, .flags = tfMPTUnauthorize});
7756 BEAST_EXPECT(ownerCount(env, bob) == 0);
7757 }
7758 else
7759 {
7760 // bob cannot delete his locked MPToken, even though the issuance
7761 // no longer exists.
7762 mptAlice.authorize(
7763 {.account = bob, .flags = tfMPTUnauthorize, .err = tecNO_PERMISSION});
7764
7765 // and the lock can never be cleared, because unlocking
7766 // requires the (destroyed) issuance
7767 mptAlice.set({
7768 .account = alice,
7769 .holder = bob,
7770 .flags = tfMPTUnlock,
7771 .err = tecOBJECT_NOT_FOUND,
7772 });
7773
7774 // the dangling locked MPToken survives
7775 BEAST_EXPECT(env.current()->exists(keylet::mptoken(mptAlice.issuanceID(), bob.id())));
7776 BEAST_EXPECT(ownerCount(env, bob) == 1);
7777 }
7778 }
7779
7780 void
7782 {
7783 testcase("Partial payment rounding");
7784
7785 // With fixCleanup3_5_0, the legacy direct-MPT partial payment rounds the
7786 // delivered amount down instead of to nearest and the transfer fee up,
7787 // so the sender is never charged more than SendMax. This matches V2.
7788
7789 using namespace test::jtx;
7790 Account const alice("alice"); // issuer
7791 Account const bob("bob"); // holder / sender
7792 Account const carol("carol"); // holder / receiver
7793
7794 Env env{*this, features};
7795
7796 MPTTester mptAlice(env, alice, {.holders = {bob, carol}});
7797
7798 // Transfer fee is 10%
7799 mptAlice.create(
7800 {.transferFee = 10'000, .ownerCount = 1, .holderCount = 0, .flags = tfMPTCanTransfer});
7801
7802 mptAlice.authorize({.account = bob});
7803 mptAlice.authorize({.account = carol});
7804 mptAlice.pay(alice, bob, 780);
7805
7806 auto const mpt = mptAlice["MPT"];
7807 env(pay(bob, carol, mpt(100)), Sendmax(mpt(90)), Txflags(tfPartialPayment));
7808 if (features[featureMPTokensV2] || features[fixCleanup3_5_0])
7809 {
7810 // 81 to carol, 9 to issuer (90 / 1.1 ~ 81.81 (rounded down) =
7811 // 81, 81 * 1.1 = 89.1 (rounded up) = 90). In V2 the payments are
7812 // executed via the payment engine, which rounds the same way.
7813 BEAST_EXPECT(mptAlice.checkMPTokenAmount(bob, 690));
7814 BEAST_EXPECT(mptAlice.checkMPTokenAmount(carol, 81));
7815 }
7816 else
7817 {
7818 // 82 to carol, 8 to issuer (90 / 1.1 ~ 81.81 (rounded to nearest) =
7819 // 82)
7820 BEAST_EXPECT(mptAlice.checkMPTokenAmount(bob, 690));
7821 BEAST_EXPECT(mptAlice.checkMPTokenAmount(carol, 82));
7822 }
7823 }
7824
7825 void
7827 {
7828 testcase("Partial payment divide overflow");
7829
7830 // Regression: legacy MPT partial-payment divide rounding overflow.
7831 //
7832 // Under MPTokensV1 (V2 disabled), a holder-to-holder partial Payment can
7833 // hit STAmount::divide's unchecked `muldiv(...) + 5` wrap and return a
7834 // zero delivered amount that is nevertheless accepted as tesSUCCESS by
7835 // the legacy direct-MPT branch. fixCleanup3_5_0 computes the
7836 // delivered amount with mulRatio (exact 128-bit arithmetic) instead.
7837 // Under MPTokensV2 the payment is routed through RippleCalc, which uses
7838 // Number.
7839
7840 using namespace test::jtx;
7841 Account const alice("alice"); // issuer
7842 Account const bob("bob"); // holder / sender
7843 Account const carol("carol"); // holder / receiver
7844
7845 // The precise sendMax chosen so that
7846 // muldiv(N * 10^17, D) == UINT64_MAX - 2
7847 // where D is the canonicalized rate mantissa for transferFee = 3
7848 // (D == 1'000'030'000'000'000). The subsequent +5 wraps to 2, and
7849 // the resulting STAmount (mantissa=2, offset=-2) canonicalizes to 0
7850 // for an integral MPT asset.
7851 constexpr std::int64_t sendMax = 184'472'974'760'317'629;
7852 // The largest integer k such that k * 1.00003 <= sendMax.
7853 constexpr std::int64_t expectedDelivered = 184'467'440'737'095'516;
7854
7855 bool const fixed = features[featureMPTokensV2] || features[fixCleanup3_5_0];
7856
7857 {
7858 Env env{*this, features};
7859
7860 MPTTester mptAlice(env, alice, {.holders = {bob, carol}});
7861 mptAlice.create({
7862 .maxAmt = kMaxMpTokenAmount,
7863 .transferFee = 3,
7864 .ownerCount = 1,
7865 .holderCount = 0,
7866 .flags = tfMPTCanTransfer,
7867 });
7868
7869 mptAlice.authorize({.account = bob});
7870 mptAlice.authorize({.account = carol});
7871 mptAlice.pay(alice, bob, sendMax);
7872
7873 auto const mpt = mptAlice["MPT"];
7874
7875 env(pay(bob, carol, mpt(sendMax)), Sendmax(mpt(sendMax)), Txflags(tfPartialPayment));
7876
7877 auto const bobBal = mptAlice.getBalance(bob);
7878 auto const carolBal = mptAlice.getBalance(carol);
7879
7880 if (!fixed)
7881 {
7882 // divide(sendMax, rate) wraps to MPT(0), accountSendMPT(0)
7883 // short-circuits to tesSUCCESS, and no balances change.
7884 BEAST_EXPECT(bobBal == sendMax);
7885 BEAST_EXPECT(carolBal == 0);
7886 BEAST_EXPECT(mptAlice.checkMPTokenOutstandingAmount(sendMax));
7887 }
7888 else
7889 {
7890 // The transfer fee is redeemed to the issuer, so
7891 // OutstandingAmount shrinks by that fee.
7892 BEAST_EXPECT(carolBal == expectedDelivered);
7893 BEAST_EXPECT(bobBal == 0);
7894 BEAST_EXPECT(mptAlice.checkMPTokenOutstandingAmount(bobBal + carolBal));
7895 }
7896
7897 auto const meta = env.meta();
7898 BEAST_EXPECT(meta->isFieldPresent(sfDeliveredAmount));
7899 if (meta->isFieldPresent(sfDeliveredAmount))
7900 {
7901 BEAST_EXPECT(
7902 meta->getFieldAmount(sfDeliveredAmount) ==
7903 mpt(fixed ? expectedDelivered : 0).value());
7904 }
7905 }
7906
7907 // A larger sendMax makes muldiv(N * 10^17, D) itself exceed
7908 // UINT64_MAX, so the legacy divide() throws instead of wrapping.
7909 {
7910 constexpr std::int64_t largeSendMax = 200'000'000'000'000'000;
7911 // The largest integer k such that k * 1.00003 <= largeSendMax.
7912 constexpr std::int64_t largeExpectedDelivered = 199'994'000'179'994'600;
7913
7914 Env env{*this, features};
7915
7916 MPTTester mptAlice(env, alice, {.holders = {bob, carol}});
7917 mptAlice.create({
7918 .maxAmt = kMaxMpTokenAmount,
7919 .transferFee = 3,
7920 .ownerCount = 1,
7921 .holderCount = 0,
7922 .flags = tfMPTCanTransfer,
7923 });
7924
7925 mptAlice.authorize({.account = bob});
7926 mptAlice.authorize({.account = carol});
7927 mptAlice.pay(alice, bob, largeSendMax);
7928
7929 auto const mpt = mptAlice["MPT"];
7930
7931 env(pay(bob, carol, mpt(largeSendMax)),
7932 Sendmax(mpt(largeSendMax)),
7933 Txflags(tfPartialPayment),
7935
7936 auto const bobBal = mptAlice.getBalance(bob);
7937 auto const carolBal = mptAlice.getBalance(carol);
7938
7939 if (!fixed)
7940 {
7941 BEAST_EXPECT(bobBal == largeSendMax);
7942 BEAST_EXPECT(carolBal == 0);
7943 BEAST_EXPECT(mptAlice.checkMPTokenOutstandingAmount(largeSendMax));
7944 }
7945 else
7946 {
7947 BEAST_EXPECT(carolBal == largeExpectedDelivered);
7948 BEAST_EXPECT(bobBal == 0 || bobBal == 1);
7949 BEAST_EXPECT(mptAlice.checkMPTokenOutstandingAmount(bobBal + carolBal));
7950 }
7951 }
7952
7953 // The cost of delivering kMaxMpTokenAmount with a transfer fee doesn't
7954 // fit in an MPT amount. Without the fix, the legacy
7955 // multiply(Amount, rate) throws. With it, or under MPTokensV2, the
7956 // overflowing cost is treated as exceeding SendMax.
7957 {
7958 Env env{*this, features};
7959
7960 MPTTester mptAlice(env, alice, {.holders = {bob, carol}});
7961 mptAlice.create({
7962 .maxAmt = kMaxMpTokenAmount,
7963 .transferFee = 3,
7964 .ownerCount = 1,
7965 .holderCount = 0,
7966 .flags = tfMPTCanTransfer,
7967 });
7968
7969 mptAlice.authorize({.account = bob});
7970 mptAlice.authorize({.account = carol});
7971 mptAlice.pay(alice, bob, 1'000);
7972
7973 auto const mpt = mptAlice["MPT"];
7974
7975 // Not a partial payment: the cost exceeds SendMax.
7976 env(pay(bob, carol, mpt(kMaxMpTokenAmount)),
7977 Sendmax(mpt(1'000)),
7979 BEAST_EXPECT(mptAlice.getBalance(bob) == 1'000);
7980 BEAST_EXPECT(mptAlice.getBalance(carol) == 0);
7981
7982 // Partial payment: deliver floor(1000 / 1.00003) = 999 for a cost
7983 // of ceil(999 * 1.00003) = 1000.
7984 env(pay(bob, carol, mpt(kMaxMpTokenAmount)),
7985 Sendmax(mpt(1'000)),
7986 Txflags(tfPartialPayment),
7988 BEAST_EXPECT(mptAlice.getBalance(bob) == (fixed ? 0 : 1'000));
7989 BEAST_EXPECT(mptAlice.getBalance(carol) == (fixed ? 999 : 0));
7990 }
7991
7992 // With the fix, a partial payment that would deliver zero fails
7993 // instead of succeeding without moving any funds.
7994 if (!features[featureMPTokensV2] && features[fixCleanup3_5_0])
7995 {
7996 Env env{*this, features};
7997
7998 MPTTester mptAlice(env, alice, {.holders = {bob, carol}});
7999 mptAlice.create(
8000 {.transferFee = 3, .ownerCount = 1, .holderCount = 0, .flags = tfMPTCanTransfer});
8001
8002 mptAlice.authorize({.account = bob});
8003 mptAlice.authorize({.account = carol});
8004 mptAlice.pay(alice, bob, 10);
8005
8006 auto const mpt = mptAlice["MPT"];
8007
8008 env(pay(bob, carol, mpt(10)),
8009 Sendmax(mpt(1)),
8010 Txflags(tfPartialPayment),
8012
8013 BEAST_EXPECT(mptAlice.getBalance(bob) == 10);
8014 BEAST_EXPECT(mptAlice.getBalance(carol) == 0);
8015 }
8016 }
8017
8018 void
8020 {
8021 testcase("Partial payment does not exceed SendMax");
8022
8023 // A partial payment must not charge bob more than SendMax. Without
8024 // fixCleanup3_5_0, the cost is computed with Number, which rounds large
8025 // amounts when it has only 16 digits (SingleAssetVault and
8026 // LendingProtocol disabled). bob is funded with 1000 extra so that an
8027 // overcharge shows up in his balance.
8028 //
8029 // SendMax = 122'354'741'735'175'090, transfer fee 3 (rate 1.00003):
8030 //
8031 // | Config | bob pays | carol receives |
8032 // |---------------------------------|--------------|------------------|
8033 // | fixCleanup3_5_0 or MPTokensV2 | SendMax | ...998 (floor) |
8034 // | neither, Number with 19 digits | SendMax | ...999 (nearest) |
8035 // | neither, Number with 16 digits | SendMax + 10 | ...999 (nearest) |
8036
8037 using namespace test::jtx;
8038 Account const alice("alice"); // issuer
8039 Account const bob("bob"); // holder / sender
8040 Account const carol("carol"); // holder / receiver
8041
8042 bool const fixed = features[featureMPTokensV2] || features[fixCleanup3_5_0];
8043 bool const smallNumber =
8044 !features[featureSingleAssetVault] && !features[featureLendingProtocol];
8045
8046 // Small enough that the legacy divide() doesn't overflow. Larger
8047 // amounts are covered by testPartialPaymentDivideOverflow.
8048 constexpr std::int64_t sendMax = 122'354'741'735'175'090;
8049
8050 Env env{*this, features};
8051
8052 MPTTester mptAlice(env, alice, {.holders = {bob, carol}});
8053 mptAlice.create({
8054 .maxAmt = kMaxMpTokenAmount,
8055 .transferFee = 3,
8056 .ownerCount = 1,
8057 .holderCount = 0,
8058 .flags = tfMPTCanTransfer,
8059 });
8060
8061 mptAlice.authorize({.account = bob});
8062 mptAlice.authorize({.account = carol});
8063 mptAlice.pay(alice, bob, sendMax + 1'000);
8064
8065 auto const mpt = mptAlice["MPT"];
8066
8067 env(pay(bob, carol, mpt(sendMax)), Sendmax(mpt(sendMax)), Txflags(tfPartialPayment));
8068
8069 if (fixed)
8070 {
8071 // fixCleanup3_5_0 or MPTokensV2: bob pays exactly SendMax, carol
8072 // receives floor(SendMax / rate)
8073 BEAST_EXPECT(mptAlice.getBalance(bob) == 1'000);
8074 BEAST_EXPECT(mptAlice.getBalance(carol) == 122'351'071'203'038'998);
8075 }
8076 else if (!smallNumber)
8077 {
8078 // Neither, Number with 19 digits: the Number cost is precise
8079 // enough, so bob pays exactly SendMax
8080 BEAST_EXPECT(mptAlice.getBalance(bob) == 1'000);
8081 // The legacy divide() rounds to nearest, so carol receives one
8082 // more than floor(SendMax / rate)
8083 BEAST_EXPECT(mptAlice.getBalance(carol) == 122'351'071'203'038'999);
8084 }
8085 else
8086 {
8087 // Neither, Number with 16 digits: the Number cost is rounded 10
8088 // above SendMax
8089 BEAST_EXPECT(mptAlice.getBalance(bob) == 1'000 - 10);
8090 // The legacy divide() rounds to nearest, so carol receives one
8091 // more than floor(SendMax / rate)
8092 BEAST_EXPECT(mptAlice.getBalance(carol) == 122'351'071'203'038'999);
8093 }
8094 }
8095
8096public:
8097 void
8098 run() override
8099 {
8100 using namespace test::jtx;
8101 FeatureBitset const all{testableAmendments()};
8102
8104 // MPTokenIssuanceCreate
8105 testCreateValidation(all - featureSingleAssetVault);
8106 testCreateValidation(all - featurePermissionedDomains);
8108 testCreateEnabled(all - featureSingleAssetVault);
8109 testCreateEnabled(all);
8110
8111 // MPTokenIssuanceDestroy
8112 testDestroyValidation(all - featureSingleAssetVault);
8113 testDestroyValidation(all - featureSingleAssetVault - featureMPTokensV2);
8114 testDestroyValidation((all | featureSingleAssetVault) - featureMPTokensV2);
8115 testDestroyValidation(all - featureMPTokensV2);
8116 testDestroyValidation(all | featureSingleAssetVault);
8117 testDestroyEnabled(all - featureSingleAssetVault);
8118 testDestroyEnabled(all - featureSingleAssetVault - featureMPTokensV2);
8119 testDestroyEnabled((all | featureSingleAssetVault) - featureMPTokensV2);
8120 testDestroyEnabled(all - featureMPTokensV2);
8121 testDestroyEnabled(all | featureSingleAssetVault);
8122
8123 // MPTokenAuthorize
8124 testAuthorizeValidation(all - featureSingleAssetVault);
8125 testAuthorizeValidation(all - featureSingleAssetVault - featureMPTokensV2);
8126 testAuthorizeValidation((all | featureSingleAssetVault) - featureMPTokensV2);
8127 testAuthorizeValidation(all - featureMPTokensV2);
8128 testAuthorizeValidation(all | featureSingleAssetVault);
8129 testAuthorizeEnabled(all - featureSingleAssetVault);
8130 testAuthorizeEnabled(all - featureSingleAssetVault - featureMPTokensV2);
8131 testAuthorizeEnabled((all | featureSingleAssetVault) - featureMPTokensV2);
8132 testAuthorizeEnabled(all - featureMPTokensV2);
8133 testAuthorizeEnabled(all | featureSingleAssetVault);
8134
8135 // MPTokenIssuanceSet
8136 testSetValidation(all - featureSingleAssetVault - featureDynamicMPT);
8137 testSetValidation(all - featureSingleAssetVault);
8138 testSetValidation(all - featureDynamicMPT);
8139 testSetValidation(all - featurePermissionedDomains);
8140 testSetValidation(all);
8141
8142 testSetEnabled(all - featureSingleAssetVault - fixCleanup3_4_0);
8143 testSetEnabled(all - featureSingleAssetVault);
8144 testSetEnabled(all - fixCleanup3_4_0);
8145 testSetEnabled(all);
8146
8147 // MPT clawback
8149 testClawbackValidation(all - featureMPTokensV2);
8150 testClawback(all);
8151 testClawback(all - featureMPTokensV2);
8152
8153 // Test Direct Payment
8154 testPayment(all);
8155 testPayment(all | featureSingleAssetVault);
8156 testPayment((all | featureSingleAssetVault) - featureMPTokensV2 - fixCleanup3_5_0);
8157 testPayment(all - featureMPTokensV2 - fixCleanup3_5_0);
8158
8159 testDepositPreauth(all);
8160 testDepositPreauth(all - featureCredentials);
8161 testDepositPreauth(all - featureMPTokensV2);
8162 testDepositPreauth(all - featureCredentials - featureMPTokensV2);
8163
8164 // Test MPT Amount is invalid in Tx, which don't support MPT
8165 testMPTInvalidInTx(all);
8167 // Test parsed MPTokenIssuanceID in API response metadata
8169
8170 // Test tokens equality
8172
8173 // Test helpers
8175
8176 // Dynamic MPT
8179 testSetMPT(all);
8180 testSetCanLock(all);
8181 testSetRequireAuth(all);
8182 testSetCanEscrow(all);
8183 testSetCanTransfer(all);
8184 testSetCanTransfer(all - featureMPTokensV2);
8185 testSetCanClawback(all);
8187
8188 // Test offer crossing
8189 testOfferCrossing(all);
8190
8191 // Test cross asset payment
8193
8194 // Test path finding
8195 testPath(all);
8196
8197 // Test checks
8198 testCheck(all);
8199
8200 // Add AMMClawback
8201 testAMMClawback(all);
8202
8203 // Test AMM
8204 testBasicAMM(all);
8205
8206 // Test Trade/Transfer
8208
8209 // Fixes
8212 testLockedMPTokenDestroyedIssuance(all - fixCleanup3_4_0);
8213 testLockedMPTokenDestroyedIssuance(all - featureSingleAssetVault);
8214 testLockedMPTokenDestroyedIssuance(all - featureSingleAssetVault - fixCleanup3_4_0);
8216 testPartialPaymentRounding(all - featureMPTokensV2);
8217 testPartialPaymentRounding(all - featureMPTokensV2 - fixCleanup3_5_0);
8219 testPartialPaymentDivideOverflow(all - featureMPTokensV2);
8220#ifndef XRPL_UBSAN
8221 // Without the fix, the legacy divide() relies on unsigned wraparound,
8222 // which UBSan reports as unsigned-integer-overflow.
8223 testPartialPaymentDivideOverflow(all - featureMPTokensV2 - fixCleanup3_5_0);
8224#endif
8225
8226 // Number has a 16-digit mantissa without these amendments.
8227 auto const smallNumber =
8228 all - featureMPTokensV2 - featureSingleAssetVault - featureLendingProtocol;
8229 testPartialPaymentRounding(smallNumber);
8230 testPartialPaymentRounding(smallNumber - fixCleanup3_5_0);
8231 testPartialPaymentSendMax(smallNumber);
8232 testPartialPaymentSendMax(smallNumber - fixCleanup3_5_0);
8233 testPartialPaymentSendMax(smallNumber | featureMPTokensV2);
8234 testPartialPaymentSendMax((smallNumber | featureMPTokensV2) - fixCleanup3_5_0);
8236 testPartialPaymentSendMax(all - featureMPTokensV2 - fixCleanup3_5_0);
8237 }
8238};
8239
8241
8242} // namespace xrpl::test
A testsuite class.
Definition suite.h:52
void fail(String const &reason, char const *file, int line)
Record a failure.
Definition suite.h:554
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
std::string const & arg() const
Return the argument associated with the runner.
Definition suite.h:292
Represents a JSON value.
Definition json_value.h:121
std::string asString() const
Returns the unquoted string value.
bool isMember(char const *key) const
Return true if the object has a member named key.
Editable, discardable view that can build metadata for one tx.
constexpr TIss const & get() const
Floating point representation of amounts with high dynamic range.
Definition IOUAmount.h:26
A currency issued by an account.
Definition Issue.h:18
std::int64_t value_type
Definition MPTAmount.h:25
static MPTAmount minPositiveAmount()
static constexpr std::array< FlagMapping, 7 > flagMapping
Slice slice() const noexcept
Definition PublicKey.h:115
Identifies fields.
Definition SField.h:131
std::string const fieldName
Definition SField.h:154
constexpr TIss const & get() const
json::Value getJson(JsonOptions=JsonOptions::Values::None) const override
void setFieldAmount(SField const &field, STAmount const &)
Definition STObject.cpp:803
std::vector< STPath >::size_type size() const
Definition STPathSet.h:624
void sign(PublicKey const &publicKey, SecretKey const &secretKey)
Sign the transaction as its account.
Definition STTx.cpp:215
static constexpr SeqProxy rawSequence(std::uint32_t v)
Factory function to return a sequence-based SeqProxy.
Definition SeqProxy.h:62
Slice slice() const noexcept
Definition Serializer.h:141
virtual beast::Journal getJournal(std::string const &name)=0
static TxFormats const & getInstance()
Definition TxFormats.cpp:60
SLE::pointer peek(Keylet const &k) override
Prepare to modify the SLE associated with key.
void testSetEnabled(FeatureBitset features)
void testSetMPT(FeatureBitset features)
void testClawback(FeatureBitset features)
void testPartialPaymentSendMax(FeatureBitset features)
void testDepositPreauth(FeatureBitset features)
void testCheck(FeatureBitset features)
void testOfferCrossing(FeatureBitset features)
void testDestroyEnabled(FeatureBitset features)
void testPayment(FeatureBitset features)
void testAMMClawback(FeatureBitset features)
void testTxJsonMetaFields(FeatureBitset features)
void testLockedMPTokenDestroyedIssuance(FeatureBitset features)
void run() override
Runs the suite.
void testNonCanonicalMPTAmountCleanup(FeatureBitset features)
void testClawbackValidation(FeatureBitset features)
void testSetRequireAuth(FeatureBitset features)
void testCrossAssetPayment(FeatureBitset features)
void testMultiSendMaximumAmount(FeatureBitset features)
void testDestroyValidation(FeatureBitset features)
void testInvalidCreateDynamic(FeatureBitset features)
void testMPTInvalidInTx(FeatureBitset features)
void testAuthorizeValidation(FeatureBitset features)
void testSetValidation(FeatureBitset features)
void testSetCanEscrow(FeatureBitset features)
void testSetImmutableFlags(FeatureBitset features)
void testSetCanLock(FeatureBitset features)
void testBasicAMM(FeatureBitset features)
void testPartialPaymentRounding(FeatureBitset features)
void testCreateEnabled(FeatureBitset features)
void testPath(FeatureBitset features)
void testSetCanClawback(FeatureBitset features)
void testSetCanTransfer(FeatureBitset features)
void testCreateValidation(FeatureBitset features)
void testPartialPaymentDivideOverflow(FeatureBitset features)
void testFixDoubleOwnerCount(FeatureBitset all)
void testInvalidSetDynamic(FeatureBitset features)
void testAuthorizeEnabled(FeatureBitset features)
Convenience class to test AMM functionality.
IOUAmount tokens() const
static json::Value createJv(AccountID const &account, STAmount const &asset1, STAmount const &asset2, std::uint16_t const &tfee)
Definition AMM.cpp:136
bool expectBalances(STAmount const &asset1, STAmount const &asset2, IOUAmount const &lpt, std::optional< AccountID > const &account=std::nullopt) const
Verify the AMM balances.
Definition AMM.cpp:269
Immutable cryptographic account descriptor.
Definition jtx/Account.h:21
std::string const & human() const
Returns the human readable public key.
std::string const & name() const
Return the name.
Definition jtx/Account.h:75
PublicKey const & pk() const
Return the public key.
Definition jtx/Account.h:84
AccountID id() const
Returns the Account ID.
Sets the DeliverMin on a JTx.
Definition delivermin.h:16
Set the domain on a JTx.
Definition domain.h:14
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
SLE::const_pointer le(Account const &account) const
Return an account root.
Definition Env.cpp:311
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
Definition Env.cpp:323
virtual void submit(JTx const &jt, std::source_location const &loc=std::source_location::current())
Submit an existing JTx.
Definition Env.cpp:414
std::uint32_t seq(Account const &account) const
Returns the next sequence number on account.
Definition Env.cpp:302
Account const & master
Definition Env.h:165
json::Value rpc(unsigned apiVersion, std::unordered_map< std::string, std::string > const &headers, std::string const &cmd, Args &&... args)
Execute an RPC command.
Definition Env.h:1058
JTx jt(JsonValue &&jv, FN const &... fN)
Create a JTx from parameters.
Definition Env.h:723
PrettyAmount balance(Account const &account) const
Returns the XRP balance on an account.
Definition Env.cpp:201
void disableFeature(UInt256 const feature)
Definition Env.cpp:717
void trust(STAmount const &amount, Account const &account)
Establish trust lines.
Definition Env.cpp:354
void enableFeature(UInt256 const feature)
Definition Env.cpp:709
std::shared_ptr< STObject const > meta()
Return metadata for the last JTx.
Definition Env.cpp:538
std::shared_ptr< STTx const > tx() const
Return the tx data for the last JTx.
Definition Env.cpp:560
void memoize(Account const &account)
Associate AccountID with account.
Definition Env.cpp:174
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
NetClock::time_point now()
Returns the current network time.
Definition Env.h:326
Set the fee on a JTx.
Definition fee.h:20
Match set account flags.
Definition flags.h:119
Converts to IOU Issue or STAmount.
Test helper for creating, mutating, and asserting MPT and confidential MPT ledger state.
Definition mpt.h:512
bool checkMPTokenAmount(Account const &holder, std::int64_t expectedAmount) const
Definition mpt.cpp:702
bool isTransferFeePresent() const
Definition mpt.cpp:757
bool checkMetadata(std::string const &metadata) const
Definition mpt.cpp:730
bool isMetadataPresent() const
Definition mpt.cpp:740
MPTID const & issuanceID() const
Definition mpt.h:777
bool checkImmutableFlags(std::uint32_t expectedFlags) const
Definition mpt.cpp:763
void pay(Account const &src, Account const &dest, std::int64_t amount, std::optional< TER > err=std::nullopt, std::optional< std::vector< std::string > > credentials=std::nullopt, std::source_location const &loc=std::source_location::current())
Definition mpt.cpp:861
void create(MPTCreate const &arg=MPTCreate{}, std::source_location const &loc=std::source_location::current())
Definition mpt.cpp:346
void set(MPTSet const &set={}, std::source_location const &loc=std::source_location::current())
Definition mpt.cpp:581
bool checkTransferFee(std::uint16_t transferFee) const
Definition mpt.cpp:747
void authorize(MPTAuthorize const &arg=MPTAuthorize{}, std::source_location const &loc=std::source_location::current())
Definition mpt.cpp:459
bool checkMPTokenOutstandingAmount(std::int64_t expectedAmount) const
Definition mpt.cpp:709
void destroy(MPTDestroy const &arg=MPTDestroy{}, std::source_location const &loc=std::source_location::current())
Definition mpt.cpp:425
Converts to MPT Issue or STAmount.
Add a path.
Definition paths.h:47
Sets the SendMax on a JTx.
Definition sendmax.h:16
Set the expected result code for a JTx The test will fail if the code doesn't match.
Definition ter.h:18
Set the flags on a JTx.
Definition txflags.h:14
T empty(T... args)
T erase(T... args)
T insert(T... args)
T is_same_v
T lock(T... args)
T make_optional(T... args)
T make_pair(T... args)
T make_shared(T... args)
T max(T... args)
T min(T... args)
Keylet offer(AccountID const &id, SeqProxy const &seq) noexcept
An offer from an account.
Definition Indexes.cpp:310
Keylet check(AccountID const &id, SeqProxy const &seq) noexcept
A Check.
Definition Indexes.cpp:372
Keylet mptoken(MPTID const &issuanceID, AccountID const &holder) noexcept
Definition Indexes.cpp:547
Keylet mptokenIssuance(MPTID const &issuanceID) noexcept
Definition Indexes.cpp:541
json::Value ammClawback(Account const &issuer, Account const &holder, Asset const &asset, Asset const &asset2, std::optional< STAmount > const &amount)
Definition AMM.cpp:912
json::Value cash(jtx::Account const &dest, UInt256 const &checkId, STAmount const &amount)
Cash a check requiring that a specific amount be delivered.
Definition check.cpp:15
json::Value create(A const &account, A const &dest, STAmount const &sendMax)
Create a check.
json::Value deleteCred(jtx::Account const &acc, jtx::Account const &subject, jtx::Account const &issuer, std::string_view credType)
Definition creds.cpp:40
json::Value accept(jtx::Account const &subject, jtx::Account const &issuer, std::string_view credType)
Definition creds.cpp:29
json::Value create(jtx::Account const &subject, jtx::Account const &issuer, std::string_view credType)
Definition creds.cpp:16
json::Value ledgerEntry(jtx::Env &env, jtx::Account const &subject, jtx::Account const &issuer, std::string_view credType)
Definition creds.cpp:56
json::Value authCredentials(jtx::Account const &account, std::vector< AuthorizeCredentials > const &auth)
Definition deposit.cpp:38
json::Value auth(Account const &account, Account const &auth)
Preauthorize for deposit.
Definition deposit.cpp:16
json::Value unauth(Account const &account, Account const &unauth)
Remove pre-authorization for deposit.
Definition deposit.cpp:27
json::Value create(AccountID const &account, AccountID const &to, STAmount const &amount)
Definition escrow.cpp:24
auto const kCondition
Definition escrow.h:100
auto const kFinishTime
Set the "FinishAfter" time tag on a JTx.
Definition escrow.h:93
std::array< std::uint8_t, 39 > const kCb1
Definition escrow.h:60
std::vector< Credential > Credentials
UInt256 getNewDomain(std::shared_ptr< STObject const > const &meta)
json::Value setTx(AccountID const &account, Credentials const &credentials, std::optional< UInt256 > domain)
jtx::Env pathTestEnv(beast::unit_test::Suite &suite)
auto const kMptDexFlags
Definition mpt.h:47
json::Value claw(Account const &account, STAmount const &amount, std::optional< Account > const &mptHolder)
Definition trust.cpp:51
json::Value pay(AccountID const &account, AccountID const &to, AnyAmount amount)
Create a payment.
Definition pay.cpp:14
std::vector< STAmount > fund(jtx::Env &env, jtx::Account const &gw, std::vector< jtx::Account > const &accounts, std::vector< STAmount > const &amts, Fund how)
Definition AMMTest.cpp:34
json::Value offerCancel(Account const &account, std::uint32_t offerSeq)
Cancel an offer.
Definition offer.cpp:31
XrpT const XRP
Converts to XRP Issue or STAmount.
Definition amount.cpp:92
std::vector< Account > Holders
Definition mpt.h:157
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)
bool expectOffers(Env const &env, AccountID const &account, std::uint16_t size, std::vector< Amounts > const &toMatch)
bool same(STPathSet const &st1, Args const &... args)
std::uint32_t ownerCount(Env const &env, Account const &account)
XRPAmount txFee(Env const &env, std::uint16_t n)
FeatureBitset testableAmendments()
Definition Env.h:92
STPathElement ipe(Asset const &asset)
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
json::Value trust(Account const &account, STAmount const &amount, std::uint32_t flags)
Modify a trust line.
Definition trust.cpp:18
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.
STPath stpath(Args const &... args)
json::Value getAccountOffers(Env &env, AccountID const &acct, bool current)
json::Value fset(Account const &account, std::uint32_t on, std::uint32_t off=0)
Add and/or remove flag.
Definition flags.cpp:15
UInt256 getCheckIndex(AccountID const &account, std::uint32_t const sequence)
constexpr XRPAmount
Convert XRP to drops (integral types).
Definition TxTest.h:53
BEAST_DEFINE_TESTSUITE_PRIO(AccountDelete, app, xrpl, 2)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
constexpr FlagValue tifMPTMetadata
Definition TxFlags.h:382
@ terNO_AMM
Definition TER.h:229
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
Definition Issue.h:108
@ RpcInvalidParams
Definition ErrorCodes.h:67
std::string strHex(FwdIt begin, FwdIt end)
Definition strHex.h:13
TAmounts< STAmount, STAmount > Amounts
Definition Quality.h:69
STAmount mulRoundStrict(STAmount const &v1, STAmount const &v2, Asset const &asset, bool roundUp)
@ tefEXCEPTION
Definition TER.h:166
BaseUInt< 160, detail::CurrencyTag > Currency
Currency is a hash representing a specific currency.
Definition UintTypes.h:42
constexpr FlagValue tifMPTTransferFee
Definition TxFlags.h:383
constexpr FlagValue tifMPTCanEscrow
Definition TxFlags.h:378
TER canMPTTradeAndTransfer(ReadView const &v, Asset const &asset, AccountID const &from, AccountID const &to)
Convenience to combine canTrade/Transfer.
MPTIssue mptIssueFromJson(json::Value const &jv)
Definition MPTIssue.cpp:66
bool toCurrency(Currency &, std::string const &)
Tries to convert a string to a Currency, returns true on success.
Definition UintTypes.cpp:65
static FunctionType fixed(Keylet const &keylet)
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:658
json::Value toJson(Asset const &asset)
Definition Asset.h:168
constexpr FlagValue tifMPTCanTransfer
Definition TxFlags.h:380
BaseUInt< 256 > UInt256
Definition base_uint.h:581
constexpr std::size_t kMaxMpTokenMetadataLength
The maximum length of MPTokenMetadata.
Definition Protocol.h:292
BaseUInt< 192 > MPTID
MPTID is a 192-bit value representing MPT Issuance ID, which is a concatenation of a 32-bit sequence ...
Definition UintTypes.h:54
MPTID makeMptID(std::uint32_t const sequence, AccountID const &account)
Definition Indexes.cpp:219
@ TapNone
Definition ApplyView.h:28
constexpr std::uint16_t kMaxTransferFee
The maximum token transfer fee allowed.
Definition Protocol.h:97
STAmount mulRound(STAmount const &v1, STAmount const &v2, Asset const &asset, bool roundUp)
constexpr FlagValue tifMPTCanHoldConfidentialBalance
Definition TxFlags.h:384
@ temBAD_PATH
Definition TER.h:84
@ temBAD_CURRENCY
Definition TER.h:78
@ temINVALID_FLAG
Definition TER.h:99
@ temMALFORMED
Definition TER.h:75
@ temBAD_PATH_LOOP
Definition TER.h:85
@ temDISABLED
Definition TER.h:102
@ temBAD_AMOUNT
Definition TER.h:77
@ temBAD_TRANSFER_FEE
Definition TER.h:130
@ temRIPPLE_EMPTY
Definition TER.h:101
@ temREDUNDANT
Definition TER.h:100
constexpr FlagValue tifMPTCanClawback
Definition TxFlags.h:381
TERSubset< CanCvtToTER > TER
Definition TER.h:657
TER requireAuth(ReadView const &view, MPTIssue const &mptIssue, AccountID const &account, AuthType authType=AuthType::Legacy, std::uint8_t depth=0)
Check if the account lacks required authorization for MPT.
MPTID badMPT()
Definition MPTIssue.h:121
@ tecLOCKED
Definition TER.h:369
@ tecPATH_PARTIAL
Definition TER.h:293
@ tecNO_ENTRY
Definition TER.h:317
@ tecPATH_DRY
Definition TER.h:305
@ tecOBJECT_NOT_FOUND
Definition TER.h:337
@ tecNO_AUTH
Definition TER.h:311
@ tecAMM_BALANCE
Definition TER.h:340
@ tecINVARIANT_FAILED
Definition TER.h:324
@ tecINSUFFICIENT_FUNDS
Definition TER.h:336
@ tecUNFUNDED_OFFER
Definition TER.h:295
@ tecINSUFFICIENT_RESERVE
Definition TER.h:318
@ tecNO_PERMISSION
Definition TER.h:316
@ tecDUPLICATE
Definition TER.h:326
@ tecHAS_OBLIGATIONS
Definition TER.h:328
@ tecNO_DST
Definition TER.h:301
STAmount multiply(STAmount const &amount, Number const &frac, Number::RoundingMode rm)
constexpr FlagValue tifMPTCanLock
Definition TxFlags.h:376
Asset assetFromJson(json::Value const &jv)
Definition Asset.cpp:59
std::vector< std::pair< AccountID, Number > > MultiplePaymentDestinations
TER accountSendMulti(ApplyView &view, AccountID const &senderID, Asset const &asset, MultiplePaymentDestinations const &receivers, beast::Journal j, WaiveTransferFee waiveFee=WaiveTransferFee::No)
Like accountSend, except one account is sending multiple payments (with the same asset!...
constexpr FlagValue tifMPTCanTrade
Definition TxFlags.h:379
constexpr std::uint64_t kMaxMpTokenAmount
The maximum amount of MPTokenIssuance.
Definition Protocol.h:297
@ SoeMptNotSupported
Definition SOTemplate.h:34
constexpr FlagValue tifMPTRequireAuth
Definition TxFlags.h:377
@ tesSUCCESS
Definition TER.h:253
constexpr bool equalTokens(Asset const &lhs, Asset const &rhs)
Definition Asset.h:286
constexpr FlagValue tfFullyCanonicalSig
Definition TxFlags.h:43
T ref(T... args)
T size(T... args)
Execution context for applying a JSON transaction.
Definition JTx.h:27
std::shared_ptr< STTx const > stx
Definition JTx.h:37
static std::vector< Account > allHolders
Definition mpt.h:164
Arguments for initializing funded MPT test accounts and issuance.
Definition mpt.h:189
STAmount const & value() const
Type used to specify DeliverMin for cashing a check.
Definition check.h:30
T tie(T... args)
T to_string(T... args)
T what(T... args)